xref: /petsc/src/dm/interface/dm.c (revision c6a7a37075f8bf8d34d92c4910d42445b7a3482d)
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 
213d083f849SBarry Smith    Logically Collective on da
2149a42bb27SBarry Smith 
215147403d9SBarry Smith    Input Parameters:
2169a42bb27SBarry Smith +  da - initial distributed array
217bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2189a42bb27SBarry Smith 
2199a42bb27SBarry Smith    Options Database:
220147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2219a42bb27SBarry Smith 
2229a42bb27SBarry Smith    Level: intermediate
2239a42bb27SBarry Smith 
224bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
225bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2269a42bb27SBarry Smith @*/
227d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype)
228d71ae5a4SJacob Faibussowitsch {
2299a42bb27SBarry Smith   PetscFunctionBegin;
2309a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2319566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2329566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
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 
239d083f849SBarry Smith    Logically Collective on da
240c0dedaeaSBarry Smith 
241c0dedaeaSBarry Smith    Input Parameter:
242c0dedaeaSBarry Smith .  da - initial distributed array
243c0dedaeaSBarry Smith 
244c0dedaeaSBarry Smith    Output Parameter:
245c0dedaeaSBarry Smith .  ctype - the vector type
246c0dedaeaSBarry Smith 
247c0dedaeaSBarry Smith    Level: intermediate
248c0dedaeaSBarry Smith 
249db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
250c0dedaeaSBarry Smith @*/
251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
252d71ae5a4SJacob Faibussowitsch {
253c0dedaeaSBarry Smith   PetscFunctionBegin;
254c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
255c0dedaeaSBarry Smith   *ctype = da->vectype;
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 
2625f1ad066SMatthew G Knepley   Not collective
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   Input Parameter:
265bb7acecfSBarry Smith . v - The `Vec`
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley   Output Parameter:
268bb7acecfSBarry Smith . dm - The `DM`
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Level: intermediate
2715f1ad066SMatthew G Knepley 
272bb7acecfSBarry Smith   Note:
273bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
274bb7acecfSBarry Smith 
275bb7acecfSBarry Smith .seealso: `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2765f1ad066SMatthew G Knepley @*/
277d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
278d71ae5a4SJacob Faibussowitsch {
2795f1ad066SMatthew G Knepley   PetscFunctionBegin;
2805f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2815f1ad066SMatthew G Knepley   PetscValidPointer(dm, 2);
2829566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
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 
2895f1ad066SMatthew G Knepley   Not collective
2905f1ad066SMatthew G Knepley 
2915f1ad066SMatthew G Knepley   Input Parameters:
292bb7acecfSBarry Smith + v - The `Vec`
293bb7acecfSBarry Smith - dm - The `DM`
2945f1ad066SMatthew G Knepley 
295bb7acecfSBarry Smith   Note:
296bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
297d9805387SMatthew G. Knepley 
298bb7acecfSBarry Smith   This is NOT the same as `DMCreateGlobalVector()` since it does not change the view methods or perform other customization, but merely sets the `DM` member.
299bb7acecfSBarry Smith 
300bb7acecfSBarry Smith   Level: developer
3015f1ad066SMatthew G Knepley 
302db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3035f1ad066SMatthew G Knepley @*/
304d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
305d71ae5a4SJacob Faibussowitsch {
3065f1ad066SMatthew G Knepley   PetscFunctionBegin;
3075f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
308d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3099566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
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 
316d083f849SBarry Smith    Logically Collective on dm
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith    Input Parameters:
319bb7acecfSBarry Smith +  dm - the `DM` context
3208f1509bcSBarry Smith -  ctype - the matrix type
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith    Options Database:
3238f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Level: intermediate
3268f1509bcSBarry Smith 
327db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
328bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3298f1509bcSBarry Smith @*/
330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
331d71ae5a4SJacob Faibussowitsch {
3328f1509bcSBarry Smith   PetscFunctionBegin;
3338f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3348f1509bcSBarry Smith   dm->coloringtype = ctype;
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 
341d083f849SBarry Smith    Logically Collective on dm
342521d9a4cSLisandro Dalcin 
343521d9a4cSLisandro Dalcin    Input Parameter:
344bb7acecfSBarry Smith .  dm - the `DM` context
3458f1509bcSBarry Smith 
3468f1509bcSBarry Smith    Output Parameter:
3478f1509bcSBarry Smith .  ctype - the matrix type
3488f1509bcSBarry Smith 
3498f1509bcSBarry Smith    Options Database:
3508f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3518f1509bcSBarry Smith 
3528f1509bcSBarry Smith    Level: intermediate
3538f1509bcSBarry Smith 
354db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
355bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3568f1509bcSBarry Smith @*/
357d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
358d71ae5a4SJacob Faibussowitsch {
3598f1509bcSBarry Smith   PetscFunctionBegin;
3608f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3618f1509bcSBarry Smith   *ctype = dm->coloringtype;
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 
368d083f849SBarry Smith    Logically Collective on dm
3698f1509bcSBarry Smith 
3708f1509bcSBarry Smith    Input Parameters:
371bb7acecfSBarry Smith +  dm - the `DM` context
372bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
373521d9a4cSLisandro Dalcin 
374521d9a4cSLisandro Dalcin    Options Database:
375bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
376521d9a4cSLisandro Dalcin 
377521d9a4cSLisandro Dalcin    Level: intermediate
378521d9a4cSLisandro Dalcin 
379bb7acecfSBarry Smith .seealso: `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
380521d9a4cSLisandro Dalcin @*/
381d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
382d71ae5a4SJacob Faibussowitsch {
383521d9a4cSLisandro Dalcin   PetscFunctionBegin;
384521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3859566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3869566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
3873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
388521d9a4cSLisandro Dalcin }
389521d9a4cSLisandro Dalcin 
390c0dedaeaSBarry Smith /*@C
391bb7acecfSBarry Smith        DMGetMatType - Gets the type of matrix created with `DMCreateMatrix()`
392c0dedaeaSBarry Smith 
393d083f849SBarry Smith    Logically Collective on dm
394c0dedaeaSBarry Smith 
395c0dedaeaSBarry Smith    Input Parameter:
396bb7acecfSBarry Smith .  dm - the `DM` context
397c0dedaeaSBarry Smith 
398c0dedaeaSBarry Smith    Output Parameter:
399c0dedaeaSBarry Smith .  ctype - the matrix type
400c0dedaeaSBarry Smith 
401c0dedaeaSBarry Smith    Level: intermediate
402c0dedaeaSBarry Smith 
403db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
404c0dedaeaSBarry Smith @*/
405d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
406d71ae5a4SJacob Faibussowitsch {
407c0dedaeaSBarry Smith   PetscFunctionBegin;
408c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409c0dedaeaSBarry Smith   *ctype = dm->mattype;
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 
416c688c046SMatthew G Knepley   Not collective
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley   Input Parameter:
419bb7acecfSBarry Smith . A - The `Mat`
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley   Output Parameter:
422bb7acecfSBarry Smith . dm - The `DM`
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Level: intermediate
425c688c046SMatthew G Knepley 
426bb7acecfSBarry Smith   Note:
427bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
428bb7acecfSBarry Smith 
429bb7acecfSBarry Smith   Developer Note:
430bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4318f1509bcSBarry Smith 
432db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
433c688c046SMatthew G Knepley @*/
434d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
435d71ae5a4SJacob Faibussowitsch {
436c688c046SMatthew G Knepley   PetscFunctionBegin;
437c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
438c688c046SMatthew G Knepley   PetscValidPointer(dm, 2);
4399566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
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 
446c688c046SMatthew G Knepley   Not collective
447c688c046SMatthew G Knepley 
448c688c046SMatthew G Knepley   Input Parameters:
449c688c046SMatthew G Knepley + A - The Mat
450c688c046SMatthew G Knepley - dm - The DM
451c688c046SMatthew G Knepley 
452bb7acecfSBarry Smith   Level: developer
453c688c046SMatthew G Knepley 
454bb7acecfSBarry Smith   Note:
455bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
456bb7acecfSBarry Smith 
457bb7acecfSBarry Smith   Developer Note:
458bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
459bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4608f1509bcSBarry Smith 
461db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
462c688c046SMatthew G Knepley @*/
463d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
464d71ae5a4SJacob Faibussowitsch {
465c688c046SMatthew G Knepley   PetscFunctionBegin;
466c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4678865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4689566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
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 
475d083f849SBarry Smith    Logically Collective on dm
4769a42bb27SBarry Smith 
477d8d19677SJose E. Roman    Input Parameters:
478bb7acecfSBarry Smith +  da - the `DM` context
479bb7acecfSBarry Smith -  prefix - the prefix to prepend
4809a42bb27SBarry Smith 
4819a42bb27SBarry Smith    Notes:
4829a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4839a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4849a42bb27SBarry Smith 
4859a42bb27SBarry Smith    Level: advanced
4869a42bb27SBarry Smith 
487bb7acecfSBarry Smith .seealso: `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4889a42bb27SBarry Smith @*/
489d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
490d71ae5a4SJacob Faibussowitsch {
4919a42bb27SBarry Smith   PetscFunctionBegin;
4929a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4939566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4941baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4951baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
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 
503d083f849SBarry Smith    Logically Collective on dm
50431697293SDave May 
50531697293SDave May    Input Parameters:
506bb7acecfSBarry Smith +  dm - the `DM` context
507bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
50831697293SDave May 
50931697293SDave May    Notes:
510bb7acecfSBarry Smith    If the `DM` does not currently have an options prefix then this value is used alone as the prefix as if `DMSetOptionsPrefix()` had been called.
51131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
51231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51331697293SDave May 
51431697293SDave May    Level: advanced
51531697293SDave May 
516bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51731697293SDave May @*/
518d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
519d71ae5a4SJacob Faibussowitsch {
52031697293SDave May   PetscFunctionBegin;
52131697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5229566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
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 
538bb7acecfSBarry Smith    Fortran Note:
53995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
54031697293SDave May    sufficient length to hold the prefix.
54131697293SDave May 
54231697293SDave May    Level: advanced
54331697293SDave May 
544bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54531697293SDave May @*/
546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
547d71ae5a4SJacob Faibussowitsch {
54831697293SDave May   PetscFunctionBegin;
54931697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5509566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
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 
623d083f849SBarry Smith     Collective on dm
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Input Parameter:
626bb7acecfSBarry Smith .   dm - the `DM` object to destroy
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Level: developer
62947c6ae99SBarry Smith 
630bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
63147c6ae99SBarry Smith @*/
632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
633d71ae5a4SJacob Faibussowitsch {
6346eb26441SStefano Zampini   PetscInt       cnt;
635dfe15315SJed Brown   DMNamedVecLink nlink, nnext;
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith   PetscFunctionBegin;
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 
807d083f849SBarry Smith     Collective on dm
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith     Input Parameter:
810bb7acecfSBarry Smith .   dm - the `DM` object to setup
811d7bf68aeSBarry Smith 
812bb7acecfSBarry Smith     Level: intermediate
813d7bf68aeSBarry Smith 
814bb7acecfSBarry Smith     Note:
815bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
816bb7acecfSBarry Smith 
817bb7acecfSBarry Smith .seealso: `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
818d7bf68aeSBarry Smith @*/
819d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
820d71ae5a4SJacob Faibussowitsch {
821d7bf68aeSBarry Smith   PetscFunctionBegin;
822171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
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 
832d083f849SBarry Smith     Collective on dm
833d7bf68aeSBarry Smith 
834d7bf68aeSBarry Smith     Input Parameter:
835bb7acecfSBarry Smith .   dm - the `DM` object to set options for
836d7bf68aeSBarry Smith 
837732e2eb9SMatthew G Knepley     Options Database:
838bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
839bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
840bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
841a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
842bb7acecfSBarry Smith -   -dm_bind_below <n>   - bind (force execution on CPU) for `Vec` and `Mat` objects with local size (number of vector entries or matrix rows) below n; currently only supported for `DMDA`
843732e2eb9SMatthew G Knepley 
8449318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8459318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8469318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
847cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8485dca41c3SJed Brown . -dm_plex_shape <shape>            - The domain shape, such as `BOX`, `SPHERE`, etc.
8499318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8509318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8519318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
852bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
853bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8549318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8559318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8569318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8579318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
858bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType` for each direction
8599318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8609318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8619318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8629318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
863bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8649318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8659318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8669318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8679318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
868bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
871d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
872d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
873d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
874909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
875909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8769318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8779318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8789318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8799318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8809318fe57SMatthew G. Knepley 
881384a6580SVaclav Hapla     DMPLEX Specific Checks
882bb7acecfSBarry Smith +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
883bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
884bb7acecfSBarry Smith .   -dm_plex_check_faces           - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - `DMPlexCheckFaces()`
885bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
886bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
887bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
888384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
889d7bf68aeSBarry Smith 
89095eb5ee5SVaclav Hapla     Level: intermediate
89195eb5ee5SVaclav Hapla 
892bb7acecfSBarry Smith .seealso: `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
893bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
894bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
895d7bf68aeSBarry Smith @*/
896d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
897d71ae5a4SJacob Faibussowitsch {
8987781c08eSBarry Smith   char      typeName[256];
899ca266f36SBarry Smith   PetscBool flg;
900d7bf68aeSBarry Smith 
901d7bf68aeSBarry Smith   PetscFunctionBegin;
902171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9049566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9059566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
906dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
907d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9101baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9121baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
913863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-dm_bind_below", "Set the size threshold (in entries) below which the Vec is bound to the CPU", "VecBindToCPU", dm->bind_below, &dm->bind_below, &flg));
916dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
917f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
918dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
919d0609cedSBarry Smith   PetscOptionsEnd();
9203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
921d7bf68aeSBarry Smith }
922d7bf68aeSBarry Smith 
923fc9bc008SSatish Balay /*@C
924bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
925fe2efc57SMark 
926bb7acecfSBarry Smith    Collective on dm
927fe2efc57SMark 
928fe2efc57SMark    Input Parameters:
929bb7acecfSBarry Smith +  dm - the `DM` object
930bb7acecfSBarry Smith .  obj - optional object that provides the prefix for the options database (if NULL then the prefix in obj is used)
931bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
932fe2efc57SMark 
933fe2efc57SMark    Level: intermediate
934bb7acecfSBarry Smith 
935bb7acecfSBarry Smith    Note:
936bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
937bb7acecfSBarry Smith 
938bb7acecfSBarry Smith .seealso: `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
939fe2efc57SMark @*/
940d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
941d71ae5a4SJacob Faibussowitsch {
942fe2efc57SMark   PetscFunctionBegin;
943fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9449566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
946fe2efc57SMark }
947fe2efc57SMark 
948fe2efc57SMark /*@C
949bb7acecfSBarry Smith     DMView - Views a `DM`. Depending on the `PetscViewer` and its `PetscViewerFormat` it may print some ASCII information about the `DM` to the screen or a file or
950bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
95147c6ae99SBarry Smith 
952d083f849SBarry Smith     Collective on dm
95347c6ae99SBarry Smith 
954d8d19677SJose E. Roman     Input Parameters:
955bb7acecfSBarry Smith +   dm - the `DM` object to view
95647c6ae99SBarry Smith -   v - the viewer
95747c6ae99SBarry Smith 
958cd7e8a5eSksagiyam     Notes:
959bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
960bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
961bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
962cd7e8a5eSksagiyam 
963224748a4SBarry Smith     Level: beginner
96447c6ae99SBarry Smith 
965bb7acecfSBarry Smith .seealso: `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat`(), `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96647c6ae99SBarry Smith 
96747c6ae99SBarry Smith @*/
968d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
969d71ae5a4SJacob Faibussowitsch {
97032c0f0efSBarry Smith   PetscBool         isbinary;
97176a8abe0SBarry Smith   PetscMPIInt       size;
97276a8abe0SBarry Smith   PetscViewerFormat format;
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith   PetscFunctionBegin;
975171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97648a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
977b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
98074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
98174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
98274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
98374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98474903a4fSStefano Zampini      in an error here */
98574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9869566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
987b1b135c8SBarry Smith 
9889566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9899566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9903ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9919566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9929566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
99332c0f0efSBarry Smith   if (isbinary) {
99455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99532c0f0efSBarry Smith     char     type[256];
99632c0f0efSBarry Smith 
9979566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
998*c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9999566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
100032c0f0efSBarry Smith   }
1001dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
10023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
100347c6ae99SBarry Smith }
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith /*@
1006bb7acecfSBarry Smith     DMCreateGlobalVector - Creates a global vector from a `DM` object. A global vector is a parallel vector that has no duplicate values shared between MPI ranks,
1007bb7acecfSBarry Smith     that is it has no ghost locations.
100847c6ae99SBarry Smith 
1009d083f849SBarry Smith     Collective on dm
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Input Parameter:
1012bb7acecfSBarry Smith .   dm - the `DM` object
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith     Output Parameter:
101547c6ae99SBarry Smith .   vec - the global vector
101647c6ae99SBarry Smith 
1017073dac72SJed Brown     Level: beginner
101847c6ae99SBarry Smith 
1019bb7acecfSBarry Smith .seealso: `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1020bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
102147c6ae99SBarry Smith @*/
1022d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1023d71ae5a4SJacob Faibussowitsch {
102447c6ae99SBarry Smith   PetscFunctionBegin;
1025171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1026b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1027dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1029c6b011d8SStefano Zampini     DM vdm;
1030c6b011d8SStefano Zampini 
10319566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10327a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1033c6b011d8SStefano Zampini   }
10343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
103547c6ae99SBarry Smith }
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith /*@
1038bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith     Not Collective
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith     Input Parameter:
1043bb7acecfSBarry Smith .   dm - the `DM` object
104447c6ae99SBarry Smith 
104547c6ae99SBarry Smith     Output Parameter:
104647c6ae99SBarry Smith .   vec - the local vector
104747c6ae99SBarry Smith 
1048073dac72SJed Brown     Level: beginner
104947c6ae99SBarry Smith 
1050bb7acecfSBarry Smith     Notes:
1051bb7acecfSBarry Smith     A local vector usually has ghost locations that contain values that are owned by different MPI ranks. A global vector has no ghost locations.
1052bb7acecfSBarry Smith 
1053bb7acecfSBarry Smith  .seealso: `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1054bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105547c6ae99SBarry Smith @*/
1056d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1057d71ae5a4SJacob Faibussowitsch {
105847c6ae99SBarry Smith   PetscFunctionBegin;
1059171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1060b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1061dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
106276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1063c6b011d8SStefano Zampini     DM vdm;
1064c6b011d8SStefano Zampini 
10659566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10667a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1067c6b011d8SStefano Zampini   }
10683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106947c6ae99SBarry Smith }
107047c6ae99SBarry Smith 
10711411c6eeSJed Brown /*@
1072bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10731411c6eeSJed Brown 
1074d083f849SBarry Smith    Collective on dm
10751411c6eeSJed Brown 
10761411c6eeSJed Brown    Input Parameter:
1077bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10781411c6eeSJed Brown 
10791411c6eeSJed Brown    Output Parameter:
10801411c6eeSJed Brown .  ltog - the mapping
10811411c6eeSJed Brown 
1082bb7acecfSBarry Smith    Level: advanced
10831411c6eeSJed Brown 
10841411c6eeSJed Brown    Notes:
1085bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10861411c6eeSJed Brown 
1087bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1088bb7acecfSBarry Smith    need to use this function with those objects.
1089bb7acecfSBarry Smith 
1090bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1091bb7acecfSBarry Smith 
1092bb7acecfSBarry Smith .seealso: `DMCreateLocalVector()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1093bb7acecfSBarry Smith           `DMCreateMatrix()`
10941411c6eeSJed Brown @*/
1095d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1096d71ae5a4SJacob Faibussowitsch {
10970be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10981411c6eeSJed Brown 
10991411c6eeSJed Brown   PetscFunctionBegin;
11001411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11011411c6eeSJed Brown   PetscValidPointer(ltog, 2);
11021411c6eeSJed Brown   if (!dm->ltogmap) {
110337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110437d0c07bSMatthew G Knepley 
11059566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110637d0c07bSMatthew G Knepley     if (section) {
1107a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1109ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
111037d0c07bSMatthew G Knepley 
11119566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11149566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1116e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111737d0c07bSMatthew G Knepley 
111837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11199566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11209566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11219566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11229566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11231a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11241a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1125ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11265227eafbSStefano Zampini 
1127e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11285227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1129e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1130e6befd46SJed Brown             cind++;
1131e6befd46SJed Brown           } else {
11325227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1133e6befd46SJed Brown           }
113437d0c07bSMatthew G Knepley         }
113537d0c07bSMatthew G Knepley       }
1136bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11379371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11389371c9d4SSatish Balay       bsLocal[1] = bs;
11399566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11409371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11419371c9d4SSatish Balay         bs = 1;
11429371c9d4SSatish Balay       } else {
11439371c9d4SSatish Balay         bs = bsMinMax[0];
11449371c9d4SSatish Balay       }
11457591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11467591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1147ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1148ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1149ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1150ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1151ca469d19SJed Brown         }
1152ccf3bd66SMatthew G. Knepley         n /= bs;
1153ccf3bd66SMatthew G. Knepley       }
11549566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1155dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115637d0c07bSMatthew G Knepley   }
11571411c6eeSJed Brown   *ltog = dm->ltogmap;
11583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11591411c6eeSJed Brown }
11601411c6eeSJed Brown 
11611411c6eeSJed Brown /*@
1162bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11631411c6eeSJed Brown 
11641411c6eeSJed Brown    Not Collective
11651411c6eeSJed Brown 
11661411c6eeSJed Brown    Input Parameter:
1167bb7acecfSBarry Smith .  dm - the `DM` with block structure
11681411c6eeSJed Brown 
11691411c6eeSJed Brown    Output Parameter:
11701411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11711411c6eeSJed Brown 
11721411c6eeSJed Brown    Level: intermediate
11731411c6eeSJed Brown 
1174bb7acecfSBarry Smith    Note:
1175bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1176bb7acecfSBarry Smith 
1177bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1178bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1179bb7acecfSBarry Smith 
1180db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11811411c6eeSJed Brown @*/
1182d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1183d71ae5a4SJacob Faibussowitsch {
11841411c6eeSJed Brown   PetscFunctionBegin;
11851411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1186534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11877a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11881411c6eeSJed Brown   *bs = dm->bs;
11893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11901411c6eeSJed Brown }
11911411c6eeSJed Brown 
119248eeb7c8SBarry Smith /*@C
1193bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1194bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119547c6ae99SBarry Smith 
1196a5bc1bf3SBarry Smith     Collective on dmc
119747c6ae99SBarry Smith 
1198d8d19677SJose E. Roman     Input Parameters:
1199bb7acecfSBarry Smith +   dmc - the `DM` object
1200bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
120147c6ae99SBarry Smith 
1202d8d19677SJose E. Roman     Output Parameters:
120347c6ae99SBarry Smith +  mat - the interpolation
1204bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith     Level: developer
120747c6ae99SBarry Smith 
120895452b02SPatrick Sanan     Notes:
1209bb7acecfSBarry Smith     For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by
1210bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1211d52bd9f3SBarry Smith 
1212bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1213bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121485afcc9aSBarry Smith 
1215bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121647c6ae99SBarry Smith @*/
1217d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1218d71ae5a4SJacob Faibussowitsch {
121947c6ae99SBarry Smith   PetscFunctionBegin;
1220a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1221a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1222c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
12239566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1224dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12259566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122747c6ae99SBarry Smith }
122847c6ae99SBarry Smith 
12293ad4599aSBarry Smith /*@
1230bb7acecfSBarry Smith     DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is the transpose of the interpolation between the `DM`.
1231bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1232bb7acecfSBarry Smith     representation of xfine.
12332ed6491fSPatrick Sanan 
12342ed6491fSPatrick Sanan   Input Parameters:
1235bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1236bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12372ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12382ed6491fSPatrick Sanan 
12392ed6491fSPatrick Sanan   Output Parameter:
12402ed6491fSPatrick Sanan .    scale - the scaled vector
12412ed6491fSPatrick Sanan 
1242bb7acecfSBarry Smith   Level: advanced
12432ed6491fSPatrick Sanan 
1244e9c74fd6SRichard Tran Mills   Developer Notes:
1245bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1246e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1247e9c74fd6SRichard Tran Mills 
1248bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12492ed6491fSPatrick Sanan @*/
1250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1251d71ae5a4SJacob Faibussowitsch {
12522ed6491fSPatrick Sanan   Vec         fine;
12532ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12549704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1255e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12569704db99SRichard Tran Mills #endif
12572ed6491fSPatrick Sanan 
12582ed6491fSPatrick Sanan   PetscFunctionBegin;
12599566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12609566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12619566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12629704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12639704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12649704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12659704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12669566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1267e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12689566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12699566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12709566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
127183aa49f4SRichard Tran Mills   }
12729704db99SRichard Tran Mills #endif
12739566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12749566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12759566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12772ed6491fSPatrick Sanan }
12782ed6491fSPatrick Sanan 
12792ed6491fSPatrick Sanan /*@
1280bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1281bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12823ad4599aSBarry Smith 
1283a5bc1bf3SBarry Smith     Collective on dmc
12843ad4599aSBarry Smith 
1285d8d19677SJose E. Roman     Input Parameters:
1286bb7acecfSBarry Smith +   dmc - the `DM` object
1287bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12883ad4599aSBarry Smith 
12893ad4599aSBarry Smith     Output Parameter:
12903ad4599aSBarry Smith .  mat - the restriction
12913ad4599aSBarry Smith 
12923ad4599aSBarry Smith     Level: developer
12933ad4599aSBarry Smith 
1294bb7acecfSBarry Smith     Note:
1295bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1296bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12973ad4599aSBarry Smith 
1298bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12993ad4599aSBarry Smith @*/
1300d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1301d71ae5a4SJacob Faibussowitsch {
13023ad4599aSBarry Smith   PetscFunctionBegin;
1303a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1304a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13055a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1307dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13103ad4599aSBarry Smith }
13113ad4599aSBarry Smith 
131247c6ae99SBarry Smith /*@
1313bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1314bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1315bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1316bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
131747c6ae99SBarry Smith 
1318a5bc1bf3SBarry Smith     Collective on dac
131947c6ae99SBarry Smith 
1320d8d19677SJose E. Roman     Input Parameters:
1321bb7acecfSBarry Smith +   dac - the `DM` object
1322bb7acecfSBarry Smith -   daf - the second, finer `DM` object
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith     Output Parameter:
13256dbf9973SLawrence Mitchell .   mat - the injection
132647c6ae99SBarry Smith 
132747c6ae99SBarry Smith     Level: developer
132847c6ae99SBarry Smith 
1329bb7acecfSBarry Smith    Note:
1330bb7acecfSBarry Smith     For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by
1331bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
133285afcc9aSBarry Smith 
1333bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1334bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133547c6ae99SBarry Smith @*/
1336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1337d71ae5a4SJacob Faibussowitsch {
133847c6ae99SBarry Smith   PetscFunctionBegin;
1339a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1340a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13415a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13429566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1343dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13449566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134647c6ae99SBarry Smith }
134747c6ae99SBarry Smith 
1348b412c318SBarry Smith /*@
1349bb7acecfSBarry Smith   DMCreateMassMatrix - Gets the mass matrix between two `DM` objects, M_ij = \int \phi_i \psi_j where the \phi are Galerkin basis functions for a
1350bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1351bd041c0cSMatthew G. Knepley 
1352a5bc1bf3SBarry Smith   Collective on dac
1353bd041c0cSMatthew G. Knepley 
1354d8d19677SJose E. Roman   Input Parameters:
1355bb7acecfSBarry Smith + dmc - the target `DM` object
1356bb7acecfSBarry Smith - dmf - the source `DM` object
1357bd041c0cSMatthew G. Knepley 
1358bd041c0cSMatthew G. Knepley   Output Parameter:
1359b4937a87SMatthew G. Knepley . mat - the mass matrix
1360bd041c0cSMatthew G. Knepley 
1361bd041c0cSMatthew G. Knepley   Level: developer
1362bd041c0cSMatthew G. Knepley 
1363bb7acecfSBarry Smith   Notes:
1364bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1365bb7acecfSBarry Smith 
1366bb7acecfSBarry Smith   if dmc is dmf then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1367bb7acecfSBarry Smith 
1368bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1369bd041c0cSMatthew G. Knepley @*/
1370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1371d71ae5a4SJacob Faibussowitsch {
1372bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1373b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1374b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13755a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13765b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1377dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13785b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1380b4937a87SMatthew G. Knepley }
1381b4937a87SMatthew G. Knepley 
1382b4937a87SMatthew G. Knepley /*@
1383bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1384b4937a87SMatthew G. Knepley 
1385b4937a87SMatthew G. Knepley   Collective on dm
1386b4937a87SMatthew G. Knepley 
1387b4937a87SMatthew G. Knepley   Input Parameter:
1388bb7acecfSBarry Smith . dm - the `DM` object
1389b4937a87SMatthew G. Knepley 
1390b4937a87SMatthew G. Knepley   Output Parameter:
1391bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1392b4937a87SMatthew G. Knepley 
1393b4937a87SMatthew G. Knepley   Level: developer
1394b4937a87SMatthew G. Knepley 
1395bb7acecfSBarry Smith   Note:
1396bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1397bb7acecfSBarry Smith 
1398bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1399b4937a87SMatthew G. Knepley @*/
1400d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1401d71ae5a4SJacob Faibussowitsch {
1402b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1403b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1404b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1405dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1407bd041c0cSMatthew G. Knepley }
1408bd041c0cSMatthew G. Knepley 
1409bd041c0cSMatthew G. Knepley /*@
1410bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1411bb7acecfSBarry Smith     of a PDE on the `DM`.
141247c6ae99SBarry Smith 
1413d083f849SBarry Smith     Collective on dm
141447c6ae99SBarry Smith 
1415d8d19677SJose E. Roman     Input Parameters:
1416bb7acecfSBarry Smith +   dm - the `DM` object
1417bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith     Output Parameter:
142047c6ae99SBarry Smith .   coloring - the coloring
142147c6ae99SBarry Smith 
14221bf8429eSBarry Smith     Level: developer
14231bf8429eSBarry Smith 
1424ec5066bdSBarry Smith     Notes:
1425bb7acecfSBarry 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
1426bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1427ec5066bdSBarry Smith 
1428bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14291bf8429eSBarry 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,
14301bf8429eSBarry Smith     otherwise an error will be generated.
1431ec5066bdSBarry Smith 
14321bf8429eSBarry Smith .seealso: `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1433aab9d709SJed Brown @*/
1434d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1435d71ae5a4SJacob Faibussowitsch {
143647c6ae99SBarry Smith   PetscFunctionBegin;
1437171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14385a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1439dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
144147c6ae99SBarry Smith }
144247c6ae99SBarry Smith 
1443b412c318SBarry Smith /*@
1444bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144547c6ae99SBarry Smith 
1446d083f849SBarry Smith     Collective on dm
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith     Input Parameter:
1449bb7acecfSBarry Smith .   dm - the `DM` object
145047c6ae99SBarry Smith 
145147c6ae99SBarry Smith     Output Parameter:
145247c6ae99SBarry Smith .   mat - the empty Jacobian
145347c6ae99SBarry Smith 
1454073dac72SJed Brown     Level: beginner
145547c6ae99SBarry Smith 
1456f27dd7c6SMatthew G. Knepley     Options Database Keys:
1457bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1458f27dd7c6SMatthew G. Knepley 
145995452b02SPatrick Sanan     Notes:
146095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
146194013140SBarry Smith     do not need to do it yourself.
146294013140SBarry Smith 
146394013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1464bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146594013140SBarry Smith 
1466bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146794013140SBarry Smith     internally by PETSc.
146894013140SBarry Smith 
1469bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1470bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
147194013140SBarry Smith 
1472bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1473aab9d709SJed Brown @*/
1474d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1475d71ae5a4SJacob Faibussowitsch {
147647c6ae99SBarry Smith   PetscFunctionBegin;
1477171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1478064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14799566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14809566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1481dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
148276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1483c6b011d8SStefano Zampini     DM mdm;
1484c6b011d8SStefano Zampini 
14859566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14867a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1487c6b011d8SStefano Zampini   }
1488e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1489e5e52638SMatthew G. Knepley   if (dm->Nf) {
1490e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1491649ef022SMatthew Knepley     PetscInt     Nf, f;
1492e571a35bSMatthew G. Knepley 
14939566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1494649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1495649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14969566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14979566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14989566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1499649ef022SMatthew Knepley         break;
1500e571a35bSMatthew G. Knepley       }
1501649ef022SMatthew Knepley     }
1502649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1503649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15049566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15059566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15069566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1507e571a35bSMatthew G. Knepley       }
1508e571a35bSMatthew G. Knepley     }
1509e571a35bSMatthew G. Knepley   }
15109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
151247c6ae99SBarry Smith }
151347c6ae99SBarry Smith 
1514732e2eb9SMatthew G Knepley /*@
1515bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1516bb7acecfSBarry 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
1517bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1518aa0f6e3cSJed Brown 
1519aa0f6e3cSJed Brown   Logically Collective on dm
1520aa0f6e3cSJed Brown 
1521aa0f6e3cSJed Brown   Input Parameters:
1522bb7acecfSBarry Smith + dm - the `DM`
1523bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1524aa0f6e3cSJed Brown 
1525aa0f6e3cSJed Brown   Level: developer
1526aa0f6e3cSJed Brown 
1527bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1528aa0f6e3cSJed Brown @*/
1529d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1530d71ae5a4SJacob Faibussowitsch {
1531aa0f6e3cSJed Brown   PetscFunctionBegin;
1532aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1533aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1535aa0f6e3cSJed Brown }
1536aa0f6e3cSJed Brown 
1537aa0f6e3cSJed Brown /*@
1538bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1539732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1540732e2eb9SMatthew G Knepley 
1541d083f849SBarry Smith   Logically Collective on dm
1542732e2eb9SMatthew G Knepley 
1543d8d19677SJose E. Roman   Input Parameters:
1544bb7acecfSBarry Smith + dm - the `DM`
1545bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1546732e2eb9SMatthew G Knepley 
1547732e2eb9SMatthew G Knepley   Level: developer
1548f27dd7c6SMatthew G. Knepley 
1549f27dd7c6SMatthew G. Knepley   Options Database Keys:
1550bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1551f27dd7c6SMatthew G. Knepley 
1552bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1553732e2eb9SMatthew G Knepley @*/
1554d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1555d71ae5a4SJacob Faibussowitsch {
1556732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1557732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1558732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1560732e2eb9SMatthew G Knepley }
1561732e2eb9SMatthew G Knepley 
1562b06ff27eSHong Zhang /*@
1563bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1564bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1565b06ff27eSHong Zhang 
1566d083f849SBarry Smith   Logically Collective on dm
1567b06ff27eSHong Zhang 
1568d8d19677SJose E. Roman   Input Parameters:
1569bb7acecfSBarry Smith + dm - the `DM`
1570da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1571b06ff27eSHong Zhang 
1572b06ff27eSHong Zhang   Level: developer
1573bb7acecfSBarry Smith 
1574bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1575b06ff27eSHong Zhang @*/
1576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1577d71ae5a4SJacob Faibussowitsch {
1578b06ff27eSHong Zhang   PetscFunctionBegin;
1579b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1580b06ff27eSHong Zhang   dm->structure_only = only;
15813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1582b06ff27eSHong Zhang }
1583b06ff27eSHong Zhang 
1584863027abSJed Brown /*@
1585863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1586863027abSJed Brown 
1587863027abSJed Brown   Logically Collective on dm
1588863027abSJed Brown 
1589863027abSJed Brown   Input Parameters:
1590863027abSJed Brown + dm - the `DM`
1591863027abSJed Brown - btype - block by topological point or field node
1592863027abSJed Brown 
1593863027abSJed Brown   Level: advanced
1594863027abSJed Brown 
1595863027abSJed Brown   Options Database Keys:
15960e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1597863027abSJed Brown 
1598863027abSJed Brown .seealso: `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1599863027abSJed Brown @*/
1600863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1601863027abSJed Brown {
1602863027abSJed Brown   PetscFunctionBegin;
1603863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1604863027abSJed Brown   dm->blocking_type = btype;
16053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1606863027abSJed Brown }
1607863027abSJed Brown 
1608863027abSJed Brown /*@
1609863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1610863027abSJed Brown 
1611863027abSJed Brown   Not Collective
1612863027abSJed Brown 
1613863027abSJed Brown   Input Parameters:
1614863027abSJed Brown . dm - the `DM`
1615863027abSJed Brown 
1616863027abSJed Brown   Output Parameters:
1617863027abSJed Brown . btype - block by topological point or field node
1618863027abSJed Brown 
1619863027abSJed Brown   Level: advanced
1620863027abSJed Brown 
1621863027abSJed Brown .seealso: `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1622863027abSJed Brown @*/
1623863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1624863027abSJed Brown {
1625863027abSJed Brown   PetscFunctionBegin;
1626863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1627863027abSJed Brown   PetscValidPointer(btype, 2);
1628863027abSJed Brown   *btype = dm->blocking_type;
16293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1630863027abSJed Brown }
1631863027abSJed Brown 
1632a89ea682SMatthew G Knepley /*@C
1633bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1634a89ea682SMatthew G Knepley 
1635a89ea682SMatthew G Knepley   Not Collective
1636a89ea682SMatthew G Knepley 
1637a89ea682SMatthew G Knepley   Input Parameters:
1638bb7acecfSBarry Smith + dm - the `DM` object
1639a5b23f4aSJose E. Roman . count - The minimum size
1640bb7acecfSBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, or MPIU_INT)
1641a89ea682SMatthew G Knepley 
1642a89ea682SMatthew G Knepley   Output Parameter:
1643a89ea682SMatthew G Knepley . array - the work array
1644a89ea682SMatthew G Knepley 
1645a89ea682SMatthew G Knepley   Level: developer
1646a89ea682SMatthew G Knepley 
1647bb7acecfSBarry Smith   Note:
1648da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1649bb7acecfSBarry Smith 
1650bb7acecfSBarry Smith   The array may contain nonzero values
1651bb7acecfSBarry Smith 
1652bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1653a89ea682SMatthew G Knepley @*/
1654d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1655d71ae5a4SJacob Faibussowitsch {
1656aa1993deSMatthew G Knepley   DMWorkLink  link;
165769291d52SBarry Smith   PetscMPIInt dsize;
1658a89ea682SMatthew G Knepley 
1659a89ea682SMatthew G Knepley   PetscFunctionBegin;
1660a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1661aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1662aa1993deSMatthew G Knepley   if (dm->workin) {
1663aa1993deSMatthew G Knepley     link       = dm->workin;
1664aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1665aa1993deSMatthew G Knepley   } else {
16664dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1667a89ea682SMatthew G Knepley   }
16689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype, &dsize));
16695056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16709566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16719566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1672854ce69bSBarry Smith     link->bytes = dsize * count;
1673aa1993deSMatthew G Knepley   }
1674aa1993deSMatthew G Knepley   link->next  = dm->workout;
1675aa1993deSMatthew G Knepley   dm->workout = link;
1676cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
167700d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
167800d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
167900d952a4SJed Brown #endif
1680aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1682a89ea682SMatthew G Knepley }
1683a89ea682SMatthew G Knepley 
1684aa1993deSMatthew G Knepley /*@C
1685bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1686aa1993deSMatthew G Knepley 
1687aa1993deSMatthew G Knepley   Not Collective
1688aa1993deSMatthew G Knepley 
1689aa1993deSMatthew G Knepley   Input Parameters:
1690bb7acecfSBarry Smith + dm - the `DM` object
1691a5b23f4aSJose E. Roman . count - The minimum size
169269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1693aa1993deSMatthew G Knepley 
1694aa1993deSMatthew G Knepley   Output Parameter:
1695aa1993deSMatthew G Knepley . array - the work array
1696aa1993deSMatthew G Knepley 
1697aa1993deSMatthew G Knepley   Level: developer
1698aa1993deSMatthew G Knepley 
169995452b02SPatrick Sanan   Developer Notes:
1700bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1701147403d9SBarry Smith 
1702bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1703aa1993deSMatthew G Knepley @*/
1704d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1705d71ae5a4SJacob Faibussowitsch {
1706aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1707aa1993deSMatthew G Knepley 
1708aa1993deSMatthew G Knepley   PetscFunctionBegin;
1709aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1710aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1711aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1712aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1713aa1993deSMatthew G Knepley       *p            = link->next;
1714aa1993deSMatthew G Knepley       link->next    = dm->workin;
1715aa1993deSMatthew G Knepley       dm->workin    = link;
17160298fd71SBarry Smith       *(void **)mem = NULL;
17173ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1718aa1993deSMatthew G Knepley     }
1719aa1993deSMatthew G Knepley   }
1720aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1721aa1993deSMatthew G Knepley }
1722e7c4fc90SDmitry Karpeev 
17238cda7954SMatthew G. Knepley /*@C
1724bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1725bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17268cda7954SMatthew G. Knepley 
1727bb7acecfSBarry Smith   Logically collective on dm
17288cda7954SMatthew G. Knepley 
17298cda7954SMatthew G. Knepley   Input Parameters:
1730bb7acecfSBarry Smith + dm     - The `DM`
17318cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17328cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17338cda7954SMatthew G. Knepley 
1734147403d9SBarry Smith   Calling sequence of nullsp:
1735147403d9SBarry Smith .vb
1736147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1737147403d9SBarry Smith .ve
1738bb7acecfSBarry Smith +  dm        - The present `DM`
1739bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1740147403d9SBarry Smith .  field     - The field number in dm
1741147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17428cda7954SMatthew G. Knepley 
174349762cbcSSatish Balay   Level: intermediate
174449762cbcSSatish Balay 
1745bb7acecfSBarry Smith   Fortran Notes:
1746bb7acecfSBarry Smith   This function is not available from Fortran.
1747bb7acecfSBarry Smith 
1748bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1749147403d9SBarry Smith @*/
1750d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1751d71ae5a4SJacob Faibussowitsch {
1752435a35e8SMatthew G Knepley   PetscFunctionBegin;
1753435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17547a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1755435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1757435a35e8SMatthew G Knepley }
1758435a35e8SMatthew G Knepley 
17598cda7954SMatthew G. Knepley /*@C
1760bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17618cda7954SMatthew G. Knepley 
17628cda7954SMatthew G. Knepley   Not collective
17638cda7954SMatthew G. Knepley 
17648cda7954SMatthew G. Knepley   Input Parameters:
1765bb7acecfSBarry Smith + dm     - The `DM`
17668cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17678cda7954SMatthew G. Knepley 
17688cda7954SMatthew G. Knepley   Output Parameter:
17698cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17708cda7954SMatthew G. Knepley 
1771147403d9SBarry Smith   Calling sequence of nullsp:
1772147403d9SBarry Smith .vb
1773147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1774147403d9SBarry Smith .ve
1775147403d9SBarry Smith +  dm        - The present DM
1776147403d9SBarry Smith .  origField - The field number given above, in the original DM
1777147403d9SBarry Smith .  field     - The field number in dm
1778147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17798cda7954SMatthew G. Knepley 
1780bb7acecfSBarry Smith   Fortran Note:
1781bb7acecfSBarry Smith   This function is not available from Fortran.
17828cda7954SMatthew G. Knepley 
178349762cbcSSatish Balay    Level: intermediate
178449762cbcSSatish Balay 
1785bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1786147403d9SBarry Smith @*/
1787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1788d71ae5a4SJacob Faibussowitsch {
17890a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17900a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1791f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17927a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17930a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17950a50eb56SMatthew G. Knepley }
17960a50eb56SMatthew G. Knepley 
17978cda7954SMatthew G. Knepley /*@C
1798bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17998cda7954SMatthew G. Knepley 
1800bb7acecfSBarry Smith   Logically collective on dm
18018cda7954SMatthew G. Knepley 
18028cda7954SMatthew G. Knepley   Input Parameters:
1803bb7acecfSBarry Smith + dm     - The `DM`
18048cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18058cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18068cda7954SMatthew G. Knepley 
1807147403d9SBarry Smith   Calling sequence of nullsp:
1808147403d9SBarry Smith .vb
1809147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1810147403d9SBarry Smith .ve
1811bb7acecfSBarry Smith +  dm        - The present `DM`
1812bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1813147403d9SBarry Smith .  field     - The field number in dm
1814147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18158cda7954SMatthew G. Knepley 
1816bb7acecfSBarry Smith   Fortran Note:
1817bb7acecfSBarry Smith   This function is not available from Fortran.
18188cda7954SMatthew G. Knepley 
181949762cbcSSatish Balay    Level: intermediate
182049762cbcSSatish Balay 
1821bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1822bb7acecfSBarry Smith           `MatNullSpace`
1823147403d9SBarry Smith @*/
1824d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1825d71ae5a4SJacob Faibussowitsch {
1826f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1827f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18287a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1829f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1831f9d4088aSMatthew G. Knepley }
1832f9d4088aSMatthew G. Knepley 
18338cda7954SMatthew G. Knepley /*@C
1834bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18358cda7954SMatthew G. Knepley 
18368cda7954SMatthew G. Knepley   Not collective
18378cda7954SMatthew G. Knepley 
18388cda7954SMatthew G. Knepley   Input Parameters:
1839bb7acecfSBarry Smith + dm     - The `DM`
18408cda7954SMatthew G. Knepley - field  - The field number for the nullspace
18418cda7954SMatthew G. Knepley 
18428cda7954SMatthew G. Knepley   Output Parameter:
18438cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18448cda7954SMatthew G. Knepley 
1845147403d9SBarry Smith   Calling sequence of nullsp:
1846147403d9SBarry Smith .vb
1847147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1848147403d9SBarry Smith .ve
1849bb7acecfSBarry Smith +  dm        - The present `DM`
1850bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1851147403d9SBarry Smith .  field     - The field number in dm
1852147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18538cda7954SMatthew G. Knepley 
1854bb7acecfSBarry Smith   Fortran Note:
1855bb7acecfSBarry Smith   This function is not available from Fortran.
18568cda7954SMatthew G. Knepley 
185749762cbcSSatish Balay    Level: intermediate
185849762cbcSSatish Balay 
1859bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1860bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1861147403d9SBarry Smith @*/
1862d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1863d71ae5a4SJacob Faibussowitsch {
1864f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1865f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1866f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18677a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1868f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1870f9d4088aSMatthew G. Knepley }
1871f9d4088aSMatthew G. Knepley 
18724f3b5142SJed Brown /*@C
1873bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18744d343eeaSMatthew G Knepley 
18754d343eeaSMatthew G Knepley   Not collective
18764d343eeaSMatthew G Knepley 
18774d343eeaSMatthew G Knepley   Input Parameter:
1878bb7acecfSBarry Smith . dm - the `DM` object
18794d343eeaSMatthew G Knepley 
18804d343eeaSMatthew G Knepley   Output Parameters:
18810298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
1882bb7acecfSBarry Smith . fieldNames - The number of each field (or NULL if not requested)
18830298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
18844d343eeaSMatthew G Knepley 
18854d343eeaSMatthew G Knepley   Level: intermediate
18864d343eeaSMatthew G Knepley 
1887bb7acecfSBarry Smith   Note:
188821c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1889bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1890bb7acecfSBarry Smith   `PetscFree()`.
189121c9b008SJed Brown 
1892bb7acecfSBarry Smith   Fortran Note:
1893bb7acecfSBarry Smith   Not available in Fortran.
1894bb7acecfSBarry Smith 
1895bb7acecfSBarry Smith   Developer Note:
1896bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1897bb7acecfSBarry Smith   likely be removed.
1898bb7acecfSBarry Smith 
1899bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1900bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19014d343eeaSMatthew G Knepley @*/
1902d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1903d71ae5a4SJacob Faibussowitsch {
190437d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19054d343eeaSMatthew G Knepley 
19064d343eeaSMatthew G Knepley   PetscFunctionBegin;
19074d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190869ca1f37SDmitry Karpeev   if (numFields) {
1909534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
191069ca1f37SDmitry Karpeev     *numFields = 0;
191169ca1f37SDmitry Karpeev   }
191237d0c07bSMatthew G Knepley   if (fieldNames) {
191337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
19140298fd71SBarry Smith     *fieldNames = NULL;
191569ca1f37SDmitry Karpeev   }
191669ca1f37SDmitry Karpeev   if (fields) {
191769ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
19180298fd71SBarry Smith     *fields = NULL;
191969ca1f37SDmitry Karpeev   }
19209566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
192137d0c07bSMatthew G Knepley   if (section) {
19223a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
192337d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
192437d0c07bSMatthew G Knepley 
19259566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19269566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19279566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
193037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
193237d0c07bSMatthew G Knepley     }
193337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193437d0c07bSMatthew G Knepley       PetscInt gdof;
193537d0c07bSMatthew G Knepley 
19369566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193737d0c07bSMatthew G Knepley       if (gdof > 0) {
193837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19393a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
194037d0c07bSMatthew G Knepley 
19419566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19429566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19433a544194SStefano Zampini           fpdof = fdof - fcdof;
19443a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19453a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19463a544194SStefano Zampini             fieldNc[f] = 1;
19473a544194SStefano Zampini           }
19483a544194SStefano Zampini           fieldSizes[f] += fpdof;
194937d0c07bSMatthew G Knepley         }
195037d0c07bSMatthew G Knepley       }
195137d0c07bSMatthew G Knepley     }
195237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
195437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195537d0c07bSMatthew G Knepley     }
195637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195837d0c07bSMatthew G Knepley 
19599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
196037d0c07bSMatthew G Knepley       if (gdof > 0) {
19619566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
196237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
196337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
196437d0c07bSMatthew G Knepley 
19659566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19669566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1967ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196837d0c07bSMatthew G Knepley         }
196937d0c07bSMatthew G Knepley       }
197037d0c07bSMatthew G Knepley     }
19718865f1eaSKarl Rupp     if (numFields) *numFields = nF;
197237d0c07bSMatthew G Knepley     if (fieldNames) {
19739566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
197437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197537d0c07bSMatthew G Knepley         const char *fieldName;
197637d0c07bSMatthew G Knepley 
19779566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19789566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197937d0c07bSMatthew G Knepley       }
198037d0c07bSMatthew G Knepley     }
198137d0c07bSMatthew G Knepley     if (fields) {
19829566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
198337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19843a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19853a544194SStefano Zampini 
19869566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19873a544194SStefano Zampini         in[0] = -fieldNc[f];
19883a544194SStefano Zampini         in[1] = fieldNc[f];
19891c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19903a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19919566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
199237d0c07bSMatthew G Knepley       }
199337d0c07bSMatthew G Knepley     }
19949566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1995dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19974d343eeaSMatthew G Knepley }
19984d343eeaSMatthew G Knepley 
199916621825SDmitry Karpeev /*@C
2000bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2001bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
2002bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2003bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2004e7c4fc90SDmitry Karpeev 
2005e7c4fc90SDmitry Karpeev   Not collective
2006e7c4fc90SDmitry Karpeev 
2007e7c4fc90SDmitry Karpeev   Input Parameter:
2008bb7acecfSBarry Smith . dm - the `DM` object
2009e7c4fc90SDmitry Karpeev 
2010e7c4fc90SDmitry Karpeev   Output Parameters:
2011bb7acecfSBarry Smith + len       - The number of fields (or NULL if not requested)
20120298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
20130298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
2014bb7acecfSBarry Smith - dmlist    - The `DM`s for each field subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
2015e7c4fc90SDmitry Karpeev 
2016e7c4fc90SDmitry Karpeev   Level: intermediate
2017e7c4fc90SDmitry Karpeev 
2018bb7acecfSBarry Smith   Note:
2019e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2020bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2021bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2022e7c4fc90SDmitry Karpeev 
2023bb7acecfSBarry Smith   Fortran Note:
2024bb7acecfSBarry Smith   Not available in Fortran.
2025bb7acecfSBarry Smith 
2026bb7acecfSBarry Smith   Developer Note:
2027bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2028bb7acecfSBarry Smith 
2029bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2030e7c4fc90SDmitry Karpeev @*/
2031d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2032d71ae5a4SJacob Faibussowitsch {
2033e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2034e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20358865f1eaSKarl Rupp   if (len) {
2036534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
20378865f1eaSKarl Rupp     *len = 0;
20388865f1eaSKarl Rupp   }
20398865f1eaSKarl Rupp   if (namelist) {
20408865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
2041ea78f98cSLisandro Dalcin     *namelist = NULL;
20428865f1eaSKarl Rupp   }
20438865f1eaSKarl Rupp   if (islist) {
20448865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
2045ea78f98cSLisandro Dalcin     *islist = NULL;
20468865f1eaSKarl Rupp   }
20478865f1eaSKarl Rupp   if (dmlist) {
20488865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
2049ea78f98cSLisandro Dalcin     *dmlist = NULL;
20508865f1eaSKarl Rupp   }
2051f3f0edfdSDmitry Karpeev   /*
2052f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2053f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2054f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2055f3f0edfdSDmitry Karpeev    */
20567a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2058435a35e8SMatthew G Knepley     PetscSection section;
2059435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2060435a35e8SMatthew G Knepley 
20619566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20629566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2063435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2064f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20659566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20669566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20679566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2068435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2069435a35e8SMatthew G Knepley         const char *fieldName;
2070435a35e8SMatthew G Knepley 
20719566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
207203dc3394SMatthew G. Knepley         if (namelist) {
20739566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20749566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2075435a35e8SMatthew G Knepley         }
207603dc3394SMatthew G. Knepley       }
2077435a35e8SMatthew G Knepley     } else {
20789566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2079e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20800298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2081e7c4fc90SDmitry Karpeev     }
2082dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
208416621825SDmitry Karpeev }
208516621825SDmitry Karpeev 
2086412a4547SAlexis Marboeuf /*@C
2087412a4547SAlexis Marboeuf   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
2088412a4547SAlexis Marboeuf                   The fields are defined by DMCreateFieldIS().
2089435a35e8SMatthew G Knepley 
2090435a35e8SMatthew G Knepley   Not collective
2091435a35e8SMatthew G Knepley 
2092435a35e8SMatthew G Knepley   Input Parameters:
2093bb7acecfSBarry Smith + dm        - The `DM` object
2094bb7acecfSBarry Smith . numFields - The number of fields to select
20952adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2096435a35e8SMatthew G Knepley 
2097435a35e8SMatthew G Knepley   Output Parameters:
2098bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2099bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2100435a35e8SMatthew G Knepley 
2101bb7acecfSBarry Smith   Note:
2102bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21035d3b26e6SMatthew G. Knepley 
2104435a35e8SMatthew G Knepley   Level: intermediate
2105435a35e8SMatthew G Knepley 
2106bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2107435a35e8SMatthew G Knepley @*/
2108d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2109d71ae5a4SJacob Faibussowitsch {
2110435a35e8SMatthew G Knepley   PetscFunctionBegin;
2111435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2112dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
21138865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
21148865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2115dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2117435a35e8SMatthew G Knepley }
2118435a35e8SMatthew G Knepley 
21192adcc780SMatthew G. Knepley /*@C
2120bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21212adcc780SMatthew G. Knepley 
21222adcc780SMatthew G. Knepley   Not collective
21232adcc780SMatthew G. Knepley 
2124d8d19677SJose E. Roman   Input Parameters:
2125bb7acecfSBarry Smith + dms - The `DM` objects
2126bb7acecfSBarry Smith - n - The number of `DM`s
21272adcc780SMatthew G. Knepley 
21282adcc780SMatthew G. Knepley   Output Parameters:
2129bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2130bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21312adcc780SMatthew G. Knepley 
2132bb7acecfSBarry Smith   Note:
2133bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21345d3b26e6SMatthew G. Knepley 
21352adcc780SMatthew G. Knepley   Level: intermediate
21362adcc780SMatthew G. Knepley 
2137bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21382adcc780SMatthew G. Knepley @*/
2139d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2140d71ae5a4SJacob Faibussowitsch {
21412adcc780SMatthew G. Knepley   PetscInt i;
21422adcc780SMatthew G. Knepley 
21432adcc780SMatthew G. Knepley   PetscFunctionBegin;
21442adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2145ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21462adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2147a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2148bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2149bb7acecfSBarry Smith   if (n) {
2150b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2151dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21522adcc780SMatthew G. Knepley   }
21533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21542adcc780SMatthew G. Knepley }
21552adcc780SMatthew G. Knepley 
215616621825SDmitry Karpeev /*@C
2157bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2158bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2159bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2160bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2161bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
216216621825SDmitry Karpeev 
216316621825SDmitry Karpeev   Not collective
216416621825SDmitry Karpeev 
216516621825SDmitry Karpeev   Input Parameter:
2166bb7acecfSBarry Smith . dm - the `DM` object
216716621825SDmitry Karpeev 
216816621825SDmitry Karpeev   Output Parameters:
2169bb7acecfSBarry Smith + n            - The number of subproblems in the domain decomposition (or NULL if not requested)
21700298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
21710298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21720298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2173bb7acecfSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
217416621825SDmitry Karpeev 
217516621825SDmitry Karpeev   Level: intermediate
217616621825SDmitry Karpeev 
2177bb7acecfSBarry Smith   Note:
217816621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2179bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2180bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
218116621825SDmitry Karpeev 
2182bb7acecfSBarry Smith   Questions:
2183bb7acecfSBarry Smith   The dmlist is for the inner subdomains or the outer subdomains or all subdomains?
2184bb7acecfSBarry Smith 
2185bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
218616621825SDmitry Karpeev @*/
2187d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2188d71ae5a4SJacob Faibussowitsch {
2189be081cd6SPeter Brune   DMSubDomainHookLink link;
2190be081cd6SPeter Brune   PetscInt            i, l;
219116621825SDmitry Karpeev 
219216621825SDmitry Karpeev   PetscFunctionBegin;
219316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21949371c9d4SSatish Balay   if (n) {
21959371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
21969371c9d4SSatish Balay     *n = 0;
21979371c9d4SSatish Balay   }
21989371c9d4SSatish Balay   if (namelist) {
21999371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
22009371c9d4SSatish Balay     *namelist = NULL;
22019371c9d4SSatish Balay   }
22029371c9d4SSatish Balay   if (innerislist) {
22039371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
22049371c9d4SSatish Balay     *innerislist = NULL;
22059371c9d4SSatish Balay   }
22069371c9d4SSatish Balay   if (outerislist) {
22079371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
22089371c9d4SSatish Balay     *outerislist = NULL;
22099371c9d4SSatish Balay   }
22109371c9d4SSatish Balay   if (dmlist) {
22119371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
22129371c9d4SSatish Balay     *dmlist = NULL;
22139371c9d4SSatish Balay   }
2214f3f0edfdSDmitry Karpeev   /*
2215f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2216f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2217f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2218f3f0edfdSDmitry Karpeev    */
22197a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
222016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2221dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
222214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2223f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2224be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2225be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22269566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2227be081cd6SPeter Brune         }
2228648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2229e7c4fc90SDmitry Karpeev       }
223014a18fd3SPeter Brune     }
2231bb7acecfSBarry Smith     if (n) *n = l;
223214a18fd3SPeter Brune   }
22333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2234e30e807fSPeter Brune }
2235e30e807fSPeter Brune 
2236e30e807fSPeter Brune /*@C
2237e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2238e30e807fSPeter Brune 
2239e30e807fSPeter Brune   Not collective
2240e30e807fSPeter Brune 
2241e30e807fSPeter Brune   Input Parameters:
2242bb7acecfSBarry Smith + dm - the `DM` object
2243e30e807fSPeter Brune . n  - the number of subdomain scatters
2244e30e807fSPeter Brune - subdms - the local subdomains
2245e30e807fSPeter Brune 
2246e30e807fSPeter Brune   Output Parameters:
22476b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2248e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2249e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2250e30e807fSPeter Brune 
2251bb7acecfSBarry Smith   Note:
2252bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2253e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2254e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2255e30e807fSPeter Brune   solution and residual data.
2256e30e807fSPeter Brune 
2257bb7acecfSBarry Smith   Questions:
2258bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2259bb7acecfSBarry Smith 
2260e30e807fSPeter Brune   Level: developer
2261e30e807fSPeter Brune 
2262bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2263e30e807fSPeter Brune @*/
2264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2265d71ae5a4SJacob Faibussowitsch {
2266e30e807fSPeter Brune   PetscFunctionBegin;
2267e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2268e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2269dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2271e7c4fc90SDmitry Karpeev }
2272e7c4fc90SDmitry Karpeev 
227347c6ae99SBarry Smith /*@
2274bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
227547c6ae99SBarry Smith 
2276d083f849SBarry Smith   Collective on dm
227747c6ae99SBarry Smith 
2278d8d19677SJose E. Roman   Input Parameters:
2279bb7acecfSBarry Smith + dm   - the `DM` object
2280bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
228147c6ae99SBarry Smith 
228247c6ae99SBarry Smith   Output Parameter:
2283bb7acecfSBarry Smith . dmf - the refined `D`M, or NULL
2284ae0a1c52SMatthew G Knepley 
2285f27dd7c6SMatthew G. Knepley   Options Database Keys:
2286412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2287412e9a14SMatthew G. Knepley 
2288bb7acecfSBarry Smith   Note:
2289bb7acecfSBarry Smith   If no refinement was done, the return value is NULL
229047c6ae99SBarry Smith 
229147c6ae99SBarry Smith   Level: developer
229247c6ae99SBarry Smith 
2293bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
229447c6ae99SBarry Smith @*/
2295d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2296d71ae5a4SJacob Faibussowitsch {
2297c833c3b5SJed Brown   DMRefineHookLink link;
229847c6ae99SBarry Smith 
229947c6ae99SBarry Smith   PetscFunctionBegin;
2300732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2302dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23034057135bSMatthew G Knepley   if (*dmf) {
230443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23058865f1eaSKarl Rupp 
23069566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23078865f1eaSKarl Rupp 
2308644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23090598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2310656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23118865f1eaSKarl Rupp 
23129566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2313c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23141baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2315c833c3b5SJed Brown     }
2316c833c3b5SJed Brown   }
23179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2319c833c3b5SJed Brown }
2320c833c3b5SJed Brown 
2321bb9467b5SJed Brown /*@C
2322c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2323c833c3b5SJed Brown 
2324bb7acecfSBarry Smith    Logically Collective on coarse
2325c833c3b5SJed Brown 
23264165533cSJose E. Roman    Input Parameters:
2327bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2328bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2329bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
23300298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2331c833c3b5SJed Brown 
2332c833c3b5SJed Brown    Calling sequence of refinehook:
2333c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2334c833c3b5SJed Brown 
2335bb7acecfSBarry Smith +  coarse - coarse level `DM`
2336bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2337c833c3b5SJed Brown -  ctx - optional user-defined function context
2338c833c3b5SJed Brown 
2339c833c3b5SJed Brown    Calling sequence for interphook:
2340c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2341c833c3b5SJed Brown 
2342bb7acecfSBarry Smith +  coarse - coarse level `DM`
2343c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2344bb7acecfSBarry Smith .  fine - fine level `DM` to update
2345c833c3b5SJed Brown -  ctx - optional user-defined function context
2346c833c3b5SJed Brown 
2347c833c3b5SJed Brown    Level: advanced
2348c833c3b5SJed Brown 
2349c833c3b5SJed Brown    Notes:
2350bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2351bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2352bb7acecfSBarry Smith 
2353bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2354c833c3b5SJed Brown 
2355c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2356c833c3b5SJed Brown 
2357bb7acecfSBarry Smith    Fortran Note:
2358bb7acecfSBarry Smith    This function is not available from Fortran.
2359bb9467b5SJed Brown 
2360bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2361c833c3b5SJed Brown @*/
2362d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2363d71ae5a4SJacob Faibussowitsch {
2364c833c3b5SJed Brown   DMRefineHookLink link, *p;
2365c833c3b5SJed Brown 
2366c833c3b5SJed Brown   PetscFunctionBegin;
2367c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23683d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23693ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23703d8e3701SJed Brown   }
23719566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2372c833c3b5SJed Brown   link->refinehook = refinehook;
2373c833c3b5SJed Brown   link->interphook = interphook;
2374c833c3b5SJed Brown   link->ctx        = ctx;
23750298fd71SBarry Smith   link->next       = NULL;
2376c833c3b5SJed Brown   *p               = link;
23773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2378c833c3b5SJed Brown }
2379c833c3b5SJed Brown 
23803d8e3701SJed Brown /*@C
2381bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2382bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23833d8e3701SJed Brown 
2384bb7acecfSBarry Smith    Logically Collective on coarse
23853d8e3701SJed Brown 
23864165533cSJose E. Roman    Input Parameters:
2387bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2388bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2389bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
23903d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23913d8e3701SJed Brown 
23923d8e3701SJed Brown    Level: advanced
23933d8e3701SJed Brown 
2394bb7acecfSBarry Smith    Note:
23953d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23963d8e3701SJed Brown 
2397bb7acecfSBarry Smith    Fortran Note:
2398bb7acecfSBarry Smith    This function is not available from Fortran.
23993d8e3701SJed Brown 
2400bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24013d8e3701SJed Brown @*/
2402d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2403d71ae5a4SJacob Faibussowitsch {
24043d8e3701SJed Brown   DMRefineHookLink link, *p;
24053d8e3701SJed Brown 
24063d8e3701SJed Brown   PetscFunctionBegin;
24073d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24083d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24093d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24103d8e3701SJed Brown       link = *p;
24113d8e3701SJed Brown       *p   = link->next;
24129566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24133d8e3701SJed Brown       break;
24143d8e3701SJed Brown     }
24153d8e3701SJed Brown   }
24163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24173d8e3701SJed Brown }
24183d8e3701SJed Brown 
2419c833c3b5SJed Brown /*@
2420bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2421c833c3b5SJed Brown 
2422c833c3b5SJed Brown    Collective if any hooks are
2423c833c3b5SJed Brown 
24244165533cSJose E. Roman    Input Parameters:
2425bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2426bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2427bb7acecfSBarry Smith -  fine - finer `DM` to update
2428c833c3b5SJed Brown 
2429c833c3b5SJed Brown    Level: developer
2430c833c3b5SJed Brown 
2431bb7acecfSBarry Smith    Developer Note:
2432bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2433bb7acecfSBarry Smith    an API with consistent terminology.
2434bb7acecfSBarry Smith 
2435bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2436c833c3b5SJed Brown @*/
2437d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2438d71ae5a4SJacob Faibussowitsch {
2439c833c3b5SJed Brown   DMRefineHookLink link;
2440c833c3b5SJed Brown 
2441c833c3b5SJed Brown   PetscFunctionBegin;
2442c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24431baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24444057135bSMatthew G Knepley   }
24453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
244647c6ae99SBarry Smith }
244747c6ae99SBarry Smith 
2448eb3f98d2SBarry Smith /*@
24491f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24501f3379b2SToby Isaac 
2451bb7acecfSBarry Smith    Collective on dm
24521f3379b2SToby Isaac 
24534165533cSJose E. Roman    Input Parameters:
2454bb7acecfSBarry Smith +  coarse - coarse `DM`
2455bb7acecfSBarry Smith .  fine   - fine `DM`
2456bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2457bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2458bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24591f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24601f3379b2SToby Isaac 
24614165533cSJose E. Roman    Output Parameter:
24621f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24631f3379b2SToby Isaac 
24641f3379b2SToby Isaac    Level: developer
24651f3379b2SToby Isaac 
2466bb7acecfSBarry Smith    Note:
2467bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24681f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24691f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24701f3379b2SToby Isaac    slope-limiting reconstruction.
24711f3379b2SToby Isaac 
2472bb7acecfSBarry Smith    Developer Note:
2473bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2474bb7acecfSBarry Smith 
2475bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
24761f3379b2SToby Isaac @*/
2477d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2478d71ae5a4SJacob Faibussowitsch {
24791f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24801f3379b2SToby Isaac 
24811f3379b2SToby Isaac   PetscFunctionBegin;
24821f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24831f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24841f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24851f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24861f3379b2SToby Isaac 
24879566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24881f3379b2SToby Isaac   if (interpsol) {
24899566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24901f3379b2SToby Isaac   } else if (interp) {
24919566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
249298921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24941f3379b2SToby Isaac }
24951f3379b2SToby Isaac 
24961f3379b2SToby Isaac /*@
2497bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2498eb3f98d2SBarry Smith 
2499eb3f98d2SBarry Smith     Not Collective
2500eb3f98d2SBarry Smith 
2501eb3f98d2SBarry Smith     Input Parameter:
2502bb7acecfSBarry Smith .   dm - the `DM` object
2503eb3f98d2SBarry Smith 
2504eb3f98d2SBarry Smith     Output Parameter:
2505eb3f98d2SBarry Smith .   level - number of refinements
2506eb3f98d2SBarry Smith 
2507eb3f98d2SBarry Smith     Level: developer
2508eb3f98d2SBarry Smith 
2509bb7acecfSBarry Smith     Note:
2510bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2511bb7acecfSBarry Smith 
2512bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2513eb3f98d2SBarry Smith @*/
2514d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2515d71ae5a4SJacob Faibussowitsch {
2516eb3f98d2SBarry Smith   PetscFunctionBegin;
2517eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2518eb3f98d2SBarry Smith   *level = dm->levelup;
25193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2520eb3f98d2SBarry Smith }
2521eb3f98d2SBarry Smith 
2522fef3a512SBarry Smith /*@
2523bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2524fef3a512SBarry Smith 
2525fef3a512SBarry Smith     Not Collective
2526fef3a512SBarry Smith 
2527d8d19677SJose E. Roman     Input Parameters:
2528bb7acecfSBarry Smith +   dm - the `DM` object
2529fef3a512SBarry Smith -   level - number of refinements
2530fef3a512SBarry Smith 
2531fef3a512SBarry Smith     Level: advanced
2532fef3a512SBarry Smith 
253395452b02SPatrick Sanan     Notes:
2534bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2535fef3a512SBarry Smith 
2536bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2537bb7acecfSBarry Smith 
2538bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2539fef3a512SBarry Smith @*/
2540d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2541d71ae5a4SJacob Faibussowitsch {
2542fef3a512SBarry Smith   PetscFunctionBegin;
2543fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2544fef3a512SBarry Smith   dm->levelup = level;
25453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2546fef3a512SBarry Smith }
2547fef3a512SBarry Smith 
2548d410b0cfSMatthew G. Knepley /*@
2549bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2550d410b0cfSMatthew G. Knepley 
2551d410b0cfSMatthew G. Knepley   Collective on dm
2552d410b0cfSMatthew G. Knepley 
2553f1a722f8SMatthew G. Knepley   Input Parameters:
2554bb7acecfSBarry Smith + dm     - the `DM` object
2555d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2556d410b0cfSMatthew G. Knepley 
2557d410b0cfSMatthew G. Knepley   Output Parameter:
2558bb7acecfSBarry Smith . dme - the extruded `DM`, or NULL
2559d410b0cfSMatthew G. Knepley 
2560bb7acecfSBarry Smith   Note:
2561bb7acecfSBarry Smith   If no extrusion was done, the return value is NULL
2562d410b0cfSMatthew G. Knepley 
2563d410b0cfSMatthew G. Knepley   Level: developer
2564d410b0cfSMatthew G. Knepley 
2565bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2566d410b0cfSMatthew G. Knepley @*/
2567d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2568d71ae5a4SJacob Faibussowitsch {
2569d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2570d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2571dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2572d410b0cfSMatthew G. Knepley   if (*dme) {
2573d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25749566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2575d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25769566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2577d410b0cfSMatthew G. Knepley   }
25783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2579d410b0cfSMatthew G. Knepley }
2580d410b0cfSMatthew G. Knepley 
2581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2582d71ae5a4SJacob Faibussowitsch {
2583ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2584ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2585ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2586ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2588ca3d3a14SMatthew G. Knepley }
2589ca3d3a14SMatthew G. Knepley 
2590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2591d71ae5a4SJacob Faibussowitsch {
2592ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2593ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2594ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2595ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2597ca3d3a14SMatthew G. Knepley }
2598ca3d3a14SMatthew G. Knepley 
2599ca3d3a14SMatthew G. Knepley /*@
2600bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2601ca3d3a14SMatthew G. Knepley 
2602ca3d3a14SMatthew G. Knepley   Input Parameter:
2603ca3d3a14SMatthew G. Knepley . dm - The DM
2604ca3d3a14SMatthew G. Knepley 
2605ca3d3a14SMatthew G. Knepley   Output Parameter:
2606ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2607ca3d3a14SMatthew G. Knepley 
2608ca3d3a14SMatthew G. Knepley   Level: developer
2609ca3d3a14SMatthew G. Knepley 
2610bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2611ca3d3a14SMatthew G. Knepley @*/
2612d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2613d71ae5a4SJacob Faibussowitsch {
2614ca3d3a14SMatthew G. Knepley   Vec tv;
2615ca3d3a14SMatthew G. Knepley 
2616ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2617ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2618534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
26199566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2620ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2622ca3d3a14SMatthew G. Knepley }
2623ca3d3a14SMatthew G. Knepley 
2624d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2625d71ae5a4SJacob Faibussowitsch {
2626ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2627ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2628ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2629ca3d3a14SMatthew G. Knepley 
2630ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26319566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26329566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26339566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26349566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26359566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26369566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2639ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2641ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2642ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2643ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26449566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2645ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26469566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26479566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2648ca3d3a14SMatthew G. Knepley     }
2649ca3d3a14SMatthew G. Knepley   }
26509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26519566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26529566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2653ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2654ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2656ca3d3a14SMatthew G. Knepley       if (dof) {
2657ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2658ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2659ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2660ca3d3a14SMatthew G. Knepley 
2661ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26629566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26639566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26649566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2665ca3d3a14SMatthew G. Knepley       }
2666ca3d3a14SMatthew G. Knepley     }
2667ca3d3a14SMatthew G. Knepley   }
26689566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2670ca3d3a14SMatthew G. Knepley }
2671ca3d3a14SMatthew G. Knepley 
2672d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2673d71ae5a4SJacob Faibussowitsch {
2674ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2675ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2676ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2677ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2678ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2679ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2680ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26819566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2683ca3d3a14SMatthew G. Knepley }
2684ca3d3a14SMatthew G. Knepley 
2685bb9467b5SJed Brown /*@C
2686bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2687baf369e7SPeter Brune 
2688bb7acecfSBarry Smith    Logically Collective on dm
2689baf369e7SPeter Brune 
26904165533cSJose E. Roman    Input Parameters:
2691bb7acecfSBarry Smith +  dm - the `DM`
2692bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2693bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
26940298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2695baf369e7SPeter Brune 
2696baf369e7SPeter Brune    Calling sequence for beginhook:
2697baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2698baf369e7SPeter Brune 
2699baf369e7SPeter Brune +  dm - global DM
2700baf369e7SPeter Brune .  g - global vector
2701baf369e7SPeter Brune .  mode - mode
2702baf369e7SPeter Brune .  l - local vector
2703baf369e7SPeter Brune -  ctx - optional user-defined function context
2704baf369e7SPeter Brune 
2705baf369e7SPeter Brune    Calling sequence for endhook:
2706ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2707baf369e7SPeter Brune 
2708baf369e7SPeter Brune +  global - global DM
2709baf369e7SPeter Brune -  ctx - optional user-defined function context
2710baf369e7SPeter Brune 
2711baf369e7SPeter Brune    Level: advanced
2712baf369e7SPeter Brune 
2713bb7acecfSBarry Smith    Note:
2714bb7acecfSBarry 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.
2715bb7acecfSBarry Smith 
2716bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2717baf369e7SPeter Brune @*/
2718d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2719d71ae5a4SJacob Faibussowitsch {
2720baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2721baf369e7SPeter Brune 
2722baf369e7SPeter Brune   PetscFunctionBegin;
2723baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2724baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27259566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2726baf369e7SPeter Brune   link->beginhook = beginhook;
2727baf369e7SPeter Brune   link->endhook   = endhook;
2728baf369e7SPeter Brune   link->ctx       = ctx;
27290298fd71SBarry Smith   link->next      = NULL;
2730baf369e7SPeter Brune   *p              = link;
27313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2732baf369e7SPeter Brune }
2733baf369e7SPeter Brune 
2734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2735d71ae5a4SJacob Faibussowitsch {
27364c274da1SToby Isaac   Mat          cMat;
273779769bd5SJed Brown   Vec          cVec, cBias;
27384c274da1SToby Isaac   PetscSection section, cSec;
27394c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27404c274da1SToby Isaac 
27414c274da1SToby Isaac   PetscFunctionBegin;
27424c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27439566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27444c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27455db9a05bSToby Isaac     PetscInt nRows;
27465db9a05bSToby Isaac 
27479566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27483ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27499566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27509566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27519566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27529566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27544c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27564c274da1SToby Isaac       if (dof) {
27574c274da1SToby Isaac         PetscScalar *vals;
27589566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27599566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27604c274da1SToby Isaac       }
27614c274da1SToby Isaac     }
27629566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27634c274da1SToby Isaac   }
27643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27654c274da1SToby Isaac }
27664c274da1SToby Isaac 
276747c6ae99SBarry Smith /*@
276801729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
276901729b5cSPatrick Sanan 
2770d083f849SBarry Smith     Neighbor-wise Collective on dm
277101729b5cSPatrick Sanan 
277201729b5cSPatrick Sanan     Input Parameters:
2773bb7acecfSBarry Smith +   dm - the `DM` object
277401729b5cSPatrick Sanan .   g - the global vector
2775bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
277601729b5cSPatrick Sanan -   l - the local vector
277701729b5cSPatrick Sanan 
277801729b5cSPatrick Sanan     Notes:
2779bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2780bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2781bb7acecfSBarry Smith 
2782bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
278301729b5cSPatrick Sanan 
278401729b5cSPatrick Sanan     Level: beginner
278501729b5cSPatrick Sanan 
2786bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2787bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2788bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
278901729b5cSPatrick Sanan @*/
2790d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2791d71ae5a4SJacob Faibussowitsch {
279201729b5cSPatrick Sanan   PetscFunctionBegin;
27939566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27949566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
279601729b5cSPatrick Sanan }
279701729b5cSPatrick Sanan 
279801729b5cSPatrick Sanan /*@
279947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
280047c6ae99SBarry Smith 
2801d083f849SBarry Smith     Neighbor-wise Collective on dm
280247c6ae99SBarry Smith 
280347c6ae99SBarry Smith     Input Parameters:
2804bb7acecfSBarry Smith +   dm - the `DM` object
280547c6ae99SBarry Smith .   g - the global vector
2806bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
280747c6ae99SBarry Smith -   l - the local vector
280847c6ae99SBarry Smith 
280901729b5cSPatrick Sanan     Level: intermediate
281047c6ae99SBarry Smith 
2811bb7acecfSBarry Smith     Notes:
2812bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2813bb7acecfSBarry Smith 
2814bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2815bb7acecfSBarry Smith 
2816bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2817bb7acecfSBarry Smith 
2818bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2819bb7acecfSBarry Smith 
2820bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
282147c6ae99SBarry Smith @*/
2822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2823d71ae5a4SJacob Faibussowitsch {
28247128ae9fSMatthew G Knepley   PetscSF                 sf;
2825baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
282647c6ae99SBarry Smith 
282747c6ae99SBarry Smith   PetscFunctionBegin;
2828171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2829baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28301baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2831baf369e7SPeter Brune   }
28329566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28337128ae9fSMatthew G Knepley   if (sf) {
2834ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2835ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2836d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28377128ae9fSMatthew G Knepley 
28387a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28399566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28409566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28419566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28429566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28439566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28447128ae9fSMatthew G Knepley   } else {
28459566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28467128ae9fSMatthew G Knepley   }
28473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
284847c6ae99SBarry Smith }
284947c6ae99SBarry Smith 
285047c6ae99SBarry Smith /*@
285147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
285247c6ae99SBarry Smith 
2853d083f849SBarry Smith     Neighbor-wise Collective on dm
285447c6ae99SBarry Smith 
285547c6ae99SBarry Smith     Input Parameters:
2856bb7acecfSBarry Smith +   dm - the `DM` object
285747c6ae99SBarry Smith .   g - the global vector
2858bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
285947c6ae99SBarry Smith -   l - the local vector
286047c6ae99SBarry Smith 
286101729b5cSPatrick Sanan     Level: intermediate
286247c6ae99SBarry Smith 
2863bb7acecfSBarry Smith     Note:
2864bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2865bb7acecfSBarry Smith 
2866bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
286747c6ae99SBarry Smith @*/
2868d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2869d71ae5a4SJacob Faibussowitsch {
28707128ae9fSMatthew G Knepley   PetscSF                 sf;
2871ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2872ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2873ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2874baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2875d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
287647c6ae99SBarry Smith 
287747c6ae99SBarry Smith   PetscFunctionBegin;
2878171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28799566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28809566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28817128ae9fSMatthew G Knepley   if (sf) {
28827a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28837128ae9fSMatthew G Knepley 
28849566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28859566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28869566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28879566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28889566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28899566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28907128ae9fSMatthew G Knepley   } else {
28919566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28927128ae9fSMatthew G Knepley   }
28939566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2894baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28959566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2896baf369e7SPeter Brune   }
28973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
289847c6ae99SBarry Smith }
289947c6ae99SBarry Smith 
2900d4d07f1eSToby Isaac /*@C
2901d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2902d4d07f1eSToby Isaac 
2903bb7acecfSBarry Smith    Logically Collective on dm
2904d4d07f1eSToby Isaac 
29054165533cSJose E. Roman    Input Parameters:
2906bb7acecfSBarry Smith +  dm - the `DM`
2907bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2908bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2909d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2910d4d07f1eSToby Isaac 
2911d4d07f1eSToby Isaac    Calling sequence for beginhook:
2912d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2913d4d07f1eSToby Isaac 
2914bb7acecfSBarry Smith +  dm - global `DM`
2915d4d07f1eSToby Isaac .  l - local vector
2916d4d07f1eSToby Isaac .  mode - mode
2917d4d07f1eSToby Isaac .  g - global vector
2918d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2919d4d07f1eSToby Isaac 
2920d4d07f1eSToby Isaac    Calling sequence for endhook:
2921d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2922d4d07f1eSToby Isaac 
2923bb7acecfSBarry Smith +  global - global `DM`
2924d4d07f1eSToby Isaac .  l - local vector
2925d4d07f1eSToby Isaac .  mode - mode
2926d4d07f1eSToby Isaac .  g - global vector
2927d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2928d4d07f1eSToby Isaac 
2929d4d07f1eSToby Isaac    Level: advanced
2930d4d07f1eSToby Isaac 
2931bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2932d4d07f1eSToby Isaac @*/
2933d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2934d71ae5a4SJacob Faibussowitsch {
2935d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2936d4d07f1eSToby Isaac 
2937d4d07f1eSToby Isaac   PetscFunctionBegin;
2938d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2939d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29409566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2941d4d07f1eSToby Isaac   link->beginhook = beginhook;
2942d4d07f1eSToby Isaac   link->endhook   = endhook;
2943d4d07f1eSToby Isaac   link->ctx       = ctx;
2944d4d07f1eSToby Isaac   link->next      = NULL;
2945d4d07f1eSToby Isaac   *p              = link;
29463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2947d4d07f1eSToby Isaac }
2948d4d07f1eSToby Isaac 
2949d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2950d71ae5a4SJacob Faibussowitsch {
29514c274da1SToby Isaac   Mat          cMat;
29524c274da1SToby Isaac   Vec          cVec;
29534c274da1SToby Isaac   PetscSection section, cSec;
29544c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29554c274da1SToby Isaac 
29564c274da1SToby Isaac   PetscFunctionBegin;
29574c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29589566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29594c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29605db9a05bSToby Isaac     PetscInt nRows;
29615db9a05bSToby Isaac 
29629566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29633ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29649566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29659566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29674c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29694c274da1SToby Isaac       if (dof) {
29704c274da1SToby Isaac         PetscInt     d;
29714c274da1SToby Isaac         PetscScalar *vals;
29729566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29739566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29744c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29754c274da1SToby Isaac          * we just extracted */
2976ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29774c274da1SToby Isaac       }
29784c274da1SToby Isaac     }
29799566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29809566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29814c274da1SToby Isaac   }
29823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29834c274da1SToby Isaac }
298401729b5cSPatrick Sanan /*@
298501729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
298601729b5cSPatrick Sanan 
2987d083f849SBarry Smith     Neighbor-wise Collective on dm
298801729b5cSPatrick Sanan 
298901729b5cSPatrick Sanan     Input Parameters:
2990bb7acecfSBarry Smith +   dm - the `DM` object
299101729b5cSPatrick Sanan .   l - the local vector
2992bb7acecfSBarry 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.
299301729b5cSPatrick Sanan -   g - the global vector
299401729b5cSPatrick Sanan 
299501729b5cSPatrick Sanan     Notes:
299601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2997bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
299801729b5cSPatrick Sanan 
2999bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3000bb7acecfSBarry Smith 
3001bb7acecfSBarry 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.
3002bb7acecfSBarry Smith 
3003bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
300401729b5cSPatrick Sanan 
300501729b5cSPatrick Sanan     Level: beginner
300601729b5cSPatrick Sanan 
3007bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
300801729b5cSPatrick Sanan @*/
3009d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3010d71ae5a4SJacob Faibussowitsch {
301101729b5cSPatrick Sanan   PetscFunctionBegin;
30129566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30139566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
301501729b5cSPatrick Sanan }
30164c274da1SToby Isaac 
301747c6ae99SBarry Smith /*@
301801729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
30199a42bb27SBarry Smith 
3020d083f849SBarry Smith     Neighbor-wise Collective on dm
30219a42bb27SBarry Smith 
30229a42bb27SBarry Smith     Input Parameters:
3023bb7acecfSBarry Smith +   dm - the `DM` object
3024f6813fd5SJed Brown .   l - the local vector
3025aa624791SPierre 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.
30261eb28f2eSBarry Smith -   g - the global vector
30279a42bb27SBarry Smith 
302895452b02SPatrick Sanan     Notes:
3029bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3030bb7acecfSBarry Smith 
3031bb7acecfSBarry 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.
3032bb7acecfSBarry Smith 
3033bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
3034bb7acecfSBarry Smith 
3035bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3036bb7acecfSBarry Smith 
3037bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30389a42bb27SBarry Smith 
303901729b5cSPatrick Sanan     Level: intermediate
30409a42bb27SBarry Smith 
3041bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30429a42bb27SBarry Smith @*/
3043d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3044d71ae5a4SJacob Faibussowitsch {
30457128ae9fSMatthew G Knepley   PetscSF                 sf;
304684330215SMatthew G. Knepley   PetscSection            s, gs;
3047d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3048ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3049ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3050ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3051fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3052d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30539a42bb27SBarry Smith 
30549a42bb27SBarry Smith   PetscFunctionBegin;
3055171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3056d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30571baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3058d4d07f1eSToby Isaac   }
30599566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30609566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30619566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30627128ae9fSMatthew G Knepley   switch (mode) {
30637128ae9fSMatthew G Knepley   case INSERT_VALUES:
30647128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3065d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3066d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3067d71ae5a4SJacob Faibussowitsch     break;
30687128ae9fSMatthew G Knepley   case ADD_VALUES:
30697128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3070d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3071d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3072d71ae5a4SJacob Faibussowitsch     break;
3073d71ae5a4SJacob Faibussowitsch   default:
3074d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30757128ae9fSMatthew G Knepley   }
3076ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30779566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3078ca3d3a14SMatthew G. Knepley     if (transform) {
30799566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30809566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30819566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30829566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3083fa88e482SJed Brown     } else if (isInsert) {
30849566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3085fa88e482SJed Brown     } else {
30869566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3087fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3088ca3d3a14SMatthew G. Knepley     }
3089fa88e482SJed Brown     if (s && isInsert) {
30909566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3091fa88e482SJed Brown     } else {
30929566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3093fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3094fa88e482SJed Brown     }
3095ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30969566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
309784330215SMatthew G. Knepley     } else if (s && isInsert) {
309884330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
309984330215SMatthew G. Knepley 
31009566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31019566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31029566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
310384330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3104b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
310584330215SMatthew G. Knepley 
31069566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31089566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31099566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31109566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3112b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
311303442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31147a8be351SBarry 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);
3115b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3116b3b16f48SMatthew G. Knepley         if (!gcdof) {
311784330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3118b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3119b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
312084330215SMatthew G. Knepley           const PetscInt *cdofs;
312184330215SMatthew G. Knepley           PetscInt        cind = 0;
312284330215SMatthew G. Knepley 
31239566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3124b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31259371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31269371c9d4SSatish Balay               ++cind;
31279371c9d4SSatish Balay               continue;
31289371c9d4SSatish Balay             }
3129b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
313084330215SMatthew G. Knepley           }
31317a8be351SBarry 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);
313284330215SMatthew G. Knepley       }
3133ca3d3a14SMatthew G. Knepley     }
3134fa88e482SJed Brown     if (g_inplace) {
31359566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3136fa88e482SJed Brown     } else {
31379566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3138fa88e482SJed Brown     }
3139ca3d3a14SMatthew G. Knepley     if (transform) {
31409566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31419566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3142fa88e482SJed Brown     } else if (l_inplace) {
31439566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3144ca3d3a14SMatthew G. Knepley     } else {
31459566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3146ca3d3a14SMatthew G. Knepley     }
31477128ae9fSMatthew G Knepley   } else {
31489566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31497128ae9fSMatthew G Knepley   }
31503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31519a42bb27SBarry Smith }
31529a42bb27SBarry Smith 
31539a42bb27SBarry Smith /*@
31549a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
315547c6ae99SBarry Smith 
3156d083f849SBarry Smith     Neighbor-wise Collective on dm
315747c6ae99SBarry Smith 
315847c6ae99SBarry Smith     Input Parameters:
3159bb7acecfSBarry Smith +   dm - the `DM` object
3160f6813fd5SJed Brown .   l - the local vector
3161bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3162f6813fd5SJed Brown -   g - the global vector
316347c6ae99SBarry Smith 
316401729b5cSPatrick Sanan     Level: intermediate
316547c6ae99SBarry Smith 
3166bb7acecfSBarry Smith     Note:
3167bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3168bb7acecfSBarry Smith 
3169bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
317047c6ae99SBarry Smith @*/
3171d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3172d71ae5a4SJacob Faibussowitsch {
31737128ae9fSMatthew G Knepley   PetscSF                 sf;
317484330215SMatthew G. Knepley   PetscSection            s;
3175d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3176ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
317747c6ae99SBarry Smith 
317847c6ae99SBarry Smith   PetscFunctionBegin;
3179171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31809566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31819566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31827128ae9fSMatthew G Knepley   switch (mode) {
31837128ae9fSMatthew G Knepley   case INSERT_VALUES:
3184d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3185d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3186d71ae5a4SJacob Faibussowitsch     break;
31877128ae9fSMatthew G Knepley   case ADD_VALUES:
3188d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3189d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3190d71ae5a4SJacob Faibussowitsch     break;
3191d71ae5a4SJacob Faibussowitsch   default:
3192d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31937128ae9fSMatthew G Knepley   }
319484330215SMatthew G. Knepley   if (sf && !isInsert) {
3195ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3196ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3197ca3d3a14SMatthew G. Knepley     Vec                tmpl;
319884330215SMatthew G. Knepley 
31999566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3200ca3d3a14SMatthew G. Knepley     if (transform) {
32019566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32029566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3203ca3d3a14SMatthew G. Knepley     } else {
32049566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3205ca3d3a14SMatthew G. Knepley     }
32069566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32079566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3208ca3d3a14SMatthew G. Knepley     if (transform) {
32099566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32109566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3211ca3d3a14SMatthew G. Knepley     } else {
32129566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3213ca3d3a14SMatthew G. Knepley     }
32149566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
321584330215SMatthew G. Knepley   } else if (s && isInsert) {
32167128ae9fSMatthew G Knepley   } else {
32179566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
32187128ae9fSMatthew G Knepley   }
3219d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32209566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3221d4d07f1eSToby Isaac   }
32223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
322347c6ae99SBarry Smith }
322447c6ae99SBarry Smith 
3225f089877aSRichard Tran Mills /*@
3226bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3227bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3228bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3229f089877aSRichard Tran Mills 
3230d083f849SBarry Smith    Neighbor-wise Collective on dm
3231f089877aSRichard Tran Mills 
3232f089877aSRichard Tran Mills    Input Parameters:
3233bb7acecfSBarry Smith +  dm - the `DM` object
3234bc0a1609SRichard Tran Mills .  g - the original local vector
3235bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3236f089877aSRichard Tran Mills 
3237bc0a1609SRichard Tran Mills    Output Parameter:
3238bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3239f089877aSRichard Tran Mills 
3240f089877aSRichard Tran Mills    Level: intermediate
3241f089877aSRichard Tran Mills 
3242bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3243f089877aSRichard Tran Mills @*/
3244d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3245d71ae5a4SJacob Faibussowitsch {
3246f089877aSRichard Tran Mills   PetscFunctionBegin;
3247f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32489f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32499f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32509f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3252f089877aSRichard Tran Mills }
3253f089877aSRichard Tran Mills 
3254f089877aSRichard Tran Mills /*@
3255bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3256bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3257f089877aSRichard Tran Mills 
3258d083f849SBarry Smith    Neighbor-wise Collective on dm
3259f089877aSRichard Tran Mills 
3260f089877aSRichard Tran Mills    Input Parameters:
3261bb7acecfSBarry Smith +  da - the `DM` object
3262bc0a1609SRichard Tran Mills .  g - the original local vector
3263bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3264f089877aSRichard Tran Mills 
3265bc0a1609SRichard Tran Mills    Output Parameter:
3266bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3267f089877aSRichard Tran Mills 
3268f089877aSRichard Tran Mills    Level: intermediate
3269f089877aSRichard Tran Mills 
3270bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3271f089877aSRichard Tran Mills @*/
3272d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3273d71ae5a4SJacob Faibussowitsch {
3274f089877aSRichard Tran Mills   PetscFunctionBegin;
3275f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32769f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32779f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32789f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3280f089877aSRichard Tran Mills }
3281f089877aSRichard Tran Mills 
328247c6ae99SBarry Smith /*@
3283bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
328447c6ae99SBarry Smith 
3285d083f849SBarry Smith     Collective on dm
328647c6ae99SBarry Smith 
3287d8d19677SJose E. Roman     Input Parameters:
3288bb7acecfSBarry Smith +   dm - the `DM` object
3289bb7acecfSBarry Smith -   comm - the communicator to contain the new `DM` object (or MPI_COMM_NULL)
329047c6ae99SBarry Smith 
329147c6ae99SBarry Smith     Output Parameter:
3292bb7acecfSBarry Smith .   dmc - the coarsened `DM`
329347c6ae99SBarry Smith 
329447c6ae99SBarry Smith     Level: developer
329547c6ae99SBarry Smith 
3296bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
329747c6ae99SBarry Smith @*/
3298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3299d71ae5a4SJacob Faibussowitsch {
3300b17ce1afSJed Brown   DMCoarsenHookLink link;
330147c6ae99SBarry Smith 
330247c6ae99SBarry Smith   PetscFunctionBegin;
3303171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3305dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3306b9d85ea2SLisandro Dalcin   if (*dmc) {
3307a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33089566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
330943842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33109566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3311644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33120598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3313656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33149566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3315b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33169566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3317b17ce1afSJed Brown     }
3318b9d85ea2SLisandro Dalcin   }
33199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33207a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3322b17ce1afSJed Brown }
3323b17ce1afSJed Brown 
3324bb9467b5SJed Brown /*@C
3325b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3326b17ce1afSJed Brown 
3327bb7acecfSBarry Smith    Logically Collective on fine
3328b17ce1afSJed Brown 
33294165533cSJose E. Roman    Input Parameters:
3330bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3331b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3332bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
33330298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3334b17ce1afSJed Brown 
3335b17ce1afSJed Brown    Calling sequence of coarsenhook:
3336b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3337b17ce1afSJed Brown 
3338bb7acecfSBarry Smith +  fine - fine level `DM`
3339bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3340b17ce1afSJed Brown -  ctx - optional user-defined function context
3341b17ce1afSJed Brown 
3342b17ce1afSJed Brown    Calling sequence for restricthook:
3343c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3344bb7acecfSBarry Smith $
3345bb7acecfSBarry Smith +  fine - fine level `DM`
3346bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3347c833c3b5SJed Brown .  rscale - scaling vector for restriction
3348c833c3b5SJed Brown .  inject - matrix restricting by injection
3349b17ce1afSJed Brown .  coarse - coarse level DM to update
3350b17ce1afSJed Brown -  ctx - optional user-defined function context
3351b17ce1afSJed Brown 
3352b17ce1afSJed Brown    Level: advanced
3353b17ce1afSJed Brown 
3354b17ce1afSJed Brown    Notes:
3355bb7acecfSBarry 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`.
3356b17ce1afSJed Brown 
3357b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3358b17ce1afSJed Brown 
3359b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3360bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3361b17ce1afSJed Brown 
3362bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3363bb7acecfSBarry Smith 
3364bb7acecfSBarry Smith    Fortran Note:
3365bb7acecfSBarry Smith    This function is not available from Fortran.
3366bb9467b5SJed Brown 
3367db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3368b17ce1afSJed Brown @*/
3369d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3370d71ae5a4SJacob Faibussowitsch {
3371b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3372b17ce1afSJed Brown 
3373b17ce1afSJed Brown   PetscFunctionBegin;
3374b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33751e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33763ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33771e3d8eccSJed Brown   }
33789566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3379b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3380b17ce1afSJed Brown   link->restricthook = restricthook;
3381b17ce1afSJed Brown   link->ctx          = ctx;
33820298fd71SBarry Smith   link->next         = NULL;
3383b17ce1afSJed Brown   *p                 = link;
33843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3385b17ce1afSJed Brown }
3386b17ce1afSJed Brown 
3387dc822a44SJed Brown /*@C
3388bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3389dc822a44SJed Brown 
3390bb7acecfSBarry Smith    Logically Collective on fine
3391dc822a44SJed Brown 
33924165533cSJose E. Roman    Input Parameters:
3393bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3394dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3395bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3396dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3397dc822a44SJed Brown 
3398dc822a44SJed Brown    Level: advanced
3399dc822a44SJed Brown 
3400bb7acecfSBarry Smith    Note:
3401dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3402dc822a44SJed Brown 
3403bb7acecfSBarry Smith    Fortran Note:
3404bb7acecfSBarry Smith    This function is not available from Fortran.
3405dc822a44SJed Brown 
3406db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3407dc822a44SJed Brown @*/
3408d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3409d71ae5a4SJacob Faibussowitsch {
3410dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3411dc822a44SJed Brown 
3412dc822a44SJed Brown   PetscFunctionBegin;
3413dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3414dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3415dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3416dc822a44SJed Brown       link = *p;
3417dc822a44SJed Brown       *p   = link->next;
34189566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3419dc822a44SJed Brown       break;
3420dc822a44SJed Brown     }
3421dc822a44SJed Brown   }
34223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3423dc822a44SJed Brown }
3424dc822a44SJed Brown 
3425b17ce1afSJed Brown /*@
3426bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3427b17ce1afSJed Brown 
3428b17ce1afSJed Brown    Collective if any hooks are
3429b17ce1afSJed Brown 
34304165533cSJose E. Roman    Input Parameters:
3431bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3432bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3433e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3434bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3435e91eccc2SStefano Zampini -  coarse - coarser DM to update
3436b17ce1afSJed Brown 
3437b17ce1afSJed Brown    Level: developer
3438b17ce1afSJed Brown 
3439bb7acecfSBarry Smith    Developer Note:
3440bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3441bb7acecfSBarry Smith 
3442bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3443b17ce1afSJed Brown @*/
3444d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3445d71ae5a4SJacob Faibussowitsch {
3446b17ce1afSJed Brown   DMCoarsenHookLink link;
3447b17ce1afSJed Brown 
3448b17ce1afSJed Brown   PetscFunctionBegin;
3449b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34501baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3451b17ce1afSJed Brown   }
34523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
345347c6ae99SBarry Smith }
345447c6ae99SBarry Smith 
3455bb9467b5SJed Brown /*@C
3456be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34575dbd56e3SPeter Brune 
3458d083f849SBarry Smith    Logically Collective on global
34595dbd56e3SPeter Brune 
34604165533cSJose E. Roman    Input Parameters:
3461bb7acecfSBarry Smith +  global - global `DM`
3462bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34635dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
34640298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
34655dbd56e3SPeter Brune 
3466ec4806b8SPeter Brune    Calling sequence for ddhook:
3467ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3468ec4806b8SPeter Brune 
3469bb7acecfSBarry Smith +  global - global `DM`
3470bb7acecfSBarry Smith .  block  - block `DM`
3471ec4806b8SPeter Brune -  ctx - optional user-defined function context
3472ec4806b8SPeter Brune 
34735dbd56e3SPeter Brune    Calling sequence for restricthook:
3474ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
34755dbd56e3SPeter Brune 
3476bb7acecfSBarry Smith +  global - global `DM`
34775dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34785dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3479bb7acecfSBarry Smith .  block  - block `DM`
34805dbd56e3SPeter Brune -  ctx - optional user-defined function context
34815dbd56e3SPeter Brune 
34825dbd56e3SPeter Brune    Level: advanced
34835dbd56e3SPeter Brune 
34845dbd56e3SPeter Brune    Notes:
3485bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34865dbd56e3SPeter Brune 
34875dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34885dbd56e3SPeter Brune 
34895dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3490bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34915dbd56e3SPeter Brune 
3492bb7acecfSBarry Smith    Fortran Note:
3493bb7acecfSBarry Smith    This function is not available from Fortran.
3494bb9467b5SJed Brown 
3495bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34965dbd56e3SPeter Brune @*/
3497d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3498d71ae5a4SJacob Faibussowitsch {
3499be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
35005dbd56e3SPeter Brune 
35015dbd56e3SPeter Brune   PetscFunctionBegin;
35025dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3503b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35043ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3505b3a6b972SJed Brown   }
35069566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35075dbd56e3SPeter Brune   link->restricthook = restricthook;
3508be081cd6SPeter Brune   link->ddhook       = ddhook;
35095dbd56e3SPeter Brune   link->ctx          = ctx;
35100298fd71SBarry Smith   link->next         = NULL;
35115dbd56e3SPeter Brune   *p                 = link;
35123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35135dbd56e3SPeter Brune }
35145dbd56e3SPeter Brune 
3515b3a6b972SJed Brown /*@C
3516b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3517b3a6b972SJed Brown 
3518bb7acecfSBarry Smith    Logically Collective on global
3519b3a6b972SJed Brown 
35204165533cSJose E. Roman    Input Parameters:
3521bb7acecfSBarry Smith +  global - global `DM`
3522bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3523b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3524b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3525b3a6b972SJed Brown 
3526b3a6b972SJed Brown    Level: advanced
3527b3a6b972SJed Brown 
3528bb7acecfSBarry Smith    Fortran Note:
3529bb7acecfSBarry Smith    This function is not available from Fortran.
3530b3a6b972SJed Brown 
3531db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3532b3a6b972SJed Brown @*/
3533d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3534d71ae5a4SJacob Faibussowitsch {
3535b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3536b3a6b972SJed Brown 
3537b3a6b972SJed Brown   PetscFunctionBegin;
3538b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3539b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3540b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3541b3a6b972SJed Brown       link = *p;
3542b3a6b972SJed Brown       *p   = link->next;
35439566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3544b3a6b972SJed Brown       break;
3545b3a6b972SJed Brown     }
3546b3a6b972SJed Brown   }
35473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3548b3a6b972SJed Brown }
3549b3a6b972SJed Brown 
35505dbd56e3SPeter Brune /*@
3551bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35525dbd56e3SPeter Brune 
35535dbd56e3SPeter Brune    Collective if any hooks are
35545dbd56e3SPeter Brune 
35554165533cSJose E. Roman    Input Parameters:
3556bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3557be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3558be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3559bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35605dbd56e3SPeter Brune 
35615dbd56e3SPeter Brune    Level: developer
35625dbd56e3SPeter Brune 
3563db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
35645dbd56e3SPeter Brune @*/
3565d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3566d71ae5a4SJacob Faibussowitsch {
3567be081cd6SPeter Brune   DMSubDomainHookLink link;
35685dbd56e3SPeter Brune 
35695dbd56e3SPeter Brune   PetscFunctionBegin;
3570be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35711baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35725dbd56e3SPeter Brune   }
35733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35745dbd56e3SPeter Brune }
35755dbd56e3SPeter Brune 
35765fe1f584SPeter Brune /*@
3577bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35785fe1f584SPeter Brune 
35795fe1f584SPeter Brune     Not Collective
35805fe1f584SPeter Brune 
35815fe1f584SPeter Brune     Input Parameter:
3582bb7acecfSBarry Smith .   dm - the `DM` object
35835fe1f584SPeter Brune 
35845fe1f584SPeter Brune     Output Parameter:
35856a7d9d85SPeter Brune .   level - number of coarsenings
35865fe1f584SPeter Brune 
35875fe1f584SPeter Brune     Level: developer
35885fe1f584SPeter Brune 
3589bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35905fe1f584SPeter Brune @*/
3591d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3592d71ae5a4SJacob Faibussowitsch {
35935fe1f584SPeter Brune   PetscFunctionBegin;
35945fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3595b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
35965fe1f584SPeter Brune   *level = dm->leveldown;
35973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35985fe1f584SPeter Brune }
35995fe1f584SPeter Brune 
36009a64c4a8SMatthew G. Knepley /*@
3601bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36029a64c4a8SMatthew G. Knepley 
3603bb7acecfSBarry Smith     Collective on dm
36049a64c4a8SMatthew G. Knepley 
36059a64c4a8SMatthew G. Knepley     Input Parameters:
3606bb7acecfSBarry Smith +   dm - the `DM` object
36079a64c4a8SMatthew G. Knepley -   level - number of coarsenings
36089a64c4a8SMatthew G. Knepley 
36099a64c4a8SMatthew G. Knepley     Level: developer
36109a64c4a8SMatthew G. Knepley 
3611bb7acecfSBarry Smith     Note:
3612bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3613bb7acecfSBarry Smith 
3614bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36159a64c4a8SMatthew G. Knepley @*/
3616d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3617d71ae5a4SJacob Faibussowitsch {
36189a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36199a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36209a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36229a64c4a8SMatthew G. Knepley }
36239a64c4a8SMatthew G. Knepley 
362447c6ae99SBarry Smith /*@C
3625bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
362647c6ae99SBarry Smith 
3627d083f849SBarry Smith     Collective on dm
362847c6ae99SBarry Smith 
3629d8d19677SJose E. Roman     Input Parameters:
3630bb7acecfSBarry Smith +   dm - the `DM` object
363147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
363247c6ae99SBarry Smith 
363347c6ae99SBarry Smith     Output Parameter:
3634bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
363547c6ae99SBarry Smith 
363647c6ae99SBarry Smith     Level: developer
363747c6ae99SBarry Smith 
3638bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363947c6ae99SBarry Smith @*/
3640d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3641d71ae5a4SJacob Faibussowitsch {
364247c6ae99SBarry Smith   PetscFunctionBegin;
3643171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36447a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36453ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
3646b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
364747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3648dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
364947c6ae99SBarry Smith   } else if (dm->ops->refine) {
365047c6ae99SBarry Smith     PetscInt i;
365147c6ae99SBarry Smith 
36529566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
365348a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3654ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
36553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365647c6ae99SBarry Smith }
365747c6ae99SBarry Smith 
365847c6ae99SBarry Smith /*@C
3659bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
366047c6ae99SBarry Smith 
3661d083f849SBarry Smith     Collective on dm
366247c6ae99SBarry Smith 
3663d8d19677SJose E. Roman     Input Parameters:
3664bb7acecfSBarry Smith +   dm - the `DM` object
366547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
366647c6ae99SBarry Smith 
366747c6ae99SBarry Smith     Output Parameter:
3668bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
366947c6ae99SBarry Smith 
367047c6ae99SBarry Smith     Level: developer
367147c6ae99SBarry Smith 
3672bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
367347c6ae99SBarry Smith @*/
3674d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3675d71ae5a4SJacob Faibussowitsch {
367647c6ae99SBarry Smith   PetscFunctionBegin;
3677171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36787a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36793ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
368047c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
368147c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3682dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
368347c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
368447c6ae99SBarry Smith     PetscInt i;
368547c6ae99SBarry Smith 
36869566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
368748a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3688ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
36893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
369047c6ae99SBarry Smith }
369147c6ae99SBarry Smith 
36921a266240SBarry Smith /*@C
3693bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36941a266240SBarry Smith 
3695bb7acecfSBarry Smith     Logically Collective if the function is collective
36961a266240SBarry Smith 
36971a266240SBarry Smith     Input Parameters:
3698bb7acecfSBarry Smith +   dm - the `DM` object
36991a266240SBarry Smith -   destroy - the destroy function
37001a266240SBarry Smith 
37011a266240SBarry Smith     Level: intermediate
37021a266240SBarry Smith 
3703bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3704f07f9ceaSJed Brown @*/
3705d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3706d71ae5a4SJacob Faibussowitsch {
37071a266240SBarry Smith   PetscFunctionBegin;
3708171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37091a266240SBarry Smith   dm->ctxdestroy = destroy;
37103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37111a266240SBarry Smith }
37121a266240SBarry Smith 
3713b07ff414SBarry Smith /*@
3714bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
371547c6ae99SBarry Smith 
371647c6ae99SBarry Smith     Not Collective
371747c6ae99SBarry Smith 
371847c6ae99SBarry Smith     Input Parameters:
3719bb7acecfSBarry Smith +   dm - the `DM` object
372047c6ae99SBarry Smith -   ctx - the user context
372147c6ae99SBarry Smith 
372247c6ae99SBarry Smith     Level: intermediate
372347c6ae99SBarry Smith 
3724bb7acecfSBarry Smith     Note:
372535cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3726bb7acecfSBarry Smith 
3727bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
372847c6ae99SBarry Smith @*/
3729d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3730d71ae5a4SJacob Faibussowitsch {
373147c6ae99SBarry Smith   PetscFunctionBegin;
3732171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373347c6ae99SBarry Smith   dm->ctx = ctx;
37343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
373547c6ae99SBarry Smith }
373647c6ae99SBarry Smith 
373747c6ae99SBarry Smith /*@
3738bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
373947c6ae99SBarry Smith 
374047c6ae99SBarry Smith     Not Collective
374147c6ae99SBarry Smith 
374247c6ae99SBarry Smith     Input Parameter:
3743bb7acecfSBarry Smith .   dm - the `DM` object
374447c6ae99SBarry Smith 
374547c6ae99SBarry Smith     Output Parameter:
374647c6ae99SBarry Smith .   ctx - the user context
374747c6ae99SBarry Smith 
374847c6ae99SBarry Smith     Level: intermediate
374947c6ae99SBarry Smith 
3750bb7acecfSBarry Smith     Note:
375135cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3752bb7acecfSBarry Smith 
3753bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
375447c6ae99SBarry Smith @*/
3755d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3756d71ae5a4SJacob Faibussowitsch {
375747c6ae99SBarry Smith   PetscFunctionBegin;
3758171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37591b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
376147c6ae99SBarry Smith }
376247c6ae99SBarry Smith 
376308da532bSDmitry Karpeev /*@C
3764bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
376508da532bSDmitry Karpeev 
3766d083f849SBarry Smith     Logically Collective on dm
376708da532bSDmitry Karpeev 
3768d8d19677SJose E. Roman     Input Parameters:
376908da532bSDmitry Karpeev +   dm - the DM object
37700298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
377108da532bSDmitry Karpeev 
377208da532bSDmitry Karpeev     Level: intermediate
377308da532bSDmitry Karpeev 
3774bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3775db781477SPatrick Sanan          `DMSetJacobian()`
377608da532bSDmitry Karpeev @*/
3777d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3778d71ae5a4SJacob Faibussowitsch {
377908da532bSDmitry Karpeev   PetscFunctionBegin;
37805a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
378308da532bSDmitry Karpeev }
378408da532bSDmitry Karpeev 
378508da532bSDmitry Karpeev /*@
3786bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
378708da532bSDmitry Karpeev 
378808da532bSDmitry Karpeev     Not Collective
378908da532bSDmitry Karpeev 
379008da532bSDmitry Karpeev     Input Parameter:
3791bb7acecfSBarry Smith .   dm - the `DM` object to destroy
379208da532bSDmitry Karpeev 
379308da532bSDmitry Karpeev     Output Parameter:
3794bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
379508da532bSDmitry Karpeev 
379608da532bSDmitry Karpeev     Level: developer
379708da532bSDmitry Karpeev 
3798bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379908da532bSDmitry Karpeev @*/
3800d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3801d71ae5a4SJacob Faibussowitsch {
380208da532bSDmitry Karpeev   PetscFunctionBegin;
38035a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3804534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
380508da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380708da532bSDmitry Karpeev }
380808da532bSDmitry Karpeev 
380908da532bSDmitry Karpeev /*@C
3810bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
381108da532bSDmitry Karpeev 
3812d083f849SBarry Smith     Logically Collective on dm
381308da532bSDmitry Karpeev 
3814f899ff85SJose E. Roman     Input Parameter:
3815bb7acecfSBarry Smith .   dm - the `DM` object
381608da532bSDmitry Karpeev 
381708da532bSDmitry Karpeev     Output parameters:
381808da532bSDmitry Karpeev +   xl - lower bound
381908da532bSDmitry Karpeev -   xu - upper bound
382008da532bSDmitry Karpeev 
3821907376e6SBarry Smith     Level: advanced
3822907376e6SBarry Smith 
382395452b02SPatrick Sanan     Notes:
382495452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
382508da532bSDmitry Karpeev 
3826bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
382708da532bSDmitry Karpeev @*/
3828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3829d71ae5a4SJacob Faibussowitsch {
383008da532bSDmitry Karpeev   PetscFunctionBegin;
38315a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
383208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38335a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3834dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
383608da532bSDmitry Karpeev }
383708da532bSDmitry Karpeev 
3838b0ae01b7SPeter Brune /*@
3839bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3840b0ae01b7SPeter Brune 
3841b0ae01b7SPeter Brune     Not Collective
3842b0ae01b7SPeter Brune 
3843b0ae01b7SPeter Brune     Input Parameter:
3844b0ae01b7SPeter Brune .   dm - the DM object
3845b0ae01b7SPeter Brune 
3846b0ae01b7SPeter Brune     Output Parameter:
3847bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3848b0ae01b7SPeter Brune 
3849b0ae01b7SPeter Brune     Level: developer
3850b0ae01b7SPeter Brune 
3851bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3852b0ae01b7SPeter Brune @*/
3853d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3854d71ae5a4SJacob Faibussowitsch {
3855b0ae01b7SPeter Brune   PetscFunctionBegin;
38565a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3857534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3858b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3860b0ae01b7SPeter Brune }
3861b0ae01b7SPeter Brune 
38623ad4599aSBarry Smith /*@
3863bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38643ad4599aSBarry Smith 
38653ad4599aSBarry Smith     Not Collective
38663ad4599aSBarry Smith 
38673ad4599aSBarry Smith     Input Parameter:
3868bb7acecfSBarry Smith .   dm - the `DM` object
38693ad4599aSBarry Smith 
38703ad4599aSBarry Smith     Output Parameter:
3871bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38723ad4599aSBarry Smith 
38733ad4599aSBarry Smith     Level: developer
38743ad4599aSBarry Smith 
3875bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38763ad4599aSBarry Smith @*/
3877d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3878d71ae5a4SJacob Faibussowitsch {
38793ad4599aSBarry Smith   PetscFunctionBegin;
38805a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3881534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
38823ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38843ad4599aSBarry Smith }
38853ad4599aSBarry Smith 
3886a7058e45SLawrence Mitchell /*@
3887bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3888a7058e45SLawrence Mitchell 
3889a7058e45SLawrence Mitchell     Not Collective
3890a7058e45SLawrence Mitchell 
3891a7058e45SLawrence Mitchell     Input Parameter:
3892bb7acecfSBarry Smith .   dm - the `DM` object
3893a7058e45SLawrence Mitchell 
3894a7058e45SLawrence Mitchell     Output Parameter:
3895bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3896a7058e45SLawrence Mitchell 
3897a7058e45SLawrence Mitchell     Level: developer
3898a7058e45SLawrence Mitchell 
3899bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3900a7058e45SLawrence Mitchell @*/
3901d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3902d71ae5a4SJacob Faibussowitsch {
3903a7058e45SLawrence Mitchell   PetscFunctionBegin;
39045a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3905534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3906dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3907ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3909a7058e45SLawrence Mitchell }
3910a7058e45SLawrence Mitchell 
39110298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3912264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3913264ace61SBarry Smith 
3914264ace61SBarry Smith /*@C
3915bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3916264ace61SBarry Smith 
3917d083f849SBarry Smith   Collective on dm
3918264ace61SBarry Smith 
3919264ace61SBarry Smith   Input Parameters:
3920bb7acecfSBarry Smith + dm     - The `DM` object
3921bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3922264ace61SBarry Smith 
3923264ace61SBarry Smith   Options Database Key:
3924bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3925264ace61SBarry Smith 
3926264ace61SBarry Smith   Level: intermediate
3927264ace61SBarry Smith 
3928bb7acecfSBarry Smith   Note:
3929bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3930bb7acecfSBarry Smith 
3931bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3932264ace61SBarry Smith @*/
3933d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3934d71ae5a4SJacob Faibussowitsch {
3935264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3936264ace61SBarry Smith   PetscBool match;
3937264ace61SBarry Smith 
3938264ace61SBarry Smith   PetscFunctionBegin;
3939264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39409566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39413ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3942264ace61SBarry Smith 
39439566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39449566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39457a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3946264ace61SBarry Smith 
3947dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39489566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39499566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39509566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3952264ace61SBarry Smith }
3953264ace61SBarry Smith 
3954264ace61SBarry Smith /*@C
3955bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3956264ace61SBarry Smith 
3957264ace61SBarry Smith   Not Collective
3958264ace61SBarry Smith 
3959264ace61SBarry Smith   Input Parameter:
3960bb7acecfSBarry Smith . dm  - The `DM`
3961264ace61SBarry Smith 
3962264ace61SBarry Smith   Output Parameter:
3963bb7acecfSBarry Smith . type - The `DMType` name
3964264ace61SBarry Smith 
3965264ace61SBarry Smith   Level: intermediate
3966264ace61SBarry Smith 
3967bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3968264ace61SBarry Smith @*/
3969d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3970d71ae5a4SJacob Faibussowitsch {
3971264ace61SBarry Smith   PetscFunctionBegin;
3972264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3973c959eef4SJed Brown   PetscValidPointer(type, 2);
39749566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3975264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3977264ace61SBarry Smith }
3978264ace61SBarry Smith 
397967a56275SMatthew G Knepley /*@C
3980bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
398167a56275SMatthew G Knepley 
3982d083f849SBarry Smith   Collective on dm
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley   Input Parameters:
3985bb7acecfSBarry Smith + dm - the `DM`
3986bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
398767a56275SMatthew G Knepley 
398867a56275SMatthew G Knepley   Output Parameter:
3989bb7acecfSBarry Smith . M - pointer to new `DM`
399067a56275SMatthew G Knepley 
399167a56275SMatthew G Knepley   Notes:
3992bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3993bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3994bb7acecfSBarry Smith   of the input `DM`.
399567a56275SMatthew G Knepley 
399667a56275SMatthew G Knepley   Level: intermediate
399767a56275SMatthew G Knepley 
3998bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
399967a56275SMatthew G Knepley @*/
4000d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
4001d71ae5a4SJacob Faibussowitsch {
400267a56275SMatthew G Knepley   DM        B;
400367a56275SMatthew G Knepley   char      convname[256];
4004c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
400567a56275SMatthew G Knepley 
400667a56275SMatthew G Knepley   PetscFunctionBegin;
400767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400867a56275SMatthew G Knepley   PetscValidType(dm, 1);
400967a56275SMatthew G Knepley   PetscValidPointer(M, 3);
40109566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40119566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4012c067b6caSMatthew G. Knepley   if (sametype) {
4013c067b6caSMatthew G. Knepley     *M = dm;
40149566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40153ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4016c067b6caSMatthew G. Knepley   } else {
40170298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
401867a56275SMatthew G Knepley 
401967a56275SMatthew G Knepley     /*
402067a56275SMatthew G Knepley        Order of precedence:
402167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
402267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
402367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
402467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
402567a56275SMatthew G Knepley        5) Use a really basic converter.
402667a56275SMatthew G Knepley     */
402767a56275SMatthew G Knepley 
402867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40299566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40309566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40319566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40329566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40339566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40349566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
403567a56275SMatthew G Knepley     if (conv) goto foundconv;
403667a56275SMatthew G Knepley 
403767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40389566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40399566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40409566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40419566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40429566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40439566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40449566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40459566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
404667a56275SMatthew G Knepley     if (conv) {
40479566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
404867a56275SMatthew G Knepley       goto foundconv;
404967a56275SMatthew G Knepley     }
405067a56275SMatthew G Knepley 
405167a56275SMatthew G Knepley #if 0
405267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
405367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40549566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
405567a56275SMatthew G Knepley     if (conv) goto foundconv;
405667a56275SMatthew G Knepley 
405767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
405867a56275SMatthew G Knepley     if (dm->ops->convert) {
405967a56275SMatthew G Knepley       conv = dm->ops->convert;
406067a56275SMatthew G Knepley     }
406167a56275SMatthew G Knepley     if (conv) goto foundconv;
406267a56275SMatthew G Knepley #endif
406367a56275SMatthew G Knepley 
406467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
406598921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
406667a56275SMatthew G Knepley 
406767a56275SMatthew G Knepley   foundconv:
40689566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40699566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
407012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
407190b157c4SStefano Zampini     {
40724fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40736858538eSMatthew G. Knepley 
40744fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40754fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4076c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40779566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40789566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40799566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40809566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
408112fa691eSMatthew G. Knepley     }
40829566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
408367a56275SMatthew G Knepley   }
40849566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
408667a56275SMatthew G Knepley }
4087264ace61SBarry Smith 
4088264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4089264ace61SBarry Smith 
4090264ace61SBarry Smith /*@C
4091bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40921c84c290SBarry Smith 
40931c84c290SBarry Smith   Not Collective
40941c84c290SBarry Smith 
40951c84c290SBarry Smith   Input Parameters:
40961c84c290SBarry Smith + name        - The name of a new user-defined creation routine
40971c84c290SBarry Smith - create_func - The creation routine itself
40981c84c290SBarry Smith 
40991c84c290SBarry Smith   Notes:
4100bb7acecfSBarry Smith   DMRegister() may be called multiple times to add several user-defined `DM`s
41011c84c290SBarry Smith 
41021c84c290SBarry Smith   Sample usage:
41031c84c290SBarry Smith .vb
4104bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41051c84c290SBarry Smith .ve
41061c84c290SBarry Smith 
41071c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
41081c84c290SBarry Smith .vb
41091c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41101c84c290SBarry Smith     DMSetType(DM,"my_da");
41111c84c290SBarry Smith .ve
41121c84c290SBarry Smith    or at runtime via the option
41131c84c290SBarry Smith .vb
41141c84c290SBarry Smith     -da_type my_da
41151c84c290SBarry Smith .ve
4116264ace61SBarry Smith 
4117264ace61SBarry Smith   Level: advanced
41181c84c290SBarry Smith 
4119bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4120264ace61SBarry Smith @*/
4121d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4122d71ae5a4SJacob Faibussowitsch {
4123264ace61SBarry Smith   PetscFunctionBegin;
41249566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41259566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4127264ace61SBarry Smith }
4128264ace61SBarry Smith 
4129b859378eSBarry Smith /*@C
4130bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4131b859378eSBarry Smith 
4132d083f849SBarry Smith   Collective on viewer
4133b859378eSBarry Smith 
4134b859378eSBarry Smith   Input Parameters:
4135bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4136bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4137bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4138bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4139b859378eSBarry Smith 
4140b859378eSBarry Smith    Level: intermediate
4141b859378eSBarry Smith 
4142b859378eSBarry Smith   Notes:
414355849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4144b859378eSBarry Smith 
4145bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4146bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4147bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4148cd7e8a5eSksagiyam 
4149b859378eSBarry Smith   Notes for advanced users:
4150b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4151bb7acecfSBarry Smith   format, since `DMLoad()` and `DMView()` completely hide these details.
4152b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4153b859378eSBarry Smith   format is
4154b859378eSBarry Smith .vb
4155b859378eSBarry Smith      has not yet been determined
4156b859378eSBarry Smith .ve
4157b859378eSBarry Smith 
4158db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4159b859378eSBarry Smith @*/
4160d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4161d71ae5a4SJacob Faibussowitsch {
41629331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4163b859378eSBarry Smith 
4164b859378eSBarry Smith   PetscFunctionBegin;
4165b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4166b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41679566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41689566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41699566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41719331c7a4SMatthew G. Knepley   if (isbinary) {
41729331c7a4SMatthew G. Knepley     PetscInt classid;
41739331c7a4SMatthew G. Knepley     char     type[256];
4174b859378eSBarry Smith 
41759566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41767a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41779566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41789566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4179dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41809331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4181dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41829331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41839566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4185b859378eSBarry Smith }
4186b859378eSBarry Smith 
41877da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41887da65231SMatthew G Knepley 
4189d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4190d71ae5a4SJacob Faibussowitsch {
41911d47ebbbSSatish Balay   PetscInt f;
41921b30c384SMatthew G Knepley 
41937da65231SMatthew G Knepley   PetscFunctionBegin;
419463a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
419548a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41977da65231SMatthew G Knepley }
41987da65231SMatthew G Knepley 
4199d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4200d71ae5a4SJacob Faibussowitsch {
42011b30c384SMatthew G Knepley   PetscInt f, g;
42027da65231SMatthew G Knepley 
42037da65231SMatthew G Knepley   PetscFunctionBegin;
420463a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42051d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42069566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
420748a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42089566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42097da65231SMatthew G Knepley   }
42103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42117da65231SMatthew G Knepley }
4212e7c4fc90SDmitry Karpeev 
4213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4214d71ae5a4SJacob Faibussowitsch {
42150c5b8624SToby Isaac   PetscInt           localSize, bs;
42160c5b8624SToby Isaac   PetscMPIInt        size;
42170c5b8624SToby Isaac   Vec                x, xglob;
42180c5b8624SToby Isaac   const PetscScalar *xarray;
4219e759306cSMatthew G. Knepley 
4220e759306cSMatthew G. Knepley   PetscFunctionBegin;
42219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42229566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42239566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
42249566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
42259566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42260c5b8624SToby Isaac   if (size > 1) {
42279566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42289566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42299566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42309566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42310c5b8624SToby Isaac   } else {
42320c5b8624SToby Isaac     xglob = x;
42330c5b8624SToby Isaac   }
42349566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42350c5b8624SToby Isaac   if (size > 1) {
42369566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42379566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42380c5b8624SToby Isaac   }
42399566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4241e759306cSMatthew G. Knepley }
4242e759306cSMatthew G. Knepley 
424388ed4aceSMatthew G Knepley /*@
4244bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4245061576a5SJed Brown 
4246061576a5SJed Brown   Input Parameter:
4247bb7acecfSBarry Smith . dm - The `DM`
4248061576a5SJed Brown 
4249061576a5SJed Brown   Output Parameter:
4250bb7acecfSBarry Smith . section - The `PetscSection`
4251061576a5SJed Brown 
4252061576a5SJed Brown   Options Database Keys:
4253bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4254061576a5SJed Brown 
4255061576a5SJed Brown   Level: advanced
4256061576a5SJed Brown 
4257061576a5SJed Brown   Notes:
4258bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4259061576a5SJed Brown 
4260bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4261061576a5SJed Brown 
4262db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4263061576a5SJed Brown @*/
4264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4265d71ae5a4SJacob Faibussowitsch {
4266061576a5SJed Brown   PetscFunctionBegin;
42679566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4269061576a5SJed Brown }
4270061576a5SJed Brown 
4271061576a5SJed Brown /*@
4272bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
427388ed4aceSMatthew G Knepley 
427488ed4aceSMatthew G Knepley   Input Parameter:
4275bb7acecfSBarry Smith . dm - The `DM`
427688ed4aceSMatthew G Knepley 
427788ed4aceSMatthew G Knepley   Output Parameter:
4278bb7acecfSBarry Smith . section - The `PetscSection`
427988ed4aceSMatthew G Knepley 
4280e5893cccSMatthew G. Knepley   Options Database Keys:
4281bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4282e5893cccSMatthew G. Knepley 
428388ed4aceSMatthew G Knepley   Level: intermediate
428488ed4aceSMatthew G Knepley 
4285bb7acecfSBarry Smith   Note:
4286bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
428788ed4aceSMatthew G Knepley 
4288db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
428988ed4aceSMatthew G Knepley @*/
4290d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4291d71ae5a4SJacob Faibussowitsch {
429288ed4aceSMatthew G Knepley   PetscFunctionBegin;
429388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42951bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4296e5e52638SMatthew G. Knepley     PetscInt d;
4297e5e52638SMatthew G. Knepley 
429845480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
429945480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
430045480ffeSMatthew G. Knepley       PetscViewer       viewer;
430145480ffeSMatthew G. Knepley       PetscViewerFormat format;
430245480ffeSMatthew G. Knepley       PetscBool         flg;
430345480ffeSMatthew G. Knepley 
43049566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43059566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
430645480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43079566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43089566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
430945480ffeSMatthew G. Knepley       }
431045480ffeSMatthew G. Knepley       if (flg) {
43119566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43129566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
43139566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
431445480ffeSMatthew G. Knepley       }
431545480ffeSMatthew G. Knepley     }
4316dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43179566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43182f0f8703SMatthew G. Knepley   }
43191bb6d2a8SBarry Smith   *section = dm->localSection;
43203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432188ed4aceSMatthew G Knepley }
432288ed4aceSMatthew G Knepley 
432388ed4aceSMatthew G Knepley /*@
4324bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4325061576a5SJed Brown 
4326061576a5SJed Brown   Input Parameters:
4327bb7acecfSBarry Smith + dm - The `DM`
4328bb7acecfSBarry Smith - section - The `PetscSection`
4329061576a5SJed Brown 
4330061576a5SJed Brown   Level: advanced
4331061576a5SJed Brown 
4332061576a5SJed Brown   Notes:
4333bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4334061576a5SJed Brown 
4335bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4336061576a5SJed Brown 
4337db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4338061576a5SJed Brown @*/
4339d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4340d71ae5a4SJacob Faibussowitsch {
4341061576a5SJed Brown   PetscFunctionBegin;
43429566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4344061576a5SJed Brown }
4345061576a5SJed Brown 
4346061576a5SJed Brown /*@
4347bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
434888ed4aceSMatthew G Knepley 
434988ed4aceSMatthew G Knepley   Input Parameters:
4350bb7acecfSBarry Smith + dm - The `DM`
4351bb7acecfSBarry Smith - section - The `PetscSection`
435288ed4aceSMatthew G Knepley 
435388ed4aceSMatthew G Knepley   Level: intermediate
435488ed4aceSMatthew G Knepley 
4355bb7acecfSBarry Smith   Note:
4356bb7acecfSBarry Smith   Any existing Section will be destroyed
435788ed4aceSMatthew G Knepley 
4358bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
435988ed4aceSMatthew G Knepley @*/
4360d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4361d71ae5a4SJacob Faibussowitsch {
4362c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4363af122d2aSMatthew G Knepley   PetscInt f;
436488ed4aceSMatthew G Knepley 
436588ed4aceSMatthew G Knepley   PetscFunctionBegin;
436688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4367b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43689566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43699566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43701bb6d2a8SBarry Smith   dm->localSection = section;
43719566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4372af122d2aSMatthew G Knepley   if (numFields) {
43739566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4374af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43750f21e855SMatthew G. Knepley       PetscObject disc;
4376af122d2aSMatthew G Knepley       const char *name;
4377af122d2aSMatthew G Knepley 
43789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43799566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43809566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4381af122d2aSMatthew G Knepley     }
4382af122d2aSMatthew G Knepley   }
4383e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43849566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
43853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
438688ed4aceSMatthew G Knepley }
438788ed4aceSMatthew G Knepley 
43889435951eSToby Isaac /*@
4389bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43909435951eSToby Isaac 
4391e228b242SToby Isaac   not collective
4392e228b242SToby Isaac 
43939435951eSToby Isaac   Input Parameter:
4394bb7acecfSBarry Smith . dm - The `DM`
43959435951eSToby Isaac 
4396d8d19677SJose E. Roman   Output Parameters:
4397bb7acecfSBarry 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.
4398bb7acecfSBarry 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.
439979769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
44009435951eSToby Isaac 
44019435951eSToby Isaac   Level: advanced
44029435951eSToby Isaac 
4403bb7acecfSBarry Smith   Note:
4404bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44059435951eSToby Isaac 
4406db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
44079435951eSToby Isaac @*/
4408d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4409d71ae5a4SJacob Faibussowitsch {
44109435951eSToby Isaac   PetscFunctionBegin;
44119435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4412dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44133b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44143b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
441579769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44179435951eSToby Isaac }
44189435951eSToby Isaac 
44199435951eSToby Isaac /*@
4420bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44219435951eSToby Isaac 
4422bb7acecfSBarry 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()`.
44239435951eSToby Isaac 
4424bb7acecfSBarry 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.
44259435951eSToby Isaac 
4426e228b242SToby Isaac   collective on dm
4427e228b242SToby Isaac 
44289435951eSToby Isaac   Input Parameters:
4429bb7acecfSBarry Smith + dm - The `DM`
4430bb7acecfSBarry 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).
4431bb7acecfSBarry 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).
4432bb7acecfSBarry 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).
44339435951eSToby Isaac 
44349435951eSToby Isaac   Level: advanced
44359435951eSToby Isaac 
4436bb7acecfSBarry Smith   Note:
4437bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44389435951eSToby Isaac 
4439db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
44409435951eSToby Isaac @*/
4441d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4442d71ae5a4SJacob Faibussowitsch {
4443e228b242SToby Isaac   PetscMPIInt result;
44449435951eSToby Isaac 
44459435951eSToby Isaac   PetscFunctionBegin;
44469435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4447e228b242SToby Isaac   if (section) {
4448e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44499566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44507a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4451e228b242SToby Isaac   }
4452e228b242SToby Isaac   if (mat) {
4453e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44549566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44557a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4456e228b242SToby Isaac   }
445779769bd5SJed Brown   if (bias) {
445879769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44599566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
446079769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
446179769bd5SJed Brown   }
44629566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44639566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44643b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44659566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44669566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44673b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44689566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44699566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
447079769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44729435951eSToby Isaac }
44739435951eSToby Isaac 
4474497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4475507e4973SMatthew G. Knepley /*
4476bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4477507e4973SMatthew G. Knepley 
4478507e4973SMatthew G. Knepley   Input Parameters:
4479bb7acecfSBarry Smith + dm - The `DM`
4480bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4481bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4482507e4973SMatthew G. Knepley 
4483507e4973SMatthew G. Knepley   Level: intermediate
4484507e4973SMatthew G. Knepley 
4485db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4486507e4973SMatthew G. Knepley */
4487d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4488d71ae5a4SJacob Faibussowitsch {
4489507e4973SMatthew G. Knepley   MPI_Comm        comm;
4490507e4973SMatthew G. Knepley   PetscLayout     layout;
4491507e4973SMatthew G. Knepley   const PetscInt *ranges;
4492507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4493507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4494507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4495507e4973SMatthew G. Knepley 
4496507e4973SMatthew G. Knepley   PetscFunctionBegin;
44979566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4498507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44999566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45009566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45019566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45029566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45039566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45049566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45059566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45069566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45079566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4508507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4509f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4510507e4973SMatthew G. Knepley 
45119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45169566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4517507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45189371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45199371c9d4SSatish 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));
45209371c9d4SSatish Balay       valid = PETSC_FALSE;
45219371c9d4SSatish Balay     }
45229371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45239371c9d4SSatish 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));
45249371c9d4SSatish Balay       valid = PETSC_FALSE;
45259371c9d4SSatish Balay     }
4526507e4973SMatthew G. Knepley     if (gdof < 0) {
4527507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4528507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4529507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4530507e4973SMatthew G. Knepley 
45319566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4532507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45339371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45349371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45359371c9d4SSatish Balay           valid = PETSC_FALSE;
45369371c9d4SSatish Balay           break;
45379371c9d4SSatish Balay         }
4538507e4973SMatthew G. Knepley       }
4539507e4973SMatthew G. Knepley     }
4540507e4973SMatthew G. Knepley   }
45419566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45429566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45431c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4544507e4973SMatthew G. Knepley   if (!gvalid) {
45459566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4546507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4547507e4973SMatthew G. Knepley   }
45483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4549507e4973SMatthew G. Knepley }
4550f741bcd2SMatthew G. Knepley #endif
4551507e4973SMatthew G. Knepley 
45525f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45536725e60dSJed Brown {
45546725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45556725e60dSJed Brown   PetscFunctionBegin;
45566725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45576725e60dSJed Brown   PetscValidPointer(sf, 2);
45585f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45596725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45606725e60dSJed Brown   else *sf = dm->sf;
45613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45626725e60dSJed Brown }
45636725e60dSJed Brown 
456488ed4aceSMatthew G Knepley /*@
4565bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
456688ed4aceSMatthew G Knepley 
4567d083f849SBarry Smith   Collective on dm
45688b1ab98fSJed Brown 
456988ed4aceSMatthew G Knepley   Input Parameter:
4570bb7acecfSBarry Smith . dm - The `DM`
457188ed4aceSMatthew G Knepley 
457288ed4aceSMatthew G Knepley   Output Parameter:
4573bb7acecfSBarry Smith . section - The `PetscSection`
457488ed4aceSMatthew G Knepley 
457588ed4aceSMatthew G Knepley   Level: intermediate
457688ed4aceSMatthew G Knepley 
4577bb7acecfSBarry Smith   Note:
4578bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
457988ed4aceSMatthew G Knepley 
4580db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
458188ed4aceSMatthew G Knepley @*/
4582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4583d71ae5a4SJacob Faibussowitsch {
458488ed4aceSMatthew G Knepley   PetscFunctionBegin;
458588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
458688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45871bb6d2a8SBarry Smith   if (!dm->globalSection) {
4588fd59a867SMatthew G. Knepley     PetscSection s;
45896725e60dSJed Brown     PetscSF      sf;
4590fd59a867SMatthew G. Knepley 
45919566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45927a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45937a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45945f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45956725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45969566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45979566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45989566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
459988ed4aceSMatthew G Knepley   }
46001bb6d2a8SBarry Smith   *section = dm->globalSection;
46013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
460288ed4aceSMatthew G Knepley }
460388ed4aceSMatthew G Knepley 
4604b21d0597SMatthew G Knepley /*@
4605bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4606b21d0597SMatthew G Knepley 
4607b21d0597SMatthew G Knepley   Input Parameters:
4608bb7acecfSBarry Smith + dm - The `DM`
46095080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4610b21d0597SMatthew G Knepley 
4611b21d0597SMatthew G Knepley   Level: intermediate
4612b21d0597SMatthew G Knepley 
4613bb7acecfSBarry Smith   Note:
4614bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4615b21d0597SMatthew G Knepley 
4616db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4617b21d0597SMatthew G Knepley @*/
4618d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4619d71ae5a4SJacob Faibussowitsch {
4620b21d0597SMatthew G Knepley   PetscFunctionBegin;
4621b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46225080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46249566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46251bb6d2a8SBarry Smith   dm->globalSection = section;
4626497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46279566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4628507e4973SMatthew G. Knepley #endif
46293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4630b21d0597SMatthew G Knepley }
4631b21d0597SMatthew G Knepley 
463288ed4aceSMatthew G Knepley /*@
4633bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4634bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
463588ed4aceSMatthew G Knepley 
463688ed4aceSMatthew G Knepley   Input Parameter:
4637bb7acecfSBarry Smith . dm - The `DM`
463888ed4aceSMatthew G Knepley 
463988ed4aceSMatthew G Knepley   Output Parameter:
4640bb7acecfSBarry Smith . sf - The `PetscSF`
464188ed4aceSMatthew G Knepley 
464288ed4aceSMatthew G Knepley   Level: intermediate
464388ed4aceSMatthew G Knepley 
4644bb7acecfSBarry Smith   Note:
4645bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
464688ed4aceSMatthew G Knepley 
4647db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
464888ed4aceSMatthew G Knepley @*/
4649d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4650d71ae5a4SJacob Faibussowitsch {
465188ed4aceSMatthew G Knepley   PetscInt nroots;
465288ed4aceSMatthew G Knepley 
465388ed4aceSMatthew G Knepley   PetscFunctionBegin;
465488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
465648a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46579566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
465888ed4aceSMatthew G Knepley   if (nroots < 0) {
465988ed4aceSMatthew G Knepley     PetscSection section, gSection;
466088ed4aceSMatthew G Knepley 
46619566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
466231ea6d37SMatthew G Knepley     if (section) {
46639566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46649566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
466531ea6d37SMatthew G Knepley     } else {
46660298fd71SBarry Smith       *sf = NULL;
46673ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
466831ea6d37SMatthew G Knepley     }
466988ed4aceSMatthew G Knepley   }
46701bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
467288ed4aceSMatthew G Knepley }
467388ed4aceSMatthew G Knepley 
467488ed4aceSMatthew G Knepley /*@
4675bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
467688ed4aceSMatthew G Knepley 
467788ed4aceSMatthew G Knepley   Input Parameters:
4678bb7acecfSBarry Smith + dm - The `DM`
4679bb7acecfSBarry Smith - sf - The `PetscSF`
468088ed4aceSMatthew G Knepley 
468188ed4aceSMatthew G Knepley   Level: intermediate
468288ed4aceSMatthew G Knepley 
4683bb7acecfSBarry Smith   Note:
4684bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
468588ed4aceSMatthew G Knepley 
4686db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
468788ed4aceSMatthew G Knepley @*/
4688d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4689d71ae5a4SJacob Faibussowitsch {
469088ed4aceSMatthew G Knepley   PetscFunctionBegin;
469188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4692b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46939566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46949566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46951bb6d2a8SBarry Smith   dm->sectionSF = sf;
46963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
469788ed4aceSMatthew G Knepley }
469888ed4aceSMatthew G Knepley 
469988ed4aceSMatthew G Knepley /*@C
4700bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
470188ed4aceSMatthew G Knepley   describing the data layout.
470288ed4aceSMatthew G Knepley 
470388ed4aceSMatthew G Knepley   Input Parameters:
4704bb7acecfSBarry Smith + dm - The `DM`
4705bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4706bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
470788ed4aceSMatthew G Knepley 
47081bb6d2a8SBarry Smith   Level: developer
47091bb6d2a8SBarry Smith 
4710bb7acecfSBarry Smith   Note:
4711bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4712bb7acecfSBarry Smith 
4713bb7acecfSBarry Smith   Developer Note:
4714bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4715bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4716bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
47171bb6d2a8SBarry Smith 
4718db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
471988ed4aceSMatthew G Knepley @*/
4720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4721d71ae5a4SJacob Faibussowitsch {
472288ed4aceSMatthew G Knepley   PetscFunctionBegin;
472388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47249566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
472688ed4aceSMatthew G Knepley }
4727af122d2aSMatthew G Knepley 
4728b21d0597SMatthew G Knepley /*@
4729bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4730bb7acecfSBarry Smith 
4731bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4732b21d0597SMatthew G Knepley 
4733b21d0597SMatthew G Knepley   Input Parameter:
4734bb7acecfSBarry Smith . dm - The `DM`
4735b21d0597SMatthew G Knepley 
4736b21d0597SMatthew G Knepley   Output Parameter:
4737bb7acecfSBarry Smith . sf - The `PetscSF`
4738b21d0597SMatthew G Knepley 
4739b21d0597SMatthew G Knepley   Level: intermediate
4740b21d0597SMatthew G Knepley 
4741bb7acecfSBarry Smith   Note:
4742bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4743b21d0597SMatthew G Knepley 
4744db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4745b21d0597SMatthew G Knepley @*/
4746d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4747d71ae5a4SJacob Faibussowitsch {
4748b21d0597SMatthew G Knepley   PetscFunctionBegin;
4749b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4750b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4751b21d0597SMatthew G Knepley   *sf = dm->sf;
47523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4753b21d0597SMatthew G Knepley }
4754b21d0597SMatthew G Knepley 
4755057b4bcdSMatthew G Knepley /*@
4756bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4757bb7acecfSBarry Smith 
4758bb7acecfSBarry Smith   Collective on dm
4759057b4bcdSMatthew G Knepley 
4760057b4bcdSMatthew G Knepley   Input Parameters:
4761bb7acecfSBarry Smith + dm - The `DM`
4762bb7acecfSBarry Smith - sf - The `PetscSF`
4763057b4bcdSMatthew G Knepley 
4764057b4bcdSMatthew G Knepley   Level: intermediate
4765057b4bcdSMatthew G Knepley 
4766db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4767057b4bcdSMatthew G Knepley @*/
4768d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4769d71ae5a4SJacob Faibussowitsch {
4770057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4771057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4772b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47739566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47749566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4775057b4bcdSMatthew G Knepley   dm->sf = sf;
47763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4777057b4bcdSMatthew G Knepley }
4778057b4bcdSMatthew G Knepley 
47794f37162bSMatthew G. Knepley /*@
4780bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47814f37162bSMatthew G. Knepley 
47824f37162bSMatthew G. Knepley   Input Parameter:
4783bb7acecfSBarry Smith . dm - The `DM`
47844f37162bSMatthew G. Knepley 
47854f37162bSMatthew G. Knepley   Output Parameter:
4786bb7acecfSBarry Smith . sf - The `PetscSF`
47874f37162bSMatthew G. Knepley 
47884f37162bSMatthew G. Knepley   Level: intermediate
47894f37162bSMatthew G. Knepley 
4790bb7acecfSBarry Smith   Note:
4791bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47924f37162bSMatthew G. Knepley 
4793db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47944f37162bSMatthew G. Knepley @*/
4795d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4796d71ae5a4SJacob Faibussowitsch {
47974f37162bSMatthew G. Knepley   PetscFunctionBegin;
47984f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47994f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
48004f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48024f37162bSMatthew G. Knepley }
48034f37162bSMatthew G. Knepley 
48044f37162bSMatthew G. Knepley /*@
48054f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48064f37162bSMatthew G. Knepley 
48074f37162bSMatthew G. Knepley   Input Parameters:
48084f37162bSMatthew G. Knepley + dm - The DM
48094f37162bSMatthew G. Knepley - sf - The PetscSF
48104f37162bSMatthew G. Knepley 
48114f37162bSMatthew G. Knepley   Level: intermediate
48124f37162bSMatthew G. Knepley 
4813db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48144f37162bSMatthew G. Knepley @*/
4815d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4816d71ae5a4SJacob Faibussowitsch {
48174f37162bSMatthew G. Knepley   PetscFunctionBegin;
48184f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48194f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48209566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48219566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48224f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48244f37162bSMatthew G. Knepley }
48254f37162bSMatthew G. Knepley 
4826d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4827d71ae5a4SJacob Faibussowitsch {
482834aa8a36SMatthew G. Knepley   PetscClassId id;
482934aa8a36SMatthew G. Knepley 
483034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48319566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
483234aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48339566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
483434aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48359566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
483617c1d62eSMatthew G. Knepley   } else {
48379566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
483834aa8a36SMatthew G. Knepley   }
48393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
484034aa8a36SMatthew G. Knepley }
484134aa8a36SMatthew G. Knepley 
4842d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4843d71ae5a4SJacob Faibussowitsch {
484444a7f3ddSMatthew G. Knepley   RegionField *tmpr;
484544a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
484644a7f3ddSMatthew G. Knepley 
484744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48483ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
485044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48519371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48529371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48539371c9d4SSatish Balay     tmpr[f].label       = NULL;
48549371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48559371c9d4SSatish Balay   }
48569566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
485744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
485844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
486044a7f3ddSMatthew G. Knepley }
486144a7f3ddSMatthew G. Knepley 
486244a7f3ddSMatthew G. Knepley /*@
486344a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
486444a7f3ddSMatthew G. Knepley 
4865d083f849SBarry Smith   Logically collective on dm
486644a7f3ddSMatthew G. Knepley 
486744a7f3ddSMatthew G. Knepley   Input Parameter:
486844a7f3ddSMatthew G. Knepley . dm - The DM
486944a7f3ddSMatthew G. Knepley 
487044a7f3ddSMatthew G. Knepley   Level: intermediate
487144a7f3ddSMatthew G. Knepley 
4872db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
487344a7f3ddSMatthew G. Knepley @*/
4874d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4875d71ae5a4SJacob Faibussowitsch {
487644a7f3ddSMatthew G. Knepley   PetscInt f;
487744a7f3ddSMatthew G. Knepley 
487844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
487944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
488044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48819566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48829566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
488344a7f3ddSMatthew G. Knepley   }
48849566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
488544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
488644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
488844a7f3ddSMatthew G. Knepley }
488944a7f3ddSMatthew G. Knepley 
4890689b5837SMatthew G. Knepley /*@
4891689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4892689b5837SMatthew G. Knepley 
4893689b5837SMatthew G. Knepley   Not collective
4894689b5837SMatthew G. Knepley 
4895689b5837SMatthew G. Knepley   Input Parameter:
4896689b5837SMatthew G. Knepley . dm - The DM
4897689b5837SMatthew G. Knepley 
4898689b5837SMatthew G. Knepley   Output Parameter:
4899689b5837SMatthew G. Knepley . Nf - The number of fields
4900689b5837SMatthew G. Knepley 
4901689b5837SMatthew G. Knepley   Level: intermediate
4902689b5837SMatthew G. Knepley 
4903db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4904689b5837SMatthew G. Knepley @*/
4905d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4906d71ae5a4SJacob Faibussowitsch {
49070f21e855SMatthew G. Knepley   PetscFunctionBegin;
49080f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4909534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
491044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4912af122d2aSMatthew G Knepley }
4913af122d2aSMatthew G Knepley 
4914689b5837SMatthew G. Knepley /*@
4915689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4916689b5837SMatthew G. Knepley 
4917d083f849SBarry Smith   Logically collective on dm
4918689b5837SMatthew G. Knepley 
4919689b5837SMatthew G. Knepley   Input Parameters:
4920689b5837SMatthew G. Knepley + dm - The DM
4921689b5837SMatthew G. Knepley - Nf - The number of fields
4922689b5837SMatthew G. Knepley 
4923689b5837SMatthew G. Knepley   Level: intermediate
4924689b5837SMatthew G. Knepley 
4925db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4926689b5837SMatthew G. Knepley @*/
4927d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4928d71ae5a4SJacob Faibussowitsch {
49290f21e855SMatthew G. Knepley   PetscInt Nf, f;
4930af122d2aSMatthew G Knepley 
4931af122d2aSMatthew G Knepley   PetscFunctionBegin;
4932af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49339566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49340f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49350f21e855SMatthew G. Knepley     PetscContainer obj;
49360f21e855SMatthew G. Knepley 
49379566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49389566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49399566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4940af122d2aSMatthew G Knepley   }
49413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4942af122d2aSMatthew G Knepley }
4943af122d2aSMatthew G Knepley 
4944c1929be8SMatthew G. Knepley /*@
4945bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4946c1929be8SMatthew G. Knepley 
4947c1929be8SMatthew G. Knepley   Not collective
4948c1929be8SMatthew G. Knepley 
4949c1929be8SMatthew G. Knepley   Input Parameters:
4950bb7acecfSBarry Smith + dm - The `DM`
4951c1929be8SMatthew G. Knepley - f  - The field number
4952c1929be8SMatthew G. Knepley 
495344a7f3ddSMatthew G. Knepley   Output Parameters:
4954bb7acecfSBarry Smith + label - The label indicating the support of the field, or NULL for the entire mesh (pass in NULL if not needed)
4955bb7acecfSBarry Smith - disc - The discretization object (pass in NULL if not needed)
4956c1929be8SMatthew G. Knepley 
495744a7f3ddSMatthew G. Knepley   Level: intermediate
4958c1929be8SMatthew G. Knepley 
4959db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4960c1929be8SMatthew G. Knepley @*/
4961d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4962d71ae5a4SJacob Faibussowitsch {
4963af122d2aSMatthew G Knepley   PetscFunctionBegin;
4964af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4965bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
49667a8be351SBarry 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);
496744a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4968bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4970decb47aaSMatthew G. Knepley }
4971decb47aaSMatthew G. Knepley 
4972083401c6SMatthew G. Knepley /* Does not clear the DS */
4973d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4974d71ae5a4SJacob Faibussowitsch {
4975083401c6SMatthew G. Knepley   PetscFunctionBegin;
49769566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49779566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49789566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4979083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4980bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4982bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4984083401c6SMatthew G. Knepley }
4985083401c6SMatthew G. Knepley 
498646560f82SMatthew G. Knepley /*@C
4987bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4988bb7acecfSBarry Smith   the field numbering.
4989c1929be8SMatthew G. Knepley 
4990d083f849SBarry Smith   Logically collective on dm
4991c1929be8SMatthew G. Knepley 
4992c1929be8SMatthew G. Knepley   Input Parameters:
4993bb7acecfSBarry Smith + dm    - The `DM`
4994c1929be8SMatthew G. Knepley . f     - The field number
499544a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4996bb7acecfSBarry Smith - disc - The discretization object
4997c1929be8SMatthew G. Knepley 
499844a7f3ddSMatthew G. Knepley   Level: intermediate
4999c1929be8SMatthew G. Knepley 
5000db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
5001c1929be8SMatthew G. Knepley @*/
5002d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5003d71ae5a4SJacob Faibussowitsch {
5004decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5005decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5006e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5007bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50087a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5009bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5010bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50119566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
501344a7f3ddSMatthew G. Knepley }
501444a7f3ddSMatthew G. Knepley 
501546560f82SMatthew G. Knepley /*@C
5016bb7acecfSBarry 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)
5017bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
501844a7f3ddSMatthew G. Knepley 
5019d083f849SBarry Smith   Logically collective on dm
502044a7f3ddSMatthew G. Knepley 
502144a7f3ddSMatthew G. Knepley   Input Parameters:
5022bb7acecfSBarry Smith + dm    - The `DM`
502344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
5024bb7acecfSBarry Smith - disc - The discretization object
502544a7f3ddSMatthew G. Knepley 
502644a7f3ddSMatthew G. Knepley   Level: intermediate
502744a7f3ddSMatthew G. Knepley 
5028bb7acecfSBarry Smith   Notes:
5029bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5030bb7acecfSBarry Smith 
5031da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5032bb7acecfSBarry 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
5033bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5034bb7acecfSBarry Smith 
5035bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
503644a7f3ddSMatthew G. Knepley @*/
5037d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5038d71ae5a4SJacob Faibussowitsch {
503944a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
504044a7f3ddSMatthew G. Knepley 
504144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
504244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5043064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5044bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50459566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
504644a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5047bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5049bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5050bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50519566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5053af122d2aSMatthew G Knepley }
50546636e97aSMatthew G Knepley 
5055e5e52638SMatthew G. Knepley /*@
5056e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5057e0b68406SMatthew Knepley 
5058e0b68406SMatthew Knepley   Logically collective on dm
5059e0b68406SMatthew Knepley 
5060e0b68406SMatthew Knepley   Input Parameters:
5061bb7acecfSBarry Smith + dm          - The `DM`
5062e0b68406SMatthew Knepley . f           - The field index
5063bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5064e0b68406SMatthew Knepley 
5065e0b68406SMatthew Knepley   Level: intermediate
5066e0b68406SMatthew Knepley 
5067db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5068e0b68406SMatthew Knepley @*/
5069d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5070d71ae5a4SJacob Faibussowitsch {
5071e0b68406SMatthew Knepley   PetscFunctionBegin;
507263a3b9bcSJacob 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);
5073e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5075e0b68406SMatthew Knepley }
5076e0b68406SMatthew Knepley 
5077e0b68406SMatthew Knepley /*@
5078e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5079e0b68406SMatthew Knepley 
5080bb7acecfSBarry Smith   Not collective
5081e0b68406SMatthew Knepley 
5082e0b68406SMatthew Knepley   Input Parameters:
5083bb7acecfSBarry Smith + dm          - The `DM`
5084e0b68406SMatthew Knepley - f           - The field index
5085e0b68406SMatthew Knepley 
5086e0b68406SMatthew Knepley   Output Parameter:
5087e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5088e0b68406SMatthew Knepley 
5089e0b68406SMatthew Knepley   Level: intermediate
5090e0b68406SMatthew Knepley 
5091bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5092e0b68406SMatthew Knepley @*/
5093d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5094d71ae5a4SJacob Faibussowitsch {
5095e0b68406SMatthew Knepley   PetscFunctionBegin;
509663a3b9bcSJacob 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);
5097e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
50983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5099e0b68406SMatthew Knepley }
5100e0b68406SMatthew Knepley 
5101e0b68406SMatthew Knepley /*@
5102bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5103e5e52638SMatthew G. Knepley 
5104d083f849SBarry Smith   Collective on dm
5105e5e52638SMatthew G. Knepley 
5106e5e52638SMatthew G. Knepley   Input Parameter:
5107bb7acecfSBarry Smith . dm - The `DM`
5108e5e52638SMatthew G. Knepley 
5109e5e52638SMatthew G. Knepley   Output Parameter:
5110bb7acecfSBarry Smith . newdm - The `DM`
5111e5e52638SMatthew G. Knepley 
5112e5e52638SMatthew G. Knepley   Level: advanced
5113e5e52638SMatthew G. Knepley 
5114db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5115e5e52638SMatthew G. Knepley @*/
5116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5117d71ae5a4SJacob Faibussowitsch {
5118e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5119e5e52638SMatthew G. Knepley 
5120e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51213ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51229566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51239566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5124e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5125e5e52638SMatthew G. Knepley     DMLabel     label;
5126e5e52638SMatthew G. Knepley     PetscObject field;
512734aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5128e5e52638SMatthew G. Knepley 
51299566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51309566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51319566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51329566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
513334aa8a36SMatthew G. Knepley   }
51343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
513534aa8a36SMatthew G. Knepley }
513634aa8a36SMatthew G. Knepley 
513734aa8a36SMatthew G. Knepley /*@
513834aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
513934aa8a36SMatthew G. Knepley 
514034aa8a36SMatthew G. Knepley   Not collective
514134aa8a36SMatthew G. Knepley 
514234aa8a36SMatthew G. Knepley   Input Parameters:
514334aa8a36SMatthew G. Knepley + dm - The DM object
514434aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
514534aa8a36SMatthew G. Knepley 
5146d8d19677SJose E. Roman   Output Parameters:
514734aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
514834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
514934aa8a36SMatthew G. Knepley 
515034aa8a36SMatthew G. Knepley   Notes:
515134aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
515234aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
515334aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5154979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
515534aa8a36SMatthew G. Knepley 
515634aa8a36SMatthew G. Knepley   Level: developer
515734aa8a36SMatthew G. Knepley 
5158db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
515934aa8a36SMatthew G. Knepley @*/
5160d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5161d71ae5a4SJacob Faibussowitsch {
516234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
516334aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5164534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
5165534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
516634aa8a36SMatthew G. Knepley   if (f < 0) {
516734aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
516834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
516934aa8a36SMatthew G. Knepley   } else {
517034aa8a36SMatthew G. Knepley     PetscInt Nf;
517134aa8a36SMatthew G. Knepley 
51729566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51737a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
517434aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
517534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
517634aa8a36SMatthew G. Knepley   }
51773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
517834aa8a36SMatthew G. Knepley }
517934aa8a36SMatthew G. Knepley 
518034aa8a36SMatthew G. Knepley /*@
518134aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
518234aa8a36SMatthew G. Knepley 
518334aa8a36SMatthew G. Knepley   Not collective
518434aa8a36SMatthew G. Knepley 
518534aa8a36SMatthew G. Knepley   Input Parameters:
518634aa8a36SMatthew G. Knepley + dm         - The DM object
518734aa8a36SMatthew G. Knepley . f          - The field number
518834aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
518934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
519034aa8a36SMatthew G. Knepley 
519134aa8a36SMatthew G. Knepley   Notes:
519234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
519334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
519434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5195979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
519634aa8a36SMatthew G. Knepley 
519734aa8a36SMatthew G. Knepley   Level: developer
519834aa8a36SMatthew G. Knepley 
5199db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
520034aa8a36SMatthew G. Knepley @*/
5201d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5202d71ae5a4SJacob Faibussowitsch {
520334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
520434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
520534aa8a36SMatthew G. Knepley   if (f < 0) {
520634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
520734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
520834aa8a36SMatthew G. Knepley   } else {
520934aa8a36SMatthew G. Knepley     PetscInt Nf;
521034aa8a36SMatthew G. Knepley 
52119566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52127a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
521334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
521434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5215e5e52638SMatthew G. Knepley   }
52163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5217e5e52638SMatthew G. Knepley }
5218e5e52638SMatthew G. Knepley 
5219b0441da4SMatthew G. Knepley /*@
5220b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5221b0441da4SMatthew G. Knepley 
5222b0441da4SMatthew G. Knepley   Not collective
5223b0441da4SMatthew G. Knepley 
5224f899ff85SJose E. Roman   Input Parameter:
5225b0441da4SMatthew G. Knepley . dm - The DM object
5226b0441da4SMatthew G. Knepley 
5227d8d19677SJose E. Roman   Output Parameters:
5228b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5229b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5230b0441da4SMatthew G. Knepley 
5231b0441da4SMatthew G. Knepley   Notes:
5232b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5233b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5234b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5235b0441da4SMatthew G. Knepley 
5236b0441da4SMatthew G. Knepley   Level: developer
5237b0441da4SMatthew G. Knepley 
5238db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5239b0441da4SMatthew G. Knepley @*/
5240d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5241d71ae5a4SJacob Faibussowitsch {
5242b0441da4SMatthew G. Knepley   PetscInt Nf;
5243b0441da4SMatthew G. Knepley 
5244b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5245b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5246064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5247064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
52489566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5249b0441da4SMatthew G. Knepley   if (!Nf) {
52509566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5251b0441da4SMatthew G. Knepley   } else {
52529566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5253b0441da4SMatthew G. Knepley   }
52543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5255b0441da4SMatthew G. Knepley }
5256b0441da4SMatthew G. Knepley 
5257b0441da4SMatthew G. Knepley /*@
5258b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5259b0441da4SMatthew G. Knepley 
5260b0441da4SMatthew G. Knepley   Not collective
5261b0441da4SMatthew G. Knepley 
5262b0441da4SMatthew G. Knepley   Input Parameters:
5263b0441da4SMatthew G. Knepley + dm         - The DM object
5264b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5265b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5266b0441da4SMatthew G. Knepley 
5267b0441da4SMatthew G. Knepley   Notes:
5268b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5269b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5270b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5271b0441da4SMatthew G. Knepley 
5272b0441da4SMatthew G. Knepley   Level: developer
5273b0441da4SMatthew G. Knepley 
5274db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5275b0441da4SMatthew G. Knepley @*/
5276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5277d71ae5a4SJacob Faibussowitsch {
5278b0441da4SMatthew G. Knepley   PetscInt Nf;
5279b0441da4SMatthew G. Knepley 
5280b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5281b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52829566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5283b0441da4SMatthew G. Knepley   if (!Nf) {
52849566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5285b0441da4SMatthew G. Knepley   } else {
52869566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5287e5e52638SMatthew G. Knepley   }
52883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5289e5e52638SMatthew G. Knepley }
5290e5e52638SMatthew G. Knepley 
5291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5292d71ae5a4SJacob Faibussowitsch {
5293799db056SMatthew G. Knepley   DM           plex;
5294799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5295799db056SMatthew G. Knepley   const char **names;
5296799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5297799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5298799db056SMatthew G. Knepley   size_t       len;
5299799db056SMatthew G. Knepley   MPI_Comm     comm;
5300799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5301783e2ec8SMatthew G. Knepley 
5302783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5303799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5304799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5305799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5306799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5307799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5308799db056SMatthew G. Knepley     PetscDS  dsBC;
5309799db056SMatthew G. Knepley     PetscInt numBd;
5310799db056SMatthew G. Knepley 
5311799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5312799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5313799db056SMatthew G. Knepley     maxLabels += numBd;
5314799db056SMatthew G. Knepley   }
5315799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5316799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5317799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5318799db056SMatthew G. Knepley     PetscDS  dsBC;
5319799db056SMatthew G. Knepley     PetscInt numBd, bd;
5320799db056SMatthew G. Knepley 
5321799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5322799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5323799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5324799db056SMatthew G. Knepley       DMLabel      label;
5325799db056SMatthew G. Knepley       PetscInt     field;
5326799db056SMatthew G. Knepley       PetscObject  obj;
5327799db056SMatthew G. Knepley       PetscClassId id;
5328799db056SMatthew G. Knepley 
5329799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53309566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53319566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5332799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53339371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53349371c9d4SSatish Balay         if (labels[l] == label) break;
5335799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5336783e2ec8SMatthew G. Knepley     }
5337799db056SMatthew G. Knepley   }
5338799db056SMatthew G. Knepley   /* Get label names */
5339799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5340799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53419371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53429371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53439371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53449371c9d4SSatish Balay   }
5345799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5346799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5347799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5348*c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5349799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5350799db056SMatthew G. Knepley   /* Put all names on all processes */
5351799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5352799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5353799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5354799db056SMatthew G. Knepley   gNl = displs[size];
53559371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53569371c9d4SSatish Balay     counts[p] *= gmaxLen;
53579371c9d4SSatish Balay     displs[p] *= gmaxLen;
53589371c9d4SSatish Balay   }
5359799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5360799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5361799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5362799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5363799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5364799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5365799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53669371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53679371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53689566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5369799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53709566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5371799db056SMatthew G. Knepley     ++gl;
5372783e2ec8SMatthew G. Knepley   }
5373799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5375783e2ec8SMatthew G. Knepley }
5376783e2ec8SMatthew G. Knepley 
5377d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5378d71ae5a4SJacob Faibussowitsch {
5379e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5380e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5381e5e52638SMatthew G. Knepley 
5382e5e52638SMatthew G. Knepley   PetscFunctionBegin;
53833ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
53849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5385e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53869371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53879371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53889371c9d4SSatish Balay     tmpd[s].label  = NULL;
53899371c9d4SSatish Balay     tmpd[s].fields = NULL;
53909371c9d4SSatish Balay   }
53919566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5392e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5393e5e52638SMatthew G. Knepley   dm->probs = tmpd;
53943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5395e5e52638SMatthew G. Knepley }
5396e5e52638SMatthew G. Knepley 
5397e5e52638SMatthew G. Knepley /*@
5398e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5399e5e52638SMatthew G. Knepley 
5400e5e52638SMatthew G. Knepley   Not collective
5401e5e52638SMatthew G. Knepley 
5402e5e52638SMatthew G. Knepley   Input Parameter:
5403e5e52638SMatthew G. Knepley . dm - The DM
5404e5e52638SMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley   Output Parameter:
5406e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5407e5e52638SMatthew G. Knepley 
5408e5e52638SMatthew G. Knepley   Level: intermediate
5409e5e52638SMatthew G. Knepley 
5410db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5411e5e52638SMatthew G. Knepley @*/
5412d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5413d71ae5a4SJacob Faibussowitsch {
5414e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5415e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5416534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5417e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5419e5e52638SMatthew G. Knepley }
5420e5e52638SMatthew G. Knepley 
5421e5e52638SMatthew G. Knepley /*@
5422e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5423e5e52638SMatthew G. Knepley 
5424d083f849SBarry Smith   Logically collective on dm
5425e5e52638SMatthew G. Knepley 
5426e5e52638SMatthew G. Knepley   Input Parameter:
5427e5e52638SMatthew G. Knepley . dm - The DM
5428e5e52638SMatthew G. Knepley 
5429e5e52638SMatthew G. Knepley   Level: intermediate
5430e5e52638SMatthew G. Knepley 
5431db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5432e5e52638SMatthew G. Knepley @*/
5433d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5434d71ae5a4SJacob Faibussowitsch {
5435e5e52638SMatthew G. Knepley   PetscInt s;
5436e5e52638SMatthew G. Knepley 
5437e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5438e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5439e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54409566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
54419566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54429566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5443e5e52638SMatthew G. Knepley   }
54449566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5445e5e52638SMatthew G. Knepley   dm->probs = NULL;
5446e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5448e5e52638SMatthew G. Knepley }
5449e5e52638SMatthew G. Knepley 
5450e5e52638SMatthew G. Knepley /*@
5451e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5452e5e52638SMatthew G. Knepley 
5453e5e52638SMatthew G. Knepley   Not collective
5454e5e52638SMatthew G. Knepley 
5455e5e52638SMatthew G. Knepley   Input Parameter:
5456e5e52638SMatthew G. Knepley . dm    - The DM
5457e5e52638SMatthew G. Knepley 
5458e5e52638SMatthew G. Knepley   Output Parameter:
5459e5e52638SMatthew G. Knepley . prob - The default PetscDS
5460e5e52638SMatthew G. Knepley 
5461e5e52638SMatthew G. Knepley   Level: intermediate
5462e5e52638SMatthew G. Knepley 
5463db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5464e5e52638SMatthew G. Knepley @*/
5465d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5466d71ae5a4SJacob Faibussowitsch {
5467e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5468e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5469e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5470b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5471b0143b4dSMatthew G. Knepley     PetscDS ds;
5472b0143b4dSMatthew G. Knepley 
54739566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
54749566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
54759566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5476b0143b4dSMatthew G. Knepley   }
5477b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
54783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5479e5e52638SMatthew G. Knepley }
5480e5e52638SMatthew G. Knepley 
5481e5e52638SMatthew G. Knepley /*@
5482e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5483e5e52638SMatthew G. Knepley 
5484e5e52638SMatthew G. Knepley   Not collective
5485e5e52638SMatthew G. Knepley 
5486e5e52638SMatthew G. Knepley   Input Parameters:
5487e5e52638SMatthew G. Knepley + dm    - The DM
5488e5e52638SMatthew G. Knepley - point - Cell for the DS
5489e5e52638SMatthew G. Knepley 
5490e5e52638SMatthew G. Knepley   Output Parameter:
5491e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5492e5e52638SMatthew G. Knepley 
5493e5e52638SMatthew G. Knepley   Level: developer
5494e5e52638SMatthew G. Knepley 
5495db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5496e5e52638SMatthew G. Knepley @*/
5497d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5498d71ae5a4SJacob Faibussowitsch {
5499e5e52638SMatthew G. Knepley   PetscDS  probDef = NULL;
5500e5e52638SMatthew G. Knepley   PetscInt s;
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5503e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5504e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
550563a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5506e5e52638SMatthew G. Knepley   *prob = NULL;
5507e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5508e5e52638SMatthew G. Knepley     PetscInt val;
5509e5e52638SMatthew G. Knepley 
55109371c9d4SSatish Balay     if (!dm->probs[s].label) {
55119371c9d4SSatish Balay       probDef = dm->probs[s].ds;
55129371c9d4SSatish Balay     } else {
55139566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55149371c9d4SSatish Balay       if (val >= 0) {
55159371c9d4SSatish Balay         *prob = dm->probs[s].ds;
55169371c9d4SSatish Balay         break;
55179371c9d4SSatish Balay       }
5518e5e52638SMatthew G. Knepley     }
5519e5e52638SMatthew G. Knepley   }
5520e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
55213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5522e5e52638SMatthew G. Knepley }
5523e5e52638SMatthew G. Knepley 
5524e5e52638SMatthew G. Knepley /*@
5525e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5526e5e52638SMatthew G. Knepley 
5527e5e52638SMatthew G. Knepley   Not collective
5528e5e52638SMatthew G. Knepley 
5529e5e52638SMatthew G. Knepley   Input Parameters:
5530e5e52638SMatthew G. Knepley + dm    - The DM
5531e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5532e5e52638SMatthew G. Knepley 
5533b3cf3223SMatthew G. Knepley   Output Parameters:
5534b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5535b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5536e5e52638SMatthew G. Knepley 
5537154ca461SJed Brown   Note:
5538154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5539154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5540154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5541e5e52638SMatthew G. Knepley 
5542e5e52638SMatthew G. Knepley   Level: advanced
5543e5e52638SMatthew G. Knepley 
5544db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5545e5e52638SMatthew G. Knepley @*/
5546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5547d71ae5a4SJacob Faibussowitsch {
5548e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5549e5e52638SMatthew G. Knepley 
5550e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5551e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5552e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55539371c9d4SSatish Balay   if (fields) {
55549371c9d4SSatish Balay     PetscValidPointer(fields, 3);
55559371c9d4SSatish Balay     *fields = NULL;
55569371c9d4SSatish Balay   }
55579371c9d4SSatish Balay   if (ds) {
55589371c9d4SSatish Balay     PetscValidPointer(ds, 4);
55599371c9d4SSatish Balay     *ds = NULL;
55609371c9d4SSatish Balay   }
5561e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5562154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5563b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5564b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
55653ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5566b3cf3223SMatthew G. Knepley     }
5567e5e52638SMatthew G. Knepley   }
55683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5569e5e52638SMatthew G. Knepley }
5570e5e52638SMatthew G. Knepley 
5571e5e52638SMatthew G. Knepley /*@
5572bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5573083401c6SMatthew G. Knepley 
5574083401c6SMatthew G. Knepley   Collective on dm
5575083401c6SMatthew G. Knepley 
5576083401c6SMatthew G. Knepley   Input Parameters:
5577bb7acecfSBarry Smith + dm     - The `DM`
5578bb7acecfSBarry Smith . label  - The `DMLabel` defining the mesh region, or NULL for the entire mesh
5579bb7acecfSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or NULL for all fields
5580bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5581083401c6SMatthew G. Knepley 
5582bb7acecfSBarry Smith   Note:
5583bb7acecfSBarry 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,
5584083401c6SMatthew G. Knepley   the fields argument is ignored.
5585083401c6SMatthew G. Knepley 
5586083401c6SMatthew G. Knepley   Level: advanced
5587083401c6SMatthew G. Knepley 
5588db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5589083401c6SMatthew G. Knepley @*/
5590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5591d71ae5a4SJacob Faibussowitsch {
5592083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5593083401c6SMatthew G. Knepley 
5594083401c6SMatthew G. Knepley   PetscFunctionBegin;
5595083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5596083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5597064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5598083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5599083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56009566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5601083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
56023ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5603083401c6SMatthew G. Knepley     }
5604083401c6SMatthew G. Knepley   }
56059566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
5609083401c6SMatthew G. Knepley   if (!label) {
5610083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5611083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5612083401c6SMatthew G. Knepley     Nds = 0;
5613083401c6SMatthew G. Knepley   }
5614083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5615083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5616083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
56173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5618083401c6SMatthew G. Knepley }
5619083401c6SMatthew G. Knepley 
5620083401c6SMatthew G. Knepley /*@
5621e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5622e5e52638SMatthew G. Knepley 
5623e5e52638SMatthew G. Knepley   Not collective
5624e5e52638SMatthew G. Knepley 
5625e5e52638SMatthew G. Knepley   Input Parameters:
5626e5e52638SMatthew G. Knepley + dm  - The DM
5627e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5628e5e52638SMatthew G. Knepley 
5629e5e52638SMatthew G. Knepley   Output Parameters:
5630b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5631b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5632083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5633e5e52638SMatthew G. Knepley 
5634e5e52638SMatthew G. Knepley   Level: advanced
5635e5e52638SMatthew G. Knepley 
5636db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5637e5e52638SMatthew G. Knepley @*/
5638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5639d71ae5a4SJacob Faibussowitsch {
5640e5e52638SMatthew G. Knepley   PetscInt Nds;
5641e5e52638SMatthew G. Knepley 
5642e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5643e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56449566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
564563a3b9bcSJacob 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);
5646e5e52638SMatthew G. Knepley   if (label) {
5647e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5648e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5649e5e52638SMatthew G. Knepley   }
5650b3cf3223SMatthew G. Knepley   if (fields) {
5651b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5652b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5653b3cf3223SMatthew G. Knepley   }
5654e5e52638SMatthew G. Knepley   if (ds) {
5655b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5656e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5657e5e52638SMatthew G. Knepley   }
56583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5659e5e52638SMatthew G. Knepley }
5660e5e52638SMatthew G. Knepley 
5661e5e52638SMatthew G. Knepley /*@
5662083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5663e5e52638SMatthew G. Knepley 
5664083401c6SMatthew G. Knepley   Not collective
5665e5e52638SMatthew G. Knepley 
5666e5e52638SMatthew G. Knepley   Input Parameters:
5667e5e52638SMatthew G. Knepley + dm     - The DM
5668083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5669083401c6SMatthew G. Knepley . label  - The region label, or NULL
5670083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5671083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5672e5e52638SMatthew G. Knepley 
5673e5e52638SMatthew G. Knepley   Level: advanced
5674e5e52638SMatthew G. Knepley 
5675db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5676e5e52638SMatthew G. Knepley @*/
5677d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5678d71ae5a4SJacob Faibussowitsch {
5679083401c6SMatthew G. Knepley   PetscInt Nds;
5680e5e52638SMatthew G. Knepley 
5681e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5682e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5683ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56849566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
568563a3b9bcSJacob 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);
56869566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56879566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5688083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5689083401c6SMatthew G. Knepley   if (fields) {
5690083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56919566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56929566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5693083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5694e5e52638SMatthew G. Knepley   }
5695083401c6SMatthew G. Knepley   if (ds) {
5696083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56979566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56989566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5699083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5700083401c6SMatthew G. Knepley   }
57013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5702e5e52638SMatthew G. Knepley }
5703e5e52638SMatthew G. Knepley 
5704e5e52638SMatthew G. Knepley /*@
57051d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
57061d3af9e0SMatthew G. Knepley 
57071d3af9e0SMatthew G. Knepley   Not collective
57081d3af9e0SMatthew G. Knepley 
57091d3af9e0SMatthew G. Knepley   Input Parameters:
57101d3af9e0SMatthew G. Knepley + dm  - The DM
57111d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
57121d3af9e0SMatthew G. Knepley 
57131d3af9e0SMatthew G. Knepley   Output Parameter:
57141d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57151d3af9e0SMatthew G. Knepley 
57161d3af9e0SMatthew G. Knepley   Level: advanced
57171d3af9e0SMatthew G. Knepley 
5718db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57191d3af9e0SMatthew G. Knepley @*/
5720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5721d71ae5a4SJacob Faibussowitsch {
57221d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57231d3af9e0SMatthew G. Knepley 
57241d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57251d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57261d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5727dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
57289566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57299371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57309371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57311d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57321d3af9e0SMatthew G. Knepley   else *num = n;
57333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57341d3af9e0SMatthew G. Knepley }
57351d3af9e0SMatthew G. Knepley 
57362df84da0SMatthew G. Knepley /*@C
5737bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57382df84da0SMatthew G. Knepley 
57392df84da0SMatthew G. Knepley   Not collective
57402df84da0SMatthew G. Knepley 
5741f1a722f8SMatthew G. Knepley   Input Parameters:
5742bb7acecfSBarry Smith + dm     - The `DM`
57432df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5744bb7acecfSBarry Smith . prefix - The options prefix for the output `PetscFE`, or NULL
5745bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57462df84da0SMatthew G. Knepley 
57472df84da0SMatthew G. Knepley   Output Parameter:
5748bb7acecfSBarry Smith . fem - The `PetscFE`
57492df84da0SMatthew G. Knepley 
5750bb7acecfSBarry Smith   Note:
5751bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57522df84da0SMatthew G. Knepley 
57532df84da0SMatthew G. Knepley   Level: intermediate
57542df84da0SMatthew G. Knepley 
5755db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57562df84da0SMatthew G. Knepley @*/
5757d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5758d71ae5a4SJacob Faibussowitsch {
57592df84da0SMatthew G. Knepley   DMPolytopeType ct;
57602df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57612df84da0SMatthew G. Knepley 
57622df84da0SMatthew G. Knepley   PetscFunctionBegin;
57632df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57642df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57652df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57662df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57672df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57689566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57719566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57732df84da0SMatthew G. Knepley }
57742df84da0SMatthew G. Knepley 
57751d3af9e0SMatthew G. Knepley /*@
5776bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5777e5e52638SMatthew G. Knepley 
5778d083f849SBarry Smith   Collective on dm
5779e5e52638SMatthew G. Knepley 
5780e5e52638SMatthew G. Knepley   Input Parameter:
5781bb7acecfSBarry Smith . dm - The `DM`
5782e5e52638SMatthew G. Knepley 
578345480ffeSMatthew G. Knepley   Options Database Keys:
5784bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
578545480ffeSMatthew G. Knepley 
5786bb7acecfSBarry Smith   Note:
5787bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5788e5e52638SMatthew G. Knepley 
5789e5e52638SMatthew G. Knepley   Level: intermediate
5790e5e52638SMatthew G. Knepley 
5791db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5792e5e52638SMatthew G. Knepley @*/
5793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5794d71ae5a4SJacob Faibussowitsch {
5795e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5796083401c6SMatthew G. Knepley   PetscDS   dsDef;
5797083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5798f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5799f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5800e5e52638SMatthew G. Knepley 
5801e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5802e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58033ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58049566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58059566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5806083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58079566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5808083401c6SMatthew G. Knepley   Nl   = 0;
5809083401c6SMatthew G. Knepley   Ndef = 0;
5810083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5811083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5812083401c6SMatthew G. Knepley     PetscInt l;
5813083401c6SMatthew G. Knepley 
5814f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5815f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5816f918ec44SMatthew G. Knepley     {
5817f918ec44SMatthew G. Knepley       PetscClassId id;
5818f918ec44SMatthew G. Knepley 
58199566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5820f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5821f918ec44SMatthew G. Knepley         Ceed ceed;
5822f918ec44SMatthew G. Knepley 
58239566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58249566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5825f918ec44SMatthew G. Knepley       }
5826f918ec44SMatthew G. Knepley     }
5827f918ec44SMatthew G. Knepley #endif
58289371c9d4SSatish Balay     if (!label) {
58299371c9d4SSatish Balay       ++Ndef;
58309371c9d4SSatish Balay       continue;
58319371c9d4SSatish Balay     }
58329371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58339371c9d4SSatish Balay       if (label == labelSet[l]) break;
5834083401c6SMatthew G. Knepley     if (l < Nl) continue;
5835083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5836083401c6SMatthew G. Knepley   }
5837083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
58389566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5839083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5840b3cf3223SMatthew G. Knepley     IS        fields;
5841b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5842b3cf3223SMatthew G. Knepley 
58439371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58449371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58457a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58469566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58479371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58489371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58499566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58509566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58519566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58529566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
585388f0c812SMatthew G. Knepley 
58549566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58559566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
58569566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58579566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58582df9ee95SMatthew G. Knepley   }
58599566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
58609566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5861083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5862083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58630122748bSMatthew G. Knepley     DM              plex;
5864083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5865083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5866083401c6SMatthew G. Knepley     PetscInt       *fields;
5867083401c6SMatthew G. Knepley     const PetscInt *cells;
5868083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58690122748bSMatthew G. Knepley 
58709566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58729566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58739566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58745fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5875083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5876083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5877083401c6SMatthew G. Knepley       IS      pointIS;
5878083401c6SMatthew G. Knepley 
58799566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58809566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58819566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58829566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5883083401c6SMatthew G. Knepley     }
58849566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5885083401c6SMatthew G. Knepley 
58869566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58879566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58889566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58899566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58909566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58919566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58929566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5893083401c6SMatthew G. Knepley 
58949566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58959371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58969371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58979566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58989566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58999566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59009566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5901083401c6SMatthew G. Knepley 
59029566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
59039566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
59049566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59059566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59069566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59079566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59089566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5909083401c6SMatthew G. Knepley   }
5910083401c6SMatthew G. Knepley   /* Create label DSes
5911083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5912083401c6SMatthew G. Knepley   */
5913083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5914083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5915083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5916083401c6SMatthew G. Knepley     PetscDS   ds;
5917083401c6SMatthew G. Knepley     IS        fields;
5918083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5919083401c6SMatthew G. Knepley 
59209566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59219371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59229371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59239566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59249371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59259371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59269566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59279566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59289566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59299566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59309566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
59319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59329566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5933083401c6SMatthew G. Knepley     {
5934083401c6SMatthew G. Knepley       DMPolytopeType ct;
5935083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59365fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59370122748bSMatthew G. Knepley 
59389566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5939665f567fSMatthew G. Knepley       if (lStart >= 0) {
59409566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5941412e9a14SMatthew G. Knepley         switch (ct) {
5942412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5943412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5944412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5945d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5946d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5947d71ae5a4SJacob Faibussowitsch           break;
5948d71ae5a4SJacob Faibussowitsch         default:
5949d71ae5a4SJacob Faibussowitsch           break;
5950412e9a14SMatthew G. Knepley         }
5951665f567fSMatthew G. Knepley       }
59529566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
59535fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59545fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59555fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59569566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59579566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
59585fedec97SMatthew G. Knepley           }
59595fedec97SMatthew G. Knepley           ++nf;
59605fedec97SMatthew G. Knepley         }
59615fedec97SMatthew G. Knepley       }
5962e5e52638SMatthew G. Knepley     }
59639566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5964e5e52638SMatthew G. Knepley   }
59659566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5966e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5967083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5968083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5969083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5970083401c6SMatthew G. Knepley     const PetscInt *fld;
59715fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59725fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5973e5e52638SMatthew G. Knepley 
59749566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59759566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59769566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59779566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5978083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5979083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59805fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5981e5e52638SMatthew G. Knepley       PetscClassId id;
5982e5e52638SMatthew G. Knepley 
59835fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59849566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59855fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
59869566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&disc));
59879566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5988083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59899566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5990e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5991e5e52638SMatthew G. Knepley     }
59929566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5993e5e52638SMatthew G. Knepley   }
5994f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
59959566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5996f9244615SMatthew G. Knepley   if (flg) {
59973b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59983b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
59993b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60003b4aee56SMatthew G. Knepley 
60019566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
60029566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
60033b4aee56SMatthew G. Knepley     }
6004f9244615SMatthew G. Knepley   }
6005e5e52638SMatthew G. Knepley   /* Setup DSes */
6006e5e52638SMatthew G. Knepley   if (doSetup) {
60079566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
6008e5e52638SMatthew G. Knepley   }
60093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6010e5e52638SMatthew G. Knepley }
6011e5e52638SMatthew G. Knepley 
6012e5e52638SMatthew G. Knepley /*@
6013bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
60147f96f943SMatthew G. Knepley 
6015bb7acecfSBarry Smith   Collective on `DM`
6016f2cacb80SMatthew G. Knepley 
60177f96f943SMatthew G. Knepley   Input Parameters:
6018bb7acecfSBarry Smith + dm   - The `DM`
60197f96f943SMatthew G. Knepley - time - The time
60207f96f943SMatthew G. Knepley 
60217f96f943SMatthew G. Knepley   Output Parameters:
6022f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
6023f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
60247f96f943SMatthew G. Knepley 
6025bb7acecfSBarry Smith   Note:
6026bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
60277f96f943SMatthew G. Knepley 
60287f96f943SMatthew G. Knepley   Level: developer
60297f96f943SMatthew G. Knepley 
6030db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
60317f96f943SMatthew G. Knepley @*/
6032d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6033d71ae5a4SJacob Faibussowitsch {
60347f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
60357f96f943SMatthew G. Knepley   void   **ectxs;
6036f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
60377f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
60387f96f943SMatthew G. Knepley 
60397f96f943SMatthew G. Knepley   PetscFunctionBegin;
6040f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6041f60fa741SMatthew G. Knepley   if (u) {
6042f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6043f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6044f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6045f60fa741SMatthew G. Knepley   }
6046f60fa741SMatthew G. Knepley   if (u_t) {
6047f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6048f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6049f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6050f60fa741SMatthew G. Knepley   }
60519566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
60529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
60539566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60547f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
60557f96f943SMatthew G. Knepley     PetscDS         ds;
60567f96f943SMatthew G. Knepley     DMLabel         label;
60577f96f943SMatthew G. Knepley     IS              fieldIS;
60587f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60597f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60607f96f943SMatthew G. Knepley 
60619566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
60629566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
60639566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
60649566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
60659566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6066f2cacb80SMatthew G. Knepley     if (u) {
6067f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6068f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6069f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
60707f96f943SMatthew G. Knepley     }
6071f2cacb80SMatthew G. Knepley     if (u_t) {
60729566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60739566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6074f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6075f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6076f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6077f2cacb80SMatthew G. Knepley     }
60789566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6079f2cacb80SMatthew G. Knepley   }
6080f2cacb80SMatthew G. Knepley   if (u) {
60819566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6083f2cacb80SMatthew G. Knepley   }
6084f2cacb80SMatthew G. Knepley   if (u_t) {
60859566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
60869566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6087f2cacb80SMatthew G. Knepley   }
60889566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6089f60fa741SMatthew G. Knepley   if (u) {
6090f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6091f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6092f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6093f60fa741SMatthew G. Knepley   }
6094f60fa741SMatthew G. Knepley   if (u_t) {
6095f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6096f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6097f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6098f60fa741SMatthew G. Knepley   }
60993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61007f96f943SMatthew G. Knepley }
61017f96f943SMatthew G. Knepley 
6102d71ae5a4SJacob Faibussowitsch PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
6103d71ae5a4SJacob Faibussowitsch {
610445480ffeSMatthew G. Knepley   PetscDS    dsNew;
610545480ffeSMatthew G. Knepley   DSBoundary b;
61066a02485aSMatthew G. Knepley   PetscInt   cdim, Nf, f, d;
61075fedec97SMatthew G. Knepley   PetscBool  isCohesive;
610845480ffeSMatthew G. Knepley   void      *ctx;
610945480ffeSMatthew G. Knepley 
611045480ffeSMatthew G. Knepley   PetscFunctionBegin;
61119566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
61129566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
61139566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
61149566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
61159566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
61169566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
611745480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
61189566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
61199566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
61209566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
61219566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
61226a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
61236a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
612445480ffeSMatthew G. Knepley   }
612545480ffeSMatthew G. Knepley   if (Nf) {
61269566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
61279566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
612845480ffeSMatthew G. Knepley   }
61299566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
613045480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
61319566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
613245480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
61337a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
613445480ffeSMatthew G. Knepley   }
613545480ffeSMatthew G. Knepley 
61369566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
61379566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
61383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
613945480ffeSMatthew G. Knepley }
614045480ffeSMatthew G. Knepley 
61417f96f943SMatthew G. Knepley /*@
6142bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6143e5e52638SMatthew G. Knepley 
6144d083f849SBarry Smith   Collective on dm
6145e5e52638SMatthew G. Knepley 
6146e5e52638SMatthew G. Knepley   Input Parameter:
6147bb7acecfSBarry Smith . dm - The `DM`
6148e5e52638SMatthew G. Knepley 
6149e5e52638SMatthew G. Knepley   Output Parameter:
6150bb7acecfSBarry Smith . newdm - The `DM`
6151e5e52638SMatthew G. Knepley 
6152e5e52638SMatthew G. Knepley   Level: advanced
6153e5e52638SMatthew G. Knepley 
6154db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6155e5e52638SMatthew G. Knepley @*/
6156d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6157d71ae5a4SJacob Faibussowitsch {
6158e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6159e5e52638SMatthew G. Knepley 
6160e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61613ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
61629566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61639566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6164e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6165e5e52638SMatthew G. Knepley     DMLabel  label;
6166b3cf3223SMatthew G. Knepley     IS       fields;
616745480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6168783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6169e5e52638SMatthew G. Knepley 
61709566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
6171b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
61729566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
6173d5b43468SJose E. Roman     /* Complete new labels in the new DS */
61749566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
61759566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6176783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6177b8025e53SMatthew G. Knepley       PetscWeakForm wf;
617845480ffeSMatthew G. Knepley       DMLabel       label;
6179783e2ec8SMatthew G. Knepley       PetscInt      field;
6180783e2ec8SMatthew G. Knepley 
61819566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61829566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6183783e2ec8SMatthew G. Knepley     }
6184e5e52638SMatthew G. Knepley   }
6185799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
61863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6187e5e52638SMatthew G. Knepley }
6188e5e52638SMatthew G. Knepley 
6189e5e52638SMatthew G. Knepley /*@
6190bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6191e5e52638SMatthew G. Knepley 
6192d083f849SBarry Smith   Collective on dm
6193e5e52638SMatthew G. Knepley 
6194e5e52638SMatthew G. Knepley   Input Parameter:
6195bb7acecfSBarry Smith . dm - The `DM`
6196e5e52638SMatthew G. Knepley 
6197e5e52638SMatthew G. Knepley   Output Parameter:
6198bb7acecfSBarry Smith . newdm - The `DM`
6199e5e52638SMatthew G. Knepley 
6200e5e52638SMatthew G. Knepley   Level: advanced
6201e5e52638SMatthew G. Knepley 
6202bb7acecfSBarry Smith   Developer Note:
6203bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6204bb7acecfSBarry Smith 
6205db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6206e5e52638SMatthew G. Knepley @*/
6207d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6208d71ae5a4SJacob Faibussowitsch {
6209e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62109566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
62119566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
62123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6213e5e52638SMatthew G. Knepley }
6214e5e52638SMatthew G. Knepley 
6215c73cfb54SMatthew G. Knepley /*@
6216bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6217c73cfb54SMatthew G. Knepley 
6218c73cfb54SMatthew G. Knepley   Not collective
6219c73cfb54SMatthew G. Knepley 
6220c73cfb54SMatthew G. Knepley   Input Parameter:
6221bb7acecfSBarry Smith . dm - The `DM`
6222c73cfb54SMatthew G. Knepley 
6223c73cfb54SMatthew G. Knepley   Output Parameter:
6224c73cfb54SMatthew G. Knepley . dim - The topological dimension
6225c73cfb54SMatthew G. Knepley 
6226c73cfb54SMatthew G. Knepley   Level: beginner
6227c73cfb54SMatthew G. Knepley 
6228db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6229c73cfb54SMatthew G. Knepley @*/
6230d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6231d71ae5a4SJacob Faibussowitsch {
6232c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6233c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6234534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6235c73cfb54SMatthew G. Knepley   *dim = dm->dim;
62363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6237c73cfb54SMatthew G. Knepley }
6238c73cfb54SMatthew G. Knepley 
6239c73cfb54SMatthew G. Knepley /*@
6240bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6241c73cfb54SMatthew G. Knepley 
6242c73cfb54SMatthew G. Knepley   Collective on dm
6243c73cfb54SMatthew G. Knepley 
6244c73cfb54SMatthew G. Knepley   Input Parameters:
6245bb7acecfSBarry Smith + dm - The `DM`
6246c73cfb54SMatthew G. Knepley - dim - The topological dimension
6247c73cfb54SMatthew G. Knepley 
6248c73cfb54SMatthew G. Knepley   Level: beginner
6249c73cfb54SMatthew G. Knepley 
6250db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6251c73cfb54SMatthew G. Knepley @*/
6252d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6253d71ae5a4SJacob Faibussowitsch {
6254e5e52638SMatthew G. Knepley   PetscDS  ds;
625545480ffeSMatthew G. Knepley   PetscInt Nds, n;
6256f17e8794SMatthew G. Knepley 
6257c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6258c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6259c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6260c73cfb54SMatthew G. Knepley   dm->dim = dim;
6261d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
62629566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
626345480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
62649566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
62659566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
626645480ffeSMatthew G. Knepley     }
6267d17bd122SMatthew G. Knepley   }
62683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6269c73cfb54SMatthew G. Knepley }
6270c73cfb54SMatthew G. Knepley 
6271793f3fe5SMatthew G. Knepley /*@
6272793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6273793f3fe5SMatthew G. Knepley 
6274d083f849SBarry Smith   Collective on dm
6275793f3fe5SMatthew G. Knepley 
6276793f3fe5SMatthew G. Knepley   Input Parameters:
6277bb7acecfSBarry Smith + dm - the `DM`
6278793f3fe5SMatthew G. Knepley - dim - the dimension
6279793f3fe5SMatthew G. Knepley 
6280793f3fe5SMatthew G. Knepley   Output Parameters:
6281793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6282aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6283793f3fe5SMatthew G. Knepley 
6284793f3fe5SMatthew G. Knepley   Note:
6285793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6286a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6287793f3fe5SMatthew G. Knepley   then the interval is empty.
6288793f3fe5SMatthew G. Knepley 
6289793f3fe5SMatthew G. Knepley   Level: intermediate
6290793f3fe5SMatthew G. Knepley 
6291db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6292793f3fe5SMatthew G. Knepley @*/
6293d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6294d71ae5a4SJacob Faibussowitsch {
6295793f3fe5SMatthew G. Knepley   PetscInt d;
6296793f3fe5SMatthew G. Knepley 
6297793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6298793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62999566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
63007a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6301dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
63023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6303793f3fe5SMatthew G. Knepley }
6304793f3fe5SMatthew G. Knepley 
63056636e97aSMatthew G Knepley /*@
6306bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6307f4d763aaSMatthew G. Knepley 
63088f700142SStefano Zampini   Collective on dm
63098f700142SStefano Zampini 
6310f4d763aaSMatthew G. Knepley   Input Parameter:
6311bb7acecfSBarry Smith . dm - The original `DM`
6312f4d763aaSMatthew G. Knepley 
6313f4d763aaSMatthew G. Knepley   Output Parameter:
6314bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6315f4d763aaSMatthew G. Knepley 
6316f4d763aaSMatthew G. Knepley   Level: intermediate
6317f4d763aaSMatthew G. Knepley 
6318bb7acecfSBarry Smith   Note:
6319bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6320bb7acecfSBarry 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
6321bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6322bb7acecfSBarry Smith 
6323bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6324f4d763aaSMatthew G. Knepley @*/
6325d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6326d71ae5a4SJacob Faibussowitsch {
6327c26acbdeSMatthew G. Knepley   PetscSection section;
63282d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
632914f150ffSMatthew G. Knepley 
633014f150ffSMatthew G. Knepley   PetscFunctionBegin;
633114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
633214f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
63339566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
63349566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
63359566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
63362d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6337c26acbdeSMatthew G. Knepley     *odm = dm;
63383ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6339c26acbdeSMatthew G. Knepley   }
634014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6341c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
634214f150ffSMatthew G. Knepley     PetscSF      sf;
634314f150ffSMatthew G. Knepley 
63449566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
63459566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
63469566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
63479566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
63489566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
63499566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
63509566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
63519566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
63529566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
635314f150ffSMatthew G. Knepley   }
635414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
63553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
635614f150ffSMatthew G. Knepley }
6357f4d763aaSMatthew G. Knepley 
6358f4d763aaSMatthew G. Knepley /*@
6359cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6360f4d763aaSMatthew G. Knepley 
6361f4d763aaSMatthew G. Knepley   Input Parameter:
6362bb7acecfSBarry Smith . dm - The original `DM`
6363f4d763aaSMatthew G. Knepley 
6364cdb7a50dSMatthew G. Knepley   Output Parameters:
6365cdb7a50dSMatthew G. Knepley + num - The output sequence number
6366cdb7a50dSMatthew G. Knepley - val - The output sequence value
6367f4d763aaSMatthew G. Knepley 
6368f4d763aaSMatthew G. Knepley   Level: intermediate
6369f4d763aaSMatthew G. Knepley 
6370bb7acecfSBarry Smith   Note:
6371bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6372bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6373bb7acecfSBarry Smith 
6374bb7acecfSBarry Smith   Developer Note:
6375bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6376bb7acecfSBarry Smith   not directly related to the `DM`.
6377f4d763aaSMatthew G. Knepley 
6378db781477SPatrick Sanan .seealso: `VecView()`
6379f4d763aaSMatthew G. Knepley @*/
6380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6381d71ae5a4SJacob Faibussowitsch {
6382f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6383f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63849371c9d4SSatish Balay   if (num) {
63859371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
63869371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63879371c9d4SSatish Balay   }
63889371c9d4SSatish Balay   if (val) {
63899371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
63909371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63919371c9d4SSatish Balay   }
63923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6393f4d763aaSMatthew G. Knepley }
6394f4d763aaSMatthew G. Knepley 
6395f4d763aaSMatthew G. Knepley /*@
6396cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6397f4d763aaSMatthew G. Knepley 
6398f4d763aaSMatthew G. Knepley   Input Parameters:
6399bb7acecfSBarry Smith + dm - The original `DM`
6400cdb7a50dSMatthew G. Knepley . num - The output sequence number
6401cdb7a50dSMatthew G. Knepley - val - The output sequence value
6402f4d763aaSMatthew G. Knepley 
6403f4d763aaSMatthew G. Knepley   Level: intermediate
6404f4d763aaSMatthew G. Knepley 
6405bb7acecfSBarry Smith   Note:
6406bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6407bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6408f4d763aaSMatthew G. Knepley 
6409db781477SPatrick Sanan .seealso: `VecView()`
6410f4d763aaSMatthew G. Knepley @*/
6411d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6412d71ae5a4SJacob Faibussowitsch {
6413f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6414f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6415f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6416cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
64173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6418cdb7a50dSMatthew G. Knepley }
6419cdb7a50dSMatthew G. Knepley 
6420cdb7a50dSMatthew G. Knepley /*@C
6421bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6422cdb7a50dSMatthew G. Knepley 
6423cdb7a50dSMatthew G. Knepley   Input Parameters:
6424bb7acecfSBarry Smith + dm   - The original `DM`
6425cdb7a50dSMatthew G. Knepley . name - The sequence name
6426cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6427cdb7a50dSMatthew G. Knepley 
6428cdb7a50dSMatthew G. Knepley   Output Parameter:
6429cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6430cdb7a50dSMatthew G. Knepley 
6431cdb7a50dSMatthew G. Knepley   Level: intermediate
6432cdb7a50dSMatthew G. Knepley 
6433bb7acecfSBarry Smith   Note:
6434bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6435bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6436bb7acecfSBarry Smith 
6437bb7acecfSBarry Smith   Developer Note:
6438bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6439cdb7a50dSMatthew G. Knepley 
6440db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6441cdb7a50dSMatthew G. Knepley @*/
6442d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6443d71ae5a4SJacob Faibussowitsch {
6444cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6445cdb7a50dSMatthew G. Knepley 
6446cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6447cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6448cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6449064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
64509566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6451cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6452cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6453cdb7a50dSMatthew G. Knepley     PetscScalar value;
6454cdb7a50dSMatthew G. Knepley 
64559566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
64564aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6457cdb7a50dSMatthew G. Knepley #endif
6458cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
64593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6460f4d763aaSMatthew G. Knepley }
64618e4ac7eaSMatthew G. Knepley 
64628e4ac7eaSMatthew G. Knepley /*@
6463bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64648e4ac7eaSMatthew G. Knepley 
64658e4ac7eaSMatthew G. Knepley   Not collective
64668e4ac7eaSMatthew G. Knepley 
64678e4ac7eaSMatthew G. Knepley   Input Parameter:
6468bb7acecfSBarry Smith . dm - The `DM`
64698e4ac7eaSMatthew G. Knepley 
64708e4ac7eaSMatthew G. Knepley   Output Parameter:
6471bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64728e4ac7eaSMatthew G. Knepley 
64738e4ac7eaSMatthew G. Knepley   Level: beginner
64748e4ac7eaSMatthew G. Knepley 
6475db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
64768e4ac7eaSMatthew G. Knepley @*/
6477d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6478d71ae5a4SJacob Faibussowitsch {
64798e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64808e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6481534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64828e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64848e4ac7eaSMatthew G. Knepley }
64858e4ac7eaSMatthew G. Knepley 
64868e4ac7eaSMatthew G. Knepley /*@
6487bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64888e4ac7eaSMatthew G. Knepley 
64898e4ac7eaSMatthew G. Knepley   Collective on dm
64908e4ac7eaSMatthew G. Knepley 
64918e4ac7eaSMatthew G. Knepley   Input Parameters:
6492bb7acecfSBarry Smith  + dm - The `DM`
6493bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64948e4ac7eaSMatthew G. Knepley 
6495bb7acecfSBarry Smith   Note:
6496bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64975d3b26e6SMatthew G. Knepley 
64988e4ac7eaSMatthew G. Knepley   Level: beginner
64998e4ac7eaSMatthew G. Knepley 
6500db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
65018e4ac7eaSMatthew G. Knepley @*/
6502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6503d71ae5a4SJacob Faibussowitsch {
65048e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65058e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65068833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
65078e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
65083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65098e4ac7eaSMatthew G. Knepley }
6510c58f1c22SToby Isaac 
6511c58f1c22SToby Isaac /*@C
6512bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6513c58f1c22SToby Isaac 
6514c58f1c22SToby Isaac   Not Collective
6515c58f1c22SToby Isaac 
6516c58f1c22SToby Isaac   Input Parameters:
6517bb7acecfSBarry Smith + dm   - The `DM` object
6518c58f1c22SToby Isaac - name - The label name
6519c58f1c22SToby Isaac 
6520c58f1c22SToby Isaac   Level: intermediate
6521c58f1c22SToby Isaac 
6522db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6523c58f1c22SToby Isaac @*/
6524d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6525d71ae5a4SJacob Faibussowitsch {
65265d80c0bfSVaclav Hapla   PetscBool flg;
65275d80c0bfSVaclav Hapla   DMLabel   label;
6528c58f1c22SToby Isaac 
6529c58f1c22SToby Isaac   PetscFunctionBegin;
6530c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6531c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65329566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6533c58f1c22SToby Isaac   if (!flg) {
65349566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65359566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65369566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6537c58f1c22SToby Isaac   }
65383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6539c58f1c22SToby Isaac }
6540c58f1c22SToby Isaac 
6541c58f1c22SToby Isaac /*@C
6542bb7acecfSBarry 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.
65430fdc7489SMatthew Knepley 
65440fdc7489SMatthew Knepley   Not Collective
65450fdc7489SMatthew Knepley 
65460fdc7489SMatthew Knepley   Input Parameters:
6547bb7acecfSBarry Smith + dm   - The `DM` object
65480fdc7489SMatthew Knepley . l    - The index for the label
65490fdc7489SMatthew Knepley - name - The label name
65500fdc7489SMatthew Knepley 
65510fdc7489SMatthew Knepley   Level: intermediate
65520fdc7489SMatthew Knepley 
6553db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
65540fdc7489SMatthew Knepley @*/
6555d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6556d71ae5a4SJacob Faibussowitsch {
65570fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
65580fdc7489SMatthew Knepley   DMLabel     label;
65590fdc7489SMatthew Knepley   PetscInt    Nl, m;
65600fdc7489SMatthew Knepley   PetscBool   flg, match;
65610fdc7489SMatthew Knepley   const char *lname;
65620fdc7489SMatthew Knepley 
65630fdc7489SMatthew Knepley   PetscFunctionBegin;
65640fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6565064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
65669566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
65670fdc7489SMatthew Knepley   if (!flg) {
65689566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65699566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65709566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65710fdc7489SMatthew Knepley   }
65729566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
657363a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65740fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65759566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65770fdc7489SMatthew Knepley     if (match) break;
65780fdc7489SMatthew Knepley   }
65793ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
65800fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65810fdc7489SMatthew Knepley   else prev->next = orig->next;
65820fdc7489SMatthew Knepley   if (!l) {
65830fdc7489SMatthew Knepley     orig->next = dm->labels;
65840fdc7489SMatthew Knepley     dm->labels = orig;
65850fdc7489SMatthew Knepley   } else {
65869371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65879371c9d4SSatish Balay       ;
65880fdc7489SMatthew Knepley     orig->next = prev->next;
65890fdc7489SMatthew Knepley     prev->next = orig;
65900fdc7489SMatthew Knepley   }
65913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65920fdc7489SMatthew Knepley }
65930fdc7489SMatthew Knepley 
65940fdc7489SMatthew Knepley /*@C
6595bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6596c58f1c22SToby Isaac 
6597c58f1c22SToby Isaac   Not Collective
6598c58f1c22SToby Isaac 
6599c58f1c22SToby Isaac   Input Parameters:
6600bb7acecfSBarry Smith + dm   - The `DM` object
6601c58f1c22SToby Isaac . name - The label name
6602c58f1c22SToby Isaac - point - The mesh point
6603c58f1c22SToby Isaac 
6604c58f1c22SToby Isaac   Output Parameter:
6605c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6606c58f1c22SToby Isaac 
6607c58f1c22SToby Isaac   Level: beginner
6608c58f1c22SToby Isaac 
6609db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6610c58f1c22SToby Isaac @*/
6611d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6612d71ae5a4SJacob Faibussowitsch {
6613c58f1c22SToby Isaac   DMLabel label;
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac   PetscFunctionBegin;
6616c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6617c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66197a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
66209566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
66213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6622c58f1c22SToby Isaac }
6623c58f1c22SToby Isaac 
6624c58f1c22SToby Isaac /*@C
6625bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6626c58f1c22SToby Isaac 
6627c58f1c22SToby Isaac   Not Collective
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   Input Parameters:
6630bb7acecfSBarry Smith + dm   - The `DM` object
6631c58f1c22SToby Isaac . name - The label name
6632c58f1c22SToby Isaac . point - The mesh point
6633c58f1c22SToby Isaac - value - The label value for this point
6634c58f1c22SToby Isaac 
6635c58f1c22SToby Isaac   Output Parameter:
6636c58f1c22SToby Isaac 
6637c58f1c22SToby Isaac   Level: beginner
6638c58f1c22SToby Isaac 
6639db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6640c58f1c22SToby Isaac @*/
6641d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6642d71ae5a4SJacob Faibussowitsch {
6643c58f1c22SToby Isaac   DMLabel label;
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac   PetscFunctionBegin;
6646c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6647c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66489566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6649c58f1c22SToby Isaac   if (!label) {
66509566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
66519566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6652c58f1c22SToby Isaac   }
66539566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
66543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6655c58f1c22SToby Isaac }
6656c58f1c22SToby Isaac 
6657c58f1c22SToby Isaac /*@C
6658bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6659c58f1c22SToby Isaac 
6660c58f1c22SToby Isaac   Not Collective
6661c58f1c22SToby Isaac 
6662c58f1c22SToby Isaac   Input Parameters:
6663bb7acecfSBarry Smith + dm   - The `DM` object
6664c58f1c22SToby Isaac . name - The label name
6665c58f1c22SToby Isaac . point - The mesh point
6666c58f1c22SToby Isaac - value - The label value for this point
6667c58f1c22SToby Isaac 
6668c58f1c22SToby Isaac   Level: beginner
6669c58f1c22SToby Isaac 
6670db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6671c58f1c22SToby Isaac @*/
6672d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6673d71ae5a4SJacob Faibussowitsch {
6674c58f1c22SToby Isaac   DMLabel label;
6675c58f1c22SToby Isaac 
6676c58f1c22SToby Isaac   PetscFunctionBegin;
6677c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6678c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66799566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66803ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
66819566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
66823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6683c58f1c22SToby Isaac }
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac /*@C
6686bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6687c58f1c22SToby Isaac 
6688c58f1c22SToby Isaac   Not Collective
6689c58f1c22SToby Isaac 
6690c58f1c22SToby Isaac   Input Parameters:
6691bb7acecfSBarry Smith + dm   - The `DM` object
6692c58f1c22SToby Isaac - name - The label name
6693c58f1c22SToby Isaac 
6694c58f1c22SToby Isaac   Output Parameter:
6695c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6696c58f1c22SToby Isaac 
6697c58f1c22SToby Isaac   Level: beginner
6698c58f1c22SToby Isaac 
6699bb7acecfSBarry Smith   Developer Note:
6700bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6701bb7acecfSBarry Smith 
6702bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6703c58f1c22SToby Isaac @*/
6704d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6705d71ae5a4SJacob Faibussowitsch {
6706c58f1c22SToby Isaac   DMLabel label;
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac   PetscFunctionBegin;
6709c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6710c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6711534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
67129566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6713c58f1c22SToby Isaac   *size = 0;
67143ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67159566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
67163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6717c58f1c22SToby Isaac }
6718c58f1c22SToby Isaac 
6719c58f1c22SToby Isaac /*@C
6720bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6721c58f1c22SToby Isaac 
6722c58f1c22SToby Isaac   Not Collective
6723c58f1c22SToby Isaac 
6724c58f1c22SToby Isaac   Input Parameters:
6725bb7acecfSBarry Smith + mesh - The `DM` object
6726c58f1c22SToby Isaac - name - The label name
6727c58f1c22SToby Isaac 
6728c58f1c22SToby Isaac   Output Parameter:
6729c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac   Level: beginner
6732c58f1c22SToby Isaac 
6733db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6734c58f1c22SToby Isaac @*/
6735d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6736d71ae5a4SJacob Faibussowitsch {
6737c58f1c22SToby Isaac   DMLabel label;
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac   PetscFunctionBegin;
6740c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6741c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6742c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
67439566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6744c58f1c22SToby Isaac   *ids = NULL;
6745dab2e251SBlaise Bourdin   if (label) {
67469566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6747dab2e251SBlaise Bourdin   } else {
6748dab2e251SBlaise Bourdin     /* returning an empty IS */
67499566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6750dab2e251SBlaise Bourdin   }
67513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6752c58f1c22SToby Isaac }
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac /*@C
6755c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6756c58f1c22SToby Isaac 
6757c58f1c22SToby Isaac   Not Collective
6758c58f1c22SToby Isaac 
6759c58f1c22SToby Isaac   Input Parameters:
6760bb7acecfSBarry Smith + dm - The `DM` object
6761c58f1c22SToby Isaac . name - The label name
6762c58f1c22SToby Isaac - value - The stratum value
6763c58f1c22SToby Isaac 
6764c58f1c22SToby Isaac   Output Parameter:
6765bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac   Level: beginner
6768c58f1c22SToby Isaac 
6769db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6770c58f1c22SToby Isaac @*/
6771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6772d71ae5a4SJacob Faibussowitsch {
6773c58f1c22SToby Isaac   DMLabel label;
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   PetscFunctionBegin;
6776c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6777c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6778534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67799566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6780c58f1c22SToby Isaac   *size = 0;
67813ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67829566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
67833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6784c58f1c22SToby Isaac }
6785c58f1c22SToby Isaac 
6786c58f1c22SToby Isaac /*@C
6787c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac   Not Collective
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac   Input Parameters:
6792bb7acecfSBarry Smith + dm - The `DM` object
6793c58f1c22SToby Isaac . name - The label name
6794c58f1c22SToby Isaac - value - The stratum value
6795c58f1c22SToby Isaac 
6796c58f1c22SToby Isaac   Output Parameter:
6797c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   Level: beginner
6800c58f1c22SToby Isaac 
6801db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6802c58f1c22SToby Isaac @*/
6803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6804d71ae5a4SJacob Faibussowitsch {
6805c58f1c22SToby Isaac   DMLabel label;
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac   PetscFunctionBegin;
6808c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6809c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6810c58f1c22SToby Isaac   PetscValidPointer(points, 4);
68119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6812c58f1c22SToby Isaac   *points = NULL;
68133ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
68153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6816c58f1c22SToby Isaac }
6817c58f1c22SToby Isaac 
68184de306b1SToby Isaac /*@C
68199044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68204de306b1SToby Isaac 
68214de306b1SToby Isaac   Not Collective
68224de306b1SToby Isaac 
68234de306b1SToby Isaac   Input Parameters:
6824bb7acecfSBarry Smith + dm - The `DM` object
68254de306b1SToby Isaac . name - The label name
68264de306b1SToby Isaac . value - The stratum value
68274de306b1SToby Isaac - points - The stratum points
68284de306b1SToby Isaac 
68294de306b1SToby Isaac   Level: beginner
68304de306b1SToby Isaac 
6831bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
68324de306b1SToby Isaac @*/
6833d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6834d71ae5a4SJacob Faibussowitsch {
68354de306b1SToby Isaac   DMLabel label;
68364de306b1SToby Isaac 
68374de306b1SToby Isaac   PetscFunctionBegin;
68384de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68394de306b1SToby Isaac   PetscValidCharPointer(name, 2);
68404de306b1SToby Isaac   PetscValidPointer(points, 4);
68419566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68423ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68439566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
68443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
68454de306b1SToby Isaac }
68464de306b1SToby Isaac 
6847c58f1c22SToby Isaac /*@C
6848bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6849c58f1c22SToby Isaac 
6850c58f1c22SToby Isaac   Not Collective
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   Input Parameters:
6853bb7acecfSBarry Smith + dm   - The `DM` object
6854c58f1c22SToby Isaac . name - The label name
6855c58f1c22SToby Isaac - value - The label value for this point
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac   Output Parameter:
6858c58f1c22SToby Isaac 
6859c58f1c22SToby Isaac   Level: beginner
6860c58f1c22SToby Isaac 
6861bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6862c58f1c22SToby Isaac @*/
6863d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6864d71ae5a4SJacob Faibussowitsch {
6865c58f1c22SToby Isaac   DMLabel label;
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac   PetscFunctionBegin;
6868c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6869c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68709566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68713ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68729566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
68733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6874c58f1c22SToby Isaac }
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac /*@
6877bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac   Not Collective
6880c58f1c22SToby Isaac 
6881c58f1c22SToby Isaac   Input Parameter:
6882bb7acecfSBarry Smith . dm   - The `DM` object
6883c58f1c22SToby Isaac 
6884c58f1c22SToby Isaac   Output Parameter:
6885c58f1c22SToby Isaac . numLabels - the number of Labels
6886c58f1c22SToby Isaac 
6887c58f1c22SToby Isaac   Level: intermediate
6888c58f1c22SToby Isaac 
6889bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6890c58f1c22SToby Isaac @*/
6891d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6892d71ae5a4SJacob Faibussowitsch {
68935d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6894c58f1c22SToby Isaac   PetscInt    n    = 0;
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac   PetscFunctionBegin;
6897c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6898534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
68999371c9d4SSatish Balay   while (next) {
69009371c9d4SSatish Balay     ++n;
69019371c9d4SSatish Balay     next = next->next;
69029371c9d4SSatish Balay   }
6903c58f1c22SToby Isaac   *numLabels = n;
69043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6905c58f1c22SToby Isaac }
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac /*@C
6908c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6909c58f1c22SToby Isaac 
6910c58f1c22SToby Isaac   Not Collective
6911c58f1c22SToby Isaac 
6912c58f1c22SToby Isaac   Input Parameters:
6913bb7acecfSBarry Smith + dm - The `DM` object
6914c58f1c22SToby Isaac - n  - the label number
6915c58f1c22SToby Isaac 
6916c58f1c22SToby Isaac   Output Parameter:
6917c58f1c22SToby Isaac . name - the label name
6918c58f1c22SToby Isaac 
6919c58f1c22SToby Isaac   Level: intermediate
6920c58f1c22SToby Isaac 
6921bb7acecfSBarry Smith   Developer Note:
6922bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6923bb7acecfSBarry Smith 
6924bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6925c58f1c22SToby Isaac @*/
6926d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6927d71ae5a4SJacob Faibussowitsch {
69285d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6929c58f1c22SToby Isaac   PetscInt    l    = 0;
6930c58f1c22SToby Isaac 
6931c58f1c22SToby Isaac   PetscFunctionBegin;
6932c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6933c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6934c58f1c22SToby Isaac   while (next) {
6935c58f1c22SToby Isaac     if (l == n) {
69369566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
69373ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
6938c58f1c22SToby Isaac     }
6939c58f1c22SToby Isaac     ++l;
6940c58f1c22SToby Isaac     next = next->next;
6941c58f1c22SToby Isaac   }
694263a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6943c58f1c22SToby Isaac }
6944c58f1c22SToby Isaac 
6945c58f1c22SToby Isaac /*@C
6946bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6947c58f1c22SToby Isaac 
6948c58f1c22SToby Isaac   Not Collective
6949c58f1c22SToby Isaac 
6950c58f1c22SToby Isaac   Input Parameters:
6951bb7acecfSBarry Smith + dm   - The `DM` object
6952c58f1c22SToby Isaac - name - The label name
6953c58f1c22SToby Isaac 
6954c58f1c22SToby Isaac   Output Parameter:
6955bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6956c58f1c22SToby Isaac 
6957c58f1c22SToby Isaac   Level: intermediate
6958c58f1c22SToby Isaac 
6959bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6960c58f1c22SToby Isaac @*/
6961d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6962d71ae5a4SJacob Faibussowitsch {
69635d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6964d67d17b1SMatthew G. Knepley   const char *lname;
6965c58f1c22SToby Isaac 
6966c58f1c22SToby Isaac   PetscFunctionBegin;
6967c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6968c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6969534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6970c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6971c58f1c22SToby Isaac   while (next) {
69729566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69739566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6974c58f1c22SToby Isaac     if (*hasLabel) break;
6975c58f1c22SToby Isaac     next = next->next;
6976c58f1c22SToby Isaac   }
69773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6978c58f1c22SToby Isaac }
6979c58f1c22SToby Isaac 
6980c58f1c22SToby Isaac /*@C
6981bb7acecfSBarry Smith   DMGetLabel - Return the label of a given name, or NULL, from a `DM`
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   Not Collective
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   Input Parameters:
6986bb7acecfSBarry Smith + dm   - The `DM` object
6987c58f1c22SToby Isaac - name - The label name
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   Output Parameter:
6990bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent
6991c58f1c22SToby Isaac 
6992bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6993bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6994bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6995bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6996bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6997bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6998bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69996d7c9049SMatthew G. Knepley 
7000c58f1c22SToby Isaac   Level: intermediate
7001c58f1c22SToby Isaac 
7002bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7003c58f1c22SToby Isaac @*/
7004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7005d71ae5a4SJacob Faibussowitsch {
70065d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7007c58f1c22SToby Isaac   PetscBool   hasLabel;
7008d67d17b1SMatthew G. Knepley   const char *lname;
7009c58f1c22SToby Isaac 
7010c58f1c22SToby Isaac   PetscFunctionBegin;
7011c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7012c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7013c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7014c58f1c22SToby Isaac   *label = NULL;
7015c58f1c22SToby Isaac   while (next) {
70169566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70179566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7018c58f1c22SToby Isaac     if (hasLabel) {
7019c58f1c22SToby Isaac       *label = next->label;
7020c58f1c22SToby Isaac       break;
7021c58f1c22SToby Isaac     }
7022c58f1c22SToby Isaac     next = next->next;
7023c58f1c22SToby Isaac   }
70243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7025c58f1c22SToby Isaac }
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac /*@C
7028bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7029c58f1c22SToby Isaac 
7030c58f1c22SToby Isaac   Not Collective
7031c58f1c22SToby Isaac 
7032c58f1c22SToby Isaac   Input Parameters:
7033bb7acecfSBarry Smith + dm - The `DM` object
7034c58f1c22SToby Isaac - n  - the label number
7035c58f1c22SToby Isaac 
7036c58f1c22SToby Isaac   Output Parameter:
7037c58f1c22SToby Isaac . label - the label
7038c58f1c22SToby Isaac 
7039c58f1c22SToby Isaac   Level: intermediate
7040c58f1c22SToby Isaac 
7041bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7042c58f1c22SToby Isaac @*/
7043d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7044d71ae5a4SJacob Faibussowitsch {
70455d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7046c58f1c22SToby Isaac   PetscInt    l    = 0;
7047c58f1c22SToby Isaac 
7048c58f1c22SToby Isaac   PetscFunctionBegin;
7049c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7050c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7051c58f1c22SToby Isaac   while (next) {
7052c58f1c22SToby Isaac     if (l == n) {
7053c58f1c22SToby Isaac       *label = next->label;
70543ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7055c58f1c22SToby Isaac     }
7056c58f1c22SToby Isaac     ++l;
7057c58f1c22SToby Isaac     next = next->next;
7058c58f1c22SToby Isaac   }
705963a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7060c58f1c22SToby Isaac }
7061c58f1c22SToby Isaac 
7062c58f1c22SToby Isaac /*@C
7063bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Not Collective
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac   Input Parameters:
7068bb7acecfSBarry Smith + dm   - The `DM` object
7069bb7acecfSBarry Smith - label - The `DMLabel`
7070c58f1c22SToby Isaac 
7071c58f1c22SToby Isaac   Level: developer
7072c58f1c22SToby Isaac 
7073bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7074c58f1c22SToby Isaac @*/
7075d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7076d71ae5a4SJacob Faibussowitsch {
70775d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7078c58f1c22SToby Isaac   PetscBool   hasLabel;
7079d67d17b1SMatthew G. Knepley   const char *lname;
70805d80c0bfSVaclav Hapla   PetscBool   flg;
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   PetscFunctionBegin;
7083c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70849566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70859566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70867a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70879566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7088c58f1c22SToby Isaac   tmpLabel->label  = label;
7089c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70905d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70915d80c0bfSVaclav Hapla   *p = tmpLabel;
70929566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70939566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70945d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70959566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7096ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
70973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7098c58f1c22SToby Isaac }
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac /*@C
71014a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
71024a7ee7d0SMatthew G. Knepley 
71034a7ee7d0SMatthew G. Knepley   Not Collective
71044a7ee7d0SMatthew G. Knepley 
71054a7ee7d0SMatthew G. Knepley   Input Parameters:
7106bb7acecfSBarry Smith + dm    - The `DM` object
7107bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
71084a7ee7d0SMatthew G. Knepley 
7109bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7110bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7111bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7112bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7113bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7114bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7115bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71164a7ee7d0SMatthew G. Knepley 
71174a7ee7d0SMatthew G. Knepley   Level: intermediate
71184a7ee7d0SMatthew G. Knepley 
7119bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
71204a7ee7d0SMatthew G. Knepley @*/
7121d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7122d71ae5a4SJacob Faibussowitsch {
71234a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
71244a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
71254a7ee7d0SMatthew G. Knepley   const char *name, *lname;
71264a7ee7d0SMatthew G. Knepley 
71274a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
71284a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71294a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
71309566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
71314a7ee7d0SMatthew G. Knepley   while (next) {
71329566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71339566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
71344a7ee7d0SMatthew G. Knepley     if (hasLabel) {
71359566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
71369566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
71374a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
71389566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
71394a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
71409566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
71414a7ee7d0SMatthew G. Knepley       next->label = label;
71424a7ee7d0SMatthew G. Knepley       break;
71434a7ee7d0SMatthew G. Knepley     }
71444a7ee7d0SMatthew G. Knepley     next = next->next;
71454a7ee7d0SMatthew G. Knepley   }
71463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
71474a7ee7d0SMatthew G. Knepley }
71484a7ee7d0SMatthew G. Knepley 
71494a7ee7d0SMatthew G. Knepley /*@C
7150bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7151c58f1c22SToby Isaac 
7152c58f1c22SToby Isaac   Not Collective
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Input Parameters:
7155bb7acecfSBarry Smith + dm   - The `DM` object
7156c58f1c22SToby Isaac - name - The label name
7157c58f1c22SToby Isaac 
7158c58f1c22SToby Isaac   Output Parameter:
7159bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent. Pass in NULL to call `DMLabelDestroy()` on the label, otherwise the
7160bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7161c58f1c22SToby Isaac 
7162c58f1c22SToby Isaac   Level: developer
7163c58f1c22SToby Isaac 
7164bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7165c58f1c22SToby Isaac @*/
7166d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7167d71ae5a4SJacob Faibussowitsch {
716895d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7169c58f1c22SToby Isaac   PetscBool   hasLabel;
7170d67d17b1SMatthew G. Knepley   const char *lname;
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac   PetscFunctionBegin;
7173c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7174e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7175e5472504SVaclav Hapla   if (label) {
7176e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7177c58f1c22SToby Isaac     *label = NULL;
7178e5472504SVaclav Hapla   }
71795d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71809566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71819566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7182c58f1c22SToby Isaac     if (hasLabel) {
718395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71849566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
718595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71869566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7187ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
718895d578d6SVaclav Hapla       if (label) *label = link->label;
71899566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71909566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7191c58f1c22SToby Isaac       break;
7192c58f1c22SToby Isaac     }
7193c58f1c22SToby Isaac   }
71943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7195c58f1c22SToby Isaac }
7196c58f1c22SToby Isaac 
7197306894acSVaclav Hapla /*@
7198bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7199306894acSVaclav Hapla 
7200306894acSVaclav Hapla   Not Collective
7201306894acSVaclav Hapla 
7202306894acSVaclav Hapla   Input Parameters:
7203bb7acecfSBarry Smith + dm   - The `DM` object
7204bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
7205306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7206306894acSVaclav Hapla 
7207306894acSVaclav Hapla   Level: developer
7208306894acSVaclav Hapla 
7209bb7acecfSBarry Smith   Note:
7210306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7211bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7212306894acSVaclav Hapla   *label nullified.
7213306894acSVaclav Hapla 
7214bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7215306894acSVaclav Hapla @*/
7216d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7217d71ae5a4SJacob Faibussowitsch {
721843e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7219306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7220306894acSVaclav Hapla 
7221306894acSVaclav Hapla   PetscFunctionBegin;
7222306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7223306894acSVaclav Hapla   PetscValidPointer(label, 2);
72243ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7225306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7226306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
72275d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
722843e45a93SVaclav Hapla     if (*label == link->label) {
7229306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
723043e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7231306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7232ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
723343e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
72349566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
72359566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7236306894acSVaclav Hapla       break;
7237306894acSVaclav Hapla     }
7238306894acSVaclav Hapla   }
72397a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
72403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7241306894acSVaclav Hapla }
7242306894acSVaclav Hapla 
7243c58f1c22SToby Isaac /*@C
7244c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7245c58f1c22SToby Isaac 
7246c58f1c22SToby Isaac   Not Collective
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Input Parameters:
7249bb7acecfSBarry Smith + dm   - The `DM` object
7250c58f1c22SToby Isaac - name - The label name
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac   Output Parameter:
7253c58f1c22SToby Isaac . output - The flag for output
7254c58f1c22SToby Isaac 
7255c58f1c22SToby Isaac   Level: developer
7256c58f1c22SToby Isaac 
7257bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7258c58f1c22SToby Isaac @*/
7259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7260d71ae5a4SJacob Faibussowitsch {
72615d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7262d67d17b1SMatthew G. Knepley   const char *lname;
7263c58f1c22SToby Isaac 
7264c58f1c22SToby Isaac   PetscFunctionBegin;
7265c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7266dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7267dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7268c58f1c22SToby Isaac   while (next) {
7269c58f1c22SToby Isaac     PetscBool flg;
7270c58f1c22SToby Isaac 
72719566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72729566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72739371c9d4SSatish Balay     if (flg) {
72749371c9d4SSatish Balay       *output = next->output;
72753ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
72769371c9d4SSatish Balay     }
7277c58f1c22SToby Isaac     next = next->next;
7278c58f1c22SToby Isaac   }
727998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7280c58f1c22SToby Isaac }
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac /*@C
7283bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac   Not Collective
7286c58f1c22SToby Isaac 
7287c58f1c22SToby Isaac   Input Parameters:
7288bb7acecfSBarry Smith + dm     - The `DM` object
7289c58f1c22SToby Isaac . name   - The label name
7290bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7291c58f1c22SToby Isaac 
7292c58f1c22SToby Isaac   Level: developer
7293c58f1c22SToby Isaac 
7294bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7295c58f1c22SToby Isaac @*/
7296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7297d71ae5a4SJacob Faibussowitsch {
72985d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7299d67d17b1SMatthew G. Knepley   const char *lname;
7300c58f1c22SToby Isaac 
7301c58f1c22SToby Isaac   PetscFunctionBegin;
7302c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7303534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7304c58f1c22SToby Isaac   while (next) {
7305c58f1c22SToby Isaac     PetscBool flg;
7306c58f1c22SToby Isaac 
73079566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73089566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73099371c9d4SSatish Balay     if (flg) {
73109371c9d4SSatish Balay       next->output = output;
73113ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73129371c9d4SSatish Balay     }
7313c58f1c22SToby Isaac     next = next->next;
7314c58f1c22SToby Isaac   }
731598921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7316c58f1c22SToby Isaac }
7317c58f1c22SToby Isaac 
7318c58f1c22SToby Isaac /*@
7319bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7320c58f1c22SToby Isaac 
7321d083f849SBarry Smith   Collective on dmA
7322c58f1c22SToby Isaac 
7323d8d19677SJose E. Roman   Input Parameters:
7324bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7325bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7326bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7327bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7328bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7329c58f1c22SToby Isaac 
7330c58f1c22SToby Isaac   Level: intermediate
7331c58f1c22SToby Isaac 
7332bb7acecfSBarry Smith   Note:
73332cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7334c58f1c22SToby Isaac 
7335bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7336c58f1c22SToby Isaac @*/
7337d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7338d71ae5a4SJacob Faibussowitsch {
73392cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7340c58f1c22SToby Isaac   const char *name;
7341c58f1c22SToby Isaac   PetscBool   flg;
73425d80c0bfSVaclav Hapla   DMLabelLink link;
7343c58f1c22SToby Isaac 
73445d80c0bfSVaclav Hapla   PetscFunctionBegin;
73455d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73465d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73475d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
73485d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73497a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73503ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
73515d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
73525d80c0bfSVaclav Hapla     label = link->label;
73539566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
73545d80c0bfSVaclav Hapla     if (!all) {
73559566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7356c58f1c22SToby Isaac       if (flg) continue;
73579566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
73587d5acc75SStefano Zampini       if (flg) continue;
73599566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7360ba2698f1SMatthew G. Knepley       if (flg) continue;
73615d80c0bfSVaclav Hapla     }
73629566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
73632cbb9b06SVaclav Hapla     if (labelOld) {
73642cbb9b06SVaclav Hapla       switch (emode) {
7365d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7366d71ae5a4SJacob Faibussowitsch         continue;
7367d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7368d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7369d71ae5a4SJacob Faibussowitsch         break;
7370d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7371d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7372d71ae5a4SJacob Faibussowitsch       default:
7373d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73742cbb9b06SVaclav Hapla       }
73752cbb9b06SVaclav Hapla     }
73765d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73779566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73785d80c0bfSVaclav Hapla     } else {
73795d80c0bfSVaclav Hapla       labelNew = label;
73805d80c0bfSVaclav Hapla     }
73819566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73829566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7383c58f1c22SToby Isaac   }
73843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7385c58f1c22SToby Isaac }
7386461a15a0SLisandro Dalcin 
7387609dae6eSVaclav Hapla /*@C
7388bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7389609dae6eSVaclav Hapla 
73905efe38ccSVaclav Hapla   Collective
7391609dae6eSVaclav Hapla 
7392609dae6eSVaclav Hapla   Input Parameters:
7393bb7acecfSBarry Smith + dm0 - First `DM` object
7394bb7acecfSBarry Smith - dm1 - Second `DM` object
7395609dae6eSVaclav Hapla 
7396609dae6eSVaclav Hapla   Output Parameters
73975efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7398609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7399609dae6eSVaclav Hapla 
7400609dae6eSVaclav Hapla   Level: intermediate
7401609dae6eSVaclav Hapla 
7402609dae6eSVaclav Hapla   Notes:
7403bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7404bb7acecfSBarry Smith 
7405bb7acecfSBarry Smith   If equal is passed as NULL and difference is found, an error is thrown on all processes.
7406bb7acecfSBarry Smith 
7407bb7acecfSBarry Smith   Make sure to pass equal is NULL on all processes or none of them.
7408609dae6eSVaclav Hapla 
74095efe38ccSVaclav Hapla   The output message is set independently on each rank.
7410bb7acecfSBarry Smith 
7411bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7412bb7acecfSBarry Smith 
7413bb7acecfSBarry Smith   If message is passed as NULL and a difference is found, the difference description is printed to stderr in synchronized manner.
7414bb7acecfSBarry Smith 
7415bb7acecfSBarry Smith   Make sure to pass message as NULL on all processes or no processes.
7416609dae6eSVaclav Hapla 
7417609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7418bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7419609dae6eSVaclav Hapla 
7420bb7acecfSBarry Smith   Fortran Note:
7421bb7acecfSBarry Smith   This function is not available from Fortran.
7422609dae6eSVaclav Hapla 
7423bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7424609dae6eSVaclav Hapla @*/
7425d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7426d71ae5a4SJacob Faibussowitsch {
74275efe38ccSVaclav Hapla   PetscInt    n, i;
7428609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
74295efe38ccSVaclav Hapla   PetscBool   eq;
7430609dae6eSVaclav Hapla   MPI_Comm    comm;
74315efe38ccSVaclav Hapla   PetscMPIInt rank;
7432609dae6eSVaclav Hapla 
7433609dae6eSVaclav Hapla   PetscFunctionBegin;
7434609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7435609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7436609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
74375efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7438609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
74399566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
74409566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
74415efe38ccSVaclav Hapla   {
74425efe38ccSVaclav Hapla     PetscInt n1;
74435efe38ccSVaclav Hapla 
74449566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
74459566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
74465efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
744748a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
74489566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
74495efe38ccSVaclav Hapla     if (!eq) goto finish;
74505efe38ccSVaclav Hapla   }
74515efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7452609dae6eSVaclav Hapla     DMLabel     l0, l1;
7453609dae6eSVaclav Hapla     const char *name;
7454609dae6eSVaclav Hapla     char       *msgInner;
7455609dae6eSVaclav Hapla 
7456609dae6eSVaclav Hapla     /* Ignore label order */
74579566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
74589566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
74599566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7460609dae6eSVaclav Hapla     if (!l1) {
746163a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
74625efe38ccSVaclav Hapla       eq = PETSC_FALSE;
74635efe38ccSVaclav Hapla       break;
7464609dae6eSVaclav Hapla     }
74659566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
74669566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
74679566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74685efe38ccSVaclav Hapla     if (!eq) break;
7469609dae6eSVaclav Hapla   }
74709566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7471609dae6eSVaclav Hapla finish:
74725efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7473609dae6eSVaclav Hapla   if (message) {
7474609dae6eSVaclav Hapla     *message = NULL;
747548a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74765efe38ccSVaclav Hapla   } else {
747748a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74789566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74795efe38ccSVaclav Hapla   }
74805efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74815efe38ccSVaclav Hapla   if (equal) *equal = eq;
74827a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
74833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7484609dae6eSVaclav Hapla }
7485609dae6eSVaclav Hapla 
7486d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7487d71ae5a4SJacob Faibussowitsch {
7488461a15a0SLisandro Dalcin   PetscFunctionBegin;
7489461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7490461a15a0SLisandro Dalcin   if (!*label) {
74919566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74929566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7493461a15a0SLisandro Dalcin   }
74949566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
74953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7496461a15a0SLisandro Dalcin }
7497461a15a0SLisandro Dalcin 
74980fdc7489SMatthew Knepley /*
74990fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
75000fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
75010fdc7489SMatthew Knepley   (label, id) pair in the DM.
75020fdc7489SMatthew Knepley 
75030fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
75040fdc7489SMatthew Knepley   each label.
75050fdc7489SMatthew Knepley */
7506d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7507d71ae5a4SJacob Faibussowitsch {
75080fdc7489SMatthew Knepley   DMUniversalLabel ul;
75090fdc7489SMatthew Knepley   PetscBool       *active;
75100fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
75110fdc7489SMatthew Knepley 
75120fdc7489SMatthew Knepley   PetscFunctionBegin;
75139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
75149566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
75159566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
75169566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
75170fdc7489SMatthew Knepley   ul->Nl = 0;
75180fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75190fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
75200fdc7489SMatthew Knepley     const char *name;
75210fdc7489SMatthew Knepley 
75229566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75239566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
75249566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
75250fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
75260fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
75270fdc7489SMatthew Knepley   }
75289566063dSJacob 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));
75290fdc7489SMatthew Knepley   ul->Nv = 0;
75300fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75310fdc7489SMatthew Knepley     DMLabel     label;
75320fdc7489SMatthew Knepley     PetscInt    nv;
75330fdc7489SMatthew Knepley     const char *name;
75340fdc7489SMatthew Knepley 
75350fdc7489SMatthew Knepley     if (!active[l]) continue;
75369566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75379566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75389566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75399566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
75400fdc7489SMatthew Knepley     ul->indices[m] = l;
75410fdc7489SMatthew Knepley     ul->Nv += nv;
75420fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
75430fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
75440fdc7489SMatthew Knepley     ++m;
75450fdc7489SMatthew Knepley   }
75460fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
75470fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
75480fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
75490fdc7489SMatthew Knepley   }
75500fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
75510fdc7489SMatthew Knepley     PetscInt b;
75520fdc7489SMatthew Knepley 
75530fdc7489SMatthew Knepley     ul->masks[l] = 0;
75540fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
75550fdc7489SMatthew Knepley   }
75569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
75570fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75580fdc7489SMatthew Knepley     DMLabel         label;
75590fdc7489SMatthew Knepley     IS              valueIS;
75600fdc7489SMatthew Knepley     const PetscInt *varr;
75610fdc7489SMatthew Knepley     PetscInt        nv, v;
75620fdc7489SMatthew Knepley 
75630fdc7489SMatthew Knepley     if (!active[l]) continue;
75649566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75659566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75669566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
75679566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7568ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75699566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75709566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75719566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75720fdc7489SMatthew Knepley     ++m;
75730fdc7489SMatthew Knepley   }
75749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75750fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75760fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75770fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75780fdc7489SMatthew Knepley 
75790fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75800fdc7489SMatthew Knepley       DMLabel  label;
75810649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75820fdc7489SMatthew Knepley 
75830fdc7489SMatthew Knepley       if (!active[l]) continue;
75849566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75859566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75869566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75879371c9d4SSatish Balay       if (val == defval) {
75889371c9d4SSatish Balay         ++m;
75899371c9d4SSatish Balay         continue;
75909371c9d4SSatish Balay       }
75910649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75920fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75939566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
759463a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75950fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75960fdc7489SMatthew Knepley       ++m;
75970fdc7489SMatthew Knepley     }
75989566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75990fdc7489SMatthew Knepley   }
76009566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
76010fdc7489SMatthew Knepley   *universal = ul;
76023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76030fdc7489SMatthew Knepley }
76040fdc7489SMatthew Knepley 
7605d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7606d71ae5a4SJacob Faibussowitsch {
76070fdc7489SMatthew Knepley   PetscInt l;
76080fdc7489SMatthew Knepley 
76090fdc7489SMatthew Knepley   PetscFunctionBegin;
76109566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
76119566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
76129566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
76139566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
76149566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
76150fdc7489SMatthew Knepley   *universal = NULL;
76163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76170fdc7489SMatthew Knepley }
76180fdc7489SMatthew Knepley 
7619d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7620d71ae5a4SJacob Faibussowitsch {
76210fdc7489SMatthew Knepley   PetscFunctionBegin;
76220fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
76230fdc7489SMatthew Knepley   *ulabel = ul->label;
76243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76250fdc7489SMatthew Knepley }
76260fdc7489SMatthew Knepley 
7627d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7628d71ae5a4SJacob Faibussowitsch {
76290fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
76300fdc7489SMatthew Knepley 
76310fdc7489SMatthew Knepley   PetscFunctionBegin;
7632064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
76330fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76349566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
76359566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
76360fdc7489SMatthew Knepley   }
76370fdc7489SMatthew Knepley   if (preserveOrder) {
76380fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
76390fdc7489SMatthew Knepley       const char *name;
76400fdc7489SMatthew Knepley       PetscBool   match;
76410fdc7489SMatthew Knepley 
76429566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
76439566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
764463a3b9bcSJacob 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]);
76450fdc7489SMatthew Knepley     }
76460fdc7489SMatthew Knepley   }
76473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76480fdc7489SMatthew Knepley }
76490fdc7489SMatthew Knepley 
7650d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7651d71ae5a4SJacob Faibussowitsch {
76520fdc7489SMatthew Knepley   PetscInt l;
76530fdc7489SMatthew Knepley 
76540fdc7489SMatthew Knepley   PetscFunctionBegin;
76550fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76560fdc7489SMatthew Knepley     DMLabel  label;
76570fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
76580fdc7489SMatthew Knepley 
76590fdc7489SMatthew Knepley     if (lval) {
76609566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
76619566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
76629566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
76630fdc7489SMatthew Knepley     }
76640fdc7489SMatthew Knepley   }
76653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76660fdc7489SMatthew Knepley }
7667a8fb8f29SToby Isaac 
7668a8fb8f29SToby Isaac /*@
7669bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7670bb7acecfSBarry Smith 
7671bb7acecfSBarry Smith   Not collective
7672a8fb8f29SToby Isaac 
7673a8fb8f29SToby Isaac   Input Parameter:
7674bb7acecfSBarry Smith . dm - The `DM` object
7675a8fb8f29SToby Isaac 
7676a8fb8f29SToby Isaac   Output Parameter:
7677bb7acecfSBarry Smith . cdm - The coarse `DM`
7678a8fb8f29SToby Isaac 
7679a8fb8f29SToby Isaac   Level: intermediate
7680a8fb8f29SToby Isaac 
7681bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7682a8fb8f29SToby Isaac @*/
7683d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7684d71ae5a4SJacob Faibussowitsch {
7685a8fb8f29SToby Isaac   PetscFunctionBegin;
7686a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7687a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7688a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
76893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7690a8fb8f29SToby Isaac }
7691a8fb8f29SToby Isaac 
7692a8fb8f29SToby Isaac /*@
7693bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7694a8fb8f29SToby Isaac 
7695a8fb8f29SToby Isaac   Input Parameters:
7696bb7acecfSBarry Smith + dm - The `DM` object
7697bb7acecfSBarry Smith - cdm - The coarse `DM`
7698a8fb8f29SToby Isaac 
7699a8fb8f29SToby Isaac   Level: intermediate
7700a8fb8f29SToby Isaac 
7701bb7acecfSBarry Smith   Note:
7702bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7703bb7acecfSBarry Smith 
7704bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7705a8fb8f29SToby Isaac @*/
7706d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7707d71ae5a4SJacob Faibussowitsch {
7708a8fb8f29SToby Isaac   PetscFunctionBegin;
7709a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7710a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
771189d734beSBarry Smith   if (dm == cdm) cdm = NULL;
77129566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
77139566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7714a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
77153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7716a8fb8f29SToby Isaac }
7717a8fb8f29SToby Isaac 
771888bdff64SToby Isaac /*@
7719bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
772088bdff64SToby Isaac 
772188bdff64SToby Isaac   Input Parameter:
7722bb7acecfSBarry Smith . dm - The `DM` object
772388bdff64SToby Isaac 
772488bdff64SToby Isaac   Output Parameter:
7725bb7acecfSBarry Smith . fdm - The fine `DM`
772688bdff64SToby Isaac 
772788bdff64SToby Isaac   Level: intermediate
772888bdff64SToby Isaac 
7729bb7acecfSBarry Smith .seealso: `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
773088bdff64SToby Isaac @*/
7731d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7732d71ae5a4SJacob Faibussowitsch {
773388bdff64SToby Isaac   PetscFunctionBegin;
773488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
773588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
773688bdff64SToby Isaac   *fdm = dm->fineMesh;
77373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
773888bdff64SToby Isaac }
773988bdff64SToby Isaac 
774088bdff64SToby Isaac /*@
7741bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
774288bdff64SToby Isaac 
774388bdff64SToby Isaac   Input Parameters:
7744bb7acecfSBarry Smith + dm - The `DM` object
7745bb7acecfSBarry Smith - fdm - The fine `DM`
774688bdff64SToby Isaac 
7747bb7acecfSBarry Smith   Level: developer
774888bdff64SToby Isaac 
7749bb7acecfSBarry Smith   Note:
7750bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7751bb7acecfSBarry Smith 
7752bb7acecfSBarry Smith .seealso: `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
775388bdff64SToby Isaac @*/
7754d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7755d71ae5a4SJacob Faibussowitsch {
775688bdff64SToby Isaac   PetscFunctionBegin;
775788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
775888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
775989d734beSBarry Smith   if (dm == fdm) fdm = NULL;
77609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
77619566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
776288bdff64SToby Isaac   dm->fineMesh = fdm;
77633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
776488bdff64SToby Isaac }
776588bdff64SToby Isaac 
7766a6ba4734SToby Isaac /*@C
7767bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7768a6ba4734SToby Isaac 
7769783e2ec8SMatthew G. Knepley   Collective on dm
7770783e2ec8SMatthew G. Knepley 
7771a6ba4734SToby Isaac   Input Parameters:
7772bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7773bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7774a6ba4734SToby Isaac . name     - The BC name
777545480ffeSMatthew G. Knepley . label    - The label defining constrained points
7776bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
777745480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7778a6ba4734SToby Isaac . field    - The field to constrain
777945480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7780a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7781a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
778256cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7783a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7784a6ba4734SToby Isaac 
778545480ffeSMatthew G. Knepley   Output Parameter:
778645480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
778745480ffeSMatthew G. Knepley 
7788a6ba4734SToby Isaac   Options Database Keys:
7789a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7790a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7791a6ba4734SToby Isaac 
7792bb7acecfSBarry Smith   Notes:
7793bb7acecfSBarry 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:
779456cf3b9cSMatthew G. Knepley 
779556cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
779656cf3b9cSMatthew G. Knepley 
7797bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
779856cf3b9cSMatthew G. Knepley 
779956cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
780056cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
780156cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
780256cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
780356cf3b9cSMatthew G. Knepley 
780456cf3b9cSMatthew G. Knepley + dim - the spatial dimension
780556cf3b9cSMatthew G. Knepley . Nf - the number of fields
780656cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
780756cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
780856cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
780956cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
781056cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
781156cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
781256cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
781356cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
781456cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
781556cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
781656cf3b9cSMatthew G. Knepley . t - current time
781756cf3b9cSMatthew G. Knepley . x - coordinates of the current point
781856cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
781956cf3b9cSMatthew G. Knepley . constants - constant parameters
782056cf3b9cSMatthew G. Knepley - bcval - output values at the current point
782156cf3b9cSMatthew G. Knepley 
7822ed808b8fSJed Brown   Level: intermediate
7823a6ba4734SToby Isaac 
7824db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7825a6ba4734SToby Isaac @*/
7826d71ae5a4SJacob 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)
7827d71ae5a4SJacob Faibussowitsch {
7828e5e52638SMatthew G. Knepley   PetscDS ds;
7829a6ba4734SToby Isaac 
7830a6ba4734SToby Isaac   PetscFunctionBegin;
7831a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7832783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
783345480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
783445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
783545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
783645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
783701a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
78389566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7839799db056SMatthew G. Knepley   /* Complete label */
7840799db056SMatthew G. Knepley   if (label) {
7841799db056SMatthew G. Knepley     PetscObject  obj;
7842799db056SMatthew G. Knepley     PetscClassId id;
7843799db056SMatthew G. Knepley 
7844799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7845799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7846799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7847799db056SMatthew G. Knepley       DM plex;
7848799db056SMatthew G. Knepley 
7849799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7850799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7851799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7852799db056SMatthew G. Knepley     }
7853799db056SMatthew G. Knepley   }
78549566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
78553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7856a6ba4734SToby Isaac }
7857a6ba4734SToby Isaac 
785845480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7859d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7860d71ae5a4SJacob Faibussowitsch {
7861e5e52638SMatthew G. Knepley   PetscDS     ds;
7862dff059c6SToby Isaac   DMBoundary *lastnext;
7863e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7864e6f8dbb6SToby Isaac 
7865e6f8dbb6SToby Isaac   PetscFunctionBegin;
78669566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7867e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
786847a1f5adSToby Isaac   if (dm->boundary) {
786947a1f5adSToby Isaac     DMBoundary next = dm->boundary;
787047a1f5adSToby Isaac 
787147a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
78723ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
787347a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
787447a1f5adSToby Isaac     while (next) {
787547a1f5adSToby Isaac       DMBoundary b = next;
787647a1f5adSToby Isaac 
787747a1f5adSToby Isaac       next = b->next;
78789566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7879a6ba4734SToby Isaac     }
788047a1f5adSToby Isaac     dm->boundary = NULL;
7881a6ba4734SToby Isaac   }
788247a1f5adSToby Isaac 
7883dff059c6SToby Isaac   lastnext = &(dm->boundary);
7884e6f8dbb6SToby Isaac   while (dsbound) {
7885e6f8dbb6SToby Isaac     DMBoundary dmbound;
7886e6f8dbb6SToby Isaac 
78879566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7888e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
788945480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
789047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7891dff059c6SToby Isaac     *lastnext = dmbound;
7892dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7893dff059c6SToby Isaac     dsbound   = dsbound->next;
7894a6ba4734SToby Isaac   }
78953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7896a6ba4734SToby Isaac }
7897a6ba4734SToby Isaac 
7898bb7acecfSBarry Smith /* TODO: missing manual page */
7899d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7900d71ae5a4SJacob Faibussowitsch {
7901b95f2879SToby Isaac   DMBoundary b;
7902a6ba4734SToby Isaac 
7903a6ba4734SToby Isaac   PetscFunctionBegin;
7904a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7905534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7906a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
79079566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7908b95f2879SToby Isaac   b = dm->boundary;
7909a6ba4734SToby Isaac   while (b && !(*isBd)) {
7910e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7911e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7912a6ba4734SToby Isaac     PetscInt   i;
7913a6ba4734SToby Isaac 
791445480ffeSMatthew G. Knepley     if (label) {
79159566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7916a6ba4734SToby Isaac     }
7917a6ba4734SToby Isaac     b = b->next;
7918a6ba4734SToby Isaac   }
79193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7920a6ba4734SToby Isaac }
79214d6f44ffSToby Isaac 
79224d6f44ffSToby Isaac /*@C
7923bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7924a6e0b375SMatthew G. Knepley 
7925bb7acecfSBarry Smith   Collective on dm
79264d6f44ffSToby Isaac 
79274d6f44ffSToby Isaac   Input Parameters:
7928bb7acecfSBarry Smith + dm      - The `DM`
79290709b2feSToby Isaac . time    - The time
79304d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
79314d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79324d6f44ffSToby Isaac - mode    - The insertion mode for values
79334d6f44ffSToby Isaac 
79344d6f44ffSToby Isaac   Output Parameter:
79354d6f44ffSToby Isaac . X - vector
79364d6f44ffSToby Isaac 
79374d6f44ffSToby Isaac    Calling sequence of func:
793877b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
79394d6f44ffSToby Isaac 
79404d6f44ffSToby Isaac +  dim - The spatial dimension
79418ec8862eSJed Brown .  time - The time at which to sample
79424d6f44ffSToby Isaac .  x   - The coordinates
794377b739a6SMatthew Knepley .  Nc  - The number of components
79444d6f44ffSToby Isaac .  u   - The output field values
79454d6f44ffSToby Isaac -  ctx - optional user-defined function context
79464d6f44ffSToby Isaac 
79474d6f44ffSToby Isaac   Level: developer
79484d6f44ffSToby Isaac 
7949bb7acecfSBarry Smith   Developer Notes:
7950bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7951bb7acecfSBarry Smith 
7952bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7953bb7acecfSBarry Smith 
7954db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
79554d6f44ffSToby Isaac @*/
7956d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7957d71ae5a4SJacob Faibussowitsch {
79584d6f44ffSToby Isaac   Vec localX;
79594d6f44ffSToby Isaac 
79604d6f44ffSToby Isaac   PetscFunctionBegin;
79614d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79629566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7963f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79649566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
79659566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79669566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79679566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
79683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79694d6f44ffSToby Isaac }
79704d6f44ffSToby Isaac 
7971a6e0b375SMatthew G. Knepley /*@C
7972bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7973a6e0b375SMatthew G. Knepley 
7974a6e0b375SMatthew G. Knepley   Not collective
7975a6e0b375SMatthew G. Knepley 
7976a6e0b375SMatthew G. Knepley   Input Parameters:
7977bb7acecfSBarry Smith + dm      - The `DM`
7978a6e0b375SMatthew G. Knepley . time    - The time
7979a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7980a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7981a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7982a6e0b375SMatthew G. Knepley 
7983a6e0b375SMatthew G. Knepley   Output Parameter:
7984a6e0b375SMatthew G. Knepley . localX - vector
7985a6e0b375SMatthew G. Knepley 
7986a6e0b375SMatthew G. Knepley    Calling sequence of func:
798777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7988a6e0b375SMatthew G. Knepley 
7989a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7990a6e0b375SMatthew G. Knepley .  x   - The coordinates
799177b739a6SMatthew Knepley .  Nc  - The number of components
7992a6e0b375SMatthew G. Knepley .  u   - The output field values
7993a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7994a6e0b375SMatthew G. Knepley 
7995a6e0b375SMatthew G. Knepley   Level: developer
7996a6e0b375SMatthew G. Knepley 
7997bb7acecfSBarry Smith   Developer Notes:
7998bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7999bb7acecfSBarry Smith 
8000bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8001bb7acecfSBarry Smith 
8002db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8003a6e0b375SMatthew G. Knepley @*/
8004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
8005d71ae5a4SJacob Faibussowitsch {
80064d6f44ffSToby Isaac   PetscFunctionBegin;
80074d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8008064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
80099566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
80103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80114d6f44ffSToby Isaac }
80124d6f44ffSToby Isaac 
8013a6e0b375SMatthew G. Knepley /*@C
8014bb7acecfSBarry 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.
8015a6e0b375SMatthew G. Knepley 
8016bb7acecfSBarry Smith   Collective on dm
8017a6e0b375SMatthew G. Knepley 
8018a6e0b375SMatthew G. Knepley   Input Parameters:
8019bb7acecfSBarry Smith + dm      - The `DM`
8020a6e0b375SMatthew G. Knepley . time    - The time
8021bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8022a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8023bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8024a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8025a6e0b375SMatthew G. Knepley 
8026a6e0b375SMatthew G. Knepley   Output Parameter:
8027a6e0b375SMatthew G. Knepley . X - vector
8028a6e0b375SMatthew G. Knepley 
8029a6e0b375SMatthew G. Knepley    Calling sequence of func:
803077b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8031a6e0b375SMatthew G. Knepley 
8032a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8033a6e0b375SMatthew G. Knepley .  x   - The coordinates
803477b739a6SMatthew Knepley .  Nc  - The number of components
8035a6e0b375SMatthew G. Knepley .  u   - The output field values
8036a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8037a6e0b375SMatthew G. Knepley 
8038a6e0b375SMatthew G. Knepley   Level: developer
8039a6e0b375SMatthew G. Knepley 
8040bb7acecfSBarry Smith   Developer Notes:
8041bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8042bb7acecfSBarry Smith 
8043bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8044bb7acecfSBarry Smith 
8045db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8046a6e0b375SMatthew G. Knepley @*/
8047d71ae5a4SJacob 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)
8048d71ae5a4SJacob Faibussowitsch {
80492c53366bSMatthew G. Knepley   Vec localX;
80502c53366bSMatthew G. Knepley 
80512c53366bSMatthew G. Knepley   PetscFunctionBegin;
80522c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80539566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8054f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80559566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80569566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80579566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80589566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
80593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80602c53366bSMatthew G. Knepley }
80612c53366bSMatthew G. Knepley 
8062a6e0b375SMatthew G. Knepley /*@C
8063bb7acecfSBarry 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.
8064a6e0b375SMatthew G. Knepley 
8065a6e0b375SMatthew G. Knepley   Not collective
8066a6e0b375SMatthew G. Knepley 
8067a6e0b375SMatthew G. Knepley   Input Parameters:
8068bb7acecfSBarry Smith + dm      - The `DM`
8069a6e0b375SMatthew G. Knepley . time    - The time
8070bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8071a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8072a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8073a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8074a6e0b375SMatthew G. Knepley 
8075a6e0b375SMatthew G. Knepley   Output Parameter:
8076a6e0b375SMatthew G. Knepley . localX - vector
8077a6e0b375SMatthew G. Knepley 
8078a6e0b375SMatthew G. Knepley    Calling sequence of func:
807977b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8080a6e0b375SMatthew G. Knepley 
8081a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8082a6e0b375SMatthew G. Knepley .  x   - The coordinates
808377b739a6SMatthew Knepley .  Nc  - The number of components
8084a6e0b375SMatthew G. Knepley .  u   - The output field values
8085a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8086a6e0b375SMatthew G. Knepley 
8087a6e0b375SMatthew G. Knepley   Level: developer
8088a6e0b375SMatthew G. Knepley 
8089bb7acecfSBarry Smith   Developer Notes:
8090bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8091bb7acecfSBarry Smith 
8092bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8093bb7acecfSBarry Smith 
8094db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8095a6e0b375SMatthew G. Knepley @*/
8096d71ae5a4SJacob 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)
8097d71ae5a4SJacob Faibussowitsch {
80984d6f44ffSToby Isaac   PetscFunctionBegin;
80994d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8100064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
81019566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81034d6f44ffSToby Isaac }
81042716604bSToby Isaac 
8105a6e0b375SMatthew G. Knepley /*@C
8106bb7acecfSBarry 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.
8107a6e0b375SMatthew G. Knepley 
8108a6e0b375SMatthew G. Knepley   Not collective
8109a6e0b375SMatthew G. Knepley 
8110a6e0b375SMatthew G. Knepley   Input Parameters:
8111bb7acecfSBarry Smith + dm      - The `DM`
8112a6e0b375SMatthew G. Knepley . time    - The time
8113eb8f539aSJed Brown . localU  - The input field vector; may be NULL if projection is defined purely by coordinates
8114a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8115a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8116a6e0b375SMatthew G. Knepley 
8117a6e0b375SMatthew G. Knepley   Output Parameter:
8118a6e0b375SMatthew G. Knepley . localX  - The output vector
8119a6e0b375SMatthew G. Knepley 
8120a6e0b375SMatthew G. Knepley    Calling sequence of func:
8121a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8122a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8123a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8124a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8125a6e0b375SMatthew G. Knepley 
8126a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8127a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8128a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8129a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8130a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8131a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8132a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8133a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8134a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8135a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8136a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8137a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8138a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8139a6e0b375SMatthew G. Knepley .  t            - The current time
8140a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8141a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8142a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8143a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8144a6e0b375SMatthew G. Knepley 
8145bb7acecfSBarry Smith   Note:
8146bb7acecfSBarry 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.
8147bb7acecfSBarry 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
8148bb7acecfSBarry 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
8149a6e0b375SMatthew 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.
8150a6e0b375SMatthew G. Knepley 
8151a6e0b375SMatthew G. Knepley   Level: intermediate
8152a6e0b375SMatthew G. Knepley 
8153bb7acecfSBarry Smith   Developer Notes:
8154bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8155bb7acecfSBarry Smith 
8156bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8157bb7acecfSBarry Smith 
8158db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8159a6e0b375SMatthew G. Knepley @*/
8160d71ae5a4SJacob 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)
8161d71ae5a4SJacob Faibussowitsch {
81628c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81638c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8164eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
81658c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
81669566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
81673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81688c6c5593SMatthew G. Knepley }
81698c6c5593SMatthew G. Knepley 
8170a6e0b375SMatthew G. Knepley /*@C
8171a6e0b375SMatthew 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.
8172a6e0b375SMatthew G. Knepley 
8173a6e0b375SMatthew G. Knepley   Not collective
8174a6e0b375SMatthew G. Knepley 
8175a6e0b375SMatthew G. Knepley   Input Parameters:
8176bb7acecfSBarry Smith + dm      - The `DM`
8177a6e0b375SMatthew G. Knepley . time    - The time
8178bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8179a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8180a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8181bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8182a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8183a6e0b375SMatthew G. Knepley . localU  - The input field vector
8184a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8185a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8186a6e0b375SMatthew G. Knepley 
8187a6e0b375SMatthew G. Knepley   Output Parameter:
8188a6e0b375SMatthew G. Knepley . localX  - The output vector
8189a6e0b375SMatthew G. Knepley 
8190a6e0b375SMatthew G. Knepley    Calling sequence of func:
8191a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8192a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8193a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8194a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8195a6e0b375SMatthew G. Knepley 
8196a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8197a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8198a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8199a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8200a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8201a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8202a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8203a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8204a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8205a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8206a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8207a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8208a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8209a6e0b375SMatthew G. Knepley .  t            - The current time
8210a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8211a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8212a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8213a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8214a6e0b375SMatthew G. Knepley 
8215bb7acecfSBarry Smith   Note:
8216bb7acecfSBarry 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.
8217bb7acecfSBarry 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
8218bb7acecfSBarry 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
8219a6e0b375SMatthew 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.
8220a6e0b375SMatthew G. Knepley 
8221a6e0b375SMatthew G. Knepley   Level: intermediate
8222a6e0b375SMatthew G. Knepley 
8223bb7acecfSBarry Smith   Developer Notes:
8224bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8225bb7acecfSBarry Smith 
8226bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8227bb7acecfSBarry Smith 
8228d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8229a6e0b375SMatthew G. Knepley @*/
8230d71ae5a4SJacob 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)
8231d71ae5a4SJacob Faibussowitsch {
82328c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82338c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8234064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8235064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
82369566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
82373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82388c6c5593SMatthew G. Knepley }
82398c6c5593SMatthew G. Knepley 
82402716604bSToby Isaac /*@C
8241d29d7c6eSMatthew 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.
8242d29d7c6eSMatthew G. Knepley 
8243d29d7c6eSMatthew G. Knepley   Not collective
8244d29d7c6eSMatthew G. Knepley 
8245d29d7c6eSMatthew G. Knepley   Input Parameters:
8246bb7acecfSBarry Smith + dm      - The `DM`
8247d29d7c6eSMatthew G. Knepley . time    - The time
8248bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8249d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8250d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8251bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8252d29d7c6eSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8253d29d7c6eSMatthew G. Knepley . U       - The input field vector
8254d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8255d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8256d29d7c6eSMatthew G. Knepley 
8257d29d7c6eSMatthew G. Knepley   Output Parameter:
8258d29d7c6eSMatthew G. Knepley . X       - The output vector
8259d29d7c6eSMatthew G. Knepley 
8260d29d7c6eSMatthew G. Knepley    Calling sequence of func:
8261d29d7c6eSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8262d29d7c6eSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8263d29d7c6eSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8264d29d7c6eSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8265d29d7c6eSMatthew G. Knepley 
8266d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8267d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8268d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8269d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8270d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8271d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8272d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8273d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8274d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8275d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8276d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8277d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8278d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8279d29d7c6eSMatthew G. Knepley .  t            - The current time
8280d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8281d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8282d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8283d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8284d29d7c6eSMatthew G. Knepley 
8285bb7acecfSBarry Smith   Note:
8286bb7acecfSBarry 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.
8287bb7acecfSBarry 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
8288bb7acecfSBarry 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
8289d29d7c6eSMatthew 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.
8290d29d7c6eSMatthew G. Knepley 
8291d29d7c6eSMatthew G. Knepley   Level: intermediate
8292d29d7c6eSMatthew G. Knepley 
8293bb7acecfSBarry Smith   Developer Notes:
8294bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8295bb7acecfSBarry Smith 
8296bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8297bb7acecfSBarry Smith 
8298d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8299d29d7c6eSMatthew G. Knepley @*/
8300d71ae5a4SJacob 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)
8301d71ae5a4SJacob Faibussowitsch {
8302d29d7c6eSMatthew G. Knepley   DM  dmIn;
8303d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8304d29d7c6eSMatthew G. Knepley 
8305d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8306d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8307d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8308d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8309d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8310f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
831172fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
831272fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8313d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8314d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8315d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8316d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8317d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
83183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8319d29d7c6eSMatthew G. Knepley }
8320d29d7c6eSMatthew G. Knepley 
8321d29d7c6eSMatthew G. Knepley /*@C
8322ece3a9fcSMatthew 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.
8323ece3a9fcSMatthew G. Knepley 
8324ece3a9fcSMatthew G. Knepley   Not collective
8325ece3a9fcSMatthew G. Knepley 
8326ece3a9fcSMatthew G. Knepley   Input Parameters:
8327bb7acecfSBarry Smith + dm      - The `DM`
8328ece3a9fcSMatthew G. Knepley . time    - The time
8329bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8330ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8331ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8332bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8333ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8334ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8335ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8336ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8337ece3a9fcSMatthew G. Knepley 
8338ece3a9fcSMatthew G. Knepley   Output Parameter:
8339ece3a9fcSMatthew G. Knepley . localX  - The output vector
8340ece3a9fcSMatthew G. Knepley 
8341ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8342ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8343ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8344ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8345ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8346ece3a9fcSMatthew G. Knepley 
8347ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8348ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8349ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8350ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8351ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8352ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8353ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8354ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8355ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8356ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8357ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8358ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8359ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8360ece3a9fcSMatthew G. Knepley .  t            - The current time
8361ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8362ece3a9fcSMatthew G. Knepley .  n            - The face normal
8363ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8364ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8365ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8366ece3a9fcSMatthew G. Knepley 
8367ece3a9fcSMatthew G. Knepley   Note:
8368bb7acecfSBarry 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.
8369bb7acecfSBarry 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
8370bb7acecfSBarry 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
8371ece3a9fcSMatthew 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.
8372ece3a9fcSMatthew G. Knepley 
8373ece3a9fcSMatthew G. Knepley   Level: intermediate
8374ece3a9fcSMatthew G. Knepley 
8375bb7acecfSBarry Smith   Developer Notes:
8376bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8377bb7acecfSBarry Smith 
8378bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8379bb7acecfSBarry Smith 
8380db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8381ece3a9fcSMatthew G. Knepley @*/
8382d71ae5a4SJacob 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)
8383d71ae5a4SJacob Faibussowitsch {
8384ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8385ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8386064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8387064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83889566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
83893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8390ece3a9fcSMatthew G. Knepley }
8391ece3a9fcSMatthew G. Knepley 
8392ece3a9fcSMatthew G. Knepley /*@C
83932716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83942716604bSToby Isaac 
8395bb7acecfSBarry Smith   Collective on dm
8396bb7acecfSBarry Smith 
83972716604bSToby Isaac   Input Parameters:
8398bb7acecfSBarry Smith + dm    - The `DM`
83990709b2feSToby Isaac . time  - The time
84002716604bSToby Isaac . funcs - The functions to evaluate for each field component
84012716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8402574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84032716604bSToby Isaac 
84042716604bSToby Isaac   Output Parameter:
84052716604bSToby Isaac . diff - The diff ||u - u_h||_2
84062716604bSToby Isaac 
84072716604bSToby Isaac   Level: developer
84082716604bSToby Isaac 
8409bb7acecfSBarry Smith   Developer Notes:
8410bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8411bb7acecfSBarry Smith 
8412bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8413bb7acecfSBarry Smith 
8414db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84152716604bSToby Isaac @*/
8416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8417d71ae5a4SJacob Faibussowitsch {
84182716604bSToby Isaac   PetscFunctionBegin;
84192716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8420b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84219566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
84223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84232716604bSToby Isaac }
8424b698f381SToby Isaac 
8425b698f381SToby Isaac /*@C
8426b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8427b698f381SToby Isaac 
8428d083f849SBarry Smith   Collective on dm
8429d083f849SBarry Smith 
8430b698f381SToby Isaac   Input Parameters:
8431bb7acecfSBarry Smith + dm    - The `DM`
8432b698f381SToby Isaac , time  - The time
8433b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8434b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8435574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8436b698f381SToby Isaac - n     - The vector to project along
8437b698f381SToby Isaac 
8438b698f381SToby Isaac   Output Parameter:
8439b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8440b698f381SToby Isaac 
8441b698f381SToby Isaac   Level: developer
8442b698f381SToby Isaac 
8443bb7acecfSBarry Smith   Developer Notes:
8444bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8445bb7acecfSBarry Smith 
8446bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8447bb7acecfSBarry Smith 
8448bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8449b698f381SToby Isaac @*/
8450d71ae5a4SJacob 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)
8451d71ae5a4SJacob Faibussowitsch {
8452b698f381SToby Isaac   PetscFunctionBegin;
8453b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8454b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84559566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
84563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8457b698f381SToby Isaac }
8458b698f381SToby Isaac 
84592a16baeaSToby Isaac /*@C
84602a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84612a16baeaSToby Isaac 
8462d083f849SBarry Smith   Collective on dm
8463d083f849SBarry Smith 
84642a16baeaSToby Isaac   Input Parameters:
8465bb7acecfSBarry Smith + dm    - The `DM`
84662a16baeaSToby Isaac . time  - The time
84672a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84682a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8469574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84702a16baeaSToby Isaac 
84712a16baeaSToby Isaac   Output Parameter:
84722a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84732a16baeaSToby Isaac 
84742a16baeaSToby Isaac   Level: developer
84752a16baeaSToby Isaac 
8476bb7acecfSBarry Smith   Developer Notes:
8477bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8478bb7acecfSBarry Smith 
8479bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8480bb7acecfSBarry Smith 
8481db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84822a16baeaSToby Isaac @*/
8483d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8484d71ae5a4SJacob Faibussowitsch {
84852a16baeaSToby Isaac   PetscFunctionBegin;
84862a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84872a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84889566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84902a16baeaSToby Isaac }
84912a16baeaSToby Isaac 
8492df0b854cSToby Isaac /*@C
8493bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8494502a2867SDave May 
8495502a2867SDave May  Not Collective
8496502a2867SDave May 
8497502a2867SDave May  Input Parameter:
8498bb7acecfSBarry Smith .  dm    - The `DM`
8499502a2867SDave May 
85000a19bb7dSprj-  Output Parameters:
85010a19bb7dSprj- +  nranks - the number of neighbours
85020a19bb7dSprj- -  ranks - the neighbors ranks
8503502a2867SDave May 
8504bb7acecfSBarry Smith  Note:
8505bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8506502a2867SDave May 
8507502a2867SDave May  Level: beginner
8508502a2867SDave May 
8509db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8510502a2867SDave May @*/
8511d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8512d71ae5a4SJacob Faibussowitsch {
8513502a2867SDave May   PetscFunctionBegin;
8514502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85159566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
85163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8517502a2867SDave May }
8518502a2867SDave May 
8519531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8520531c7667SBarry Smith 
8521531c7667SBarry Smith /*
8522531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8523531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8524531c7667SBarry Smith */
8525d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8526d71ae5a4SJacob Faibussowitsch {
8527531c7667SBarry Smith   PetscFunctionBegin;
8528531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8529531c7667SBarry Smith     Vec x1local;
8530531c7667SBarry Smith     DM  dm;
85319566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85327a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
85339566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
85349566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
85359566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8536531c7667SBarry Smith     x1 = x1local;
8537531c7667SBarry Smith   }
85389566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8539531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8540531c7667SBarry Smith     DM dm;
85419566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85429566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8543531c7667SBarry Smith   }
85443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8545531c7667SBarry Smith }
8546531c7667SBarry Smith 
8547531c7667SBarry Smith /*@
8548bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8549531c7667SBarry Smith 
8550531c7667SBarry Smith     Input Parameter:
8551bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8552531c7667SBarry Smith 
8553bb7acecfSBarry Smith     Developer Note:
8554bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8555531c7667SBarry Smith 
85561b266c99SBarry Smith     Level: advanced
85571b266c99SBarry Smith 
8558db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8559531c7667SBarry Smith @*/
8560d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8561d71ae5a4SJacob Faibussowitsch {
8562531c7667SBarry Smith   PetscFunctionBegin;
8563531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
85643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8565531c7667SBarry Smith }
85668320bc6fSPatrick Sanan 
85678320bc6fSPatrick Sanan /*@
8568bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85698320bc6fSPatrick Sanan 
85708320bc6fSPatrick Sanan     Collective
85718320bc6fSPatrick Sanan 
85728320bc6fSPatrick Sanan     Input Parameters:
8573bb7acecfSBarry Smith +    dm1 - the first `DM`
8574bb7acecfSBarry Smith -    dm2 - the second `DM`
85758320bc6fSPatrick Sanan 
85768320bc6fSPatrick Sanan     Output Parameters:
8577bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8578bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85798320bc6fSPatrick Sanan 
85808320bc6fSPatrick Sanan     Notes:
8581bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85823d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85838320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8584bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85853d862458SPatrick Sanan     decomposition, but hold different data.
85868320bc6fSPatrick Sanan 
85878320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8588bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85898320bc6fSPatrick Sanan 
8590bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85918320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85928320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8593bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85948320bc6fSPatrick Sanan 
8595bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8596bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85978320bc6fSPatrick Sanan .vb
85988320bc6fSPatrick Sanan   ...
85999566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
86008320bc6fSPatrick Sanan   if (set && compatible)  {
86019566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
86029566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
86039566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
86048320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86058320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86068320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86078320bc6fSPatrick Sanan       }
86088320bc6fSPatrick Sanan     }
86099566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
86109566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
86118320bc6fSPatrick Sanan   } else {
86128320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86138320bc6fSPatrick Sanan   }
86148320bc6fSPatrick Sanan   ...
86158320bc6fSPatrick Sanan .ve
86168320bc6fSPatrick Sanan 
8617bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
86188320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
86198320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
86208320bc6fSPatrick Sanan     always check the "set" output parameter.
86218320bc6fSPatrick Sanan 
8622bb7acecfSBarry Smith     A `DM` is always compatible with itself.
86238320bc6fSPatrick Sanan 
8624bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
86258320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
86268320bc6fSPatrick Sanan     incompatible.
86278320bc6fSPatrick Sanan 
86288320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8629bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
86308320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
86318320bc6fSPatrick Sanan 
8632bb7acecfSBarry Smith     Developer Note:
8633bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
86343d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8635a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8636bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
86378320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
86383d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8639bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
86408320bc6fSPatrick Sanan 
86418320bc6fSPatrick Sanan     Level: advanced
86428320bc6fSPatrick Sanan 
8643db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
86448320bc6fSPatrick Sanan @*/
8645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8646d71ae5a4SJacob Faibussowitsch {
86478320bc6fSPatrick Sanan   PetscMPIInt compareResult;
86488320bc6fSPatrick Sanan   DMType      type, type2;
86498320bc6fSPatrick Sanan   PetscBool   sameType;
86508320bc6fSPatrick Sanan 
86518320bc6fSPatrick Sanan   PetscFunctionBegin;
8652a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
86538320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
86548320bc6fSPatrick Sanan 
86558320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8656a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86578320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86588320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86593ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86608320bc6fSPatrick Sanan   }
86618320bc6fSPatrick Sanan 
86628320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86638320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86648320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86658320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86669566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
86678320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86688320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86698320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86703ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86718320bc6fSPatrick Sanan   }
86728320bc6fSPatrick Sanan 
86738320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8674a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8675dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
86763ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
86778320bc6fSPatrick Sanan   }
86788320bc6fSPatrick Sanan 
8679a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86808320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86819566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86829566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86839566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86848320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8685dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86868320bc6fSPatrick Sanan   } else {
86878320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86888320bc6fSPatrick Sanan   }
86893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86908320bc6fSPatrick Sanan }
8691c0f0dcc3SMatthew G. Knepley 
8692c0f0dcc3SMatthew G. Knepley /*@C
8693bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8694c0f0dcc3SMatthew G. Knepley 
8695bb7acecfSBarry Smith   Logically Collective on dm
8696c0f0dcc3SMatthew G. Knepley 
8697c0f0dcc3SMatthew G. Knepley   Input Parameters:
8698bb7acecfSBarry Smith + DM - the `DM`
8699c0f0dcc3SMatthew G. Knepley . f - the monitor function
8700c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8701c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8702c0f0dcc3SMatthew G. Knepley 
8703c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8704bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8705c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8706c0f0dcc3SMatthew G. Knepley 
8707bb7acecfSBarry Smith   Note:
8708c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8709bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8710c0f0dcc3SMatthew G. Knepley   order in which they were set.
8711c0f0dcc3SMatthew G. Knepley 
8712bb7acecfSBarry Smith   Fortran Note:
8713bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8714bb7acecfSBarry Smith 
8715bb7acecfSBarry Smith   Developer Note:
8716bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8717c0f0dcc3SMatthew G. Knepley 
8718c0f0dcc3SMatthew G. Knepley   Level: intermediate
8719c0f0dcc3SMatthew G. Knepley 
8720bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8721c0f0dcc3SMatthew G. Knepley @*/
8722d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8723d71ae5a4SJacob Faibussowitsch {
8724c0f0dcc3SMatthew G. Knepley   PetscInt m;
8725c0f0dcc3SMatthew G. Knepley 
8726c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8727c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8728c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8729c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8730c0f0dcc3SMatthew G. Knepley 
87319566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
87323ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8733c0f0dcc3SMatthew G. Knepley   }
87347a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8735c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8736c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8737c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
87383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8739c0f0dcc3SMatthew G. Knepley }
8740c0f0dcc3SMatthew G. Knepley 
8741c0f0dcc3SMatthew G. Knepley /*@
8742bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8743c0f0dcc3SMatthew G. Knepley 
8744bb7acecfSBarry Smith   Logically Collective on dm
8745c0f0dcc3SMatthew G. Knepley 
8746c0f0dcc3SMatthew G. Knepley   Input Parameter:
8747c0f0dcc3SMatthew G. Knepley . dm - the DM
8748c0f0dcc3SMatthew G. Knepley 
8749c0f0dcc3SMatthew G. Knepley   Options Database Key:
8750c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8751bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8752c0f0dcc3SMatthew G. Knepley   set via the options database
8753c0f0dcc3SMatthew G. Knepley 
8754bb7acecfSBarry Smith   Note:
8755bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8756c0f0dcc3SMatthew G. Knepley 
8757c0f0dcc3SMatthew G. Knepley   Level: intermediate
8758c0f0dcc3SMatthew G. Knepley 
8759bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8760c0f0dcc3SMatthew G. Knepley @*/
8761d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8762d71ae5a4SJacob Faibussowitsch {
8763c0f0dcc3SMatthew G. Knepley   PetscInt m;
8764c0f0dcc3SMatthew G. Knepley 
8765c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8766c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8767c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87689566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8769c0f0dcc3SMatthew G. Knepley   }
8770c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
87713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8772c0f0dcc3SMatthew G. Knepley }
8773c0f0dcc3SMatthew G. Knepley 
8774c0f0dcc3SMatthew G. Knepley /*@C
8775c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8776c0f0dcc3SMatthew G. Knepley 
8777bb7acecfSBarry Smith   Collective on dm
8778c0f0dcc3SMatthew G. Knepley 
8779c0f0dcc3SMatthew G. Knepley   Input Parameters:
8780bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8781c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8782c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8783c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8784c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8785bb7acecfSBarry 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
8786c0f0dcc3SMatthew G. Knepley 
8787c0f0dcc3SMatthew G. Knepley   Output Parameter:
8788c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8789c0f0dcc3SMatthew G. Knepley 
8790c0f0dcc3SMatthew G. Knepley   Level: developer
8791c0f0dcc3SMatthew G. Knepley 
8792db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8793db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8794db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8795db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8796c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8797db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8798bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8799c0f0dcc3SMatthew G. Knepley @*/
8800d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8801d71ae5a4SJacob Faibussowitsch {
8802c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8803c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8804c0f0dcc3SMatthew G. Knepley 
8805c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8806c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8808c0f0dcc3SMatthew G. Knepley   if (*flg) {
8809c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8810c0f0dcc3SMatthew G. Knepley 
88119566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
88129566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
88139566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
88149566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8815c0f0dcc3SMatthew G. Knepley   }
88163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8817c0f0dcc3SMatthew G. Knepley }
8818c0f0dcc3SMatthew G. Knepley 
8819c0f0dcc3SMatthew G. Knepley /*@
8820c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8821c0f0dcc3SMatthew G. Knepley 
8822bb7acecfSBarry Smith    Collective on dm
8823c0f0dcc3SMatthew G. Knepley 
8824c0f0dcc3SMatthew G. Knepley    Input Parameters:
8825bb7acecfSBarry Smith .  dm - The `DM`
8826c0f0dcc3SMatthew G. Knepley 
8827c0f0dcc3SMatthew G. Knepley    Level: developer
8828c0f0dcc3SMatthew G. Knepley 
8829bb7acecfSBarry Smith    Question:
8830bb7acecfSBarry 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
8831bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8832bb7acecfSBarry Smith 
8833bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8834c0f0dcc3SMatthew G. Knepley @*/
8835d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8836d71ae5a4SJacob Faibussowitsch {
8837c0f0dcc3SMatthew G. Knepley   PetscInt m;
8838c0f0dcc3SMatthew G. Knepley 
8839c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
88403ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8841c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
884248a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
88433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8844c0f0dcc3SMatthew G. Knepley }
88452e4af2aeSMatthew G. Knepley 
88462e4af2aeSMatthew G. Knepley /*@
8847bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
88482e4af2aeSMatthew G. Knepley 
8849bb7acecfSBarry Smith   Collective on dm
88502e4af2aeSMatthew G. Knepley 
88512e4af2aeSMatthew G. Knepley   Input Parameters:
8852bb7acecfSBarry Smith + dm     - The `DM`
88536b867d5aSJose E. Roman - sol    - The solution vector
88542e4af2aeSMatthew G. Knepley 
88556b867d5aSJose E. Roman   Input/Output Parameter:
88566b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
88576b867d5aSJose E. Roman            contains the error in each field
88586b867d5aSJose E. Roman 
88596b867d5aSJose E. Roman   Output Parameter:
88606b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
88612e4af2aeSMatthew G. Knepley 
8862bb7acecfSBarry Smith   Note:
8863bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88642e4af2aeSMatthew G. Knepley 
88652e4af2aeSMatthew G. Knepley   Level: developer
88662e4af2aeSMatthew G. Knepley 
8867db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88682e4af2aeSMatthew G. Knepley @*/
8869d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8870d71ae5a4SJacob Faibussowitsch {
88712e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88722e4af2aeSMatthew G. Knepley   void    **ctxs;
88732e4af2aeSMatthew G. Knepley   PetscReal time;
88742e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88752e4af2aeSMatthew G. Knepley 
88762e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88779566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88789566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88799566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88802e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88812e4af2aeSMatthew G. Knepley     PetscDS         ds;
88822e4af2aeSMatthew G. Knepley     DMLabel         label;
88832e4af2aeSMatthew G. Knepley     IS              fieldIS;
88842e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88852e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88862e4af2aeSMatthew G. Knepley 
88879566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
88889566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88899566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88902e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88912e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88929566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88932e4af2aeSMatthew G. Knepley     }
88949566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88952e4af2aeSMatthew G. Knepley   }
8896ad540459SPierre 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);
88979566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88989566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88992e4af2aeSMatthew G. Knepley   if (errorVec) {
89002e4af2aeSMatthew G. Knepley     DM             edm;
89012e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
89022e4af2aeSMatthew G. Knepley     PetscBool      simplex;
89032e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
89042e4af2aeSMatthew G. Knepley 
89059566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
89069566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
89079566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89089566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
89092e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
89109566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
89112e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
89122e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
89132e4af2aeSMatthew G. Knepley       PetscQuadrature q;
89142e4af2aeSMatthew G. Knepley       const char     *name;
89152e4af2aeSMatthew G. Knepley 
89169566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
89179566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
89189566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
89199566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
89209566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
89219566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
89229566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
89239566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
89242e4af2aeSMatthew G. Knepley     }
89259566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
89262e4af2aeSMatthew G. Knepley 
89279566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
89289566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
89299566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
89309566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
89312e4af2aeSMatthew G. Knepley   }
89329566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
89333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89342e4af2aeSMatthew G. Knepley }
89359a2a23afSMatthew G. Knepley 
89369a2a23afSMatthew G. Knepley /*@
8937bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
89389a2a23afSMatthew G. Knepley 
89399a2a23afSMatthew G. Knepley   Not collective
89409a2a23afSMatthew G. Knepley 
89419a2a23afSMatthew G. Knepley   Input Parameter:
8942bb7acecfSBarry Smith . dm     - The `DM`
89439a2a23afSMatthew G. Knepley 
89449a2a23afSMatthew G. Knepley   Output Parameter:
8945a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
89469a2a23afSMatthew G. Knepley 
89479a2a23afSMatthew G. Knepley   Level: advanced
89489a2a23afSMatthew G. Knepley 
8949bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
89509a2a23afSMatthew G. Knepley @*/
8951d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8952d71ae5a4SJacob Faibussowitsch {
89539a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89549a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89559566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89579a2a23afSMatthew G. Knepley }
89589a2a23afSMatthew G. Knepley 
89599a2a23afSMatthew G. Knepley /*@
8960ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89619a2a23afSMatthew G. Knepley 
89629a2a23afSMatthew G. Knepley   Not collective
89639a2a23afSMatthew G. Knepley 
89649a2a23afSMatthew G. Knepley   Input Parameters:
8965bb7acecfSBarry Smith + dm     - The `DM`
8966bb7acecfSBarry Smith . label  - The `DMLabel`
8967ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8968ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89699a2a23afSMatthew G. Knepley 
89709a2a23afSMatthew G. Knepley   Output Parameter:
8971bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89729a2a23afSMatthew G. Knepley 
8973bb7acecfSBarry Smith   Note:
8974bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
897504c51a94SMatthew G. Knepley 
89769a2a23afSMatthew G. Knepley   Level: advanced
89779a2a23afSMatthew G. Knepley 
8978bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89799a2a23afSMatthew G. Knepley @*/
8980d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8981d71ae5a4SJacob Faibussowitsch {
8982ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
898304c51a94SMatthew G. Knepley   PetscBool       has;
89849a2a23afSMatthew G. Knepley 
89859a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89869a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89879a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89889a2a23afSMatthew G. Knepley   key.label = label;
89899a2a23afSMatthew G. Knepley   key.value = value;
8990ac17215fSMatthew G. Knepley   key.part  = part;
89919566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89929566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89939566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89959a2a23afSMatthew G. Knepley }
89969a2a23afSMatthew G. Knepley 
89979a2a23afSMatthew G. Knepley /*@
8998bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89999a2a23afSMatthew G. Knepley 
9000da81f932SPierre Jolivet   Not collective because auxiliary vectors are not parallel
90019a2a23afSMatthew G. Knepley 
90029a2a23afSMatthew G. Knepley   Input Parameters:
9003bb7acecfSBarry Smith + dm     - The `DM`
9004bb7acecfSBarry Smith . label  - The `DMLabel`
90059a2a23afSMatthew G. Knepley . value  - The label value indicating the region
9006ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
9007bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
90089a2a23afSMatthew G. Knepley 
90099a2a23afSMatthew G. Knepley   Level: advanced
90109a2a23afSMatthew G. Knepley 
9011bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
90129a2a23afSMatthew G. Knepley @*/
9013d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9014d71ae5a4SJacob Faibussowitsch {
90159a2a23afSMatthew G. Knepley   Vec             old;
90169a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
90179a2a23afSMatthew G. Knepley 
90189a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90199a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90209a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90219a2a23afSMatthew G. Knepley   key.label = label;
90229a2a23afSMatthew G. Knepley   key.value = value;
9023ac17215fSMatthew G. Knepley   key.part  = part;
90249566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
90259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
90269566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
90279566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
90289566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
90293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90309a2a23afSMatthew G. Knepley }
90319a2a23afSMatthew G. Knepley 
90329a2a23afSMatthew G. Knepley /*@C
9033bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
90349a2a23afSMatthew G. Knepley 
90359a2a23afSMatthew G. Knepley   Not collective
90369a2a23afSMatthew G. Knepley 
90379a2a23afSMatthew G. Knepley   Input Parameter:
9038bb7acecfSBarry Smith . dm      - The `DM`
90399a2a23afSMatthew G. Knepley 
90409a2a23afSMatthew G. Knepley   Output Parameters:
9041bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
9042bb7acecfSBarry Smith . values  - The label values for each `Vec`
9043bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
90449a2a23afSMatthew G. Knepley 
9045bb7acecfSBarry Smith   Note:
9046bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
90479a2a23afSMatthew G. Knepley 
90489a2a23afSMatthew G. Knepley   Level: advanced
90499a2a23afSMatthew G. Knepley 
9050bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
90519a2a23afSMatthew G. Knepley @*/
9052d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9053d71ae5a4SJacob Faibussowitsch {
90549a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
90559a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90569a2a23afSMatthew G. Knepley 
90579a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90589a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90599a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9060dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9061dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
90629566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90639566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90649566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
90659371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
90669371c9d4SSatish Balay     labels[i] = keys[i].label;
90679371c9d4SSatish Balay     values[i] = keys[i].value;
90689371c9d4SSatish Balay     parts[i]  = keys[i].part;
90699371c9d4SSatish Balay   }
90709566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90729a2a23afSMatthew G. Knepley }
90739a2a23afSMatthew G. Knepley 
90749a2a23afSMatthew G. Knepley /*@
9075bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90769a2a23afSMatthew G. Knepley 
90779a2a23afSMatthew G. Knepley   Not collective
90789a2a23afSMatthew G. Knepley 
90799a2a23afSMatthew G. Knepley   Input Parameter:
9080bb7acecfSBarry Smith . dm    - The `DM`
90819a2a23afSMatthew G. Knepley 
90829a2a23afSMatthew G. Knepley   Output Parameter:
9083bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90849a2a23afSMatthew G. Knepley 
90859a2a23afSMatthew G. Knepley   Level: advanced
90869a2a23afSMatthew G. Knepley 
9087bb7acecfSBarry Smith   Note:
9088bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9089bb7acecfSBarry Smith 
9090db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90919a2a23afSMatthew G. Knepley @*/
9092d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9093d71ae5a4SJacob Faibussowitsch {
90949a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90959a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90969566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90979566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90999a2a23afSMatthew G. Knepley }
9100b5a892a1SMatthew G. Knepley 
9101b5a892a1SMatthew G. Knepley /*@C
9102bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9103b5a892a1SMatthew G. Knepley 
9104b5a892a1SMatthew G. Knepley   Not collective
9105b5a892a1SMatthew G. Knepley 
9106b5a892a1SMatthew G. Knepley   Input Parameters:
9107bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9108b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9109b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9110b5a892a1SMatthew G. Knepley 
9111b5a892a1SMatthew G. Knepley   Output Parameters:
9112bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9113b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9114b5a892a1SMatthew G. Knepley 
9115b5a892a1SMatthew G. Knepley   Level: advanced
9116b5a892a1SMatthew G. Knepley 
9117bb7acecfSBarry Smith   Note:
9118bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9119bb7acecfSBarry Smith 
9120bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9121bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9122bb7acecfSBarry Smith 
9123bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9124bb7acecfSBarry Smith 
9125bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9126b5a892a1SMatthew G. Knepley @*/
9127d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9128d71ae5a4SJacob Faibussowitsch {
9129b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9130b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9131b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9132b5a892a1SMatthew G. Knepley 
9133b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91349371c9d4SSatish Balay   if (!nO) {
91359371c9d4SSatish Balay     *ornt  = 0;
91369371c9d4SSatish Balay     *found = PETSC_TRUE;
91373ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
91389371c9d4SSatish Balay   }
9139b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9140b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9141b5a892a1SMatthew G. Knepley 
91429371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91439371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
91449371c9d4SSatish Balay     if (c == cS) {
91459371c9d4SSatish Balay       *ornt = o;
91469371c9d4SSatish Balay       break;
91479371c9d4SSatish Balay     }
9148b5a892a1SMatthew G. Knepley   }
9149b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
91503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9151b5a892a1SMatthew G. Knepley }
9152b5a892a1SMatthew G. Knepley 
9153b5a892a1SMatthew G. Knepley /*@C
9154bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9155b5a892a1SMatthew G. Knepley 
9156b5a892a1SMatthew G. Knepley   Not collective
9157b5a892a1SMatthew G. Knepley 
9158b5a892a1SMatthew G. Knepley   Input Parameters:
9159bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9160b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9161b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9162b5a892a1SMatthew G. Knepley 
9163b5a892a1SMatthew G. Knepley   Output Parameters:
9164bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9165b5a892a1SMatthew G. Knepley 
9166b5a892a1SMatthew G. Knepley   Level: advanced
9167b5a892a1SMatthew G. Knepley 
9168bb7acecfSBarry Smith   Note:
9169bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9170bb7acecfSBarry Smith 
9171bb7acecfSBarry Smith   Developer Note:
9172bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9173bb7acecfSBarry Smith 
9174bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9175b5a892a1SMatthew G. Knepley @*/
9176d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9177d71ae5a4SJacob Faibussowitsch {
9178b5a892a1SMatthew G. Knepley   PetscBool found;
9179b5a892a1SMatthew G. Knepley 
9180b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91819566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91827a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
91833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9184b5a892a1SMatthew G. Knepley }
9185b5a892a1SMatthew G. Knepley 
9186b5a892a1SMatthew G. Knepley /*@C
9187bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9188b5a892a1SMatthew G. Knepley 
9189b5a892a1SMatthew G. Knepley   Not collective
9190b5a892a1SMatthew G. Knepley 
9191b5a892a1SMatthew G. Knepley   Input Parameters:
9192bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9193b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9194b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9195b5a892a1SMatthew G. Knepley 
9196b5a892a1SMatthew G. Knepley   Output Parameters:
9197bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9198b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9199b5a892a1SMatthew G. Knepley 
9200b5a892a1SMatthew G. Knepley   Level: advanced
9201b5a892a1SMatthew G. Knepley 
9202bb7acecfSBarry Smith   Note:
9203bb7acecfSBarry Smith   An arrangement is a vertex order
9204bb7acecfSBarry Smith 
9205bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9206bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9207bb7acecfSBarry Smith 
9208bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9209bb7acecfSBarry Smith 
9210bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9211b5a892a1SMatthew G. Knepley @*/
9212d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9213d71ae5a4SJacob Faibussowitsch {
9214b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9215b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9216b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9217b5a892a1SMatthew G. Knepley 
9218b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92199371c9d4SSatish Balay   if (!nO) {
92209371c9d4SSatish Balay     *ornt  = 0;
92219371c9d4SSatish Balay     *found = PETSC_TRUE;
92223ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92239371c9d4SSatish Balay   }
9224b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9225b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9226b5a892a1SMatthew G. Knepley 
92279371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92289371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
92299371c9d4SSatish Balay     if (c == cS) {
92309371c9d4SSatish Balay       *ornt = o;
92319371c9d4SSatish Balay       break;
92329371c9d4SSatish Balay     }
9233b5a892a1SMatthew G. Knepley   }
9234b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9236b5a892a1SMatthew G. Knepley }
9237b5a892a1SMatthew G. Knepley 
9238b5a892a1SMatthew G. Knepley /*@C
9239bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9240b5a892a1SMatthew G. Knepley 
9241b5a892a1SMatthew G. Knepley   Not collective
9242b5a892a1SMatthew G. Knepley 
9243b5a892a1SMatthew G. Knepley   Input Parameters:
9244bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9245b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9246b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9247b5a892a1SMatthew G. Knepley 
9248b5a892a1SMatthew G. Knepley   Output Parameters:
9249bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9250b5a892a1SMatthew G. Knepley 
9251b5a892a1SMatthew G. Knepley   Level: advanced
9252b5a892a1SMatthew G. Knepley 
9253bb7acecfSBarry Smith   Note:
9254bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9255bb7acecfSBarry Smith 
9256bb7acecfSBarry Smith   Developer Note:
9257bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9258bb7acecfSBarry Smith 
9259bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9260b5a892a1SMatthew G. Knepley @*/
9261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9262d71ae5a4SJacob Faibussowitsch {
9263b5a892a1SMatthew G. Knepley   PetscBool found;
9264b5a892a1SMatthew G. Knepley 
9265b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92669566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
92677a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
92683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9269b5a892a1SMatthew G. Knepley }
9270012bc364SMatthew G. Knepley 
9271012bc364SMatthew G. Knepley /*@C
9272012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9273012bc364SMatthew G. Knepley 
9274012bc364SMatthew G. Knepley   Not collective
9275012bc364SMatthew G. Knepley 
9276012bc364SMatthew G. Knepley   Input Parameters:
9277bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9278012bc364SMatthew G. Knepley - point - Coordinates of the point
9279012bc364SMatthew G. Knepley 
9280012bc364SMatthew G. Knepley   Output Parameters:
9281012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9282012bc364SMatthew G. Knepley 
9283012bc364SMatthew G. Knepley   Level: advanced
9284012bc364SMatthew G. Knepley 
9285bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
9286012bc364SMatthew G. Knepley @*/
9287d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9288d71ae5a4SJacob Faibussowitsch {
9289012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9290012bc364SMatthew G. Knepley   PetscInt  d;
9291012bc364SMatthew G. Knepley 
9292012bc364SMatthew G. Knepley   PetscFunctionBegin;
9293012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9294012bc364SMatthew G. Knepley   switch (ct) {
9295012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9296012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9297012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92989371c9d4SSatish Balay       if (point[d] < -1.0) {
92999371c9d4SSatish Balay         *inside = PETSC_FALSE;
93009371c9d4SSatish Balay         break;
93019371c9d4SSatish Balay       }
9302012bc364SMatthew G. Knepley       sum += point[d];
9303012bc364SMatthew G. Knepley     }
93049371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
93059371c9d4SSatish Balay       *inside = PETSC_FALSE;
93069371c9d4SSatish Balay       break;
93079371c9d4SSatish Balay     }
9308012bc364SMatthew G. Knepley     break;
9309012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9310012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9311012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
93129371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
93139371c9d4SSatish Balay         *inside = PETSC_FALSE;
9314012bc364SMatthew G. Knepley         break;
93159371c9d4SSatish Balay       }
93169371c9d4SSatish Balay     break;
9317d71ae5a4SJacob Faibussowitsch   default:
9318d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9319012bc364SMatthew G. Knepley   }
93203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9321012bc364SMatthew G. Knepley }
9322