xref: /petsc/src/dm/interface/dm.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
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};
249371c9d4SSatish Balay const char *const DMPolytopeTypes[]   = {"vertex",  "segment",       "tensor_segment",      "triangle", "quadrilateral", "tensor_quad",    "tetrahedron",  "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism",
259371c9d4SSatish Balay                                          "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown",  "invalid",       "DMPolytopeType", "DM_POLYTOPE_", NULL};
262cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2760c22052SBarry Smith 
28a4121054SBarry Smith /*@
29bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
30bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
31a4121054SBarry Smith 
32d083f849SBarry Smith   Collective
33a4121054SBarry Smith 
34a4121054SBarry Smith   Input Parameter:
35bb7acecfSBarry Smith . comm - The communicator for the `DM` object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Output Parameter:
38bb7acecfSBarry Smith . dm - The `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Level: beginner
41a4121054SBarry Smith 
42bb7acecfSBarry Smith   Notes:
43bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
44bb7acecfSBarry Smith 
45bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
46bb7acecfSBarry Smith   error when you try to use the dm.
47bb7acecfSBarry Smith 
48bb7acecfSBarry Smith .seealso: `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
49a4121054SBarry Smith @*/
509371c9d4SSatish Balay PetscErrorCode DMCreate(MPI_Comm comm, DM *dm) {
51a4121054SBarry Smith   DM      v;
52e5e52638SMatthew G. Knepley   PetscDS ds;
53a4121054SBarry Smith 
54a4121054SBarry Smith   PetscFunctionBegin;
551411c6eeSJed Brown   PetscValidPointer(dm, 2);
560298fd71SBarry Smith   *dm = NULL;
579566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
58a4121054SBarry Smith 
599566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
60e7c4fc90SDmitry Karpeev 
6149be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6249be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
630298fd71SBarry Smith   v->ltogmap              = NULL;
64a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
651411c6eeSJed Brown   v->bs                   = 1;
66171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
679566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
689566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
69c58f1c22SToby Isaac   v->labels                    = NULL;
7034aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7134aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
72c58f1c22SToby Isaac   v->depthLabel                = NULL;
73ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
741bb6d2a8SBarry Smith   v->localSection              = NULL;
751bb6d2a8SBarry Smith   v->globalSection             = NULL;
763b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
773b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
7879769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
796858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
806858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
816858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8296173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
83435a35e8SMatthew G Knepley   {
84435a35e8SMatthew G Knepley     PetscInt i;
85435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
860298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
87f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
88435a35e8SMatthew G Knepley     }
89435a35e8SMatthew G Knepley   }
909566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
919566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(v, NULL, NULL, ds));
929566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
939566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9414f150ffSMatthew G. Knepley   v->dmBC              = NULL;
95a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
96f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
97cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
989566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
999566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1004a7a4c06SLawrence Mitchell 
1011411c6eeSJed Brown   *dm = v;
102a4121054SBarry Smith   PetscFunctionReturn(0);
103a4121054SBarry Smith }
104a4121054SBarry Smith 
10538221697SMatthew G. Knepley /*@
106bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
10738221697SMatthew G. Knepley 
108d083f849SBarry Smith   Collective
10938221697SMatthew G. Knepley 
11038221697SMatthew G. Knepley   Input Parameter:
111bb7acecfSBarry Smith . dm - The original `DM` object
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Output Parameter:
114bb7acecfSBarry Smith . newdm  - The new `DM` object
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Level: beginner
11738221697SMatthew G. Knepley 
1181cb8cacdSPatrick Sanan   Notes:
119bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
120bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
121bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1221bb6d2a8SBarry Smith 
123bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12489706ed2SPatrick Sanan 
125bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
126bb7acecfSBarry Smith 
127bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMSetType()`, `DMConvert()`
1281bb6d2a8SBarry Smith 
12938221697SMatthew G. Knepley @*/
1309371c9d4SSatish Balay PetscErrorCode DMClone(DM dm, DM *newdm) {
13138221697SMatthew G. Knepley   PetscSF  sf;
13238221697SMatthew G. Knepley   Vec      coords;
13338221697SMatthew G. Knepley   void    *ctx;
1346858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
13538221697SMatthew G. Knepley 
13638221697SMatthew G. Knepley   PetscFunctionBegin;
13738221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13838221697SMatthew G. Knepley   PetscValidPointer(newdm, 2);
1399566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1409566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
141ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
142ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
143c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1449566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1459566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1469566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1479566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1499566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
150dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1513f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1529566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1539566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1549566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1559566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1566858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1576858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
158be4c1c3eSMatthew G. Knepley       DM           ncdm;
159be4c1c3eSMatthew G. Knepley       PetscSection cs;
1605a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
161be4c1c3eSMatthew G. Knepley 
1626858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1639566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1649566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1655a0206caSToby Isaac       if (pEndMax >= 0) {
1666858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1676858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1689566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1696858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1706858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1719566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
172be4c1c3eSMatthew G. Knepley       }
173be4c1c3eSMatthew G. Knepley     }
1746858538eSMatthew G. Knepley   }
1759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1769566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1779566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
17838221697SMatthew G. Knepley   if (coords) {
1799566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18038221697SMatthew G. Knepley   } else {
1819566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1829566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18338221697SMatthew G. Knepley   }
1846858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1856858538eSMatthew G. Knepley   if (coords) {
1866858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1876858538eSMatthew G. Knepley   } else {
1886858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1896858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1906858538eSMatthew G. Knepley   }
19190b157c4SStefano Zampini   {
1924fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1936858538eSMatthew G. Knepley 
1944fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
1954fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
196c6b900c6SMatthew G. Knepley   }
19734aa8a36SMatthew G. Knepley   {
19834aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
19934aa8a36SMatthew G. Knepley 
2009566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2019566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20234aa8a36SMatthew G. Knepley   }
20338221697SMatthew G. Knepley   PetscFunctionReturn(0);
20438221697SMatthew G. Knepley }
20538221697SMatthew G. Knepley 
2069a42bb27SBarry Smith /*@C
207bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2089a42bb27SBarry Smith 
209d083f849SBarry Smith    Logically Collective on da
2109a42bb27SBarry Smith 
211147403d9SBarry Smith    Input Parameters:
2129a42bb27SBarry Smith +  da - initial distributed array
213bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2149a42bb27SBarry Smith 
2159a42bb27SBarry Smith    Options Database:
216147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2179a42bb27SBarry Smith 
2189a42bb27SBarry Smith    Level: intermediate
2199a42bb27SBarry Smith 
220bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
221bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2229a42bb27SBarry Smith @*/
2239371c9d4SSatish Balay PetscErrorCode DMSetVecType(DM da, VecType ctype) {
2249a42bb27SBarry Smith   PetscFunctionBegin;
2259a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2269566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2279566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
2289a42bb27SBarry Smith   PetscFunctionReturn(0);
2299a42bb27SBarry Smith }
2309a42bb27SBarry Smith 
231c0dedaeaSBarry Smith /*@C
232bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
233c0dedaeaSBarry Smith 
234d083f849SBarry Smith    Logically Collective on da
235c0dedaeaSBarry Smith 
236c0dedaeaSBarry Smith    Input Parameter:
237c0dedaeaSBarry Smith .  da - initial distributed array
238c0dedaeaSBarry Smith 
239c0dedaeaSBarry Smith    Output Parameter:
240c0dedaeaSBarry Smith .  ctype - the vector type
241c0dedaeaSBarry Smith 
242c0dedaeaSBarry Smith    Level: intermediate
243c0dedaeaSBarry Smith 
244db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
245c0dedaeaSBarry Smith @*/
2469371c9d4SSatish Balay PetscErrorCode DMGetVecType(DM da, VecType *ctype) {
247c0dedaeaSBarry Smith   PetscFunctionBegin;
248c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
249c0dedaeaSBarry Smith   *ctype = da->vectype;
250c0dedaeaSBarry Smith   PetscFunctionReturn(0);
251c0dedaeaSBarry Smith }
252c0dedaeaSBarry Smith 
2535f1ad066SMatthew G Knepley /*@
254bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley   Not collective
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley   Input Parameter:
259bb7acecfSBarry Smith . v - The `Vec`
2605f1ad066SMatthew G Knepley 
2615f1ad066SMatthew G Knepley   Output Parameter:
262bb7acecfSBarry Smith . dm - The `DM`
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   Level: intermediate
2655f1ad066SMatthew G Knepley 
266bb7acecfSBarry Smith   Note:
267bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
268bb7acecfSBarry Smith 
269bb7acecfSBarry Smith .seealso: `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2705f1ad066SMatthew G Knepley @*/
2719371c9d4SSatish Balay PetscErrorCode VecGetDM(Vec v, DM *dm) {
2725f1ad066SMatthew G Knepley   PetscFunctionBegin;
2735f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2745f1ad066SMatthew G Knepley   PetscValidPointer(dm, 2);
2759566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2765f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2775f1ad066SMatthew G Knepley }
2785f1ad066SMatthew G Knepley 
2795f1ad066SMatthew G Knepley /*@
280bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2815f1ad066SMatthew G Knepley 
2825f1ad066SMatthew G Knepley   Not collective
2835f1ad066SMatthew G Knepley 
2845f1ad066SMatthew G Knepley   Input Parameters:
285bb7acecfSBarry Smith + v - The `Vec`
286bb7acecfSBarry Smith - dm - The `DM`
2875f1ad066SMatthew G Knepley 
288bb7acecfSBarry Smith   Note:
289bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
290d9805387SMatthew G. Knepley 
291bb7acecfSBarry 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.
292bb7acecfSBarry Smith 
293bb7acecfSBarry Smith   Level: developer
2945f1ad066SMatthew G Knepley 
295db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2965f1ad066SMatthew G Knepley @*/
2979371c9d4SSatish Balay PetscErrorCode VecSetDM(Vec v, DM dm) {
2985f1ad066SMatthew G Knepley   PetscFunctionBegin;
2995f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
300d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3019566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3025f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
3035f1ad066SMatthew G Knepley }
3045f1ad066SMatthew G Knepley 
305521d9a4cSLisandro Dalcin /*@C
306bb7acecfSBarry Smith        DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3078f1509bcSBarry Smith 
308d083f849SBarry Smith    Logically Collective on dm
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith    Input Parameters:
311bb7acecfSBarry Smith +  dm - the `DM` context
3128f1509bcSBarry Smith -  ctype - the matrix type
3138f1509bcSBarry Smith 
3148f1509bcSBarry Smith    Options Database:
3158f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith    Level: intermediate
3188f1509bcSBarry Smith 
319db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
320bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3218f1509bcSBarry Smith @*/
3229371c9d4SSatish Balay PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype) {
3238f1509bcSBarry Smith   PetscFunctionBegin;
3248f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3258f1509bcSBarry Smith   dm->coloringtype = ctype;
3268f1509bcSBarry Smith   PetscFunctionReturn(0);
3278f1509bcSBarry Smith }
3288f1509bcSBarry Smith 
3298f1509bcSBarry Smith /*@C
330bb7acecfSBarry Smith        DMGetISColoringType - Gets the type of coloring,  `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
331521d9a4cSLisandro Dalcin 
332d083f849SBarry Smith    Logically Collective on dm
333521d9a4cSLisandro Dalcin 
334521d9a4cSLisandro Dalcin    Input Parameter:
335bb7acecfSBarry Smith .  dm - the `DM` context
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith    Output Parameter:
3388f1509bcSBarry Smith .  ctype - the matrix type
3398f1509bcSBarry Smith 
3408f1509bcSBarry Smith    Options Database:
3418f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3428f1509bcSBarry Smith 
3438f1509bcSBarry Smith    Level: intermediate
3448f1509bcSBarry Smith 
345db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
346bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3478f1509bcSBarry Smith @*/
3489371c9d4SSatish Balay PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype) {
3498f1509bcSBarry Smith   PetscFunctionBegin;
3508f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3518f1509bcSBarry Smith   *ctype = dm->coloringtype;
3528f1509bcSBarry Smith   PetscFunctionReturn(0);
3538f1509bcSBarry Smith }
3548f1509bcSBarry Smith 
3558f1509bcSBarry Smith /*@C
356bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3578f1509bcSBarry Smith 
358d083f849SBarry Smith    Logically Collective on dm
3598f1509bcSBarry Smith 
3608f1509bcSBarry Smith    Input Parameters:
361bb7acecfSBarry Smith +  dm - the `DM` context
362bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
363521d9a4cSLisandro Dalcin 
364521d9a4cSLisandro Dalcin    Options Database:
365bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
366521d9a4cSLisandro Dalcin 
367521d9a4cSLisandro Dalcin    Level: intermediate
368521d9a4cSLisandro Dalcin 
369bb7acecfSBarry Smith .seealso: `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
370521d9a4cSLisandro Dalcin @*/
3719371c9d4SSatish Balay PetscErrorCode DMSetMatType(DM dm, MatType ctype) {
372521d9a4cSLisandro Dalcin   PetscFunctionBegin;
373521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3749566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3759566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
376521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
377521d9a4cSLisandro Dalcin }
378521d9a4cSLisandro Dalcin 
379c0dedaeaSBarry Smith /*@C
380bb7acecfSBarry Smith        DMGetMatType - Gets the type of matrix created with `DMCreateMatrix()`
381c0dedaeaSBarry Smith 
382d083f849SBarry Smith    Logically Collective on dm
383c0dedaeaSBarry Smith 
384c0dedaeaSBarry Smith    Input Parameter:
385bb7acecfSBarry Smith .  dm - the `DM` context
386c0dedaeaSBarry Smith 
387c0dedaeaSBarry Smith    Output Parameter:
388c0dedaeaSBarry Smith .  ctype - the matrix type
389c0dedaeaSBarry Smith 
390c0dedaeaSBarry Smith    Level: intermediate
391c0dedaeaSBarry Smith 
392db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
393c0dedaeaSBarry Smith @*/
3949371c9d4SSatish Balay PetscErrorCode DMGetMatType(DM dm, MatType *ctype) {
395c0dedaeaSBarry Smith   PetscFunctionBegin;
396c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397c0dedaeaSBarry Smith   *ctype = dm->mattype;
398c0dedaeaSBarry Smith   PetscFunctionReturn(0);
399c0dedaeaSBarry Smith }
400c0dedaeaSBarry Smith 
401c688c046SMatthew G Knepley /*@
402bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
403c688c046SMatthew G Knepley 
404c688c046SMatthew G Knepley   Not collective
405c688c046SMatthew G Knepley 
406c688c046SMatthew G Knepley   Input Parameter:
407bb7acecfSBarry Smith . A - The `Mat`
408c688c046SMatthew G Knepley 
409c688c046SMatthew G Knepley   Output Parameter:
410bb7acecfSBarry Smith . dm - The `DM`
411c688c046SMatthew G Knepley 
412c688c046SMatthew G Knepley   Level: intermediate
413c688c046SMatthew G Knepley 
414bb7acecfSBarry Smith   Note:
415bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
416bb7acecfSBarry Smith 
417bb7acecfSBarry Smith   Developer Note:
418bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4198f1509bcSBarry Smith 
420db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
421c688c046SMatthew G Knepley @*/
4229371c9d4SSatish Balay PetscErrorCode MatGetDM(Mat A, DM *dm) {
423c688c046SMatthew G Knepley   PetscFunctionBegin;
424c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
425c688c046SMatthew G Knepley   PetscValidPointer(dm, 2);
4269566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
427c688c046SMatthew G Knepley   PetscFunctionReturn(0);
428c688c046SMatthew G Knepley }
429c688c046SMatthew G Knepley 
430c688c046SMatthew G Knepley /*@
431bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
432c688c046SMatthew G Knepley 
433c688c046SMatthew G Knepley   Not collective
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   Input Parameters:
436c688c046SMatthew G Knepley + A - The Mat
437c688c046SMatthew G Knepley - dm - The DM
438c688c046SMatthew G Knepley 
439bb7acecfSBarry Smith   Level: developer
440c688c046SMatthew G Knepley 
441bb7acecfSBarry Smith   Note:
442bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
443bb7acecfSBarry Smith 
444bb7acecfSBarry Smith   Developer Note:
445bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
446bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4478f1509bcSBarry Smith 
448db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
449c688c046SMatthew G Knepley @*/
4509371c9d4SSatish Balay PetscErrorCode MatSetDM(Mat A, DM dm) {
451c688c046SMatthew G Knepley   PetscFunctionBegin;
452c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4538865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4549566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
455c688c046SMatthew G Knepley   PetscFunctionReturn(0);
456c688c046SMatthew G Knepley }
457c688c046SMatthew G Knepley 
4589a42bb27SBarry Smith /*@C
459bb7acecfSBarry Smith    DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4609a42bb27SBarry Smith 
461d083f849SBarry Smith    Logically Collective on dm
4629a42bb27SBarry Smith 
463d8d19677SJose E. Roman    Input Parameters:
464bb7acecfSBarry Smith +  da - the `DM` context
465bb7acecfSBarry Smith -  prefix - the prefix to prepend
4669a42bb27SBarry Smith 
4679a42bb27SBarry Smith    Notes:
4689a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4699a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4709a42bb27SBarry Smith 
4719a42bb27SBarry Smith    Level: advanced
4729a42bb27SBarry Smith 
473bb7acecfSBarry Smith .seealso: `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4749a42bb27SBarry Smith @*/
4759371c9d4SSatish Balay PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[]) {
4769a42bb27SBarry Smith   PetscFunctionBegin;
4779a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4789566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4791baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4801baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
4819a42bb27SBarry Smith   PetscFunctionReturn(0);
4829a42bb27SBarry Smith }
4839a42bb27SBarry Smith 
48431697293SDave May /*@C
485bb7acecfSBarry Smith    DMAppendOptionsPrefix - Appends an additional string to an already exising prefix used for searching for
486bb7acecfSBarry Smith    `DM` options in the options database.
48731697293SDave May 
488d083f849SBarry Smith    Logically Collective on dm
48931697293SDave May 
49031697293SDave May    Input Parameters:
491bb7acecfSBarry Smith +  dm - the `DM` context
492bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
49331697293SDave May 
49431697293SDave May    Notes:
495bb7acecfSBarry 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.
49631697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49731697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49831697293SDave May 
49931697293SDave May    Level: advanced
50031697293SDave May 
501bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
50231697293SDave May @*/
5039371c9d4SSatish Balay PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[]) {
50431697293SDave May   PetscFunctionBegin;
50531697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5069566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
50731697293SDave May   PetscFunctionReturn(0);
50831697293SDave May }
50931697293SDave May 
51031697293SDave May /*@C
51131697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
512bb7acecfSBarry Smith    DM options in the options database.
51331697293SDave May 
51431697293SDave May    Not Collective
51531697293SDave May 
51631697293SDave May    Input Parameters:
517bb7acecfSBarry Smith .  dm - the `DM` context
51831697293SDave May 
51931697293SDave May    Output Parameters:
52031697293SDave May .  prefix - pointer to the prefix string used is returned
52131697293SDave May 
522bb7acecfSBarry Smith    Fortran Note:
52395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52431697293SDave May    sufficient length to hold the prefix.
52531697293SDave May 
52631697293SDave May    Level: advanced
52731697293SDave May 
528bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
52931697293SDave May @*/
5309371c9d4SSatish Balay PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[]) {
53131697293SDave May   PetscFunctionBegin;
53231697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5339566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
53431697293SDave May   PetscFunctionReturn(0);
53531697293SDave May }
53631697293SDave May 
5379371c9d4SSatish Balay static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct) {
5386eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
53988bdff64SToby Isaac 
54088bdff64SToby Isaac   PetscFunctionBegin;
541aab5bcd8SJed Brown   *ncrefct = 0;
54288bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54388bdff64SToby Isaac     refct--;
54488bdff64SToby Isaac     if (recurseCoarse) {
54588bdff64SToby Isaac       PetscInt coarseCount;
54688bdff64SToby Isaac 
5479566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
54888bdff64SToby Isaac       refct += coarseCount;
54988bdff64SToby Isaac     }
55088bdff64SToby Isaac   }
55188bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55288bdff64SToby Isaac     refct--;
55388bdff64SToby Isaac     if (recurseFine) {
55488bdff64SToby Isaac       PetscInt fineCount;
55588bdff64SToby Isaac 
5569566063dSJacob Faibussowitsch       PetscCall(DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
55788bdff64SToby Isaac       refct += fineCount;
55888bdff64SToby Isaac     }
55988bdff64SToby Isaac   }
56088bdff64SToby Isaac   *ncrefct = refct;
56188bdff64SToby Isaac   PetscFunctionReturn(0);
56288bdff64SToby Isaac }
56388bdff64SToby Isaac 
5649371c9d4SSatish Balay PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm) {
5655d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
566354557abSToby Isaac 
567354557abSToby Isaac   PetscFunctionBegin;
568354557abSToby Isaac   /* destroy the labels */
569354557abSToby Isaac   while (next) {
570354557abSToby Isaac     DMLabelLink tmp = next->next;
571354557abSToby Isaac 
5725d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
573ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
5749566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
5759566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
576354557abSToby Isaac     next = tmp;
577354557abSToby Isaac   }
5785d80c0bfSVaclav Hapla   dm->labels = NULL;
579354557abSToby Isaac   PetscFunctionReturn(0);
580354557abSToby Isaac }
581354557abSToby Isaac 
5829371c9d4SSatish Balay PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c) {
5836858538eSMatthew G. Knepley   PetscFunctionBegin;
5846858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
5856858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
5866858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
5876858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
5886858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
5896858538eSMatthew G. Knepley   PetscFunctionReturn(0);
5906858538eSMatthew G. Knepley }
5916858538eSMatthew G. Knepley 
5921fb7b255SJunchao Zhang /*@C
593bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
59447c6ae99SBarry Smith 
595d083f849SBarry Smith     Collective on dm
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith     Input Parameter:
598bb7acecfSBarry Smith .   dm - the `DM` object to destroy
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith     Level: developer
60147c6ae99SBarry Smith 
602bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
60347c6ae99SBarry Smith 
60447c6ae99SBarry Smith @*/
6059371c9d4SSatish Balay PetscErrorCode DMDestroy(DM *dm) {
6066eb26441SStefano Zampini   PetscInt       cnt;
607dfe15315SJed Brown   DMNamedVecLink nlink, nnext;
60847c6ae99SBarry Smith 
60947c6ae99SBarry Smith   PetscFunctionBegin;
6106bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6116bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
61287e657c6SBarry Smith 
61388bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
6149566063dSJacob Faibussowitsch   PetscCall(DMCountNonCyclicReferences(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
61588bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6169371c9d4SSatish Balay   if (--cnt > 0) {
6179371c9d4SSatish Balay     *dm = NULL;
6189371c9d4SSatish Balay     PetscFunctionReturn(0);
6199371c9d4SSatish Balay   }
6206bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6216bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6226eb26441SStefano Zampini 
6239566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6249566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6256eb26441SStefano Zampini 
626f490541aSPeter Brune   nnext              = (*dm)->namedglobal;
6270298fd71SBarry Smith   (*dm)->namedglobal = NULL;
628f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named vectors */
6292348bcf4SPeter Brune     nnext = nlink->next;
6307a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6319566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6329566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6339566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6342348bcf4SPeter Brune   }
635f490541aSPeter Brune   nnext             = (*dm)->namedlocal;
6360298fd71SBarry Smith   (*dm)->namedlocal = NULL;
637f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named local vectors */
638f490541aSPeter Brune     nnext = nlink->next;
6397a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6409566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6419566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6429566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
643f490541aSPeter Brune   }
6442348bcf4SPeter Brune 
645b17ce1afSJed Brown   /* Destroy the list of hooks */
646c833c3b5SJed Brown   {
647c833c3b5SJed Brown     DMCoarsenHookLink link, next;
648b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
649b17ce1afSJed Brown       next = link->next;
6509566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
651b17ce1afSJed Brown     }
6520298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
653c833c3b5SJed Brown   }
654c833c3b5SJed Brown   {
655c833c3b5SJed Brown     DMRefineHookLink link, next;
656c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
657c833c3b5SJed Brown       next = link->next;
6589566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
659c833c3b5SJed Brown     }
6600298fd71SBarry Smith     (*dm)->refinehook = NULL;
661c833c3b5SJed Brown   }
662be081cd6SPeter Brune   {
663be081cd6SPeter Brune     DMSubDomainHookLink link, next;
664be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
665be081cd6SPeter Brune       next = link->next;
6669566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
667be081cd6SPeter Brune     }
6680298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
669be081cd6SPeter Brune   }
670baf369e7SPeter Brune   {
671baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
672baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
673baf369e7SPeter Brune       next = link->next;
6749566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
675baf369e7SPeter Brune     }
6760298fd71SBarry Smith     (*dm)->gtolhook = NULL;
677baf369e7SPeter Brune   }
678d4d07f1eSToby Isaac   {
679d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
680d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
681d4d07f1eSToby Isaac       next = link->next;
6829566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
683d4d07f1eSToby Isaac     }
684d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
685d4d07f1eSToby Isaac   }
686aa1993deSMatthew G Knepley   /* Destroy the work arrays */
687aa1993deSMatthew G Knepley   {
688aa1993deSMatthew G Knepley     DMWorkLink link, next;
6897a8be351SBarry Smith     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
690aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
691aa1993deSMatthew G Knepley       next = link->next;
6929566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
6939566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
694aa1993deSMatthew G Knepley     }
6950298fd71SBarry Smith     (*dm)->workin = NULL;
696aa1993deSMatthew G Knepley   }
697c58f1c22SToby Isaac   /* destroy the labels */
6989566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
699f4cdcedcSVaclav Hapla   /* destroy the fields */
7009566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
701f4cdcedcSVaclav Hapla   /* destroy the boundaries */
702e6f8dbb6SToby Isaac   {
703e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
704e6f8dbb6SToby Isaac     while (next) {
705e6f8dbb6SToby Isaac       DMBoundary b = next;
706e6f8dbb6SToby Isaac 
707e6f8dbb6SToby Isaac       next = b->next;
7089566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
709e6f8dbb6SToby Isaac     }
710e6f8dbb6SToby Isaac   }
711b17ce1afSJed Brown 
7129566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7139566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7149566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
71552536dc3SBarry Smith 
716*48a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7179566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7189566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7199566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7209566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
72188ed4aceSMatthew G Knepley 
7229566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7239566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7249566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7259566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7269566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7279566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7289566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
729736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
730*48a46eb9SPierre Jolivet     if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7319566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
732736995cdSBlaise Bourdin   }
7339a2a23afSMatthew G. Knepley   {
7349a2a23afSMatthew G. Knepley     Vec     *auxData;
7359a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7369a2a23afSMatthew G. Knepley 
7379566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7389566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7399566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7409566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7419566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7429566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7439a2a23afSMatthew G. Knepley   }
744*48a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7456eb26441SStefano Zampini 
7469566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
747*48a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7489566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7494fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7519566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7526858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7536858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7549566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7559566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7569566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7576636e97aSMatthew G Knepley 
7589566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7599566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
760e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7619566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
762732e2eb9SMatthew G Knepley 
763*48a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7649566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
765f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7669566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7679566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
768f918ec44SMatthew G. Knepley #endif
769435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7709566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
77147c6ae99SBarry Smith   PetscFunctionReturn(0);
77247c6ae99SBarry Smith }
77347c6ae99SBarry Smith 
774d7bf68aeSBarry Smith /*@
775bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
776d7bf68aeSBarry Smith 
777d083f849SBarry Smith     Collective on dm
778d7bf68aeSBarry Smith 
779d7bf68aeSBarry Smith     Input Parameter:
780bb7acecfSBarry Smith .   dm - the `DM` object to setup
781d7bf68aeSBarry Smith 
782bb7acecfSBarry Smith     Level: intermediate
783d7bf68aeSBarry Smith 
784bb7acecfSBarry Smith     Note:
785bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
786bb7acecfSBarry Smith 
787bb7acecfSBarry Smith .seealso: `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
788d7bf68aeSBarry Smith 
789d7bf68aeSBarry Smith @*/
7909371c9d4SSatish Balay PetscErrorCode DMSetUp(DM dm) {
791d7bf68aeSBarry Smith   PetscFunctionBegin;
792171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7938387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
794dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
7958387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
796d7bf68aeSBarry Smith   PetscFunctionReturn(0);
797d7bf68aeSBarry Smith }
798d7bf68aeSBarry Smith 
799d7bf68aeSBarry Smith /*@
800bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
801d7bf68aeSBarry Smith 
802d083f849SBarry Smith     Collective on dm
803d7bf68aeSBarry Smith 
804d7bf68aeSBarry Smith     Input Parameter:
805bb7acecfSBarry Smith .   dm - the `DM` object to set options for
806d7bf68aeSBarry Smith 
807732e2eb9SMatthew G Knepley     Options Database:
808bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
809bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
810bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
811a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
812bb7acecfSBarry 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`
813732e2eb9SMatthew G Knepley 
8149318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8159318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8169318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
817cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
818bb7acecfSBarry Smith . -dm_plex_shape <shape>            - The domain shape, such as `DM_SHAPE_BOX`, `DM_SHAPE_SPHERE`, etc.
8199318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8209318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8219318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
822bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
823bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8249318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8259318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8269318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8279318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
828bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType `for each direction
8299318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8309318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8319318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8329318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
833bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8349318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8359318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8369318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8379318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
838bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
839d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
840d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
841d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
842d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
843d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
844909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
845909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8469318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8479318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8489318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8499318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8509318fe57SMatthew G. Knepley 
851384a6580SVaclav Hapla     DMPLEX Specific Checks
852bb7acecfSBarry Smith +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
853bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
854bb7acecfSBarry 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()`
855bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
856bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
857bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
858384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
859d7bf68aeSBarry Smith 
86095eb5ee5SVaclav Hapla     Level: intermediate
86195eb5ee5SVaclav Hapla 
862bb7acecfSBarry Smith .seealso: `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
863bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
864bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
865d7bf68aeSBarry Smith 
866d7bf68aeSBarry Smith @*/
8679371c9d4SSatish Balay PetscErrorCode DMSetFromOptions(DM dm) {
8687781c08eSBarry Smith   char      typeName[256];
869ca266f36SBarry Smith   PetscBool flg;
870d7bf68aeSBarry Smith 
871d7bf68aeSBarry Smith   PetscFunctionBegin;
872171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8749566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8759566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
876d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
8789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
8791baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
8809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
8811baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
8829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
8839566063dSJacob 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));
884dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
885f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
886dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
887d0609cedSBarry Smith   PetscOptionsEnd();
888d7bf68aeSBarry Smith   PetscFunctionReturn(0);
889d7bf68aeSBarry Smith }
890d7bf68aeSBarry Smith 
891fc9bc008SSatish Balay /*@C
892bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
893fe2efc57SMark 
894bb7acecfSBarry Smith    Collective on dm
895fe2efc57SMark 
896fe2efc57SMark    Input Parameters:
897bb7acecfSBarry Smith +  dm - the `DM` object
898bb7acecfSBarry Smith .  obj - optional object that provides the prefix for the options database (if NULL then the prefix in obj is used)
899bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
900fe2efc57SMark 
901fe2efc57SMark    Level: intermediate
902bb7acecfSBarry Smith 
903bb7acecfSBarry Smith    Note:
904bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
905bb7acecfSBarry Smith 
906bb7acecfSBarry Smith .seealso: `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
907fe2efc57SMark @*/
9089371c9d4SSatish Balay PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[]) {
909fe2efc57SMark   PetscFunctionBegin;
910fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9119566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
912fe2efc57SMark   PetscFunctionReturn(0);
913fe2efc57SMark }
914fe2efc57SMark 
915fe2efc57SMark /*@C
916bb7acecfSBarry 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
917bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
91847c6ae99SBarry Smith 
919d083f849SBarry Smith     Collective on dm
92047c6ae99SBarry Smith 
921d8d19677SJose E. Roman     Input Parameters:
922bb7acecfSBarry Smith +   dm - the `DM` object to view
92347c6ae99SBarry Smith -   v - the viewer
92447c6ae99SBarry Smith 
925cd7e8a5eSksagiyam     Notes:
926bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
927bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
928bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
929cd7e8a5eSksagiyam 
930224748a4SBarry Smith     Level: beginner
93147c6ae99SBarry Smith 
932bb7acecfSBarry Smith .seealso: `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat`(), `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith @*/
9359371c9d4SSatish Balay PetscErrorCode DMView(DM dm, PetscViewer v) {
93632c0f0efSBarry Smith   PetscBool         isbinary;
93776a8abe0SBarry Smith   PetscMPIInt       size;
93876a8abe0SBarry Smith   PetscViewerFormat format;
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith   PetscFunctionBegin;
941171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
942*48a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
943b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
94474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
94574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
94674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
94774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
94874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
94974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
95074903a4fSStefano Zampini      in an error here */
95174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9529566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
953b1b135c8SBarry Smith 
9549566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
95676a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9579566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9589566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
95932c0f0efSBarry Smith   if (isbinary) {
96055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
96132c0f0efSBarry Smith     char     type[256];
96232c0f0efSBarry Smith 
9639566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
9649566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, 256));
9659566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
96632c0f0efSBarry Smith   }
967dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
96847c6ae99SBarry Smith   PetscFunctionReturn(0);
96947c6ae99SBarry Smith }
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith /*@
972bb7acecfSBarry 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,
973bb7acecfSBarry Smith     that is it has no ghost locations.
97447c6ae99SBarry Smith 
975d083f849SBarry Smith     Collective on dm
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith     Input Parameter:
978bb7acecfSBarry Smith .   dm - the `DM` object
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith     Output Parameter:
98147c6ae99SBarry Smith .   vec - the global vector
98247c6ae99SBarry Smith 
983073dac72SJed Brown     Level: beginner
98447c6ae99SBarry Smith 
985bb7acecfSBarry Smith .seealso: `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
986bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
98747c6ae99SBarry Smith 
98847c6ae99SBarry Smith @*/
9899371c9d4SSatish Balay PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec) {
99047c6ae99SBarry Smith   PetscFunctionBegin;
991171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
992b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
993dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
99476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
995c6b011d8SStefano Zampini     DM vdm;
996c6b011d8SStefano Zampini 
9979566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
9987a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
999c6b011d8SStefano Zampini   }
100047c6ae99SBarry Smith   PetscFunctionReturn(0);
100147c6ae99SBarry Smith }
100247c6ae99SBarry Smith 
100347c6ae99SBarry Smith /*@
1004bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith     Not Collective
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith     Input Parameter:
1009bb7acecfSBarry Smith .   dm - the `DM` object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Output Parameter:
101247c6ae99SBarry Smith .   vec - the local vector
101347c6ae99SBarry Smith 
1014073dac72SJed Brown     Level: beginner
101547c6ae99SBarry Smith 
1016bb7acecfSBarry Smith     Notes:
1017bb7acecfSBarry 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.
1018bb7acecfSBarry Smith 
1019bb7acecfSBarry Smith  .seealso: `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1020bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
102147c6ae99SBarry Smith 
102247c6ae99SBarry Smith @*/
10239371c9d4SSatish Balay PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec) {
102447c6ae99SBarry Smith   PetscFunctionBegin;
1025171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1026b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1027dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, 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_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1033c6b011d8SStefano Zampini   }
103447c6ae99SBarry Smith   PetscFunctionReturn(0);
103547c6ae99SBarry Smith }
103647c6ae99SBarry Smith 
10371411c6eeSJed Brown /*@
1038bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10391411c6eeSJed Brown 
1040d083f849SBarry Smith    Collective on dm
10411411c6eeSJed Brown 
10421411c6eeSJed Brown    Input Parameter:
1043bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10441411c6eeSJed Brown 
10451411c6eeSJed Brown    Output Parameter:
10461411c6eeSJed Brown .  ltog - the mapping
10471411c6eeSJed Brown 
1048bb7acecfSBarry Smith    Level: advanced
10491411c6eeSJed Brown 
10501411c6eeSJed Brown    Notes:
1051bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10521411c6eeSJed Brown 
1053bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1054bb7acecfSBarry Smith    need to use this function with those objects.
1055bb7acecfSBarry Smith 
1056bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1057bb7acecfSBarry Smith 
1058bb7acecfSBarry Smith .seealso: `DMCreateLocalVector()`,  `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1059bb7acecfSBarry Smith           `DMCreateMatrix()`
10601411c6eeSJed Brown @*/
10619371c9d4SSatish Balay PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog) {
10620be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10631411c6eeSJed Brown 
10641411c6eeSJed Brown   PetscFunctionBegin;
10651411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10661411c6eeSJed Brown   PetscValidPointer(ltog, 2);
10671411c6eeSJed Brown   if (!dm->ltogmap) {
106837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
106937d0c07bSMatthew G Knepley 
10709566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
107137d0c07bSMatthew G Knepley     if (section) {
1072a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
107337d0c07bSMatthew G Knepley       PetscInt       *ltog;
1074ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
107537d0c07bSMatthew G Knepley 
10769566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
10779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
10789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
10799566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
108037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1081e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
108237d0c07bSMatthew G Knepley 
108337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
10849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
10859566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
10869566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
10879566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
10881a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10891a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
10909371c9d4SSatish Balay         if (dof) { bs = bs < 0 ? bdof : PetscGCD(bs, bdof); }
10915227eafbSStefano Zampini 
1092e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
10935227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1094e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1095e6befd46SJed Brown             cind++;
1096e6befd46SJed Brown           } else {
10975227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1098e6befd46SJed Brown           }
109937d0c07bSMatthew G Knepley         }
110037d0c07bSMatthew G Knepley       }
1101bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11029371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11039371c9d4SSatish Balay       bsLocal[1] = bs;
11049566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11059371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11069371c9d4SSatish Balay         bs = 1;
11079371c9d4SSatish Balay       } else {
11089371c9d4SSatish Balay         bs = bsMinMax[0];
11099371c9d4SSatish Balay       }
11107591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11117591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1112ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1113ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1114ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1115ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1116ca469d19SJed Brown         }
1117ccf3bd66SMatthew G. Knepley         n /= bs;
1118ccf3bd66SMatthew G. Knepley       }
11199566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
11209566063dSJacob Faibussowitsch       PetscCall(PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap));
1121dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
112237d0c07bSMatthew G Knepley   }
11231411c6eeSJed Brown   *ltog = dm->ltogmap;
11241411c6eeSJed Brown   PetscFunctionReturn(0);
11251411c6eeSJed Brown }
11261411c6eeSJed Brown 
11271411c6eeSJed Brown /*@
1128bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11291411c6eeSJed Brown 
11301411c6eeSJed Brown    Not Collective
11311411c6eeSJed Brown 
11321411c6eeSJed Brown    Input Parameter:
1133bb7acecfSBarry Smith .  dm - the `DM` with block structure
11341411c6eeSJed Brown 
11351411c6eeSJed Brown    Output Parameter:
11361411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11371411c6eeSJed Brown 
11381411c6eeSJed Brown    Level: intermediate
11391411c6eeSJed Brown 
1140bb7acecfSBarry Smith    Note:
1141bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1142bb7acecfSBarry Smith 
1143bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1144bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1145bb7acecfSBarry Smith 
1146db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11471411c6eeSJed Brown @*/
11489371c9d4SSatish Balay PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs) {
11491411c6eeSJed Brown   PetscFunctionBegin;
11501411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1151534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11527a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11531411c6eeSJed Brown   *bs = dm->bs;
11541411c6eeSJed Brown   PetscFunctionReturn(0);
11551411c6eeSJed Brown }
11561411c6eeSJed Brown 
115748eeb7c8SBarry Smith /*@C
1158bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1159bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
116047c6ae99SBarry Smith 
1161a5bc1bf3SBarry Smith     Collective on dmc
116247c6ae99SBarry Smith 
1163d8d19677SJose E. Roman     Input Parameters:
1164bb7acecfSBarry Smith +   dmc - the `DM` object
1165bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
116647c6ae99SBarry Smith 
1167d8d19677SJose E. Roman     Output Parameters:
116847c6ae99SBarry Smith +  mat - the interpolation
1169bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
117047c6ae99SBarry Smith 
117147c6ae99SBarry Smith     Level: developer
117247c6ae99SBarry Smith 
117395452b02SPatrick Sanan     Notes:
1174bb7acecfSBarry 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
1175bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1176d52bd9f3SBarry Smith 
1177bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1178bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
117985afcc9aSBarry Smith 
1180bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
118147c6ae99SBarry Smith 
118247c6ae99SBarry Smith @*/
11839371c9d4SSatish Balay PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec) {
118447c6ae99SBarry Smith   PetscFunctionBegin;
1185a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1186a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1187c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
11889566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1189dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
11909566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
119147c6ae99SBarry Smith   PetscFunctionReturn(0);
119247c6ae99SBarry Smith }
119347c6ae99SBarry Smith 
11943ad4599aSBarry Smith /*@
1195bb7acecfSBarry 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`.
1196bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1197bb7acecfSBarry Smith     representation of xfine.
11982ed6491fSPatrick Sanan 
11992ed6491fSPatrick Sanan   Input Parameters:
1200bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1201bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12022ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12032ed6491fSPatrick Sanan 
12042ed6491fSPatrick Sanan   Output Parameter:
12052ed6491fSPatrick Sanan .    scale - the scaled vector
12062ed6491fSPatrick Sanan 
1207bb7acecfSBarry Smith   Level: advanced
12082ed6491fSPatrick Sanan 
1209e9c74fd6SRichard Tran Mills   Developer Notes:
1210bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1211e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1212e9c74fd6SRichard Tran Mills 
1213bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12142ed6491fSPatrick Sanan 
12152ed6491fSPatrick Sanan @*/
12169371c9d4SSatish Balay PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale) {
12172ed6491fSPatrick Sanan   Vec         fine;
12182ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12199704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1220e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12219704db99SRichard Tran Mills #endif
12222ed6491fSPatrick Sanan 
12232ed6491fSPatrick Sanan   PetscFunctionBegin;
12249566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12259566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12269566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12279704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12289704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12299704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12309704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12319566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1232e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12339566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12349566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12359566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
123683aa49f4SRichard Tran Mills   }
12379704db99SRichard Tran Mills #endif
12389566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12399566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12409566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12412ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12422ed6491fSPatrick Sanan }
12432ed6491fSPatrick Sanan 
12442ed6491fSPatrick Sanan /*@
1245bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1246bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12473ad4599aSBarry Smith 
1248a5bc1bf3SBarry Smith     Collective on dmc
12493ad4599aSBarry Smith 
1250d8d19677SJose E. Roman     Input Parameters:
1251bb7acecfSBarry Smith +   dmc - the `DM` object
1252bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12533ad4599aSBarry Smith 
12543ad4599aSBarry Smith     Output Parameter:
12553ad4599aSBarry Smith .  mat - the restriction
12563ad4599aSBarry Smith 
12573ad4599aSBarry Smith     Level: developer
12583ad4599aSBarry Smith 
1259bb7acecfSBarry Smith     Note:
1260bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1261bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12623ad4599aSBarry Smith 
1263bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12643ad4599aSBarry Smith 
12653ad4599aSBarry Smith @*/
12669371c9d4SSatish Balay PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat) {
12673ad4599aSBarry Smith   PetscFunctionBegin;
1268a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1269a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12705a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
12719566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1272dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
12739566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
12743ad4599aSBarry Smith   PetscFunctionReturn(0);
12753ad4599aSBarry Smith }
12763ad4599aSBarry Smith 
127747c6ae99SBarry Smith /*@
1278bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1279bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1280bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1281bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
128247c6ae99SBarry Smith 
1283a5bc1bf3SBarry Smith     Collective on dac
128447c6ae99SBarry Smith 
1285d8d19677SJose E. Roman     Input Parameters:
1286bb7acecfSBarry Smith +   dac - the `DM` object
1287bb7acecfSBarry Smith -   daf - the second, finer `DM` object
128847c6ae99SBarry Smith 
128947c6ae99SBarry Smith     Output Parameter:
12906dbf9973SLawrence Mitchell .   mat - the injection
129147c6ae99SBarry Smith 
129247c6ae99SBarry Smith     Level: developer
129347c6ae99SBarry Smith 
1294bb7acecfSBarry Smith    Note:
1295bb7acecfSBarry 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
1296bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
129785afcc9aSBarry Smith 
1298bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1299bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
130047c6ae99SBarry Smith 
130147c6ae99SBarry Smith @*/
13029371c9d4SSatish Balay PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat) {
130347c6ae99SBarry Smith   PetscFunctionBegin;
1304a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1305a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13065a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13079566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1308dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
131047c6ae99SBarry Smith   PetscFunctionReturn(0);
131147c6ae99SBarry Smith }
131247c6ae99SBarry Smith 
1313b412c318SBarry Smith /*@
1314bb7acecfSBarry 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
1315bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1316bd041c0cSMatthew G. Knepley 
1317a5bc1bf3SBarry Smith   Collective on dac
1318bd041c0cSMatthew G. Knepley 
1319d8d19677SJose E. Roman   Input Parameters:
1320bb7acecfSBarry Smith + dmc - the target `DM` object
1321bb7acecfSBarry Smith - dmf - the source `DM` object
1322bd041c0cSMatthew G. Knepley 
1323bd041c0cSMatthew G. Knepley   Output Parameter:
1324b4937a87SMatthew G. Knepley . mat - the mass matrix
1325bd041c0cSMatthew G. Knepley 
1326bd041c0cSMatthew G. Knepley   Level: developer
1327bd041c0cSMatthew G. Knepley 
1328bb7acecfSBarry Smith   Notes:
1329bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1330bb7acecfSBarry Smith 
1331bb7acecfSBarry 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()`
1332bb7acecfSBarry Smith 
1333bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1334bd041c0cSMatthew G. Knepley @*/
13359371c9d4SSatish Balay PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat) {
1336bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1337b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1338b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13395a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13405b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1341dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13425b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
1343b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1344b4937a87SMatthew G. Knepley }
1345b4937a87SMatthew G. Knepley 
1346b4937a87SMatthew G. Knepley /*@
1347bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1348b4937a87SMatthew G. Knepley 
1349b4937a87SMatthew G. Knepley   Collective on dm
1350b4937a87SMatthew G. Knepley 
1351b4937a87SMatthew G. Knepley   Input Parameter:
1352bb7acecfSBarry Smith . dm - the `DM` object
1353b4937a87SMatthew G. Knepley 
1354b4937a87SMatthew G. Knepley   Output Parameter:
1355bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1356b4937a87SMatthew G. Knepley 
1357b4937a87SMatthew G. Knepley   Level: developer
1358b4937a87SMatthew G. Knepley 
1359bb7acecfSBarry Smith   Note:
1360bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1361bb7acecfSBarry Smith 
1362bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1363b4937a87SMatthew G. Knepley @*/
13649371c9d4SSatish Balay PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm) {
1365b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1366b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1367b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1368dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
1369bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1370bd041c0cSMatthew G. Knepley }
1371bd041c0cSMatthew G. Knepley 
1372bd041c0cSMatthew G. Knepley /*@
1373bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1374bb7acecfSBarry Smith     of a PDE on the `DM`.
137547c6ae99SBarry Smith 
1376d083f849SBarry Smith     Collective on dm
137747c6ae99SBarry Smith 
1378d8d19677SJose E. Roman     Input Parameters:
1379bb7acecfSBarry Smith +   dm - the `DM` object
1380bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
138147c6ae99SBarry Smith 
138247c6ae99SBarry Smith     Output Parameter:
138347c6ae99SBarry Smith .   coloring - the coloring
138447c6ae99SBarry Smith 
1385ec5066bdSBarry Smith     Notes:
1386bb7acecfSBarry 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
1387bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1388ec5066bdSBarry Smith 
1389bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
1390ec5066bdSBarry Smith 
139147c6ae99SBarry Smith     Level: developer
139247c6ae99SBarry Smith 
1393bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
139447c6ae99SBarry Smith 
1395aab9d709SJed Brown @*/
13969371c9d4SSatish Balay PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring) {
139747c6ae99SBarry Smith   PetscFunctionBegin;
1398171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13995a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1400dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
140147c6ae99SBarry Smith   PetscFunctionReturn(0);
140247c6ae99SBarry Smith }
140347c6ae99SBarry Smith 
1404b412c318SBarry Smith /*@
1405bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
140647c6ae99SBarry Smith 
1407d083f849SBarry Smith     Collective on dm
140847c6ae99SBarry Smith 
140947c6ae99SBarry Smith     Input Parameter:
1410bb7acecfSBarry Smith .   dm - the `DM` object
141147c6ae99SBarry Smith 
141247c6ae99SBarry Smith     Output Parameter:
141347c6ae99SBarry Smith .   mat - the empty Jacobian
141447c6ae99SBarry Smith 
1415073dac72SJed Brown     Level: beginner
141647c6ae99SBarry Smith 
1417f27dd7c6SMatthew G. Knepley     Options Database Keys:
1418bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1419f27dd7c6SMatthew G. Knepley 
142095452b02SPatrick Sanan     Notes:
142195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
142294013140SBarry Smith     do not need to do it yourself.
142394013140SBarry Smith 
142494013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1425bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
142694013140SBarry Smith 
1427bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
142894013140SBarry Smith     internally by PETSc.
142994013140SBarry Smith 
1430bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1431bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
143294013140SBarry Smith 
1433bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
143447c6ae99SBarry Smith 
1435aab9d709SJed Brown @*/
14369371c9d4SSatish Balay PetscErrorCode DMCreateMatrix(DM dm, Mat *mat) {
143747c6ae99SBarry Smith   PetscFunctionBegin;
1438171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1439064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14409566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14419566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1442dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
144376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1444c6b011d8SStefano Zampini     DM mdm;
1445c6b011d8SStefano Zampini 
14469566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14477a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1448c6b011d8SStefano Zampini   }
1449e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1450e5e52638SMatthew G. Knepley   if (dm->Nf) {
1451e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1452649ef022SMatthew Knepley     PetscInt     Nf, f;
1453e571a35bSMatthew G. Knepley 
14549566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1455649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1456649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14579566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14589566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14599566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1460649ef022SMatthew Knepley         break;
1461e571a35bSMatthew G. Knepley       }
1462649ef022SMatthew Knepley     }
1463649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1464649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14659566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14669566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
14679566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1468e571a35bSMatthew G. Knepley       }
1469e571a35bSMatthew G. Knepley     }
1470e571a35bSMatthew G. Knepley   }
14719566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
147247c6ae99SBarry Smith   PetscFunctionReturn(0);
147347c6ae99SBarry Smith }
147447c6ae99SBarry Smith 
1475732e2eb9SMatthew G Knepley /*@
1476bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1477bb7acecfSBarry 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
1478bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1479aa0f6e3cSJed Brown 
1480aa0f6e3cSJed Brown   Logically Collective on dm
1481aa0f6e3cSJed Brown 
1482aa0f6e3cSJed Brown   Input Parameters:
1483bb7acecfSBarry Smith + dm - the `DM`
1484bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1485aa0f6e3cSJed Brown 
1486aa0f6e3cSJed Brown   Level: developer
1487aa0f6e3cSJed Brown 
1488bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1489aa0f6e3cSJed Brown @*/
14909371c9d4SSatish Balay PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip) {
1491aa0f6e3cSJed Brown   PetscFunctionBegin;
1492aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1493aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1494aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1495aa0f6e3cSJed Brown }
1496aa0f6e3cSJed Brown 
1497aa0f6e3cSJed Brown /*@
1498bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1499732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1500732e2eb9SMatthew G Knepley 
1501d083f849SBarry Smith   Logically Collective on dm
1502732e2eb9SMatthew G Knepley 
1503d8d19677SJose E. Roman   Input Parameters:
1504bb7acecfSBarry Smith + dm - the `DM`
1505bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1506732e2eb9SMatthew G Knepley 
1507732e2eb9SMatthew G Knepley   Level: developer
1508f27dd7c6SMatthew G. Knepley 
1509f27dd7c6SMatthew G. Knepley   Options Database Keys:
1510bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1511f27dd7c6SMatthew G. Knepley 
1512bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1513732e2eb9SMatthew G Knepley @*/
15149371c9d4SSatish Balay PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only) {
1515732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1516732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1517732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1518732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1519732e2eb9SMatthew G Knepley }
1520732e2eb9SMatthew G Knepley 
1521b06ff27eSHong Zhang /*@
1522bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1523bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1524b06ff27eSHong Zhang 
1525d083f849SBarry Smith   Logically Collective on dm
1526b06ff27eSHong Zhang 
1527d8d19677SJose E. Roman   Input Parameters:
1528bb7acecfSBarry Smith + dm - the `DM`
1529bb7acecfSBarry Smith - only - `PETSC_TRUE` if you only want matrix stucture
1530b06ff27eSHong Zhang 
1531b06ff27eSHong Zhang   Level: developer
1532bb7acecfSBarry Smith 
1533bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1534b06ff27eSHong Zhang @*/
15359371c9d4SSatish Balay PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only) {
1536b06ff27eSHong Zhang   PetscFunctionBegin;
1537b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1538b06ff27eSHong Zhang   dm->structure_only = only;
1539b06ff27eSHong Zhang   PetscFunctionReturn(0);
1540b06ff27eSHong Zhang }
1541b06ff27eSHong Zhang 
1542a89ea682SMatthew G Knepley /*@C
1543bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1544a89ea682SMatthew G Knepley 
1545a89ea682SMatthew G Knepley   Not Collective
1546a89ea682SMatthew G Knepley 
1547a89ea682SMatthew G Knepley   Input Parameters:
1548bb7acecfSBarry Smith + dm - the `DM` object
1549a5b23f4aSJose E. Roman . count - The minimum size
1550bb7acecfSBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, or MPIU_INT)
1551a89ea682SMatthew G Knepley 
1552a89ea682SMatthew G Knepley   Output Parameter:
1553a89ea682SMatthew G Knepley . array - the work array
1554a89ea682SMatthew G Knepley 
1555a89ea682SMatthew G Knepley   Level: developer
1556a89ea682SMatthew G Knepley 
1557bb7acecfSBarry Smith   Note:
1558bb7acecfSBarry Smith   A `DM` may stash the array between instantations so using this routine may be more efficient than calling `PetscMalloc()`
1559bb7acecfSBarry Smith 
1560bb7acecfSBarry Smith   The array may contain nonzero values
1561bb7acecfSBarry Smith 
1562bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1563a89ea682SMatthew G Knepley @*/
15649371c9d4SSatish Balay PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem) {
1565aa1993deSMatthew G Knepley   DMWorkLink  link;
156669291d52SBarry Smith   PetscMPIInt dsize;
1567a89ea682SMatthew G Knepley 
1568a89ea682SMatthew G Knepley   PetscFunctionBegin;
1569a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1570aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1571aa1993deSMatthew G Knepley   if (dm->workin) {
1572aa1993deSMatthew G Knepley     link       = dm->workin;
1573aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1574aa1993deSMatthew G Knepley   } else {
15759566063dSJacob Faibussowitsch     PetscCall(PetscNewLog(dm, &link));
1576a89ea682SMatthew G Knepley   }
15779566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype, &dsize));
15785056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
15799566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
15809566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1581854ce69bSBarry Smith     link->bytes = dsize * count;
1582aa1993deSMatthew G Knepley   }
1583aa1993deSMatthew G Knepley   link->next  = dm->workout;
1584aa1993deSMatthew G Knepley   dm->workout = link;
1585cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
158600d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
158700d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
158800d952a4SJed Brown #endif
1589aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
1590a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1591a89ea682SMatthew G Knepley }
1592a89ea682SMatthew G Knepley 
1593aa1993deSMatthew G Knepley /*@C
1594bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1595aa1993deSMatthew G Knepley 
1596aa1993deSMatthew G Knepley   Not Collective
1597aa1993deSMatthew G Knepley 
1598aa1993deSMatthew G Knepley   Input Parameters:
1599bb7acecfSBarry Smith + dm - the `DM` object
1600a5b23f4aSJose E. Roman . count - The minimum size
160169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1602aa1993deSMatthew G Knepley 
1603aa1993deSMatthew G Knepley   Output Parameter:
1604aa1993deSMatthew G Knepley . array - the work array
1605aa1993deSMatthew G Knepley 
1606aa1993deSMatthew G Knepley   Level: developer
1607aa1993deSMatthew G Knepley 
160895452b02SPatrick Sanan   Developer Notes:
1609bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1610147403d9SBarry Smith 
1611bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1612aa1993deSMatthew G Knepley @*/
16139371c9d4SSatish Balay PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem) {
1614aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1615aa1993deSMatthew G Knepley 
1616aa1993deSMatthew G Knepley   PetscFunctionBegin;
1617aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1618aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1619aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1620aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1621aa1993deSMatthew G Knepley       *p            = link->next;
1622aa1993deSMatthew G Knepley       link->next    = dm->workin;
1623aa1993deSMatthew G Knepley       dm->workin    = link;
16240298fd71SBarry Smith       *(void **)mem = NULL;
1625aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1626aa1993deSMatthew G Knepley     }
1627aa1993deSMatthew G Knepley   }
1628aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1629aa1993deSMatthew G Knepley }
1630e7c4fc90SDmitry Karpeev 
16318cda7954SMatthew G. Knepley /*@C
1632bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1633bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
16348cda7954SMatthew G. Knepley 
1635bb7acecfSBarry Smith   Logically collective on dm
16368cda7954SMatthew G. Knepley 
16378cda7954SMatthew G. Knepley   Input Parameters:
1638bb7acecfSBarry Smith + dm     - The `DM`
16398cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16408cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16418cda7954SMatthew G. Knepley 
1642147403d9SBarry Smith   Calling sequence of nullsp:
1643147403d9SBarry Smith .vb
1644147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1645147403d9SBarry Smith .ve
1646bb7acecfSBarry Smith +  dm        - The present `DM`
1647bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1648147403d9SBarry Smith .  field     - The field number in dm
1649147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16508cda7954SMatthew G. Knepley 
165149762cbcSSatish Balay   Level: intermediate
165249762cbcSSatish Balay 
1653bb7acecfSBarry Smith   Fortran Notes:
1654bb7acecfSBarry Smith   This function is not available from Fortran.
1655bb7acecfSBarry Smith 
1656bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1657147403d9SBarry Smith @*/
16589371c9d4SSatish Balay PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
1659435a35e8SMatthew G Knepley   PetscFunctionBegin;
1660435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16617a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1662435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1663435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1664435a35e8SMatthew G Knepley }
1665435a35e8SMatthew G Knepley 
16668cda7954SMatthew G. Knepley /*@C
1667bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
16688cda7954SMatthew G. Knepley 
16698cda7954SMatthew G. Knepley   Not collective
16708cda7954SMatthew G. Knepley 
16718cda7954SMatthew G. Knepley   Input Parameters:
1672bb7acecfSBarry Smith + dm     - The `DM`
16738cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16748cda7954SMatthew G. Knepley 
16758cda7954SMatthew G. Knepley   Output Parameter:
16768cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16778cda7954SMatthew G. Knepley 
1678147403d9SBarry Smith   Calling sequence of nullsp:
1679147403d9SBarry Smith .vb
1680147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1681147403d9SBarry Smith .ve
1682147403d9SBarry Smith +  dm        - The present DM
1683147403d9SBarry Smith .  origField - The field number given above, in the original DM
1684147403d9SBarry Smith .  field     - The field number in dm
1685147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16868cda7954SMatthew G. Knepley 
1687bb7acecfSBarry Smith   Fortran Note:
1688bb7acecfSBarry Smith   This function is not available from Fortran.
16898cda7954SMatthew G. Knepley 
169049762cbcSSatish Balay    Level: intermediate
169149762cbcSSatish Balay 
1692bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1693147403d9SBarry Smith @*/
16949371c9d4SSatish Balay PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
16950a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16960a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1697f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16987a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
16990a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17000a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17010a50eb56SMatthew G. Knepley }
17020a50eb56SMatthew G. Knepley 
17038cda7954SMatthew G. Knepley /*@C
1704bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17058cda7954SMatthew G. Knepley 
1706bb7acecfSBarry Smith   Logically collective on dm
17078cda7954SMatthew G. Knepley 
17088cda7954SMatthew G. Knepley   Input Parameters:
1709bb7acecfSBarry Smith + dm     - The `DM`
17108cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17118cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17128cda7954SMatthew G. Knepley 
1713147403d9SBarry Smith   Calling sequence of nullsp:
1714147403d9SBarry Smith .vb
1715147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1716147403d9SBarry Smith .ve
1717bb7acecfSBarry Smith +  dm        - The present `DM`
1718bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1719147403d9SBarry Smith .  field     - The field number in dm
1720147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17218cda7954SMatthew G. Knepley 
1722bb7acecfSBarry Smith   Fortran Note:
1723bb7acecfSBarry Smith   This function is not available from Fortran.
17248cda7954SMatthew G. Knepley 
172549762cbcSSatish Balay    Level: intermediate
172649762cbcSSatish Balay 
1727bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1728bb7acecfSBarry Smith           `MatNullSpace`
1729147403d9SBarry Smith @*/
17309371c9d4SSatish Balay PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
1731f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1732f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17337a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1734f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1735f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1736f9d4088aSMatthew G. Knepley }
1737f9d4088aSMatthew G. Knepley 
17388cda7954SMatthew G. Knepley /*@C
1739bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17408cda7954SMatthew G. Knepley 
17418cda7954SMatthew G. Knepley   Not collective
17428cda7954SMatthew G. Knepley 
17438cda7954SMatthew G. Knepley   Input Parameters:
1744bb7acecfSBarry Smith + dm     - The `DM`
17458cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17468cda7954SMatthew G. Knepley 
17478cda7954SMatthew G. Knepley   Output Parameter:
17488cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17498cda7954SMatthew G. Knepley 
1750147403d9SBarry Smith   Calling sequence of nullsp:
1751147403d9SBarry Smith .vb
1752147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1753147403d9SBarry Smith .ve
1754bb7acecfSBarry Smith +  dm        - The present `DM`
1755bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1756147403d9SBarry Smith .  field     - The field number in dm
1757147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17588cda7954SMatthew G. Knepley 
1759bb7acecfSBarry Smith   Fortran Note:
1760bb7acecfSBarry Smith   This function is not available from Fortran.
17618cda7954SMatthew G. Knepley 
176249762cbcSSatish Balay    Level: intermediate
176349762cbcSSatish Balay 
1764bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1765bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1766147403d9SBarry Smith @*/
17679371c9d4SSatish Balay PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
1768f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1769f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1770f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17717a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1772f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1773f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1774f9d4088aSMatthew G. Knepley }
1775f9d4088aSMatthew G. Knepley 
17764f3b5142SJed Brown /*@C
1777bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
17784d343eeaSMatthew G Knepley 
17794d343eeaSMatthew G Knepley   Not collective
17804d343eeaSMatthew G Knepley 
17814d343eeaSMatthew G Knepley   Input Parameter:
1782bb7acecfSBarry Smith . dm - the `DM` object
17834d343eeaSMatthew G Knepley 
17844d343eeaSMatthew G Knepley   Output Parameters:
17850298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
1786bb7acecfSBarry Smith . fieldNames - The number of each field (or NULL if not requested)
17870298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17884d343eeaSMatthew G Knepley 
17894d343eeaSMatthew G Knepley   Level: intermediate
17904d343eeaSMatthew G Knepley 
1791bb7acecfSBarry Smith   Note:
179221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1793bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1794bb7acecfSBarry Smith   `PetscFree()`.
179521c9b008SJed Brown 
1796bb7acecfSBarry Smith   Fortran Note:
1797bb7acecfSBarry Smith   Not available in Fortran.
1798bb7acecfSBarry Smith 
1799bb7acecfSBarry Smith   Developer Note:
1800bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1801bb7acecfSBarry Smith   likely be removed.
1802bb7acecfSBarry Smith 
1803bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1804bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18054d343eeaSMatthew G Knepley @*/
18069371c9d4SSatish Balay PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields) {
180737d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18084d343eeaSMatthew G Knepley 
18094d343eeaSMatthew G Knepley   PetscFunctionBegin;
18104d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
181169ca1f37SDmitry Karpeev   if (numFields) {
1812534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
181369ca1f37SDmitry Karpeev     *numFields = 0;
181469ca1f37SDmitry Karpeev   }
181537d0c07bSMatthew G Knepley   if (fieldNames) {
181637d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
18170298fd71SBarry Smith     *fieldNames = NULL;
181869ca1f37SDmitry Karpeev   }
181969ca1f37SDmitry Karpeev   if (fields) {
182069ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
18210298fd71SBarry Smith     *fields = NULL;
182269ca1f37SDmitry Karpeev   }
18239566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
182437d0c07bSMatthew G Knepley   if (section) {
18253a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
182637d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
182737d0c07bSMatthew G Knepley 
18289566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
18299566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
18309566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
18319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
183237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
183337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
183537d0c07bSMatthew G Knepley     }
183637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
183737d0c07bSMatthew G Knepley       PetscInt gdof;
183837d0c07bSMatthew G Knepley 
18399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
184037d0c07bSMatthew G Knepley       if (gdof > 0) {
184137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18423a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
184337d0c07bSMatthew G Knepley 
18449566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18459566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18463a544194SStefano Zampini           fpdof = fdof - fcdof;
18473a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18483a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18493a544194SStefano Zampini             fieldNc[f] = 1;
18503a544194SStefano Zampini           }
18513a544194SStefano Zampini           fieldSizes[f] += fpdof;
185237d0c07bSMatthew G Knepley         }
185337d0c07bSMatthew G Knepley       }
185437d0c07bSMatthew G Knepley     }
185537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
18569566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
185737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
185837d0c07bSMatthew G Knepley     }
185937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
186037d0c07bSMatthew G Knepley       PetscInt gdof, goff;
186137d0c07bSMatthew G Knepley 
18629566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
186337d0c07bSMatthew G Knepley       if (gdof > 0) {
18649566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
186537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
186637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
186737d0c07bSMatthew G Knepley 
18689566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18699566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18709371c9d4SSatish Balay           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) { fieldIndices[f][fieldSizes[f]] = goff++; }
187137d0c07bSMatthew G Knepley         }
187237d0c07bSMatthew G Knepley       }
187337d0c07bSMatthew G Knepley     }
18748865f1eaSKarl Rupp     if (numFields) *numFields = nF;
187537d0c07bSMatthew G Knepley     if (fieldNames) {
18769566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
187737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
187837d0c07bSMatthew G Knepley         const char *fieldName;
187937d0c07bSMatthew G Knepley 
18809566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
18819566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
188237d0c07bSMatthew G Knepley       }
188337d0c07bSMatthew G Knepley     }
188437d0c07bSMatthew G Knepley     if (fields) {
18859566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
188637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18873a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18883a544194SStefano Zampini 
18899566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
18903a544194SStefano Zampini         in[0] = -fieldNc[f];
18913a544194SStefano Zampini         in[1] = fieldNc[f];
18921c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
18933a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
18949566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
189537d0c07bSMatthew G Knepley       }
189637d0c07bSMatthew G Knepley     }
18979566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1898dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
18994d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
19004d343eeaSMatthew G Knepley }
19014d343eeaSMatthew G Knepley 
190216621825SDmitry Karpeev /*@C
1903bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1904bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1905bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1906bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
1907e7c4fc90SDmitry Karpeev 
1908e7c4fc90SDmitry Karpeev   Not collective
1909e7c4fc90SDmitry Karpeev 
1910e7c4fc90SDmitry Karpeev   Input Parameter:
1911bb7acecfSBarry Smith . dm - the `DM` object
1912e7c4fc90SDmitry Karpeev 
1913e7c4fc90SDmitry Karpeev   Output Parameters:
1914bb7acecfSBarry Smith + len       - The number of fields (or NULL if not requested)
19150298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19160298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
1917bb7acecfSBarry Smith - dmlist    - The `DM`s for each field subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
1918e7c4fc90SDmitry Karpeev 
1919e7c4fc90SDmitry Karpeev   Level: intermediate
1920e7c4fc90SDmitry Karpeev 
1921bb7acecfSBarry Smith   Note:
1922e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1923bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
1924bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
1925e7c4fc90SDmitry Karpeev 
1926bb7acecfSBarry Smith   Fortran Note:
1927bb7acecfSBarry Smith   Not available in Fortran.
1928bb7acecfSBarry Smith 
1929bb7acecfSBarry Smith   Developer Note:
1930bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
1931bb7acecfSBarry Smith 
1932bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1933e7c4fc90SDmitry Karpeev @*/
19349371c9d4SSatish Balay PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist) {
1935e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1936e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19378865f1eaSKarl Rupp   if (len) {
1938534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
19398865f1eaSKarl Rupp     *len = 0;
19408865f1eaSKarl Rupp   }
19418865f1eaSKarl Rupp   if (namelist) {
19428865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
1943ea78f98cSLisandro Dalcin     *namelist = NULL;
19448865f1eaSKarl Rupp   }
19458865f1eaSKarl Rupp   if (islist) {
19468865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
1947ea78f98cSLisandro Dalcin     *islist = NULL;
19488865f1eaSKarl Rupp   }
19498865f1eaSKarl Rupp   if (dmlist) {
19508865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
1951ea78f98cSLisandro Dalcin     *dmlist = NULL;
19528865f1eaSKarl Rupp   }
1953f3f0edfdSDmitry Karpeev   /*
1954f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1955f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1956f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1957f3f0edfdSDmitry Karpeev    */
19587a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
195916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1960435a35e8SMatthew G Knepley     PetscSection section;
1961435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1962435a35e8SMatthew G Knepley 
19639566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
19649566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
1965435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1966f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
19679566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
19689566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
19699566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
1970435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1971435a35e8SMatthew G Knepley         const char *fieldName;
1972435a35e8SMatthew G Knepley 
19739566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
197403dc3394SMatthew G. Knepley         if (namelist) {
19759566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19769566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
1977435a35e8SMatthew G Knepley         }
197803dc3394SMatthew G. Knepley       }
1979435a35e8SMatthew G Knepley     } else {
19809566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
1981e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19820298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1983e7c4fc90SDmitry Karpeev     }
1984dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
198516621825SDmitry Karpeev   PetscFunctionReturn(0);
198616621825SDmitry Karpeev }
198716621825SDmitry Karpeev 
1988412a4547SAlexis Marboeuf /*@C
1989412a4547SAlexis Marboeuf   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1990412a4547SAlexis Marboeuf                   The fields are defined by DMCreateFieldIS().
1991435a35e8SMatthew G Knepley 
1992435a35e8SMatthew G Knepley   Not collective
1993435a35e8SMatthew G Knepley 
1994435a35e8SMatthew G Knepley   Input Parameters:
1995bb7acecfSBarry Smith + dm        - The `DM `object
1996bb7acecfSBarry Smith . numFields - The number of fields to select
19972adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1998435a35e8SMatthew G Knepley 
1999435a35e8SMatthew G Knepley   Output Parameters:
2000bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2001bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2002435a35e8SMatthew G Knepley 
2003bb7acecfSBarry Smith   Note:
2004bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20055d3b26e6SMatthew G. Knepley 
2006435a35e8SMatthew G Knepley   Level: intermediate
2007435a35e8SMatthew G Knepley 
2008bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2009435a35e8SMatthew G Knepley @*/
20109371c9d4SSatish Balay PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm) {
2011435a35e8SMatthew G Knepley   PetscFunctionBegin;
2012435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2013dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
20148865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
20158865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2016dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
2017435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2018435a35e8SMatthew G Knepley }
2019435a35e8SMatthew G Knepley 
20202adcc780SMatthew G. Knepley /*@C
2021bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
20222adcc780SMatthew G. Knepley 
20232adcc780SMatthew G. Knepley   Not collective
20242adcc780SMatthew G. Knepley 
2025d8d19677SJose E. Roman   Input Parameters:
2026bb7acecfSBarry Smith + dms - The `DM` objects
2027bb7acecfSBarry Smith - n - The number of `DM`s
20282adcc780SMatthew G. Knepley 
20292adcc780SMatthew G. Knepley   Output Parameters:
2030bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2031bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
20322adcc780SMatthew G. Knepley 
2033bb7acecfSBarry Smith   Note:
2034bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20355d3b26e6SMatthew G. Knepley 
20362adcc780SMatthew G. Knepley   Level: intermediate
20372adcc780SMatthew G. Knepley 
2038bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20392adcc780SMatthew G. Knepley @*/
20409371c9d4SSatish Balay PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm) {
20412adcc780SMatthew G. Knepley   PetscInt i;
20422adcc780SMatthew G. Knepley 
20432adcc780SMatthew G. Knepley   PetscFunctionBegin;
20442adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2045bb7acecfSBarry Smith   for (i = 0; i < n; ++i) { PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1); }
20462adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2047a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2048bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2049bb7acecfSBarry Smith   if (n) {
2050b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2051dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
20522adcc780SMatthew G. Knepley   }
20532adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20542adcc780SMatthew G. Knepley }
20552adcc780SMatthew G. Knepley 
205616621825SDmitry Karpeev /*@C
2057bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2058bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2059bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2060bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2061bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
206216621825SDmitry Karpeev 
206316621825SDmitry Karpeev   Not collective
206416621825SDmitry Karpeev 
206516621825SDmitry Karpeev   Input Parameter:
2066bb7acecfSBarry Smith . dm - the `DM` object
206716621825SDmitry Karpeev 
206816621825SDmitry Karpeev   Output Parameters:
2069bb7acecfSBarry Smith + n            - The number of subproblems in the domain decomposition (or NULL if not requested)
20700298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20710298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20720298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2073bb7acecfSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
207416621825SDmitry Karpeev 
207516621825SDmitry Karpeev   Level: intermediate
207616621825SDmitry Karpeev 
2077bb7acecfSBarry Smith   Note:
207816621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2079bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2080bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
208116621825SDmitry Karpeev 
2082bb7acecfSBarry Smith   Questions:
2083bb7acecfSBarry Smith   The dmlist is for the inner subdomains or the outer subdomains or all subdomains?
2084bb7acecfSBarry Smith 
2085bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
208616621825SDmitry Karpeev @*/
20879371c9d4SSatish Balay PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist) {
2088be081cd6SPeter Brune   DMSubDomainHookLink link;
2089be081cd6SPeter Brune   PetscInt            i, l;
209016621825SDmitry Karpeev 
209116621825SDmitry Karpeev   PetscFunctionBegin;
209216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20939371c9d4SSatish Balay   if (n) {
20949371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
20959371c9d4SSatish Balay     *n = 0;
20969371c9d4SSatish Balay   }
20979371c9d4SSatish Balay   if (namelist) {
20989371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
20999371c9d4SSatish Balay     *namelist = NULL;
21009371c9d4SSatish Balay   }
21019371c9d4SSatish Balay   if (innerislist) {
21029371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
21039371c9d4SSatish Balay     *innerislist = NULL;
21049371c9d4SSatish Balay   }
21059371c9d4SSatish Balay   if (outerislist) {
21069371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
21079371c9d4SSatish Balay     *outerislist = NULL;
21089371c9d4SSatish Balay   }
21099371c9d4SSatish Balay   if (dmlist) {
21109371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
21119371c9d4SSatish Balay     *dmlist = NULL;
21129371c9d4SSatish Balay   }
2113f3f0edfdSDmitry Karpeev   /*
2114f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2115f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2116f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2117f3f0edfdSDmitry Karpeev    */
21187a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
211916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2120dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
212114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2122f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2123be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2124be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
21259566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2126be081cd6SPeter Brune         }
2127648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2128e7c4fc90SDmitry Karpeev       }
212914a18fd3SPeter Brune     }
2130bb7acecfSBarry Smith     if (n) *n = l;
213114a18fd3SPeter Brune   }
2132e30e807fSPeter Brune   PetscFunctionReturn(0);
2133e30e807fSPeter Brune }
2134e30e807fSPeter Brune 
2135e30e807fSPeter Brune /*@C
2136e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2137e30e807fSPeter Brune 
2138e30e807fSPeter Brune   Not collective
2139e30e807fSPeter Brune 
2140e30e807fSPeter Brune   Input Parameters:
2141bb7acecfSBarry Smith + dm - the `DM` object
2142e30e807fSPeter Brune . n  - the number of subdomain scatters
2143e30e807fSPeter Brune - subdms - the local subdomains
2144e30e807fSPeter Brune 
2145e30e807fSPeter Brune   Output Parameters:
21466b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2147e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2148e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2149e30e807fSPeter Brune 
2150bb7acecfSBarry Smith   Note:
2151bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2152e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2153e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2154e30e807fSPeter Brune   solution and residual data.
2155e30e807fSPeter Brune 
2156bb7acecfSBarry Smith   Questions:
2157bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2158bb7acecfSBarry Smith 
2159e30e807fSPeter Brune   Level: developer
2160e30e807fSPeter Brune 
2161bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2162e30e807fSPeter Brune @*/
21639371c9d4SSatish Balay PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat) {
2164e30e807fSPeter Brune   PetscFunctionBegin;
2165e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2166e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2167dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
2168e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2169e7c4fc90SDmitry Karpeev }
2170e7c4fc90SDmitry Karpeev 
217147c6ae99SBarry Smith /*@
2172bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
217347c6ae99SBarry Smith 
2174d083f849SBarry Smith   Collective on dm
217547c6ae99SBarry Smith 
2176d8d19677SJose E. Roman   Input Parameters:
2177bb7acecfSBarry Smith + dm   - the `DM` object
2178bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
217947c6ae99SBarry Smith 
218047c6ae99SBarry Smith   Output Parameter:
2181bb7acecfSBarry Smith . dmf - the refined `D`M, or NULL
2182ae0a1c52SMatthew G Knepley 
2183f27dd7c6SMatthew G. Knepley   Options Database Keys:
2184412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2185412e9a14SMatthew G. Knepley 
2186bb7acecfSBarry Smith   Note:
2187bb7acecfSBarry Smith   If no refinement was done, the return value is NULL
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith   Level: developer
219047c6ae99SBarry Smith 
2191bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
219247c6ae99SBarry Smith @*/
21939371c9d4SSatish Balay PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf) {
2194c833c3b5SJed Brown   DMRefineHookLink link;
219547c6ae99SBarry Smith 
219647c6ae99SBarry Smith   PetscFunctionBegin;
2197732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21989566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2199dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22004057135bSMatthew G Knepley   if (*dmf) {
220143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22028865f1eaSKarl Rupp 
22039566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
22048865f1eaSKarl Rupp 
2205644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22060598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2207656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22088865f1eaSKarl Rupp 
22099566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2210c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
22111baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2212c833c3b5SJed Brown     }
2213c833c3b5SJed Brown   }
22149566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
2215c833c3b5SJed Brown   PetscFunctionReturn(0);
2216c833c3b5SJed Brown }
2217c833c3b5SJed Brown 
2218bb9467b5SJed Brown /*@C
2219c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2220c833c3b5SJed Brown 
2221bb7acecfSBarry Smith    Logically Collective on coarse
2222c833c3b5SJed Brown 
22234165533cSJose E. Roman    Input Parameters:
2224bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2225bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2226bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
22270298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2228c833c3b5SJed Brown 
2229c833c3b5SJed Brown    Calling sequence of refinehook:
2230c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2231c833c3b5SJed Brown 
2232bb7acecfSBarry Smith +  coarse - coarse level `DM`
2233bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2234c833c3b5SJed Brown -  ctx - optional user-defined function context
2235c833c3b5SJed Brown 
2236c833c3b5SJed Brown    Calling sequence for interphook:
2237c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2238c833c3b5SJed Brown 
2239bb7acecfSBarry Smith +  coarse - coarse level `DM`
2240c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2241bb7acecfSBarry Smith .  fine - fine level `DM` to update
2242c833c3b5SJed Brown -  ctx - optional user-defined function context
2243c833c3b5SJed Brown 
2244c833c3b5SJed Brown    Level: advanced
2245c833c3b5SJed Brown 
2246c833c3b5SJed Brown    Notes:
2247bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2248bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2249bb7acecfSBarry Smith 
2250bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2251c833c3b5SJed Brown 
2252c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2253c833c3b5SJed Brown 
2254bb7acecfSBarry Smith    Fortran Note:
2255bb7acecfSBarry Smith    This function is not available from Fortran.
2256bb9467b5SJed Brown 
2257bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2258c833c3b5SJed Brown @*/
22599371c9d4SSatish Balay PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx) {
2260c833c3b5SJed Brown   DMRefineHookLink link, *p;
2261c833c3b5SJed Brown 
2262c833c3b5SJed Brown   PetscFunctionBegin;
2263c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
22643d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
22653d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22663d8e3701SJed Brown   }
22679566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2268c833c3b5SJed Brown   link->refinehook = refinehook;
2269c833c3b5SJed Brown   link->interphook = interphook;
2270c833c3b5SJed Brown   link->ctx        = ctx;
22710298fd71SBarry Smith   link->next       = NULL;
2272c833c3b5SJed Brown   *p               = link;
2273c833c3b5SJed Brown   PetscFunctionReturn(0);
2274c833c3b5SJed Brown }
2275c833c3b5SJed Brown 
22763d8e3701SJed Brown /*@C
2277bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2278bb7acecfSBarry Smith     a nonlinear problem to a finer grid
22793d8e3701SJed Brown 
2280bb7acecfSBarry Smith    Logically Collective on coarse
22813d8e3701SJed Brown 
22824165533cSJose E. Roman    Input Parameters:
2283bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2284bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2285bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
22863d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22873d8e3701SJed Brown 
22883d8e3701SJed Brown    Level: advanced
22893d8e3701SJed Brown 
2290bb7acecfSBarry Smith    Note:
22913d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22923d8e3701SJed Brown 
2293bb7acecfSBarry Smith    Fortran Note:
2294bb7acecfSBarry Smith    This function is not available from Fortran.
22953d8e3701SJed Brown 
2296bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
22973d8e3701SJed Brown @*/
22989371c9d4SSatish Balay PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx) {
22993d8e3701SJed Brown   DMRefineHookLink link, *p;
23003d8e3701SJed Brown 
23013d8e3701SJed Brown   PetscFunctionBegin;
23023d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23033d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23043d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23053d8e3701SJed Brown       link = *p;
23063d8e3701SJed Brown       *p   = link->next;
23079566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23083d8e3701SJed Brown       break;
23093d8e3701SJed Brown     }
23103d8e3701SJed Brown   }
23113d8e3701SJed Brown   PetscFunctionReturn(0);
23123d8e3701SJed Brown }
23133d8e3701SJed Brown 
2314c833c3b5SJed Brown /*@
2315bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2316c833c3b5SJed Brown 
2317c833c3b5SJed Brown    Collective if any hooks are
2318c833c3b5SJed Brown 
23194165533cSJose E. Roman    Input Parameters:
2320bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2321bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2322bb7acecfSBarry Smith -  fine - finer `DM` to update
2323c833c3b5SJed Brown 
2324c833c3b5SJed Brown    Level: developer
2325c833c3b5SJed Brown 
2326bb7acecfSBarry Smith    Developer Note:
2327bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2328bb7acecfSBarry Smith    an API with consistent terminology.
2329bb7acecfSBarry Smith 
2330bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2331c833c3b5SJed Brown @*/
23329371c9d4SSatish Balay PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine) {
2333c833c3b5SJed Brown   DMRefineHookLink link;
2334c833c3b5SJed Brown 
2335c833c3b5SJed Brown   PetscFunctionBegin;
2336c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
23371baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
23384057135bSMatthew G Knepley   }
233947c6ae99SBarry Smith   PetscFunctionReturn(0);
234047c6ae99SBarry Smith }
234147c6ae99SBarry Smith 
2342eb3f98d2SBarry Smith /*@
23431f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23441f3379b2SToby Isaac 
2345bb7acecfSBarry Smith    Collective on dm
23461f3379b2SToby Isaac 
23474165533cSJose E. Roman    Input Parameters:
2348bb7acecfSBarry Smith +  coarse - coarse `DM`
2349bb7acecfSBarry Smith .  fine   - fine `DM`
2350bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2351bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2352bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
23531f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23541f3379b2SToby Isaac 
23554165533cSJose E. Roman    Output Parameter:
23561f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23571f3379b2SToby Isaac 
23581f3379b2SToby Isaac    Level: developer
23591f3379b2SToby Isaac 
2360bb7acecfSBarry Smith    Note:
2361bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
23621f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23631f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23641f3379b2SToby Isaac    slope-limiting reconstruction.
23651f3379b2SToby Isaac 
2366bb7acecfSBarry Smith    Developer Note:
2367bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2368bb7acecfSBarry Smith 
2369bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
23701f3379b2SToby Isaac @*/
23719371c9d4SSatish Balay PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol) {
23721f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
23731f3379b2SToby Isaac 
23741f3379b2SToby Isaac   PetscFunctionBegin;
23751f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23761f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
23771f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
23781f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
23791f3379b2SToby Isaac 
23809566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
23811f3379b2SToby Isaac   if (interpsol) {
23829566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
23831f3379b2SToby Isaac   } else if (interp) {
23849566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
238598921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23861f3379b2SToby Isaac   PetscFunctionReturn(0);
23871f3379b2SToby Isaac }
23881f3379b2SToby Isaac 
23891f3379b2SToby Isaac /*@
2390bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2391eb3f98d2SBarry Smith 
2392eb3f98d2SBarry Smith     Not Collective
2393eb3f98d2SBarry Smith 
2394eb3f98d2SBarry Smith     Input Parameter:
2395bb7acecfSBarry Smith .   dm - the `DM` object
2396eb3f98d2SBarry Smith 
2397eb3f98d2SBarry Smith     Output Parameter:
2398eb3f98d2SBarry Smith .   level - number of refinements
2399eb3f98d2SBarry Smith 
2400eb3f98d2SBarry Smith     Level: developer
2401eb3f98d2SBarry Smith 
2402bb7acecfSBarry Smith     Note:
2403bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2404bb7acecfSBarry Smith 
2405bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2406eb3f98d2SBarry Smith 
2407eb3f98d2SBarry Smith @*/
24089371c9d4SSatish Balay PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level) {
2409eb3f98d2SBarry Smith   PetscFunctionBegin;
2410eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2411eb3f98d2SBarry Smith   *level = dm->levelup;
2412eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2413eb3f98d2SBarry Smith }
2414eb3f98d2SBarry Smith 
2415fef3a512SBarry Smith /*@
2416bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2417fef3a512SBarry Smith 
2418fef3a512SBarry Smith     Not Collective
2419fef3a512SBarry Smith 
2420d8d19677SJose E. Roman     Input Parameters:
2421bb7acecfSBarry Smith +   dm - the `DM` object
2422fef3a512SBarry Smith -   level - number of refinements
2423fef3a512SBarry Smith 
2424fef3a512SBarry Smith     Level: advanced
2425fef3a512SBarry Smith 
242695452b02SPatrick Sanan     Notes:
2427bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2428fef3a512SBarry Smith 
2429bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2430bb7acecfSBarry Smith 
2431bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2432fef3a512SBarry Smith 
2433fef3a512SBarry Smith @*/
24349371c9d4SSatish Balay PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level) {
2435fef3a512SBarry Smith   PetscFunctionBegin;
2436fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2437fef3a512SBarry Smith   dm->levelup = level;
2438fef3a512SBarry Smith   PetscFunctionReturn(0);
2439fef3a512SBarry Smith }
2440fef3a512SBarry Smith 
2441d410b0cfSMatthew G. Knepley /*@
2442bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2443d410b0cfSMatthew G. Knepley 
2444d410b0cfSMatthew G. Knepley   Collective on dm
2445d410b0cfSMatthew G. Knepley 
2446f1a722f8SMatthew G. Knepley   Input Parameters:
2447bb7acecfSBarry Smith + dm     - the `DM` object
2448d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2449d410b0cfSMatthew G. Knepley 
2450d410b0cfSMatthew G. Knepley   Output Parameter:
2451bb7acecfSBarry Smith . dme - the extruded `DM`, or NULL
2452d410b0cfSMatthew G. Knepley 
2453bb7acecfSBarry Smith   Note:
2454bb7acecfSBarry Smith   If no extrusion was done, the return value is NULL
2455d410b0cfSMatthew G. Knepley 
2456d410b0cfSMatthew G. Knepley   Level: developer
2457d410b0cfSMatthew G. Knepley 
2458bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2459d410b0cfSMatthew G. Knepley @*/
24609371c9d4SSatish Balay PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme) {
2461d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2462d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2463dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2464d410b0cfSMatthew G. Knepley   if (*dme) {
2465d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
24669566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2467d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
24689566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2469d410b0cfSMatthew G. Knepley   }
2470d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2471d410b0cfSMatthew G. Knepley }
2472d410b0cfSMatthew G. Knepley 
24739371c9d4SSatish Balay PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm) {
2474ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2475ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2476ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2477ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2478ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2479ca3d3a14SMatthew G. Knepley }
2480ca3d3a14SMatthew G. Knepley 
24819371c9d4SSatish Balay PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv) {
2482ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2483ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2484ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2485ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2486ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2487ca3d3a14SMatthew G. Knepley }
2488ca3d3a14SMatthew G. Knepley 
2489ca3d3a14SMatthew G. Knepley /*@
2490bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2491ca3d3a14SMatthew G. Knepley 
2492ca3d3a14SMatthew G. Knepley   Input Parameter:
2493ca3d3a14SMatthew G. Knepley . dm - The DM
2494ca3d3a14SMatthew G. Knepley 
2495ca3d3a14SMatthew G. Knepley   Output Parameter:
2496ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2497ca3d3a14SMatthew G. Knepley 
2498ca3d3a14SMatthew G. Knepley   Level: developer
2499ca3d3a14SMatthew G. Knepley 
2500bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2501ca3d3a14SMatthew G. Knepley @*/
25029371c9d4SSatish Balay PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg) {
2503ca3d3a14SMatthew G. Knepley   Vec tv;
2504ca3d3a14SMatthew G. Knepley 
2505ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2506ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2507534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
25089566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2509ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2510ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2511ca3d3a14SMatthew G. Knepley }
2512ca3d3a14SMatthew G. Knepley 
25139371c9d4SSatish Balay PetscErrorCode DMConstructBasisTransform_Internal(DM dm) {
2514ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2515ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2516ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2517ca3d3a14SMatthew G. Knepley 
2518ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
25199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
25209566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
25219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
25229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
25239566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
25249566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
25259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
25269566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2527ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
25289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2529ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2530ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2531ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25329566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2533ca3d3a14SMatthew G. Knepley       if (!dof) continue;
25349566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
25359566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2536ca3d3a14SMatthew G. Knepley     }
2537ca3d3a14SMatthew G. Knepley   }
25389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
25399566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
25409566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2541ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2542ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
25439566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2544ca3d3a14SMatthew G. Knepley       if (dof) {
2545ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2546ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2547ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2548ca3d3a14SMatthew G. Knepley 
2549ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
25509566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
25519566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
25529566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2553ca3d3a14SMatthew G. Knepley       }
2554ca3d3a14SMatthew G. Knepley     }
2555ca3d3a14SMatthew G. Knepley   }
25569566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2557ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2558ca3d3a14SMatthew G. Knepley }
2559ca3d3a14SMatthew G. Knepley 
25609371c9d4SSatish Balay PetscErrorCode DMCopyTransform(DM dm, DM newdm) {
2561ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2562ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2563ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2564ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2565ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2566ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2567ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
25689566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2569ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2570ca3d3a14SMatthew G. Knepley }
2571ca3d3a14SMatthew G. Knepley 
2572bb9467b5SJed Brown /*@C
2573bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2574baf369e7SPeter Brune 
2575bb7acecfSBarry Smith    Logically Collective on dm
2576baf369e7SPeter Brune 
25774165533cSJose E. Roman    Input Parameters:
2578bb7acecfSBarry Smith +  dm - the `DM`
2579bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2580bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
25810298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2582baf369e7SPeter Brune 
2583baf369e7SPeter Brune    Calling sequence for beginhook:
2584baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2585baf369e7SPeter Brune 
2586baf369e7SPeter Brune +  dm - global DM
2587baf369e7SPeter Brune .  g - global vector
2588baf369e7SPeter Brune .  mode - mode
2589baf369e7SPeter Brune .  l - local vector
2590baf369e7SPeter Brune -  ctx - optional user-defined function context
2591baf369e7SPeter Brune 
2592baf369e7SPeter Brune    Calling sequence for endhook:
2593ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2594baf369e7SPeter Brune 
2595baf369e7SPeter Brune +  global - global DM
2596baf369e7SPeter Brune -  ctx - optional user-defined function context
2597baf369e7SPeter Brune 
2598baf369e7SPeter Brune    Level: advanced
2599baf369e7SPeter Brune 
2600bb7acecfSBarry Smith    Note:
2601bb7acecfSBarry 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.
2602bb7acecfSBarry Smith 
2603bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2604baf369e7SPeter Brune @*/
26059371c9d4SSatish Balay PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx) {
2606baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2607baf369e7SPeter Brune 
2608baf369e7SPeter Brune   PetscFunctionBegin;
2609baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2610baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
26119566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2612baf369e7SPeter Brune   link->beginhook = beginhook;
2613baf369e7SPeter Brune   link->endhook   = endhook;
2614baf369e7SPeter Brune   link->ctx       = ctx;
26150298fd71SBarry Smith   link->next      = NULL;
2616baf369e7SPeter Brune   *p              = link;
2617baf369e7SPeter Brune   PetscFunctionReturn(0);
2618baf369e7SPeter Brune }
2619baf369e7SPeter Brune 
26209371c9d4SSatish Balay static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx) {
26214c274da1SToby Isaac   Mat          cMat;
262279769bd5SJed Brown   Vec          cVec, cBias;
26234c274da1SToby Isaac   PetscSection section, cSec;
26244c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
26254c274da1SToby Isaac 
26264c274da1SToby Isaac   PetscFunctionBegin;
26274c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26289566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
26294c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
26305db9a05bSToby Isaac     PetscInt nRows;
26315db9a05bSToby Isaac 
26329566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
26335db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
26349566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
26359566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
26369566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
26379566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
26389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
26394c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26409566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
26414c274da1SToby Isaac       if (dof) {
26424c274da1SToby Isaac         PetscScalar *vals;
26439566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
26449566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
26454c274da1SToby Isaac       }
26464c274da1SToby Isaac     }
26479566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
26484c274da1SToby Isaac   }
26494c274da1SToby Isaac   PetscFunctionReturn(0);
26504c274da1SToby Isaac }
26514c274da1SToby Isaac 
265247c6ae99SBarry Smith /*@
265301729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
265401729b5cSPatrick Sanan 
2655d083f849SBarry Smith     Neighbor-wise Collective on dm
265601729b5cSPatrick Sanan 
265701729b5cSPatrick Sanan     Input Parameters:
2658bb7acecfSBarry Smith +   dm - the `DM` object
265901729b5cSPatrick Sanan .   g - the global vector
2660bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
266101729b5cSPatrick Sanan -   l - the local vector
266201729b5cSPatrick Sanan 
266301729b5cSPatrick Sanan     Notes:
2664bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2665bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2666bb7acecfSBarry Smith 
2667bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
266801729b5cSPatrick Sanan 
266901729b5cSPatrick Sanan     Level: beginner
267001729b5cSPatrick Sanan 
2671bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2672bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2673bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
267401729b5cSPatrick Sanan 
267501729b5cSPatrick Sanan @*/
26769371c9d4SSatish Balay PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l) {
267701729b5cSPatrick Sanan   PetscFunctionBegin;
26789566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
26799566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
268001729b5cSPatrick Sanan   PetscFunctionReturn(0);
268101729b5cSPatrick Sanan }
268201729b5cSPatrick Sanan 
268301729b5cSPatrick Sanan /*@
268447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
268547c6ae99SBarry Smith 
2686d083f849SBarry Smith     Neighbor-wise Collective on dm
268747c6ae99SBarry Smith 
268847c6ae99SBarry Smith     Input Parameters:
2689bb7acecfSBarry Smith +   dm - the `DM` object
269047c6ae99SBarry Smith .   g - the global vector
2691bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
269247c6ae99SBarry Smith -   l - the local vector
269347c6ae99SBarry Smith 
269401729b5cSPatrick Sanan     Level: intermediate
269547c6ae99SBarry Smith 
2696bb7acecfSBarry Smith     Notes:
2697bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2698bb7acecfSBarry Smith 
2699bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2700bb7acecfSBarry Smith 
2701bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2702bb7acecfSBarry Smith 
2703bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2704bb7acecfSBarry Smith 
2705bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
270647c6ae99SBarry Smith 
270747c6ae99SBarry Smith @*/
27089371c9d4SSatish Balay PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l) {
27097128ae9fSMatthew G Knepley   PetscSF                 sf;
2710baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
271147c6ae99SBarry Smith 
271247c6ae99SBarry Smith   PetscFunctionBegin;
2713171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2714baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
27151baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2716baf369e7SPeter Brune   }
27179566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27187128ae9fSMatthew G Knepley   if (sf) {
2719ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2720ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2721d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
27227128ae9fSMatthew G Knepley 
27237a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27249566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27259566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27269566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
27279566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27289566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27297128ae9fSMatthew G Knepley   } else {
27309566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
27317128ae9fSMatthew G Knepley   }
273247c6ae99SBarry Smith   PetscFunctionReturn(0);
273347c6ae99SBarry Smith }
273447c6ae99SBarry Smith 
273547c6ae99SBarry Smith /*@
273647c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
273747c6ae99SBarry Smith 
2738d083f849SBarry Smith     Neighbor-wise Collective on dm
273947c6ae99SBarry Smith 
274047c6ae99SBarry Smith     Input Parameters:
2741bb7acecfSBarry Smith +   dm - the `DM` object
274247c6ae99SBarry Smith .   g - the global vector
2743bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
274447c6ae99SBarry Smith -   l - the local vector
274547c6ae99SBarry Smith 
274601729b5cSPatrick Sanan     Level: intermediate
274747c6ae99SBarry Smith 
2748bb7acecfSBarry Smith     Note:
2749bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2750bb7acecfSBarry Smith 
2751bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
275247c6ae99SBarry Smith 
275347c6ae99SBarry Smith @*/
27549371c9d4SSatish Balay PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l) {
27557128ae9fSMatthew G Knepley   PetscSF                 sf;
2756ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2757ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2758ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2759baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2760d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
276147c6ae99SBarry Smith 
276247c6ae99SBarry Smith   PetscFunctionBegin;
2763171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27649566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27659566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
27667128ae9fSMatthew G Knepley   if (sf) {
27677a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27687128ae9fSMatthew G Knepley 
27699566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27709566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27719566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
27729566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27739566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27749566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
27757128ae9fSMatthew G Knepley   } else {
27769566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
27777128ae9fSMatthew G Knepley   }
27789566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2779baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
27809566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2781baf369e7SPeter Brune   }
278247c6ae99SBarry Smith   PetscFunctionReturn(0);
278347c6ae99SBarry Smith }
278447c6ae99SBarry Smith 
2785d4d07f1eSToby Isaac /*@C
2786d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2787d4d07f1eSToby Isaac 
2788bb7acecfSBarry Smith    Logically Collective on dm
2789d4d07f1eSToby Isaac 
27904165533cSJose E. Roman    Input Parameters:
2791bb7acecfSBarry Smith +  dm - the `DM`
2792bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2793bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2794d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2795d4d07f1eSToby Isaac 
2796d4d07f1eSToby Isaac    Calling sequence for beginhook:
2797d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2798d4d07f1eSToby Isaac 
2799bb7acecfSBarry Smith +  dm - global `DM`
2800d4d07f1eSToby Isaac .  l - local vector
2801d4d07f1eSToby Isaac .  mode - mode
2802d4d07f1eSToby Isaac .  g - global vector
2803d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2804d4d07f1eSToby Isaac 
2805d4d07f1eSToby Isaac    Calling sequence for endhook:
2806d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2807d4d07f1eSToby Isaac 
2808bb7acecfSBarry Smith +  global - global `DM`
2809d4d07f1eSToby Isaac .  l - local vector
2810d4d07f1eSToby Isaac .  mode - mode
2811d4d07f1eSToby Isaac .  g - global vector
2812d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2813d4d07f1eSToby Isaac 
2814d4d07f1eSToby Isaac    Level: advanced
2815d4d07f1eSToby Isaac 
2816bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2817d4d07f1eSToby Isaac @*/
28189371c9d4SSatish Balay PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx) {
2819d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2820d4d07f1eSToby Isaac 
2821d4d07f1eSToby Isaac   PetscFunctionBegin;
2822d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2823d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
28249566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2825d4d07f1eSToby Isaac   link->beginhook = beginhook;
2826d4d07f1eSToby Isaac   link->endhook   = endhook;
2827d4d07f1eSToby Isaac   link->ctx       = ctx;
2828d4d07f1eSToby Isaac   link->next      = NULL;
2829d4d07f1eSToby Isaac   *p              = link;
2830d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2831d4d07f1eSToby Isaac }
2832d4d07f1eSToby Isaac 
28339371c9d4SSatish Balay static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx) {
28344c274da1SToby Isaac   Mat          cMat;
28354c274da1SToby Isaac   Vec          cVec;
28364c274da1SToby Isaac   PetscSection section, cSec;
28374c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
28384c274da1SToby Isaac 
28394c274da1SToby Isaac   PetscFunctionBegin;
28404c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28419566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
28424c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
28435db9a05bSToby Isaac     PetscInt nRows;
28445db9a05bSToby Isaac 
28459566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
28465db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
28479566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
28489566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
28499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
28504c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
28519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
28524c274da1SToby Isaac       if (dof) {
28534c274da1SToby Isaac         PetscInt     d;
28544c274da1SToby Isaac         PetscScalar *vals;
28559566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
28569566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
28574c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28584c274da1SToby Isaac          * we just extracted */
28599371c9d4SSatish Balay         for (d = 0; d < dof; d++) { vals[d] = 0.; }
28604c274da1SToby Isaac       }
28614c274da1SToby Isaac     }
28629566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
28639566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
28644c274da1SToby Isaac   }
28654c274da1SToby Isaac   PetscFunctionReturn(0);
28664c274da1SToby Isaac }
286701729b5cSPatrick Sanan /*@
286801729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
286901729b5cSPatrick Sanan 
2870d083f849SBarry Smith     Neighbor-wise Collective on dm
287101729b5cSPatrick Sanan 
287201729b5cSPatrick Sanan     Input Parameters:
2873bb7acecfSBarry Smith +   dm - the `DM` object
287401729b5cSPatrick Sanan .   l - the local vector
2875bb7acecfSBarry 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.
287601729b5cSPatrick Sanan -   g - the global vector
287701729b5cSPatrick Sanan 
287801729b5cSPatrick Sanan     Notes:
287901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2880bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
288101729b5cSPatrick Sanan 
2882bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2883bb7acecfSBarry Smith 
2884bb7acecfSBarry 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.
2885bb7acecfSBarry Smith 
2886bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
288701729b5cSPatrick Sanan 
288801729b5cSPatrick Sanan     Level: beginner
288901729b5cSPatrick Sanan 
2890bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`,  `DMLocalToGlobalHookAdd()`,  `DMGlobaToLocallHookAdd()`
289101729b5cSPatrick Sanan 
289201729b5cSPatrick Sanan @*/
28939371c9d4SSatish Balay PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g) {
289401729b5cSPatrick Sanan   PetscFunctionBegin;
28959566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
28969566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
289701729b5cSPatrick Sanan   PetscFunctionReturn(0);
289801729b5cSPatrick Sanan }
28994c274da1SToby Isaac 
290047c6ae99SBarry Smith /*@
290101729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
29029a42bb27SBarry Smith 
2903d083f849SBarry Smith     Neighbor-wise Collective on dm
29049a42bb27SBarry Smith 
29059a42bb27SBarry Smith     Input Parameters:
2906bb7acecfSBarry Smith +   dm - the `DM` object
2907f6813fd5SJed Brown .   l - the local vector
2908bb7acecfSBarry 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.
29091eb28f2eSBarry Smith -   g - the global vector
29109a42bb27SBarry Smith 
291195452b02SPatrick Sanan     Notes:
2912bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2913bb7acecfSBarry Smith 
2914bb7acecfSBarry 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.
2915bb7acecfSBarry Smith 
2916bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
2917bb7acecfSBarry Smith 
2918bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
2919bb7acecfSBarry Smith 
2920bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
29219a42bb27SBarry Smith 
292201729b5cSPatrick Sanan     Level: intermediate
29239a42bb27SBarry Smith 
2924bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
29259a42bb27SBarry Smith 
29269a42bb27SBarry Smith @*/
29279371c9d4SSatish Balay PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g) {
29287128ae9fSMatthew G Knepley   PetscSF                 sf;
292984330215SMatthew G. Knepley   PetscSection            s, gs;
2930d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2931ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2932ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2933ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2934fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2935d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
29369a42bb27SBarry Smith 
29379a42bb27SBarry Smith   PetscFunctionBegin;
2938171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2939d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
29401baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
2941d4d07f1eSToby Isaac   }
29429566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
29439566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
29449566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
29457128ae9fSMatthew G Knepley   switch (mode) {
29467128ae9fSMatthew G Knepley   case INSERT_VALUES:
29477128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
29489371c9d4SSatish Balay   case INSERT_BC_VALUES: isInsert = PETSC_TRUE; break;
29497128ae9fSMatthew G Knepley   case ADD_VALUES:
29507128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
29519371c9d4SSatish Balay   case ADD_BC_VALUES: isInsert = PETSC_FALSE; break;
29529371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
29537128ae9fSMatthew G Knepley   }
2954ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
29559566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
2956ca3d3a14SMatthew G. Knepley     if (transform) {
29579566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
29589566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
29599566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
29609566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
2961fa88e482SJed Brown     } else if (isInsert) {
29629566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
2963fa88e482SJed Brown     } else {
29649566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
2965fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2966ca3d3a14SMatthew G. Knepley     }
2967fa88e482SJed Brown     if (s && isInsert) {
29689566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
2969fa88e482SJed Brown     } else {
29709566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
2971fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2972fa88e482SJed Brown     }
2973ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
29749566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
297584330215SMatthew G. Knepley     } else if (s && isInsert) {
297684330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
297784330215SMatthew G. Knepley 
29789566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
29799566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29809566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
298184330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2982b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
298384330215SMatthew G. Knepley 
29849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
29859566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
29869566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29879566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
29889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
29899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
2990b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
299103442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
29927a8be351SBarry 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);
2993b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2994b3b16f48SMatthew G. Knepley         if (!gcdof) {
299584330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
2996b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2997b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
299884330215SMatthew G. Knepley           const PetscInt *cdofs;
299984330215SMatthew G. Knepley           PetscInt        cind = 0;
300084330215SMatthew G. Knepley 
30019566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3002b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
30039371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
30049371c9d4SSatish Balay               ++cind;
30059371c9d4SSatish Balay               continue;
30069371c9d4SSatish Balay             }
3007b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
300884330215SMatthew G. Knepley           }
30097a8be351SBarry 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);
301084330215SMatthew G. Knepley       }
3011ca3d3a14SMatthew G. Knepley     }
3012fa88e482SJed Brown     if (g_inplace) {
30139566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3014fa88e482SJed Brown     } else {
30159566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3016fa88e482SJed Brown     }
3017ca3d3a14SMatthew G. Knepley     if (transform) {
30189566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30199566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3020fa88e482SJed Brown     } else if (l_inplace) {
30219566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3022ca3d3a14SMatthew G. Knepley     } else {
30239566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3024ca3d3a14SMatthew G. Knepley     }
30257128ae9fSMatthew G Knepley   } else {
30269566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
30277128ae9fSMatthew G Knepley   }
30289a42bb27SBarry Smith   PetscFunctionReturn(0);
30299a42bb27SBarry Smith }
30309a42bb27SBarry Smith 
30319a42bb27SBarry Smith /*@
30329a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
303347c6ae99SBarry Smith 
3034d083f849SBarry Smith     Neighbor-wise Collective on dm
303547c6ae99SBarry Smith 
303647c6ae99SBarry Smith     Input Parameters:
3037bb7acecfSBarry Smith +   dm - the `DM` object
3038f6813fd5SJed Brown .   l - the local vector
3039bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3040f6813fd5SJed Brown -   g - the global vector
304147c6ae99SBarry Smith 
304201729b5cSPatrick Sanan     Level: intermediate
304347c6ae99SBarry Smith 
3044bb7acecfSBarry Smith     Note:
3045bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3046bb7acecfSBarry Smith 
3047bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
304847c6ae99SBarry Smith 
304947c6ae99SBarry Smith @*/
30509371c9d4SSatish Balay PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g) {
30517128ae9fSMatthew G Knepley   PetscSF                 sf;
305284330215SMatthew G. Knepley   PetscSection            s;
3053d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3054ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
305547c6ae99SBarry Smith 
305647c6ae99SBarry Smith   PetscFunctionBegin;
3057171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30589566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30599566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30607128ae9fSMatthew G Knepley   switch (mode) {
30617128ae9fSMatthew G Knepley   case INSERT_VALUES:
30629371c9d4SSatish Balay   case INSERT_ALL_VALUES: isInsert = PETSC_TRUE; break;
30637128ae9fSMatthew G Knepley   case ADD_VALUES:
30649371c9d4SSatish Balay   case ADD_ALL_VALUES: isInsert = PETSC_FALSE; break;
30659371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30667128ae9fSMatthew G Knepley   }
306784330215SMatthew G. Knepley   if (sf && !isInsert) {
3068ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3069ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3070ca3d3a14SMatthew G. Knepley     Vec                tmpl;
307184330215SMatthew G. Knepley 
30729566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3073ca3d3a14SMatthew G. Knepley     if (transform) {
30749566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30759566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3076ca3d3a14SMatthew G. Knepley     } else {
30779566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3078ca3d3a14SMatthew G. Knepley     }
30799566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
30809566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3081ca3d3a14SMatthew G. Knepley     if (transform) {
30829566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30839566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3084ca3d3a14SMatthew G. Knepley     } else {
30859566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3086ca3d3a14SMatthew G. Knepley     }
30879566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
308884330215SMatthew G. Knepley   } else if (s && isInsert) {
30897128ae9fSMatthew G Knepley   } else {
30909566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
30917128ae9fSMatthew G Knepley   }
3092d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30939566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3094d4d07f1eSToby Isaac   }
309547c6ae99SBarry Smith   PetscFunctionReturn(0);
309647c6ae99SBarry Smith }
309747c6ae99SBarry Smith 
3098f089877aSRichard Tran Mills /*@
3099bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3100bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3101bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3102f089877aSRichard Tran Mills 
3103d083f849SBarry Smith    Neighbor-wise Collective on dm
3104f089877aSRichard Tran Mills 
3105f089877aSRichard Tran Mills    Input Parameters:
3106bb7acecfSBarry Smith +  dm - the `DM` object
3107bc0a1609SRichard Tran Mills .  g - the original local vector
3108bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3109f089877aSRichard Tran Mills 
3110bc0a1609SRichard Tran Mills    Output Parameter:
3111bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3112f089877aSRichard Tran Mills 
3113f089877aSRichard Tran Mills    Level: intermediate
3114f089877aSRichard Tran Mills 
3115bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3116f089877aSRichard Tran Mills 
3117f089877aSRichard Tran Mills @*/
31189371c9d4SSatish Balay PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l) {
3119f089877aSRichard Tran Mills   PetscFunctionBegin;
3120f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31219566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
3122f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3123f089877aSRichard Tran Mills }
3124f089877aSRichard Tran Mills 
3125f089877aSRichard Tran Mills /*@
3126bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3127bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3128f089877aSRichard Tran Mills 
3129d083f849SBarry Smith    Neighbor-wise Collective on dm
3130f089877aSRichard Tran Mills 
3131f089877aSRichard Tran Mills    Input Parameters:
3132bb7acecfSBarry Smith +  da - the `DM` object
3133bc0a1609SRichard Tran Mills .  g - the original local vector
3134bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3135f089877aSRichard Tran Mills 
3136bc0a1609SRichard Tran Mills    Output Parameter:
3137bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3138f089877aSRichard Tran Mills 
3139f089877aSRichard Tran Mills    Level: intermediate
3140f089877aSRichard Tran Mills 
3141bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3142f089877aSRichard Tran Mills 
3143f089877aSRichard Tran Mills @*/
31449371c9d4SSatish Balay PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l) {
3145f089877aSRichard Tran Mills   PetscFunctionBegin;
3146f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31479566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
3148f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3149f089877aSRichard Tran Mills }
3150f089877aSRichard Tran Mills 
315147c6ae99SBarry Smith /*@
3152bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
315347c6ae99SBarry Smith 
3154d083f849SBarry Smith     Collective on dm
315547c6ae99SBarry Smith 
3156d8d19677SJose E. Roman     Input Parameters:
3157bb7acecfSBarry Smith +   dm - the `DM` object
3158bb7acecfSBarry Smith -   comm - the communicator to contain the new `DM` object (or MPI_COMM_NULL)
315947c6ae99SBarry Smith 
316047c6ae99SBarry Smith     Output Parameter:
3161bb7acecfSBarry Smith .   dmc - the coarsened `DM`
316247c6ae99SBarry Smith 
316347c6ae99SBarry Smith     Level: developer
316447c6ae99SBarry Smith 
3165bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
316647c6ae99SBarry Smith 
316747c6ae99SBarry Smith @*/
31689371c9d4SSatish Balay PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc) {
3169b17ce1afSJed Brown   DMCoarsenHookLink link;
317047c6ae99SBarry Smith 
317147c6ae99SBarry Smith   PetscFunctionBegin;
3172171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31739566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3174dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3175b9d85ea2SLisandro Dalcin   if (*dmc) {
3176a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
31779566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
317843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31799566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3180644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
31810598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3182656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
31839566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3184b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
31859566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3186b17ce1afSJed Brown     }
3187b9d85ea2SLisandro Dalcin   }
31889566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
31897a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3190b17ce1afSJed Brown   PetscFunctionReturn(0);
3191b17ce1afSJed Brown }
3192b17ce1afSJed Brown 
3193bb9467b5SJed Brown /*@C
3194b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3195b17ce1afSJed Brown 
3196bb7acecfSBarry Smith    Logically Collective on fine
3197b17ce1afSJed Brown 
31984165533cSJose E. Roman    Input Parameters:
3199bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3200b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3201bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
32020298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3203b17ce1afSJed Brown 
3204b17ce1afSJed Brown    Calling sequence of coarsenhook:
3205b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3206b17ce1afSJed Brown 
3207bb7acecfSBarry Smith +  fine - fine level `DM`
3208bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3209b17ce1afSJed Brown -  ctx - optional user-defined function context
3210b17ce1afSJed Brown 
3211b17ce1afSJed Brown    Calling sequence for restricthook:
3212c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3213bb7acecfSBarry Smith $
3214bb7acecfSBarry Smith +  fine - fine level `DM`
3215bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3216c833c3b5SJed Brown .  rscale - scaling vector for restriction
3217c833c3b5SJed Brown .  inject - matrix restricting by injection
3218b17ce1afSJed Brown .  coarse - coarse level DM to update
3219b17ce1afSJed Brown -  ctx - optional user-defined function context
3220b17ce1afSJed Brown 
3221b17ce1afSJed Brown    Level: advanced
3222b17ce1afSJed Brown 
3223b17ce1afSJed Brown    Notes:
3224bb7acecfSBarry 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`.
3225b17ce1afSJed Brown 
3226b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3227b17ce1afSJed Brown 
3228b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3229bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3230b17ce1afSJed Brown 
3231bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3232bb7acecfSBarry Smith 
3233bb7acecfSBarry Smith    Fortran Note:
3234bb7acecfSBarry Smith    This function is not available from Fortran.
3235bb9467b5SJed Brown 
3236db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3237b17ce1afSJed Brown @*/
32389371c9d4SSatish Balay PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx) {
3239b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3240b17ce1afSJed Brown 
3241b17ce1afSJed Brown   PetscFunctionBegin;
3242b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
32431e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
32441e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32451e3d8eccSJed Brown   }
32469566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3247b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3248b17ce1afSJed Brown   link->restricthook = restricthook;
3249b17ce1afSJed Brown   link->ctx          = ctx;
32500298fd71SBarry Smith   link->next         = NULL;
3251b17ce1afSJed Brown   *p                 = link;
3252b17ce1afSJed Brown   PetscFunctionReturn(0);
3253b17ce1afSJed Brown }
3254b17ce1afSJed Brown 
3255dc822a44SJed Brown /*@C
3256bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3257dc822a44SJed Brown 
3258bb7acecfSBarry Smith    Logically Collective on fine
3259dc822a44SJed Brown 
32604165533cSJose E. Roman    Input Parameters:
3261bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3262dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3263bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3264dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3265dc822a44SJed Brown 
3266dc822a44SJed Brown    Level: advanced
3267dc822a44SJed Brown 
3268bb7acecfSBarry Smith    Note:
3269dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3270dc822a44SJed Brown 
3271bb7acecfSBarry Smith    Fortran Note:
3272bb7acecfSBarry Smith    This function is not available from Fortran.
3273dc822a44SJed Brown 
3274db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3275dc822a44SJed Brown @*/
32769371c9d4SSatish Balay PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx) {
3277dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3278dc822a44SJed Brown 
3279dc822a44SJed Brown   PetscFunctionBegin;
3280dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3281dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3282dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3283dc822a44SJed Brown       link = *p;
3284dc822a44SJed Brown       *p   = link->next;
32859566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3286dc822a44SJed Brown       break;
3287dc822a44SJed Brown     }
3288dc822a44SJed Brown   }
3289dc822a44SJed Brown   PetscFunctionReturn(0);
3290dc822a44SJed Brown }
3291dc822a44SJed Brown 
3292b17ce1afSJed Brown /*@
3293bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3294b17ce1afSJed Brown 
3295b17ce1afSJed Brown    Collective if any hooks are
3296b17ce1afSJed Brown 
32974165533cSJose E. Roman    Input Parameters:
3298bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3299bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3300e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3301bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3302e91eccc2SStefano Zampini -  coarse - coarser DM to update
3303b17ce1afSJed Brown 
3304b17ce1afSJed Brown    Level: developer
3305b17ce1afSJed Brown 
3306bb7acecfSBarry Smith    Developer Note:
3307bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3308bb7acecfSBarry Smith 
3309bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3310b17ce1afSJed Brown @*/
33119371c9d4SSatish Balay PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse) {
3312b17ce1afSJed Brown   DMCoarsenHookLink link;
3313b17ce1afSJed Brown 
3314b17ce1afSJed Brown   PetscFunctionBegin;
3315b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
33161baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3317b17ce1afSJed Brown   }
331847c6ae99SBarry Smith   PetscFunctionReturn(0);
331947c6ae99SBarry Smith }
332047c6ae99SBarry Smith 
3321bb9467b5SJed Brown /*@C
3322be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
33235dbd56e3SPeter Brune 
3324d083f849SBarry Smith    Logically Collective on global
33255dbd56e3SPeter Brune 
33264165533cSJose E. Roman    Input Parameters:
3327bb7acecfSBarry Smith +  global - global `DM`
3328bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
33295dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
33300298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
33315dbd56e3SPeter Brune 
3332ec4806b8SPeter Brune    Calling sequence for ddhook:
3333ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3334ec4806b8SPeter Brune 
3335bb7acecfSBarry Smith +  global - global `DM`
3336bb7acecfSBarry Smith .  block  - block `DM`
3337ec4806b8SPeter Brune -  ctx - optional user-defined function context
3338ec4806b8SPeter Brune 
33395dbd56e3SPeter Brune    Calling sequence for restricthook:
3340ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33415dbd56e3SPeter Brune 
3342bb7acecfSBarry Smith +  global - global `DM`
33435dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33445dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3345bb7acecfSBarry Smith .  block  - block `DM`
33465dbd56e3SPeter Brune -  ctx - optional user-defined function context
33475dbd56e3SPeter Brune 
33485dbd56e3SPeter Brune    Level: advanced
33495dbd56e3SPeter Brune 
33505dbd56e3SPeter Brune    Notes:
3351bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
33525dbd56e3SPeter Brune 
33535dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33545dbd56e3SPeter Brune 
33555dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3356bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
33575dbd56e3SPeter Brune 
3358bb7acecfSBarry Smith    Fortran Note:
3359bb7acecfSBarry Smith    This function is not available from Fortran.
3360bb9467b5SJed Brown 
3361bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
33625dbd56e3SPeter Brune @*/
33639371c9d4SSatish Balay PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx) {
3364be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
33655dbd56e3SPeter Brune 
33665dbd56e3SPeter Brune   PetscFunctionBegin;
33675dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3368b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
3369b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3370b3a6b972SJed Brown   }
33719566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
33725dbd56e3SPeter Brune   link->restricthook = restricthook;
3373be081cd6SPeter Brune   link->ddhook       = ddhook;
33745dbd56e3SPeter Brune   link->ctx          = ctx;
33750298fd71SBarry Smith   link->next         = NULL;
33765dbd56e3SPeter Brune   *p                 = link;
33775dbd56e3SPeter Brune   PetscFunctionReturn(0);
33785dbd56e3SPeter Brune }
33795dbd56e3SPeter Brune 
3380b3a6b972SJed Brown /*@C
3381b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3382b3a6b972SJed Brown 
3383bb7acecfSBarry Smith    Logically Collective on global
3384b3a6b972SJed Brown 
33854165533cSJose E. Roman    Input Parameters:
3386bb7acecfSBarry Smith +  global - global `DM`
3387bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3388b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3389b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3390b3a6b972SJed Brown 
3391b3a6b972SJed Brown    Level: advanced
3392b3a6b972SJed Brown 
3393bb7acecfSBarry Smith    Fortran Note:
3394bb7acecfSBarry Smith    This function is not available from Fortran.
3395b3a6b972SJed Brown 
3396db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3397b3a6b972SJed Brown @*/
33989371c9d4SSatish Balay PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx) {
3399b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3400b3a6b972SJed Brown 
3401b3a6b972SJed Brown   PetscFunctionBegin;
3402b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3403b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3404b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3405b3a6b972SJed Brown       link = *p;
3406b3a6b972SJed Brown       *p   = link->next;
34079566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3408b3a6b972SJed Brown       break;
3409b3a6b972SJed Brown     }
3410b3a6b972SJed Brown   }
3411b3a6b972SJed Brown   PetscFunctionReturn(0);
3412b3a6b972SJed Brown }
3413b3a6b972SJed Brown 
34145dbd56e3SPeter Brune /*@
3415bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
34165dbd56e3SPeter Brune 
34175dbd56e3SPeter Brune    Collective if any hooks are
34185dbd56e3SPeter Brune 
34194165533cSJose E. Roman    Input Parameters:
3420bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3421be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3422be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3423bb7acecfSBarry Smith -  coarse - coarser `DM` to update
34245dbd56e3SPeter Brune 
34255dbd56e3SPeter Brune    Level: developer
34265dbd56e3SPeter Brune 
3427db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
34285dbd56e3SPeter Brune @*/
34299371c9d4SSatish Balay PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm) {
3430be081cd6SPeter Brune   DMSubDomainHookLink link;
34315dbd56e3SPeter Brune 
34325dbd56e3SPeter Brune   PetscFunctionBegin;
3433be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
34341baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
34355dbd56e3SPeter Brune   }
34365dbd56e3SPeter Brune   PetscFunctionReturn(0);
34375dbd56e3SPeter Brune }
34385dbd56e3SPeter Brune 
34395fe1f584SPeter Brune /*@
3440bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
34415fe1f584SPeter Brune 
34425fe1f584SPeter Brune     Not Collective
34435fe1f584SPeter Brune 
34445fe1f584SPeter Brune     Input Parameter:
3445bb7acecfSBarry Smith .   dm - the `DM` object
34465fe1f584SPeter Brune 
34475fe1f584SPeter Brune     Output Parameter:
34486a7d9d85SPeter Brune .   level - number of coarsenings
34495fe1f584SPeter Brune 
34505fe1f584SPeter Brune     Level: developer
34515fe1f584SPeter Brune 
3452bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34535fe1f584SPeter Brune 
34545fe1f584SPeter Brune @*/
34559371c9d4SSatish Balay PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level) {
34565fe1f584SPeter Brune   PetscFunctionBegin;
34575fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3458b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
34595fe1f584SPeter Brune   *level = dm->leveldown;
34605fe1f584SPeter Brune   PetscFunctionReturn(0);
34615fe1f584SPeter Brune }
34625fe1f584SPeter Brune 
34639a64c4a8SMatthew G. Knepley /*@
3464bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
34659a64c4a8SMatthew G. Knepley 
3466bb7acecfSBarry Smith     Collective on dm
34679a64c4a8SMatthew G. Knepley 
34689a64c4a8SMatthew G. Knepley     Input Parameters:
3469bb7acecfSBarry Smith +   dm - the `DM` object
34709a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34719a64c4a8SMatthew G. Knepley 
34729a64c4a8SMatthew G. Knepley     Level: developer
34739a64c4a8SMatthew G. Knepley 
3474bb7acecfSBarry Smith     Note:
3475bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3476bb7acecfSBarry Smith 
3477bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34789a64c4a8SMatthew G. Knepley @*/
34799371c9d4SSatish Balay PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level) {
34809a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34819a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34829a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34839a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34849a64c4a8SMatthew G. Knepley }
34859a64c4a8SMatthew G. Knepley 
348647c6ae99SBarry Smith /*@C
3487bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
348847c6ae99SBarry Smith 
3489d083f849SBarry Smith     Collective on dm
349047c6ae99SBarry Smith 
3491d8d19677SJose E. Roman     Input Parameters:
3492bb7acecfSBarry Smith +   dm - the `DM` object
349347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
349447c6ae99SBarry Smith 
349547c6ae99SBarry Smith     Output Parameter:
3496bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
349747c6ae99SBarry Smith 
349847c6ae99SBarry Smith     Level: developer
349947c6ae99SBarry Smith 
3500bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
350147c6ae99SBarry Smith 
350247c6ae99SBarry Smith @*/
35039371c9d4SSatish Balay PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[]) {
350447c6ae99SBarry Smith   PetscFunctionBegin;
3505171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35067a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
350747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3508b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
350947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3510dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
351147c6ae99SBarry Smith   } else if (dm->ops->refine) {
351247c6ae99SBarry Smith     PetscInt i;
351347c6ae99SBarry Smith 
35149566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
3515*48a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3516ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
351747c6ae99SBarry Smith   PetscFunctionReturn(0);
351847c6ae99SBarry Smith }
351947c6ae99SBarry Smith 
352047c6ae99SBarry Smith /*@C
3521bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
352247c6ae99SBarry Smith 
3523d083f849SBarry Smith     Collective on dm
352447c6ae99SBarry Smith 
3525d8d19677SJose E. Roman     Input Parameters:
3526bb7acecfSBarry Smith +   dm - the `DM` object
352747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
352847c6ae99SBarry Smith 
352947c6ae99SBarry Smith     Output Parameter:
3530bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
353147c6ae99SBarry Smith 
353247c6ae99SBarry Smith     Level: developer
353347c6ae99SBarry Smith 
3534bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
353547c6ae99SBarry Smith 
353647c6ae99SBarry Smith @*/
35379371c9d4SSatish Balay PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[]) {
353847c6ae99SBarry Smith   PetscFunctionBegin;
3539171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35407a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
354147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
354247c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
354347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3544dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
354547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
354647c6ae99SBarry Smith     PetscInt i;
354747c6ae99SBarry Smith 
35489566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
3549*48a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3550ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
355147c6ae99SBarry Smith   PetscFunctionReturn(0);
355247c6ae99SBarry Smith }
355347c6ae99SBarry Smith 
35541a266240SBarry Smith /*@C
3555bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
35561a266240SBarry Smith 
3557bb7acecfSBarry Smith     Logically Collective if the function is collective
35581a266240SBarry Smith 
35591a266240SBarry Smith     Input Parameters:
3560bb7acecfSBarry Smith +   dm - the `DM` object
35611a266240SBarry Smith -   destroy - the destroy function
35621a266240SBarry Smith 
35631a266240SBarry Smith     Level: intermediate
35641a266240SBarry Smith 
3565bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
35661a266240SBarry Smith 
3567f07f9ceaSJed Brown @*/
35689371c9d4SSatish Balay PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **)) {
35691a266240SBarry Smith   PetscFunctionBegin;
3570171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35711a266240SBarry Smith   dm->ctxdestroy = destroy;
35721a266240SBarry Smith   PetscFunctionReturn(0);
35731a266240SBarry Smith }
35741a266240SBarry Smith 
3575b07ff414SBarry Smith /*@
3576bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
357747c6ae99SBarry Smith 
357847c6ae99SBarry Smith     Not Collective
357947c6ae99SBarry Smith 
358047c6ae99SBarry Smith     Input Parameters:
3581bb7acecfSBarry Smith +   dm - the `DM` object
358247c6ae99SBarry Smith -   ctx - the user context
358347c6ae99SBarry Smith 
358447c6ae99SBarry Smith     Level: intermediate
358547c6ae99SBarry Smith 
3586bb7acecfSBarry Smith     Note:
3587bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3588bb7acecfSBarry Smith 
3589bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
359047c6ae99SBarry Smith 
359147c6ae99SBarry Smith @*/
35929371c9d4SSatish Balay PetscErrorCode DMSetApplicationContext(DM dm, void *ctx) {
359347c6ae99SBarry Smith   PetscFunctionBegin;
3594171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
359547c6ae99SBarry Smith   dm->ctx = ctx;
359647c6ae99SBarry Smith   PetscFunctionReturn(0);
359747c6ae99SBarry Smith }
359847c6ae99SBarry Smith 
359947c6ae99SBarry Smith /*@
3600bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
360147c6ae99SBarry Smith 
360247c6ae99SBarry Smith     Not Collective
360347c6ae99SBarry Smith 
360447c6ae99SBarry Smith     Input Parameter:
3605bb7acecfSBarry Smith .   dm - the `DM` object
360647c6ae99SBarry Smith 
360747c6ae99SBarry Smith     Output Parameter:
360847c6ae99SBarry Smith .   ctx - the user context
360947c6ae99SBarry Smith 
361047c6ae99SBarry Smith     Level: intermediate
361147c6ae99SBarry Smith 
3612bb7acecfSBarry Smith     Note:
3613bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3614bb7acecfSBarry Smith 
3615bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
361647c6ae99SBarry Smith 
361747c6ae99SBarry Smith @*/
36189371c9d4SSatish Balay PetscErrorCode DMGetApplicationContext(DM dm, void *ctx) {
361947c6ae99SBarry Smith   PetscFunctionBegin;
3620171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36211b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
362247c6ae99SBarry Smith   PetscFunctionReturn(0);
362347c6ae99SBarry Smith }
362447c6ae99SBarry Smith 
362508da532bSDmitry Karpeev /*@C
3626bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
362708da532bSDmitry Karpeev 
3628d083f849SBarry Smith     Logically Collective on dm
362908da532bSDmitry Karpeev 
3630d8d19677SJose E. Roman     Input Parameters:
363108da532bSDmitry Karpeev +   dm - the DM object
36320298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
363308da532bSDmitry Karpeev 
363408da532bSDmitry Karpeev     Level: intermediate
363508da532bSDmitry Karpeev 
3636bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3637db781477SPatrick Sanan          `DMSetJacobian()`
363808da532bSDmitry Karpeev 
363908da532bSDmitry Karpeev @*/
36409371c9d4SSatish Balay PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec)) {
364108da532bSDmitry Karpeev   PetscFunctionBegin;
36425a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
364308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
364408da532bSDmitry Karpeev   PetscFunctionReturn(0);
364508da532bSDmitry Karpeev }
364608da532bSDmitry Karpeev 
364708da532bSDmitry Karpeev /*@
3648bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
364908da532bSDmitry Karpeev 
365008da532bSDmitry Karpeev     Not Collective
365108da532bSDmitry Karpeev 
365208da532bSDmitry Karpeev     Input Parameter:
3653bb7acecfSBarry Smith .   dm - the `DM` object to destroy
365408da532bSDmitry Karpeev 
365508da532bSDmitry Karpeev     Output Parameter:
3656bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
365708da532bSDmitry Karpeev 
365808da532bSDmitry Karpeev     Level: developer
365908da532bSDmitry Karpeev 
3660bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
366108da532bSDmitry Karpeev 
366208da532bSDmitry Karpeev @*/
36639371c9d4SSatish Balay PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg) {
366408da532bSDmitry Karpeev   PetscFunctionBegin;
36655a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3666534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
366708da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
366808da532bSDmitry Karpeev   PetscFunctionReturn(0);
366908da532bSDmitry Karpeev }
367008da532bSDmitry Karpeev 
367108da532bSDmitry Karpeev /*@C
3672bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
367308da532bSDmitry Karpeev 
3674d083f849SBarry Smith     Logically Collective on dm
367508da532bSDmitry Karpeev 
3676f899ff85SJose E. Roman     Input Parameter:
3677bb7acecfSBarry Smith .   dm - the `DM` object
367808da532bSDmitry Karpeev 
367908da532bSDmitry Karpeev     Output parameters:
368008da532bSDmitry Karpeev +   xl - lower bound
368108da532bSDmitry Karpeev -   xu - upper bound
368208da532bSDmitry Karpeev 
3683907376e6SBarry Smith     Level: advanced
3684907376e6SBarry Smith 
368595452b02SPatrick Sanan     Notes:
368695452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
368708da532bSDmitry Karpeev 
3688bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
368908da532bSDmitry Karpeev 
369008da532bSDmitry Karpeev @*/
36919371c9d4SSatish Balay PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu) {
369208da532bSDmitry Karpeev   PetscFunctionBegin;
36935a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
36955a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3696dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
369708da532bSDmitry Karpeev   PetscFunctionReturn(0);
369808da532bSDmitry Karpeev }
369908da532bSDmitry Karpeev 
3700b0ae01b7SPeter Brune /*@
3701bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3702b0ae01b7SPeter Brune 
3703b0ae01b7SPeter Brune     Not Collective
3704b0ae01b7SPeter Brune 
3705b0ae01b7SPeter Brune     Input Parameter:
3706b0ae01b7SPeter Brune .   dm - the DM object
3707b0ae01b7SPeter Brune 
3708b0ae01b7SPeter Brune     Output Parameter:
3709bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3710b0ae01b7SPeter Brune 
3711b0ae01b7SPeter Brune     Level: developer
3712b0ae01b7SPeter Brune 
3713bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3714b0ae01b7SPeter Brune 
3715b0ae01b7SPeter Brune @*/
37169371c9d4SSatish Balay PetscErrorCode DMHasColoring(DM dm, PetscBool *flg) {
3717b0ae01b7SPeter Brune   PetscFunctionBegin;
37185a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3719534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3720b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3721b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3722b0ae01b7SPeter Brune }
3723b0ae01b7SPeter Brune 
37243ad4599aSBarry Smith /*@
3725bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
37263ad4599aSBarry Smith 
37273ad4599aSBarry Smith     Not Collective
37283ad4599aSBarry Smith 
37293ad4599aSBarry Smith     Input Parameter:
3730bb7acecfSBarry Smith .   dm - the `DM` object
37313ad4599aSBarry Smith 
37323ad4599aSBarry Smith     Output Parameter:
3733bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
37343ad4599aSBarry Smith 
37353ad4599aSBarry Smith     Level: developer
37363ad4599aSBarry Smith 
3737bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
37383ad4599aSBarry Smith 
37393ad4599aSBarry Smith @*/
37409371c9d4SSatish Balay PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg) {
37413ad4599aSBarry Smith   PetscFunctionBegin;
37425a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3743534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
37443ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37453ad4599aSBarry Smith   PetscFunctionReturn(0);
37463ad4599aSBarry Smith }
37473ad4599aSBarry Smith 
3748a7058e45SLawrence Mitchell /*@
3749bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3750a7058e45SLawrence Mitchell 
3751a7058e45SLawrence Mitchell     Not Collective
3752a7058e45SLawrence Mitchell 
3753a7058e45SLawrence Mitchell     Input Parameter:
3754bb7acecfSBarry Smith .   dm - the `DM` object
3755a7058e45SLawrence Mitchell 
3756a7058e45SLawrence Mitchell     Output Parameter:
3757bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3758a7058e45SLawrence Mitchell 
3759a7058e45SLawrence Mitchell     Level: developer
3760a7058e45SLawrence Mitchell 
3761bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3762a7058e45SLawrence Mitchell 
3763a7058e45SLawrence Mitchell @*/
37649371c9d4SSatish Balay PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg) {
3765a7058e45SLawrence Mitchell   PetscFunctionBegin;
37665a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3767534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3768dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
37699371c9d4SSatish Balay   else { *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE; }
3770a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3771a7058e45SLawrence Mitchell }
3772a7058e45SLawrence Mitchell 
37730298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3774264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3775264ace61SBarry Smith 
3776264ace61SBarry Smith /*@C
3777bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3778264ace61SBarry Smith 
3779d083f849SBarry Smith   Collective on dm
3780264ace61SBarry Smith 
3781264ace61SBarry Smith   Input Parameters:
3782bb7acecfSBarry Smith + dm     - The `DM` object
3783bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3784264ace61SBarry Smith 
3785264ace61SBarry Smith   Options Database Key:
3786bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3787264ace61SBarry Smith 
3788264ace61SBarry Smith   Level: intermediate
3789264ace61SBarry Smith 
3790bb7acecfSBarry Smith   Note:
3791bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3792bb7acecfSBarry Smith 
3793bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3794264ace61SBarry Smith @*/
37959371c9d4SSatish Balay PetscErrorCode DMSetType(DM dm, DMType method) {
3796264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3797264ace61SBarry Smith   PetscBool match;
3798264ace61SBarry Smith 
3799264ace61SBarry Smith   PetscFunctionBegin;
3800264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38019566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
3802264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3803264ace61SBarry Smith 
38049566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
38059566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
38067a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3807264ace61SBarry Smith 
3808dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
38099566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
38109566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
38119566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3812264ace61SBarry Smith   PetscFunctionReturn(0);
3813264ace61SBarry Smith }
3814264ace61SBarry Smith 
3815264ace61SBarry Smith /*@C
3816bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3817264ace61SBarry Smith 
3818264ace61SBarry Smith   Not Collective
3819264ace61SBarry Smith 
3820264ace61SBarry Smith   Input Parameter:
3821bb7acecfSBarry Smith . dm  - The `DM`
3822264ace61SBarry Smith 
3823264ace61SBarry Smith   Output Parameter:
3824bb7acecfSBarry Smith . type - The `DMType` name
3825264ace61SBarry Smith 
3826264ace61SBarry Smith   Level: intermediate
3827264ace61SBarry Smith 
3828bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3829264ace61SBarry Smith @*/
38309371c9d4SSatish Balay PetscErrorCode DMGetType(DM dm, DMType *type) {
3831264ace61SBarry Smith   PetscFunctionBegin;
3832264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3833c959eef4SJed Brown   PetscValidPointer(type, 2);
38349566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3835264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3836264ace61SBarry Smith   PetscFunctionReturn(0);
3837264ace61SBarry Smith }
3838264ace61SBarry Smith 
383967a56275SMatthew G Knepley /*@C
3840bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
384167a56275SMatthew G Knepley 
3842d083f849SBarry Smith   Collective on dm
384367a56275SMatthew G Knepley 
384467a56275SMatthew G Knepley   Input Parameters:
3845bb7acecfSBarry Smith + dm - the `DM`
3846bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
384767a56275SMatthew G Knepley 
384867a56275SMatthew G Knepley   Output Parameter:
3849bb7acecfSBarry Smith . M - pointer to new `DM`
385067a56275SMatthew G Knepley 
385167a56275SMatthew G Knepley   Notes:
3852bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3853bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3854bb7acecfSBarry Smith   of the input `DM`.
385567a56275SMatthew G Knepley 
385667a56275SMatthew G Knepley   Level: intermediate
385767a56275SMatthew G Knepley 
3858bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
385967a56275SMatthew G Knepley @*/
38609371c9d4SSatish Balay PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M) {
386167a56275SMatthew G Knepley   DM        B;
386267a56275SMatthew G Knepley   char      convname[256];
3863c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
386467a56275SMatthew G Knepley 
386567a56275SMatthew G Knepley   PetscFunctionBegin;
386667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
386767a56275SMatthew G Knepley   PetscValidType(dm, 1);
386867a56275SMatthew G Knepley   PetscValidPointer(M, 3);
38699566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
38709566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3871c067b6caSMatthew G. Knepley   if (sametype) {
3872c067b6caSMatthew G. Knepley     *M = dm;
38739566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
3874c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3875c067b6caSMatthew G. Knepley   } else {
38760298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
387767a56275SMatthew G Knepley 
387867a56275SMatthew G Knepley     /*
387967a56275SMatthew G Knepley        Order of precedence:
388067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
388167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
388267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
388367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
388467a56275SMatthew G Knepley        5) Use a really basic converter.
388567a56275SMatthew G Knepley     */
388667a56275SMatthew G Knepley 
388767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
38889566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
38899566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
38909566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
38919566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
38929566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
38939566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
389467a56275SMatthew G Knepley     if (conv) goto foundconv;
389567a56275SMatthew G Knepley 
389667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
38979566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
38989566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
38999566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39009566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39019566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39029566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39039566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39049566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
390567a56275SMatthew G Knepley     if (conv) {
39069566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
390767a56275SMatthew G Knepley       goto foundconv;
390867a56275SMatthew G Knepley     }
390967a56275SMatthew G Knepley 
391067a56275SMatthew G Knepley #if 0
391167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
391267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
39139566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
391467a56275SMatthew G Knepley     if (conv) goto foundconv;
391567a56275SMatthew G Knepley 
391667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
391767a56275SMatthew G Knepley     if (dm->ops->convert) {
391867a56275SMatthew G Knepley       conv = dm->ops->convert;
391967a56275SMatthew G Knepley     }
392067a56275SMatthew G Knepley     if (conv) goto foundconv;
392167a56275SMatthew G Knepley #endif
392267a56275SMatthew G Knepley 
392367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
392498921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
392567a56275SMatthew G Knepley 
392667a56275SMatthew G Knepley   foundconv:
39279566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
39289566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
392912fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
393090b157c4SStefano Zampini     {
39314fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
39326858538eSMatthew G. Knepley 
39334fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
39344fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
3935c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
39369566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
39379566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
39389566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
39399566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
394012fa691eSMatthew G. Knepley     }
39419566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
394267a56275SMatthew G Knepley   }
39439566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
394467a56275SMatthew G Knepley   PetscFunctionReturn(0);
394567a56275SMatthew G Knepley }
3946264ace61SBarry Smith 
3947264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3948264ace61SBarry Smith 
3949264ace61SBarry Smith /*@C
3950bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
39511c84c290SBarry Smith 
39521c84c290SBarry Smith   Not Collective
39531c84c290SBarry Smith 
39541c84c290SBarry Smith   Input Parameters:
39551c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39561c84c290SBarry Smith - create_func - The creation routine itself
39571c84c290SBarry Smith 
39581c84c290SBarry Smith   Notes:
3959bb7acecfSBarry Smith   DMRegister() may be called multiple times to add several user-defined `DM`s
39601c84c290SBarry Smith 
39611c84c290SBarry Smith   Sample usage:
39621c84c290SBarry Smith .vb
3963bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39641c84c290SBarry Smith .ve
39651c84c290SBarry Smith 
39661c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39671c84c290SBarry Smith .vb
39681c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39691c84c290SBarry Smith     DMSetType(DM,"my_da");
39701c84c290SBarry Smith .ve
39711c84c290SBarry Smith    or at runtime via the option
39721c84c290SBarry Smith .vb
39731c84c290SBarry Smith     -da_type my_da
39741c84c290SBarry Smith .ve
3975264ace61SBarry Smith 
3976264ace61SBarry Smith   Level: advanced
39771c84c290SBarry Smith 
3978bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
39791c84c290SBarry Smith 
3980264ace61SBarry Smith @*/
39819371c9d4SSatish Balay PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM)) {
3982264ace61SBarry Smith   PetscFunctionBegin;
39839566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
39849566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
3985264ace61SBarry Smith   PetscFunctionReturn(0);
3986264ace61SBarry Smith }
3987264ace61SBarry Smith 
3988b859378eSBarry Smith /*@C
3989bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
3990b859378eSBarry Smith 
3991d083f849SBarry Smith   Collective on viewer
3992b859378eSBarry Smith 
3993b859378eSBarry Smith   Input Parameters:
3994bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
3995bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
3996bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
3997bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
3998b859378eSBarry Smith 
3999b859378eSBarry Smith    Level: intermediate
4000b859378eSBarry Smith 
4001b859378eSBarry Smith   Notes:
400255849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4003b859378eSBarry Smith 
4004bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4005bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4006bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4007cd7e8a5eSksagiyam 
4008b859378eSBarry Smith   Notes for advanced users:
4009b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4010bb7acecfSBarry Smith   format, since `DMLoad()` and `DMView()` completely hide these details.
4011b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4012b859378eSBarry Smith   format is
4013b859378eSBarry Smith .vb
4014b859378eSBarry Smith      has not yet been determined
4015b859378eSBarry Smith .ve
4016b859378eSBarry Smith 
4017db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4018b859378eSBarry Smith @*/
40199371c9d4SSatish Balay PetscErrorCode DMLoad(DM newdm, PetscViewer viewer) {
40209331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4021b859378eSBarry Smith 
4022b859378eSBarry Smith   PetscFunctionBegin;
4023b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4024b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
40259566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
40269566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
40279566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
40289566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
40299331c7a4SMatthew G. Knepley   if (isbinary) {
40309331c7a4SMatthew G. Knepley     PetscInt classid;
40319331c7a4SMatthew G. Knepley     char     type[256];
4032b859378eSBarry Smith 
40339566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
40347a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
40359566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
40369566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4037dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
40389331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4039dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
40409331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
40419566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
4042b859378eSBarry Smith   PetscFunctionReturn(0);
4043b859378eSBarry Smith }
4044b859378eSBarry Smith 
40457da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40467da65231SMatthew G Knepley 
40479371c9d4SSatish Balay PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
40481d47ebbbSSatish Balay   PetscInt f;
40491b30c384SMatthew G Knepley 
40507da65231SMatthew G Knepley   PetscFunctionBegin;
405163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
4052*48a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
40537da65231SMatthew G Knepley   PetscFunctionReturn(0);
40547da65231SMatthew G Knepley }
40557da65231SMatthew G Knepley 
40569371c9d4SSatish Balay PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
40571b30c384SMatthew G Knepley   PetscInt f, g;
40587da65231SMatthew G Knepley 
40597da65231SMatthew G Knepley   PetscFunctionBegin;
406063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
40611d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
40629566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
4063*48a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
40649566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
40657da65231SMatthew G Knepley   }
40667da65231SMatthew G Knepley   PetscFunctionReturn(0);
40677da65231SMatthew G Knepley }
4068e7c4fc90SDmitry Karpeev 
40699371c9d4SSatish Balay PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X) {
40700c5b8624SToby Isaac   PetscInt           localSize, bs;
40710c5b8624SToby Isaac   PetscMPIInt        size;
40720c5b8624SToby Isaac   Vec                x, xglob;
40730c5b8624SToby Isaac   const PetscScalar *xarray;
4074e759306cSMatthew G. Knepley 
4075e759306cSMatthew G. Knepley   PetscFunctionBegin;
40769566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
40779566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
40789566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
40799566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
40809566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
40810c5b8624SToby Isaac   if (size > 1) {
40829566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
40839566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
40849566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
40859566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
40860c5b8624SToby Isaac   } else {
40870c5b8624SToby Isaac     xglob = x;
40880c5b8624SToby Isaac   }
40899566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
40900c5b8624SToby Isaac   if (size > 1) {
40919566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
40929566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
40930c5b8624SToby Isaac   }
40949566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4095e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4096e759306cSMatthew G. Knepley }
4097e759306cSMatthew G. Knepley 
409888ed4aceSMatthew G Knepley /*@
4099bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4100061576a5SJed Brown 
4101061576a5SJed Brown   Input Parameter:
4102bb7acecfSBarry Smith . dm - The `DM`
4103061576a5SJed Brown 
4104061576a5SJed Brown   Output Parameter:
4105bb7acecfSBarry Smith . section - The `PetscSection`
4106061576a5SJed Brown 
4107061576a5SJed Brown   Options Database Keys:
4108bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4109061576a5SJed Brown 
4110061576a5SJed Brown   Level: advanced
4111061576a5SJed Brown 
4112061576a5SJed Brown   Notes:
4113bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4114061576a5SJed Brown 
4115bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4116061576a5SJed Brown 
4117db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4118061576a5SJed Brown @*/
41199371c9d4SSatish Balay PetscErrorCode DMGetSection(DM dm, PetscSection *section) {
4120061576a5SJed Brown   PetscFunctionBegin;
41219566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
4122061576a5SJed Brown   PetscFunctionReturn(0);
4123061576a5SJed Brown }
4124061576a5SJed Brown 
4125061576a5SJed Brown /*@
4126bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
412788ed4aceSMatthew G Knepley 
412888ed4aceSMatthew G Knepley   Input Parameter:
4129bb7acecfSBarry Smith . dm - The `DM`
413088ed4aceSMatthew G Knepley 
413188ed4aceSMatthew G Knepley   Output Parameter:
4132bb7acecfSBarry Smith . section - The `PetscSection`
413388ed4aceSMatthew G Knepley 
4134e5893cccSMatthew G. Knepley   Options Database Keys:
4135bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4136e5893cccSMatthew G. Knepley 
413788ed4aceSMatthew G Knepley   Level: intermediate
413888ed4aceSMatthew G Knepley 
4139bb7acecfSBarry Smith   Note:
4140bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
414188ed4aceSMatthew G Knepley 
4142db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
414388ed4aceSMatthew G Knepley @*/
41449371c9d4SSatish Balay PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section) {
414588ed4aceSMatthew G Knepley   PetscFunctionBegin;
414688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
414788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41481bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4149e5e52638SMatthew G. Knepley     PetscInt d;
4150e5e52638SMatthew G. Knepley 
415145480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
415245480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
415345480ffeSMatthew G. Knepley       PetscViewer       viewer;
415445480ffeSMatthew G. Knepley       PetscViewerFormat format;
415545480ffeSMatthew G. Knepley       PetscBool         flg;
415645480ffeSMatthew G. Knepley 
41579566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
41589566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
415945480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
41609566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
41619566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
416245480ffeSMatthew G. Knepley       }
416345480ffeSMatthew G. Knepley       if (flg) {
41649566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
41659566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
41669566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
416745480ffeSMatthew G. Knepley       }
416845480ffeSMatthew G. Knepley     }
4169dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
41709566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
41712f0f8703SMatthew G. Knepley   }
41721bb6d2a8SBarry Smith   *section = dm->localSection;
417388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
417488ed4aceSMatthew G Knepley }
417588ed4aceSMatthew G Knepley 
417688ed4aceSMatthew G Knepley /*@
4177bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4178061576a5SJed Brown 
4179061576a5SJed Brown   Input Parameters:
4180bb7acecfSBarry Smith + dm - The `DM`
4181bb7acecfSBarry Smith - section - The `PetscSection`
4182061576a5SJed Brown 
4183061576a5SJed Brown   Level: advanced
4184061576a5SJed Brown 
4185061576a5SJed Brown   Notes:
4186bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4187061576a5SJed Brown 
4188bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4189061576a5SJed Brown 
4190db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4191061576a5SJed Brown @*/
41929371c9d4SSatish Balay PetscErrorCode DMSetSection(DM dm, PetscSection section) {
4193061576a5SJed Brown   PetscFunctionBegin;
41949566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
4195061576a5SJed Brown   PetscFunctionReturn(0);
4196061576a5SJed Brown }
4197061576a5SJed Brown 
4198061576a5SJed Brown /*@
4199bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
420088ed4aceSMatthew G Knepley 
420188ed4aceSMatthew G Knepley   Input Parameters:
4202bb7acecfSBarry Smith + dm - The `DM`
4203bb7acecfSBarry Smith - section - The `PetscSection`
420488ed4aceSMatthew G Knepley 
420588ed4aceSMatthew G Knepley   Level: intermediate
420688ed4aceSMatthew G Knepley 
4207bb7acecfSBarry Smith   Note:
4208bb7acecfSBarry Smith   Any existing Section will be destroyed
420988ed4aceSMatthew G Knepley 
4210bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
421188ed4aceSMatthew G Knepley @*/
42129371c9d4SSatish Balay PetscErrorCode DMSetLocalSection(DM dm, PetscSection section) {
4213c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4214af122d2aSMatthew G Knepley   PetscInt f;
421588ed4aceSMatthew G Knepley 
421688ed4aceSMatthew G Knepley   PetscFunctionBegin;
421788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4218b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42199566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
42209566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
42211bb6d2a8SBarry Smith   dm->localSection = section;
42229566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4223af122d2aSMatthew G Knepley   if (numFields) {
42249566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4225af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
42260f21e855SMatthew G. Knepley       PetscObject disc;
4227af122d2aSMatthew G Knepley       const char *name;
4228af122d2aSMatthew G Knepley 
42299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
42309566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
42319566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4232af122d2aSMatthew G Knepley     }
4233af122d2aSMatthew G Knepley   }
4234e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
42359566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
423688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
423788ed4aceSMatthew G Knepley }
423888ed4aceSMatthew G Knepley 
42399435951eSToby Isaac /*@
4240bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
42419435951eSToby Isaac 
4242e228b242SToby Isaac   not collective
4243e228b242SToby Isaac 
42449435951eSToby Isaac   Input Parameter:
4245bb7acecfSBarry Smith . dm - The `DM`
42469435951eSToby Isaac 
4247d8d19677SJose E. Roman   Output Parameters:
4248bb7acecfSBarry 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.
4249bb7acecfSBarry 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.
425079769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
42519435951eSToby Isaac 
42529435951eSToby Isaac   Level: advanced
42539435951eSToby Isaac 
4254bb7acecfSBarry Smith   Note:
4255bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
42569435951eSToby Isaac 
4257db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
42589435951eSToby Isaac @*/
42599371c9d4SSatish Balay PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias) {
42609435951eSToby Isaac   PetscFunctionBegin;
42619435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4262dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
42633b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
42643b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
426579769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
42669435951eSToby Isaac   PetscFunctionReturn(0);
42679435951eSToby Isaac }
42689435951eSToby Isaac 
42699435951eSToby Isaac /*@
4270bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
42719435951eSToby Isaac 
4272bb7acecfSBarry 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()`.
42739435951eSToby Isaac 
4274bb7acecfSBarry 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.
42759435951eSToby Isaac 
4276e228b242SToby Isaac   collective on dm
4277e228b242SToby Isaac 
42789435951eSToby Isaac   Input Parameters:
4279bb7acecfSBarry Smith + dm - The `DM`
4280bb7acecfSBarry 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).
4281bb7acecfSBarry 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).
4282bb7acecfSBarry 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).
42839435951eSToby Isaac 
42849435951eSToby Isaac   Level: advanced
42859435951eSToby Isaac 
4286bb7acecfSBarry Smith   Note:
4287bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
42889435951eSToby Isaac 
4289db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
42909435951eSToby Isaac @*/
42919371c9d4SSatish Balay PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias) {
4292e228b242SToby Isaac   PetscMPIInt result;
42939435951eSToby Isaac 
42949435951eSToby Isaac   PetscFunctionBegin;
42959435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4296e228b242SToby Isaac   if (section) {
4297e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42989566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
42997a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4300e228b242SToby Isaac   }
4301e228b242SToby Isaac   if (mat) {
4302e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
43039566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
43047a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4305e228b242SToby Isaac   }
430679769bd5SJed Brown   if (bias) {
430779769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
43089566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
430979769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
431079769bd5SJed Brown   }
43119566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43129566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
43133b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
43149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
43159566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
43163b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
43179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
43189566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
431979769bd5SJed Brown   dm->defaultConstraint.bias = bias;
43209435951eSToby Isaac   PetscFunctionReturn(0);
43219435951eSToby Isaac }
43229435951eSToby Isaac 
4323497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4324507e4973SMatthew G. Knepley /*
4325bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4326507e4973SMatthew G. Knepley 
4327507e4973SMatthew G. Knepley   Input Parameters:
4328bb7acecfSBarry Smith + dm - The `DM`
4329bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4330bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4331507e4973SMatthew G. Knepley 
4332507e4973SMatthew G. Knepley   Level: intermediate
4333507e4973SMatthew G. Knepley 
4334db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4335507e4973SMatthew G. Knepley */
43369371c9d4SSatish Balay static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection) {
4337507e4973SMatthew G. Knepley   MPI_Comm        comm;
4338507e4973SMatthew G. Knepley   PetscLayout     layout;
4339507e4973SMatthew G. Knepley   const PetscInt *ranges;
4340507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4341507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4342507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4343507e4973SMatthew G. Knepley 
4344507e4973SMatthew G. Knepley   PetscFunctionBegin;
43459566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4346507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43479566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
43489566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
43499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
43509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
43519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
43529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
43539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
43549566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
43559566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4356507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4357f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4358507e4973SMatthew G. Knepley 
43599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
43609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
43619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
43629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
43639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
43649566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4365507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
43669371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
43679371c9d4SSatish 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));
43689371c9d4SSatish Balay       valid = PETSC_FALSE;
43699371c9d4SSatish Balay     }
43709371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
43719371c9d4SSatish 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));
43729371c9d4SSatish Balay       valid = PETSC_FALSE;
43739371c9d4SSatish Balay     }
4374507e4973SMatthew G. Knepley     if (gdof < 0) {
4375507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4376507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4377507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4378507e4973SMatthew G. Knepley 
43799566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4380507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
43819371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
43829371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
43839371c9d4SSatish Balay           valid = PETSC_FALSE;
43849371c9d4SSatish Balay           break;
43859371c9d4SSatish Balay         }
4386507e4973SMatthew G. Knepley       }
4387507e4973SMatthew G. Knepley     }
4388507e4973SMatthew G. Knepley   }
43899566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
43909566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
43911c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4392507e4973SMatthew G. Knepley   if (!gvalid) {
43939566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4394507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4395507e4973SMatthew G. Knepley   }
4396507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4397507e4973SMatthew G. Knepley }
4398f741bcd2SMatthew G. Knepley #endif
4399507e4973SMatthew G. Knepley 
440088ed4aceSMatthew G Knepley /*@
4401bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
440288ed4aceSMatthew G Knepley 
4403d083f849SBarry Smith   Collective on dm
44048b1ab98fSJed Brown 
440588ed4aceSMatthew G Knepley   Input Parameter:
4406bb7acecfSBarry Smith . dm - The `DM`
440788ed4aceSMatthew G Knepley 
440888ed4aceSMatthew G Knepley   Output Parameter:
4409bb7acecfSBarry Smith . section - The `PetscSection`
441088ed4aceSMatthew G Knepley 
441188ed4aceSMatthew G Knepley   Level: intermediate
441288ed4aceSMatthew G Knepley 
4413bb7acecfSBarry Smith   Note:
4414bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
441588ed4aceSMatthew G Knepley 
4416db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
441788ed4aceSMatthew G Knepley @*/
44189371c9d4SSatish Balay PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section) {
441988ed4aceSMatthew G Knepley   PetscFunctionBegin;
442088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44221bb6d2a8SBarry Smith   if (!dm->globalSection) {
4423fd59a867SMatthew G. Knepley     PetscSection s;
4424fd59a867SMatthew G. Knepley 
44259566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
44267a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
44277a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
44289566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
44299566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
44309566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
44319566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
443288ed4aceSMatthew G Knepley   }
44331bb6d2a8SBarry Smith   *section = dm->globalSection;
443488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
443588ed4aceSMatthew G Knepley }
443688ed4aceSMatthew G Knepley 
4437b21d0597SMatthew G Knepley /*@
4438bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4439b21d0597SMatthew G Knepley 
4440b21d0597SMatthew G Knepley   Input Parameters:
4441bb7acecfSBarry Smith + dm - The `DM`
44425080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4443b21d0597SMatthew G Knepley 
4444b21d0597SMatthew G Knepley   Level: intermediate
4445b21d0597SMatthew G Knepley 
4446bb7acecfSBarry Smith   Note:
4447bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4448b21d0597SMatthew G Knepley 
4449db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4450b21d0597SMatthew G Knepley @*/
44519371c9d4SSatish Balay PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section) {
4452b21d0597SMatthew G Knepley   PetscFunctionBegin;
4453b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44545080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44569566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
44571bb6d2a8SBarry Smith   dm->globalSection = section;
4458497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
44599566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4460507e4973SMatthew G. Knepley #endif
4461b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4462b21d0597SMatthew G Knepley }
4463b21d0597SMatthew G Knepley 
446488ed4aceSMatthew G Knepley /*@
4465bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4466bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
446788ed4aceSMatthew G Knepley 
446888ed4aceSMatthew G Knepley   Input Parameter:
4469bb7acecfSBarry Smith . dm - The `DM`
447088ed4aceSMatthew G Knepley 
447188ed4aceSMatthew G Knepley   Output Parameter:
4472bb7acecfSBarry Smith . sf - The `PetscSF`
447388ed4aceSMatthew G Knepley 
447488ed4aceSMatthew G Knepley   Level: intermediate
447588ed4aceSMatthew G Knepley 
4476bb7acecfSBarry Smith   Note:
4477bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
447888ed4aceSMatthew G Knepley 
4479db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
448088ed4aceSMatthew G Knepley @*/
44819371c9d4SSatish Balay PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf) {
448288ed4aceSMatthew G Knepley   PetscInt nroots;
448388ed4aceSMatthew G Knepley 
448488ed4aceSMatthew G Knepley   PetscFunctionBegin;
448588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
448688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
4487*48a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
44889566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
448988ed4aceSMatthew G Knepley   if (nroots < 0) {
449088ed4aceSMatthew G Knepley     PetscSection section, gSection;
449188ed4aceSMatthew G Knepley 
44929566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
449331ea6d37SMatthew G Knepley     if (section) {
44949566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
44959566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
449631ea6d37SMatthew G Knepley     } else {
44970298fd71SBarry Smith       *sf = NULL;
449831ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
449931ea6d37SMatthew G Knepley     }
450088ed4aceSMatthew G Knepley   }
45011bb6d2a8SBarry Smith   *sf = dm->sectionSF;
450288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
450388ed4aceSMatthew G Knepley }
450488ed4aceSMatthew G Knepley 
450588ed4aceSMatthew G Knepley /*@
4506bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
450788ed4aceSMatthew G Knepley 
450888ed4aceSMatthew G Knepley   Input Parameters:
4509bb7acecfSBarry Smith + dm - The `DM`
4510bb7acecfSBarry Smith - sf - The `PetscSF`
451188ed4aceSMatthew G Knepley 
451288ed4aceSMatthew G Knepley   Level: intermediate
451388ed4aceSMatthew G Knepley 
4514bb7acecfSBarry Smith   Note:
4515bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
451688ed4aceSMatthew G Knepley 
4517db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
451888ed4aceSMatthew G Knepley @*/
45199371c9d4SSatish Balay PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf) {
452088ed4aceSMatthew G Knepley   PetscFunctionBegin;
452188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4522b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
45249566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
45251bb6d2a8SBarry Smith   dm->sectionSF = sf;
452688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
452788ed4aceSMatthew G Knepley }
452888ed4aceSMatthew G Knepley 
452988ed4aceSMatthew G Knepley /*@C
4530bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
453188ed4aceSMatthew G Knepley   describing the data layout.
453288ed4aceSMatthew G Knepley 
453388ed4aceSMatthew G Knepley   Input Parameters:
4534bb7acecfSBarry Smith + dm - The `DM`
4535bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4536bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
453788ed4aceSMatthew G Knepley 
45381bb6d2a8SBarry Smith   Level: developer
45391bb6d2a8SBarry Smith 
4540bb7acecfSBarry Smith   Note:
4541bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4542bb7acecfSBarry Smith 
4543bb7acecfSBarry Smith   Developer Note:
4544bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4545bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4546bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
45471bb6d2a8SBarry Smith 
4548db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
454988ed4aceSMatthew G Knepley @*/
45509371c9d4SSatish Balay PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection) {
455188ed4aceSMatthew G Knepley   PetscFunctionBegin;
455288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45539566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
455488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
455588ed4aceSMatthew G Knepley }
4556af122d2aSMatthew G Knepley 
4557b21d0597SMatthew G Knepley /*@
4558bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4559bb7acecfSBarry Smith 
4560bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4561b21d0597SMatthew G Knepley 
4562b21d0597SMatthew G Knepley   Input Parameter:
4563bb7acecfSBarry Smith . dm - The `DM`
4564b21d0597SMatthew G Knepley 
4565b21d0597SMatthew G Knepley   Output Parameter:
4566bb7acecfSBarry Smith . sf - The `PetscSF`
4567b21d0597SMatthew G Knepley 
4568b21d0597SMatthew G Knepley   Level: intermediate
4569b21d0597SMatthew G Knepley 
4570bb7acecfSBarry Smith   Note:
4571bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4572b21d0597SMatthew G Knepley 
4573db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4574b21d0597SMatthew G Knepley @*/
45759371c9d4SSatish Balay PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
4576b21d0597SMatthew G Knepley   PetscFunctionBegin;
4577b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4578b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4579b21d0597SMatthew G Knepley   *sf = dm->sf;
4580b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4581b21d0597SMatthew G Knepley }
4582b21d0597SMatthew G Knepley 
4583057b4bcdSMatthew G Knepley /*@
4584bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4585bb7acecfSBarry Smith 
4586bb7acecfSBarry Smith   Collective on dm
4587057b4bcdSMatthew G Knepley 
4588057b4bcdSMatthew G Knepley   Input Parameters:
4589bb7acecfSBarry Smith + dm - The `DM`
4590bb7acecfSBarry Smith - sf - The` PetscSF`
4591057b4bcdSMatthew G Knepley 
4592057b4bcdSMatthew G Knepley   Level: intermediate
4593057b4bcdSMatthew G Knepley 
4594db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4595057b4bcdSMatthew G Knepley @*/
45969371c9d4SSatish Balay PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) {
4597057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4598057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4599b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46019566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4602057b4bcdSMatthew G Knepley   dm->sf = sf;
4603057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4604057b4bcdSMatthew G Knepley }
4605057b4bcdSMatthew G Knepley 
46064f37162bSMatthew G. Knepley /*@
4607bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
46084f37162bSMatthew G. Knepley 
46094f37162bSMatthew G. Knepley   Input Parameter:
4610bb7acecfSBarry Smith . dm - The `DM`
46114f37162bSMatthew G. Knepley 
46124f37162bSMatthew G. Knepley   Output Parameter:
4613bb7acecfSBarry Smith . sf - The `PetscSF`
46144f37162bSMatthew G. Knepley 
46154f37162bSMatthew G. Knepley   Level: intermediate
46164f37162bSMatthew G. Knepley 
4617bb7acecfSBarry Smith   Note:
4618bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
46194f37162bSMatthew G. Knepley 
4620db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46214f37162bSMatthew G. Knepley @*/
46229371c9d4SSatish Balay PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf) {
46234f37162bSMatthew G. Knepley   PetscFunctionBegin;
46244f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46254f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
46264f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
46274f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46284f37162bSMatthew G. Knepley }
46294f37162bSMatthew G. Knepley 
46304f37162bSMatthew G. Knepley /*@
46314f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
46324f37162bSMatthew G. Knepley 
46334f37162bSMatthew G. Knepley   Input Parameters:
46344f37162bSMatthew G. Knepley + dm - The DM
46354f37162bSMatthew G. Knepley - sf - The PetscSF
46364f37162bSMatthew G. Knepley 
46374f37162bSMatthew G. Knepley   Level: intermediate
46384f37162bSMatthew G. Knepley 
4639db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46404f37162bSMatthew G. Knepley @*/
46419371c9d4SSatish Balay PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf) {
46424f37162bSMatthew G. Knepley   PetscFunctionBegin;
46434f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46444f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46459566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46469566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
46474f37162bSMatthew G. Knepley   dm->sfNatural = sf;
46484f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46494f37162bSMatthew G. Knepley }
46504f37162bSMatthew G. Knepley 
46519371c9d4SSatish Balay static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc) {
465234aa8a36SMatthew G. Knepley   PetscClassId id;
465334aa8a36SMatthew G. Knepley 
465434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
46559566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
465634aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
46579566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
465834aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
46599566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
466017c1d62eSMatthew G. Knepley   } else {
46619566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
466234aa8a36SMatthew G. Knepley   }
466334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
466434aa8a36SMatthew G. Knepley }
466534aa8a36SMatthew G. Knepley 
46669371c9d4SSatish Balay static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew) {
466744a7f3ddSMatthew G. Knepley   RegionField *tmpr;
466844a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
466944a7f3ddSMatthew G. Knepley 
467044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
467144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
46729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
467344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
46749371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
46759371c9d4SSatish Balay     tmpr[f].disc        = NULL;
46769371c9d4SSatish Balay     tmpr[f].label       = NULL;
46779371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
46789371c9d4SSatish Balay   }
46799566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
468044a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
468144a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
468244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
468344a7f3ddSMatthew G. Knepley }
468444a7f3ddSMatthew G. Knepley 
468544a7f3ddSMatthew G. Knepley /*@
468644a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
468744a7f3ddSMatthew G. Knepley 
4688d083f849SBarry Smith   Logically collective on dm
468944a7f3ddSMatthew G. Knepley 
469044a7f3ddSMatthew G. Knepley   Input Parameter:
469144a7f3ddSMatthew G. Knepley . dm - The DM
469244a7f3ddSMatthew G. Knepley 
469344a7f3ddSMatthew G. Knepley   Level: intermediate
469444a7f3ddSMatthew G. Knepley 
4695db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
469644a7f3ddSMatthew G. Knepley @*/
46979371c9d4SSatish Balay PetscErrorCode DMClearFields(DM dm) {
469844a7f3ddSMatthew G. Knepley   PetscInt f;
469944a7f3ddSMatthew G. Knepley 
470044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
470144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
470244a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
47039566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
47049566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
470544a7f3ddSMatthew G. Knepley   }
47069566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
470744a7f3ddSMatthew G. Knepley   dm->fields = NULL;
470844a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
470944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
471044a7f3ddSMatthew G. Knepley }
471144a7f3ddSMatthew G. Knepley 
4712689b5837SMatthew G. Knepley /*@
4713689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4714689b5837SMatthew G. Knepley 
4715689b5837SMatthew G. Knepley   Not collective
4716689b5837SMatthew G. Knepley 
4717689b5837SMatthew G. Knepley   Input Parameter:
4718689b5837SMatthew G. Knepley . dm - The DM
4719689b5837SMatthew G. Knepley 
4720689b5837SMatthew G. Knepley   Output Parameter:
4721689b5837SMatthew G. Knepley . Nf - The number of fields
4722689b5837SMatthew G. Knepley 
4723689b5837SMatthew G. Knepley   Level: intermediate
4724689b5837SMatthew G. Knepley 
4725db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4726689b5837SMatthew G. Knepley @*/
47279371c9d4SSatish Balay PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields) {
47280f21e855SMatthew G. Knepley   PetscFunctionBegin;
47290f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4730534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
473144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4732af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4733af122d2aSMatthew G Knepley }
4734af122d2aSMatthew G Knepley 
4735689b5837SMatthew G. Knepley /*@
4736689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4737689b5837SMatthew G. Knepley 
4738d083f849SBarry Smith   Logically collective on dm
4739689b5837SMatthew G. Knepley 
4740689b5837SMatthew G. Knepley   Input Parameters:
4741689b5837SMatthew G. Knepley + dm - The DM
4742689b5837SMatthew G. Knepley - Nf - The number of fields
4743689b5837SMatthew G. Knepley 
4744689b5837SMatthew G. Knepley   Level: intermediate
4745689b5837SMatthew G. Knepley 
4746db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4747689b5837SMatthew G. Knepley @*/
47489371c9d4SSatish Balay PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields) {
47490f21e855SMatthew G. Knepley   PetscInt Nf, f;
4750af122d2aSMatthew G Knepley 
4751af122d2aSMatthew G Knepley   PetscFunctionBegin;
4752af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47539566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
47540f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47550f21e855SMatthew G. Knepley     PetscContainer obj;
47560f21e855SMatthew G. Knepley 
47579566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
47589566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
47599566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4760af122d2aSMatthew G Knepley   }
4761af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4762af122d2aSMatthew G Knepley }
4763af122d2aSMatthew G Knepley 
4764c1929be8SMatthew G. Knepley /*@
4765bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4766c1929be8SMatthew G. Knepley 
4767c1929be8SMatthew G. Knepley   Not collective
4768c1929be8SMatthew G. Knepley 
4769c1929be8SMatthew G. Knepley   Input Parameters:
4770bb7acecfSBarry Smith + dm - The `DM`
4771c1929be8SMatthew G. Knepley - f  - The field number
4772c1929be8SMatthew G. Knepley 
477344a7f3ddSMatthew G. Knepley   Output Parameters:
4774bb7acecfSBarry Smith + label - The label indicating the support of the field, or NULL for the entire mesh (pass in NULL if not needed)
4775bb7acecfSBarry Smith - disc - The discretization object (pass in NULL if not needed)
4776c1929be8SMatthew G. Knepley 
477744a7f3ddSMatthew G. Knepley   Level: intermediate
4778c1929be8SMatthew G. Knepley 
4779db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4780c1929be8SMatthew G. Knepley @*/
47819371c9d4SSatish Balay PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc) {
4782af122d2aSMatthew G Knepley   PetscFunctionBegin;
4783af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4784bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
47857a8be351SBarry 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);
478644a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4787bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
4788decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4789decb47aaSMatthew G. Knepley }
4790decb47aaSMatthew G. Knepley 
4791083401c6SMatthew G. Knepley /* Does not clear the DS */
47929371c9d4SSatish Balay PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc) {
4793083401c6SMatthew G. Knepley   PetscFunctionBegin;
47949566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
47959566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
47969566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4797083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4798bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
47999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4800bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4801083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4802083401c6SMatthew G. Knepley }
4803083401c6SMatthew G. Knepley 
4804c1929be8SMatthew G. Knepley /*@
4805bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4806bb7acecfSBarry Smith   the field numbering.
4807c1929be8SMatthew G. Knepley 
4808d083f849SBarry Smith   Logically collective on dm
4809c1929be8SMatthew G. Knepley 
4810c1929be8SMatthew G. Knepley   Input Parameters:
4811bb7acecfSBarry Smith + dm    - The `DM`
4812c1929be8SMatthew G. Knepley . f     - The field number
481344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4814bb7acecfSBarry Smith - disc - The discretization object
4815c1929be8SMatthew G. Knepley 
481644a7f3ddSMatthew G. Knepley   Level: intermediate
4817c1929be8SMatthew G. Knepley 
4818db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4819c1929be8SMatthew G. Knepley @*/
48209371c9d4SSatish Balay PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc) {
4821decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4822decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4823e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4824bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
48257a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4826bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4827bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
48289566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
482944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
483044a7f3ddSMatthew G. Knepley }
483144a7f3ddSMatthew G. Knepley 
483244a7f3ddSMatthew G. Knepley /*@
4833bb7acecfSBarry 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)
4834bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
483544a7f3ddSMatthew G. Knepley 
4836d083f849SBarry Smith   Logically collective on dm
483744a7f3ddSMatthew G. Knepley 
483844a7f3ddSMatthew G. Knepley   Input Parameters:
4839bb7acecfSBarry Smith + dm    - The `DM`
484044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4841bb7acecfSBarry Smith - disc - The discretization object
484244a7f3ddSMatthew G. Knepley 
484344a7f3ddSMatthew G. Knepley   Level: intermediate
484444a7f3ddSMatthew G. Knepley 
4845bb7acecfSBarry Smith   Notes:
4846bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4847bb7acecfSBarry Smith 
4848bb7acecfSBarry Smith   For example, a piecewise continous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4849bb7acecfSBarry 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
4850bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4851bb7acecfSBarry Smith 
4852bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
485344a7f3ddSMatthew G. Knepley @*/
48549371c9d4SSatish Balay PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc) {
485544a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
485644a7f3ddSMatthew G. Knepley 
485744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
485844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4859064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4860bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
48619566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
486244a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
4863bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
48649566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4865bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4866bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
48679566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
4868af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4869af122d2aSMatthew G Knepley }
48706636e97aSMatthew G Knepley 
4871e5e52638SMatthew G. Knepley /*@
4872e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4873e0b68406SMatthew Knepley 
4874e0b68406SMatthew Knepley   Logically collective on dm
4875e0b68406SMatthew Knepley 
4876e0b68406SMatthew Knepley   Input Parameters:
4877bb7acecfSBarry Smith + dm          - The `DM`
4878e0b68406SMatthew Knepley . f           - The field index
4879bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
4880e0b68406SMatthew Knepley 
4881e0b68406SMatthew Knepley   Level: intermediate
4882e0b68406SMatthew Knepley 
4883db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4884e0b68406SMatthew Knepley @*/
48859371c9d4SSatish Balay PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor) {
4886e0b68406SMatthew Knepley   PetscFunctionBegin;
488763a3b9bcSJacob 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);
4888e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4889e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4890e0b68406SMatthew Knepley }
4891e0b68406SMatthew Knepley 
4892e0b68406SMatthew Knepley /*@
4893e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4894e0b68406SMatthew Knepley 
4895bb7acecfSBarry Smith   Not collective
4896e0b68406SMatthew Knepley 
4897e0b68406SMatthew Knepley   Input Parameters:
4898bb7acecfSBarry Smith + dm          - The `DM`
4899e0b68406SMatthew Knepley - f           - The field index
4900e0b68406SMatthew Knepley 
4901e0b68406SMatthew Knepley   Output Parameter:
4902e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4903e0b68406SMatthew Knepley 
4904e0b68406SMatthew Knepley   Level: intermediate
4905e0b68406SMatthew Knepley 
4906bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4907e0b68406SMatthew Knepley @*/
49089371c9d4SSatish Balay PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor) {
4909e0b68406SMatthew Knepley   PetscFunctionBegin;
491063a3b9bcSJacob 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);
4911e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4912e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4913e0b68406SMatthew Knepley }
4914e0b68406SMatthew Knepley 
4915e0b68406SMatthew Knepley /*@
4916bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
4917e5e52638SMatthew G. Knepley 
4918d083f849SBarry Smith   Collective on dm
4919e5e52638SMatthew G. Knepley 
4920e5e52638SMatthew G. Knepley   Input Parameter:
4921bb7acecfSBarry Smith . dm - The `DM`
4922e5e52638SMatthew G. Knepley 
4923e5e52638SMatthew G. Knepley   Output Parameter:
4924bb7acecfSBarry Smith . newdm - The `DM`
4925e5e52638SMatthew G. Knepley 
4926e5e52638SMatthew G. Knepley   Level: advanced
4927e5e52638SMatthew G. Knepley 
4928db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
4929e5e52638SMatthew G. Knepley @*/
49309371c9d4SSatish Balay PetscErrorCode DMCopyFields(DM dm, DM newdm) {
4931e5e52638SMatthew G. Knepley   PetscInt Nf, f;
4932e5e52638SMatthew G. Knepley 
4933e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4934e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
49359566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49369566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
4937e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4938e5e52638SMatthew G. Knepley     DMLabel     label;
4939e5e52638SMatthew G. Knepley     PetscObject field;
494034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4941e5e52638SMatthew G. Knepley 
49429566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
49439566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
49449566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
49459566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
494634aa8a36SMatthew G. Knepley   }
494734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
494834aa8a36SMatthew G. Knepley }
494934aa8a36SMatthew G. Knepley 
495034aa8a36SMatthew G. Knepley /*@
495134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
495234aa8a36SMatthew G. Knepley 
495334aa8a36SMatthew G. Knepley   Not collective
495434aa8a36SMatthew G. Knepley 
495534aa8a36SMatthew G. Knepley   Input Parameters:
495634aa8a36SMatthew G. Knepley + dm - The DM object
495734aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
495834aa8a36SMatthew G. Knepley 
4959d8d19677SJose E. Roman   Output Parameters:
496034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
496134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
496234aa8a36SMatthew G. Knepley 
496334aa8a36SMatthew G. Knepley   Notes:
496434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
496534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
496634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4967979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
496834aa8a36SMatthew G. Knepley 
496934aa8a36SMatthew G. Knepley   Level: developer
497034aa8a36SMatthew G. Knepley 
4971db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
497234aa8a36SMatthew G. Knepley @*/
49739371c9d4SSatish Balay PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure) {
497434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
497534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4976534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
4977534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
497834aa8a36SMatthew G. Knepley   if (f < 0) {
497934aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
498034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
498134aa8a36SMatthew G. Knepley   } else {
498234aa8a36SMatthew G. Knepley     PetscInt Nf;
498334aa8a36SMatthew G. Knepley 
49849566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
49857a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
498634aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
498734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
498834aa8a36SMatthew G. Knepley   }
498934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
499034aa8a36SMatthew G. Knepley }
499134aa8a36SMatthew G. Knepley 
499234aa8a36SMatthew G. Knepley /*@
499334aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
499434aa8a36SMatthew G. Knepley 
499534aa8a36SMatthew G. Knepley   Not collective
499634aa8a36SMatthew G. Knepley 
499734aa8a36SMatthew G. Knepley   Input Parameters:
499834aa8a36SMatthew G. Knepley + dm         - The DM object
499934aa8a36SMatthew G. Knepley . f          - The field number
500034aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
500134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
500234aa8a36SMatthew G. Knepley 
500334aa8a36SMatthew G. Knepley   Notes:
500434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
500534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
500634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5007979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
500834aa8a36SMatthew G. Knepley 
500934aa8a36SMatthew G. Knepley   Level: developer
501034aa8a36SMatthew G. Knepley 
5011db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
501234aa8a36SMatthew G. Knepley @*/
50139371c9d4SSatish Balay PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure) {
501434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
501534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
501634aa8a36SMatthew G. Knepley   if (f < 0) {
501734aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
501834aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
501934aa8a36SMatthew G. Knepley   } else {
502034aa8a36SMatthew G. Knepley     PetscInt Nf;
502134aa8a36SMatthew G. Knepley 
50229566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
50237a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
502434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
502534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5026e5e52638SMatthew G. Knepley   }
5027e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5028e5e52638SMatthew G. Knepley }
5029e5e52638SMatthew G. Knepley 
5030b0441da4SMatthew G. Knepley /*@
5031b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5032b0441da4SMatthew G. Knepley 
5033b0441da4SMatthew G. Knepley   Not collective
5034b0441da4SMatthew G. Knepley 
5035f899ff85SJose E. Roman   Input Parameter:
5036b0441da4SMatthew G. Knepley . dm - The DM object
5037b0441da4SMatthew G. Knepley 
5038d8d19677SJose E. Roman   Output Parameters:
5039b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5040b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5041b0441da4SMatthew G. Knepley 
5042b0441da4SMatthew G. Knepley   Notes:
5043b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5044b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5045b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5046b0441da4SMatthew G. Knepley 
5047b0441da4SMatthew G. Knepley   Level: developer
5048b0441da4SMatthew G. Knepley 
5049db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5050b0441da4SMatthew G. Knepley @*/
50519371c9d4SSatish Balay PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure) {
5052b0441da4SMatthew G. Knepley   PetscInt Nf;
5053b0441da4SMatthew G. Knepley 
5054b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5055b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5056064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5057064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
50589566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5059b0441da4SMatthew G. Knepley   if (!Nf) {
50609566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5061b0441da4SMatthew G. Knepley   } else {
50629566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5063b0441da4SMatthew G. Knepley   }
5064b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5065b0441da4SMatthew G. Knepley }
5066b0441da4SMatthew G. Knepley 
5067b0441da4SMatthew G. Knepley /*@
5068b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5069b0441da4SMatthew G. Knepley 
5070b0441da4SMatthew G. Knepley   Not collective
5071b0441da4SMatthew G. Knepley 
5072b0441da4SMatthew G. Knepley   Input Parameters:
5073b0441da4SMatthew G. Knepley + dm         - The DM object
5074b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5075b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5076b0441da4SMatthew G. Knepley 
5077b0441da4SMatthew G. Knepley   Notes:
5078b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5079b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5080b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5081b0441da4SMatthew G. Knepley 
5082b0441da4SMatthew G. Knepley   Level: developer
5083b0441da4SMatthew G. Knepley 
5084db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5085b0441da4SMatthew G. Knepley @*/
50869371c9d4SSatish Balay PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure) {
5087b0441da4SMatthew G. Knepley   PetscInt Nf;
5088b0441da4SMatthew G. Knepley 
5089b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5090b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50919566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5092b0441da4SMatthew G. Knepley   if (!Nf) {
50939566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5094b0441da4SMatthew G. Knepley   } else {
50959566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5096e5e52638SMatthew G. Knepley   }
5097e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5098e5e52638SMatthew G. Knepley }
5099e5e52638SMatthew G. Knepley 
51009371c9d4SSatish Balay PetscErrorCode DMCompleteBCLabels_Internal(DM dm) {
5101799db056SMatthew G. Knepley   DM           plex;
5102799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5103799db056SMatthew G. Knepley   const char **names;
5104799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5105799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5106799db056SMatthew G. Knepley   size_t       len;
5107799db056SMatthew G. Knepley   MPI_Comm     comm;
5108799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5109783e2ec8SMatthew G. Knepley 
5110783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5111799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5112799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5113799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5114799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5115799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5116799db056SMatthew G. Knepley     PetscDS  dsBC;
5117799db056SMatthew G. Knepley     PetscInt numBd;
5118799db056SMatthew G. Knepley 
5119799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5120799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5121799db056SMatthew G. Knepley     maxLabels += numBd;
5122799db056SMatthew G. Knepley   }
5123799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5124799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5125799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5126799db056SMatthew G. Knepley     PetscDS  dsBC;
5127799db056SMatthew G. Knepley     PetscInt numBd, bd;
5128799db056SMatthew G. Knepley 
5129799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5130799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5131799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5132799db056SMatthew G. Knepley       DMLabel      label;
5133799db056SMatthew G. Knepley       PetscInt     field;
5134799db056SMatthew G. Knepley       PetscObject  obj;
5135799db056SMatthew G. Knepley       PetscClassId id;
5136799db056SMatthew G. Knepley 
5137799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
51389566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
51399566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5140799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
51419371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
51429371c9d4SSatish Balay         if (labels[l] == label) break;
5143799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5144783e2ec8SMatthew G. Knepley     }
5145799db056SMatthew G. Knepley   }
5146799db056SMatthew G. Knepley   /* Get label names */
5147799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5148799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
51499371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
51509371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
51519371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
51529371c9d4SSatish Balay   }
5153799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5154799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5155799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5156799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen * l], names[l]));
5157799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5158799db056SMatthew G. Knepley   /* Put all names on all processes */
5159799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5160799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5161799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5162799db056SMatthew G. Knepley   gNl = displs[size];
51639371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
51649371c9d4SSatish Balay     counts[p] *= gmaxLen;
51659371c9d4SSatish Balay     displs[p] *= gmaxLen;
51669371c9d4SSatish Balay   }
5167799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5168799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5169799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5170799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5171799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5172799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5173799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
51749371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
51759371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
51769566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5177799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
51789566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5179799db056SMatthew G. Knepley     ++gl;
5180783e2ec8SMatthew G. Knepley   }
5181799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5182783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5183783e2ec8SMatthew G. Knepley }
5184783e2ec8SMatthew G. Knepley 
51859371c9d4SSatish Balay static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew) {
5186e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5187e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5188e5e52638SMatthew G. Knepley 
5189e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5190e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
51919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5192e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
51939371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
51949371c9d4SSatish Balay     tmpd[s].ds     = NULL;
51959371c9d4SSatish Balay     tmpd[s].label  = NULL;
51969371c9d4SSatish Balay     tmpd[s].fields = NULL;
51979371c9d4SSatish Balay   }
51989566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5199e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5200e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5201e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5202e5e52638SMatthew G. Knepley }
5203e5e52638SMatthew G. Knepley 
5204e5e52638SMatthew G. Knepley /*@
5205e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5206e5e52638SMatthew G. Knepley 
5207e5e52638SMatthew G. Knepley   Not collective
5208e5e52638SMatthew G. Knepley 
5209e5e52638SMatthew G. Knepley   Input Parameter:
5210e5e52638SMatthew G. Knepley . dm - The DM
5211e5e52638SMatthew G. Knepley 
5212e5e52638SMatthew G. Knepley   Output Parameter:
5213e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5214e5e52638SMatthew G. Knepley 
5215e5e52638SMatthew G. Knepley   Level: intermediate
5216e5e52638SMatthew G. Knepley 
5217db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5218e5e52638SMatthew G. Knepley @*/
52199371c9d4SSatish Balay PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds) {
5220e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5221e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5222534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5223e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5224e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5225e5e52638SMatthew G. Knepley }
5226e5e52638SMatthew G. Knepley 
5227e5e52638SMatthew G. Knepley /*@
5228e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5229e5e52638SMatthew G. Knepley 
5230d083f849SBarry Smith   Logically collective on dm
5231e5e52638SMatthew G. Knepley 
5232e5e52638SMatthew G. Knepley   Input Parameter:
5233e5e52638SMatthew G. Knepley . dm - The DM
5234e5e52638SMatthew G. Knepley 
5235e5e52638SMatthew G. Knepley   Level: intermediate
5236e5e52638SMatthew G. Knepley 
5237db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5238e5e52638SMatthew G. Knepley @*/
52399371c9d4SSatish Balay PetscErrorCode DMClearDS(DM dm) {
5240e5e52638SMatthew G. Knepley   PetscInt s;
5241e5e52638SMatthew G. Knepley 
5242e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5243e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5244e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
52459566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
52469566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
52479566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5248e5e52638SMatthew G. Knepley   }
52499566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5250e5e52638SMatthew G. Knepley   dm->probs = NULL;
5251e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5252e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5253e5e52638SMatthew G. Knepley }
5254e5e52638SMatthew G. Knepley 
5255e5e52638SMatthew G. Knepley /*@
5256e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5257e5e52638SMatthew G. Knepley 
5258e5e52638SMatthew G. Knepley   Not collective
5259e5e52638SMatthew G. Knepley 
5260e5e52638SMatthew G. Knepley   Input Parameter:
5261e5e52638SMatthew G. Knepley . dm    - The DM
5262e5e52638SMatthew G. Knepley 
5263e5e52638SMatthew G. Knepley   Output Parameter:
5264e5e52638SMatthew G. Knepley . prob - The default PetscDS
5265e5e52638SMatthew G. Knepley 
5266e5e52638SMatthew G. Knepley   Level: intermediate
5267e5e52638SMatthew G. Knepley 
5268db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5269e5e52638SMatthew G. Knepley @*/
52709371c9d4SSatish Balay PetscErrorCode DMGetDS(DM dm, PetscDS *prob) {
5271e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5272e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5273e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5274b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5275b0143b4dSMatthew G. Knepley     PetscDS ds;
5276b0143b4dSMatthew G. Knepley 
52779566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
52789566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
52799566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5280b0143b4dSMatthew G. Knepley   }
5281b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5282e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5283e5e52638SMatthew G. Knepley }
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley /*@
5286e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5287e5e52638SMatthew G. Knepley 
5288e5e52638SMatthew G. Knepley   Not collective
5289e5e52638SMatthew G. Knepley 
5290e5e52638SMatthew G. Knepley   Input Parameters:
5291e5e52638SMatthew G. Knepley + dm    - The DM
5292e5e52638SMatthew G. Knepley - point - Cell for the DS
5293e5e52638SMatthew G. Knepley 
5294e5e52638SMatthew G. Knepley   Output Parameter:
5295e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5296e5e52638SMatthew G. Knepley 
5297e5e52638SMatthew G. Knepley   Level: developer
5298e5e52638SMatthew G. Knepley 
5299db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5300e5e52638SMatthew G. Knepley @*/
53019371c9d4SSatish Balay PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob) {
5302e5e52638SMatthew G. Knepley   PetscDS  probDef = NULL;
5303e5e52638SMatthew G. Knepley   PetscInt s;
5304e5e52638SMatthew G. Knepley 
5305e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5306e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5307e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
530863a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5309e5e52638SMatthew G. Knepley   *prob = NULL;
5310e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5311e5e52638SMatthew G. Knepley     PetscInt val;
5312e5e52638SMatthew G. Knepley 
53139371c9d4SSatish Balay     if (!dm->probs[s].label) {
53149371c9d4SSatish Balay       probDef = dm->probs[s].ds;
53159371c9d4SSatish Balay     } else {
53169566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
53179371c9d4SSatish Balay       if (val >= 0) {
53189371c9d4SSatish Balay         *prob = dm->probs[s].ds;
53199371c9d4SSatish Balay         break;
53209371c9d4SSatish Balay       }
5321e5e52638SMatthew G. Knepley     }
5322e5e52638SMatthew G. Knepley   }
5323e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5324e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5325e5e52638SMatthew G. Knepley }
5326e5e52638SMatthew G. Knepley 
5327e5e52638SMatthew G. Knepley /*@
5328e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5329e5e52638SMatthew G. Knepley 
5330e5e52638SMatthew G. Knepley   Not collective
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley   Input Parameters:
5333e5e52638SMatthew G. Knepley + dm    - The DM
5334e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5335e5e52638SMatthew G. Knepley 
5336b3cf3223SMatthew G. Knepley   Output Parameters:
5337b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5338b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5339e5e52638SMatthew G. Knepley 
5340154ca461SJed Brown   Note:
5341154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5342154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5343154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5344e5e52638SMatthew G. Knepley 
5345e5e52638SMatthew G. Knepley   Level: advanced
5346e5e52638SMatthew G. Knepley 
5347db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5348e5e52638SMatthew G. Knepley @*/
53499371c9d4SSatish Balay PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds) {
5350e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5351e5e52638SMatthew G. Knepley 
5352e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5353e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5354e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
53559371c9d4SSatish Balay   if (fields) {
53569371c9d4SSatish Balay     PetscValidPointer(fields, 3);
53579371c9d4SSatish Balay     *fields = NULL;
53589371c9d4SSatish Balay   }
53599371c9d4SSatish Balay   if (ds) {
53609371c9d4SSatish Balay     PetscValidPointer(ds, 4);
53619371c9d4SSatish Balay     *ds = NULL;
53629371c9d4SSatish Balay   }
5363e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5364154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5365b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5366b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
5367154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5368b3cf3223SMatthew G. Knepley     }
5369e5e52638SMatthew G. Knepley   }
53702df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5371e5e52638SMatthew G. Knepley }
5372e5e52638SMatthew G. Knepley 
5373e5e52638SMatthew G. Knepley /*@
5374bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5375083401c6SMatthew G. Knepley 
5376083401c6SMatthew G. Knepley   Collective on dm
5377083401c6SMatthew G. Knepley 
5378083401c6SMatthew G. Knepley   Input Parameters:
5379bb7acecfSBarry Smith + dm     - The `DM`
5380bb7acecfSBarry Smith . label  - The `DMLabel` defining the mesh region, or NULL for the entire mesh
5381bb7acecfSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or NULL for all fields
5382bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5383083401c6SMatthew G. Knepley 
5384bb7acecfSBarry Smith   Note:
5385bb7acecfSBarry 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,
5386083401c6SMatthew G. Knepley   the fields argument is ignored.
5387083401c6SMatthew G. Knepley 
5388083401c6SMatthew G. Knepley   Level: advanced
5389083401c6SMatthew G. Knepley 
5390db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5391083401c6SMatthew G. Knepley @*/
53929371c9d4SSatish Balay PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds) {
5393083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5394083401c6SMatthew G. Knepley 
5395083401c6SMatthew G. Knepley   PetscFunctionBegin;
5396083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5397083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5398064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5399083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5400083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
54019566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5402083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5403083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5404083401c6SMatthew G. Knepley     }
5405083401c6SMatthew G. Knepley   }
54069566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
54079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
54089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
54099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
5410083401c6SMatthew G. Knepley   if (!label) {
5411083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5412083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5413083401c6SMatthew G. Knepley     Nds = 0;
5414083401c6SMatthew G. Knepley   }
5415083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5416083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5417083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5418083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5419083401c6SMatthew G. Knepley }
5420083401c6SMatthew G. Knepley 
5421083401c6SMatthew G. Knepley /*@
5422e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5423e5e52638SMatthew G. Knepley 
5424e5e52638SMatthew G. Knepley   Not collective
5425e5e52638SMatthew G. Knepley 
5426e5e52638SMatthew G. Knepley   Input Parameters:
5427e5e52638SMatthew G. Knepley + dm  - The DM
5428e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5429e5e52638SMatthew G. Knepley 
5430e5e52638SMatthew G. Knepley   Output Parameters:
5431b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5432b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5433083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   Level: advanced
5436e5e52638SMatthew G. Knepley 
5437db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5438e5e52638SMatthew G. Knepley @*/
54399371c9d4SSatish Balay PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds) {
5440e5e52638SMatthew G. Knepley   PetscInt Nds;
5441e5e52638SMatthew G. Knepley 
5442e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5443e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54449566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
544563a3b9bcSJacob 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);
5446e5e52638SMatthew G. Knepley   if (label) {
5447e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5448e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5449e5e52638SMatthew G. Knepley   }
5450b3cf3223SMatthew G. Knepley   if (fields) {
5451b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5452b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5453b3cf3223SMatthew G. Knepley   }
5454e5e52638SMatthew G. Knepley   if (ds) {
5455b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5456e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5457e5e52638SMatthew G. Knepley   }
5458e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5459e5e52638SMatthew G. Knepley }
5460e5e52638SMatthew G. Knepley 
5461e5e52638SMatthew G. Knepley /*@
5462083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5463e5e52638SMatthew G. Knepley 
5464083401c6SMatthew G. Knepley   Not collective
5465e5e52638SMatthew G. Knepley 
5466e5e52638SMatthew G. Knepley   Input Parameters:
5467e5e52638SMatthew G. Knepley + dm     - The DM
5468083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5469083401c6SMatthew G. Knepley . label  - The region label, or NULL
5470083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5471083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5472e5e52638SMatthew G. Knepley 
5473e5e52638SMatthew G. Knepley   Level: advanced
5474e5e52638SMatthew G. Knepley 
5475db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5476e5e52638SMatthew G. Knepley @*/
54779371c9d4SSatish Balay PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds) {
5478083401c6SMatthew G. Knepley   PetscInt Nds;
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5481e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5482083401c6SMatthew G. Knepley   if (label) { PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3); }
54839566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
548463a3b9bcSJacob 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);
54859566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
54869566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5487083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5488083401c6SMatthew G. Knepley   if (fields) {
5489083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
54909566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
54919566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5492083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5493e5e52638SMatthew G. Knepley   }
5494083401c6SMatthew G. Knepley   if (ds) {
5495083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
54969566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
54979566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5498083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5499083401c6SMatthew G. Knepley   }
5500e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5501e5e52638SMatthew G. Knepley }
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley /*@
55041d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55051d3af9e0SMatthew G. Knepley 
55061d3af9e0SMatthew G. Knepley   Not collective
55071d3af9e0SMatthew G. Knepley 
55081d3af9e0SMatthew G. Knepley   Input Parameters:
55091d3af9e0SMatthew G. Knepley + dm  - The DM
55101d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55111d3af9e0SMatthew G. Knepley 
55121d3af9e0SMatthew G. Knepley   Output Parameter:
55131d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55141d3af9e0SMatthew G. Knepley 
55151d3af9e0SMatthew G. Knepley   Level: advanced
55161d3af9e0SMatthew G. Knepley 
5517db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
55181d3af9e0SMatthew G. Knepley @*/
55199371c9d4SSatish Balay PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num) {
55201d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
55211d3af9e0SMatthew G. Knepley 
55221d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55231d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55241d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5525dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
55269566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
55279371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
55289371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
55291d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55301d3af9e0SMatthew G. Knepley   else *num = n;
55311d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55321d3af9e0SMatthew G. Knepley }
55331d3af9e0SMatthew G. Knepley 
55342df84da0SMatthew G. Knepley /*@C
5535bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
55362df84da0SMatthew G. Knepley 
55372df84da0SMatthew G. Knepley   Not collective
55382df84da0SMatthew G. Knepley 
5539f1a722f8SMatthew G. Knepley   Input Parameters:
5540bb7acecfSBarry Smith + dm     - The `DM`
55412df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5542bb7acecfSBarry Smith . prefix - The options prefix for the output `PetscFE`, or NULL
5543bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
55442df84da0SMatthew G. Knepley 
55452df84da0SMatthew G. Knepley   Output Parameter:
5546bb7acecfSBarry Smith . fem - The `PetscFE`
55472df84da0SMatthew G. Knepley 
5548bb7acecfSBarry Smith   Note:
5549bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
55502df84da0SMatthew G. Knepley 
55512df84da0SMatthew G. Knepley   Level: intermediate
55522df84da0SMatthew G. Knepley 
5553db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
55542df84da0SMatthew G. Knepley @*/
55559371c9d4SSatish Balay PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem) {
55562df84da0SMatthew G. Knepley   DMPolytopeType ct;
55572df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
55582df84da0SMatthew G. Knepley 
55592df84da0SMatthew G. Knepley   PetscFunctionBegin;
55602df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55612df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
55622df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
55632df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
55642df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
55659566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
55669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
55679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
55689566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
55692df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
55702df84da0SMatthew G. Knepley }
55712df84da0SMatthew G. Knepley 
55721d3af9e0SMatthew G. Knepley /*@
5573bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5574e5e52638SMatthew G. Knepley 
5575d083f849SBarry Smith   Collective on dm
5576e5e52638SMatthew G. Knepley 
5577e5e52638SMatthew G. Knepley   Input Parameter:
5578bb7acecfSBarry Smith . dm - The `DM`
5579e5e52638SMatthew G. Knepley 
558045480ffeSMatthew G. Knepley   Options Database Keys:
5581bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
558245480ffeSMatthew G. Knepley 
5583bb7acecfSBarry Smith   Note:
5584bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5585e5e52638SMatthew G. Knepley 
5586e5e52638SMatthew G. Knepley   Level: intermediate
5587e5e52638SMatthew G. Knepley 
5588db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5589e5e52638SMatthew G. Knepley @*/
55909371c9d4SSatish Balay PetscErrorCode DMCreateDS(DM dm) {
5591e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5592083401c6SMatthew G. Knepley   PetscDS   dsDef;
5593083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5594f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5595f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5596e5e52638SMatthew G. Knepley 
5597e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5598e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5599e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
56009566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
56019566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5602083401c6SMatthew G. Knepley   /* Determine how many regions we have */
56039566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5604083401c6SMatthew G. Knepley   Nl   = 0;
5605083401c6SMatthew G. Knepley   Ndef = 0;
5606083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5607083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5608083401c6SMatthew G. Knepley     PetscInt l;
5609083401c6SMatthew G. Knepley 
5610f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5611f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5612f918ec44SMatthew G. Knepley     {
5613f918ec44SMatthew G. Knepley       PetscClassId id;
5614f918ec44SMatthew G. Knepley 
56159566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5616f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5617f918ec44SMatthew G. Knepley         Ceed ceed;
5618f918ec44SMatthew G. Knepley 
56199566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
56209566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5621f918ec44SMatthew G. Knepley       }
5622f918ec44SMatthew G. Knepley     }
5623f918ec44SMatthew G. Knepley #endif
56249371c9d4SSatish Balay     if (!label) {
56259371c9d4SSatish Balay       ++Ndef;
56269371c9d4SSatish Balay       continue;
56279371c9d4SSatish Balay     }
56289371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
56299371c9d4SSatish Balay       if (label == labelSet[l]) break;
5630083401c6SMatthew G. Knepley     if (l < Nl) continue;
5631083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5632083401c6SMatthew G. Knepley   }
5633083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
56349566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5635083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5636b3cf3223SMatthew G. Knepley     IS        fields;
5637b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5638b3cf3223SMatthew G. Knepley 
56399371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
56409371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
56417a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
56429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
56439371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
56449371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
56459566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
56469566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
56479566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
56489566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
564988f0c812SMatthew G. Knepley 
56509566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56519566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
56529566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
56539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
56542df9ee95SMatthew G. Knepley   }
56559566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
56569566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5657083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5658083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56590122748bSMatthew G. Knepley     DM              plex;
5660083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5661083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5662083401c6SMatthew G. Knepley     PetscInt       *fields;
5663083401c6SMatthew G. Knepley     const PetscInt *cells;
5664083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56650122748bSMatthew G. Knepley 
56669566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
56679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
56689566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
56699566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
56705fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5671083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5672083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5673083401c6SMatthew G. Knepley       IS      pointIS;
5674083401c6SMatthew G. Knepley 
56759566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
56769566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
56779566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
56789566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5679083401c6SMatthew G. Knepley     }
56809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5681083401c6SMatthew G. Knepley 
56829566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
56839566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
56849566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
56859566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
56869566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
56879566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
56889566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5689083401c6SMatthew G. Knepley 
56909566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
56919371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
56929371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
56939566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
56949566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
56959566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
56969566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5697083401c6SMatthew G. Knepley 
56989566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56999566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
57009566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
57019566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
57029566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
57039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
57049566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5705083401c6SMatthew G. Knepley   }
5706083401c6SMatthew G. Knepley   /* Create label DSes
5707083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5708083401c6SMatthew G. Knepley   */
5709083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5710083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5711083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5712083401c6SMatthew G. Knepley     PetscDS   ds;
5713083401c6SMatthew G. Knepley     IS        fields;
5714083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5715083401c6SMatthew G. Knepley 
57169566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
57179371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57189371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
57199566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
57209371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57219371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
57229566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57239566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
57249566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
57259566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
57269566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
57279566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
57289566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5729083401c6SMatthew G. Knepley     {
5730083401c6SMatthew G. Knepley       DMPolytopeType ct;
5731083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
57325fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
57330122748bSMatthew G. Knepley 
57349566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5735665f567fSMatthew G. Knepley       if (lStart >= 0) {
57369566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5737412e9a14SMatthew G. Knepley         switch (ct) {
5738412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5739412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5740412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
57419371c9d4SSatish Balay         case DM_POLYTOPE_QUAD_PRISM_TENSOR: isCohesiveLocal = PETSC_TRUE; break;
5742083401c6SMatthew G. Knepley         default: break;
5743412e9a14SMatthew G. Knepley         }
5744665f567fSMatthew G. Knepley       }
57459566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
57465fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
57475fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
57485fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
57499566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
57509566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
57515fedec97SMatthew G. Knepley           }
57525fedec97SMatthew G. Knepley           ++nf;
57535fedec97SMatthew G. Knepley         }
57545fedec97SMatthew G. Knepley       }
5755e5e52638SMatthew G. Knepley     }
57569566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5757e5e52638SMatthew G. Knepley   }
57589566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5759e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5760083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5761083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5762083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5763083401c6SMatthew G. Knepley     const PetscInt *fld;
57645fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
57655fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5766e5e52638SMatthew G. Knepley 
57679566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
57689566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
57699566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
57709566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5771083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5772083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
57735fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5774e5e52638SMatthew G. Knepley       PetscClassId id;
5775e5e52638SMatthew G. Knepley 
57765fedec97SMatthew G. Knepley       /* Handle DS with no fields */
57779566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
57785fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
57799566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&disc));
57809566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5781083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
57829566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5783e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5784e5e52638SMatthew G. Knepley     }
57859566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5786e5e52638SMatthew G. Knepley   }
5787f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
57889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5789f9244615SMatthew G. Knepley   if (flg) {
57903b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
57913b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
57923b4aee56SMatthew G. Knepley       PetscInt Nf, f;
57933b4aee56SMatthew G. Knepley 
57949566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
57959566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
57963b4aee56SMatthew G. Knepley     }
5797f9244615SMatthew G. Knepley   }
5798e5e52638SMatthew G. Knepley   /* Setup DSes */
5799e5e52638SMatthew G. Knepley   if (doSetup) {
58009566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5801e5e52638SMatthew G. Knepley   }
5802e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5803e5e52638SMatthew G. Knepley }
5804e5e52638SMatthew G. Knepley 
5805e5e52638SMatthew G. Knepley /*@
5806bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
58077f96f943SMatthew G. Knepley 
5808bb7acecfSBarry Smith   Collective on `DM`
5809f2cacb80SMatthew G. Knepley 
58107f96f943SMatthew G. Knepley   Input Parameters:
5811bb7acecfSBarry Smith + dm   - The `DM`
58127f96f943SMatthew G. Knepley - time - The time
58137f96f943SMatthew G. Knepley 
58147f96f943SMatthew G. Knepley   Output Parameters:
5815f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5816f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
58177f96f943SMatthew G. Knepley 
5818bb7acecfSBarry Smith   Note:
5819bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
58207f96f943SMatthew G. Knepley 
58217f96f943SMatthew G. Knepley   Level: developer
58227f96f943SMatthew G. Knepley 
5823db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
58247f96f943SMatthew G. Knepley @*/
58259371c9d4SSatish Balay PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t) {
58267f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
58277f96f943SMatthew G. Knepley   void   **ectxs;
58287f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
58297f96f943SMatthew G. Knepley 
58307f96f943SMatthew G. Knepley   PetscFunctionBegin;
5831f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5832f2cacb80SMatthew G. Knepley   if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5833f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
58349566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
58359566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
58369566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58377f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
58387f96f943SMatthew G. Knepley     PetscDS         ds;
58397f96f943SMatthew G. Knepley     DMLabel         label;
58407f96f943SMatthew G. Knepley     IS              fieldIS;
58417f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58427f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58437f96f943SMatthew G. Knepley 
58449566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
58459566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
58469566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
58479566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
58489566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5849f2cacb80SMatthew G. Knepley     if (u) {
58507f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58517f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58529566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
58537f96f943SMatthew G. Knepley       }
58549566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
58557f96f943SMatthew G. Knepley       if (label) {
58569566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
58577f96f943SMatthew G. Knepley       } else {
58589566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
58597f96f943SMatthew G. Knepley       }
58607f96f943SMatthew G. Knepley     }
5861f2cacb80SMatthew G. Knepley     if (u_t) {
58629566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
58639566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
5864f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5865f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
58669566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
5867f2cacb80SMatthew G. Knepley       }
58689566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
5869f2cacb80SMatthew G. Knepley       if (label) {
58709566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5871f2cacb80SMatthew G. Knepley       } else {
58729566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5873f2cacb80SMatthew G. Knepley       }
5874f2cacb80SMatthew G. Knepley     }
5875f2cacb80SMatthew G. Knepley   }
5876f2cacb80SMatthew G. Knepley   if (u) {
58779566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
58789566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
5879f2cacb80SMatthew G. Knepley   }
5880f2cacb80SMatthew G. Knepley   if (u_t) {
58819566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
58829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
5883f2cacb80SMatthew G. Knepley   }
58849566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
58857f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58867f96f943SMatthew G. Knepley }
58877f96f943SMatthew G. Knepley 
58889371c9d4SSatish Balay PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds) {
588945480ffeSMatthew G. Knepley   PetscDS    dsNew;
589045480ffeSMatthew G. Knepley   DSBoundary b;
58916a02485aSMatthew G. Knepley   PetscInt   cdim, Nf, f, d;
58925fedec97SMatthew G. Knepley   PetscBool  isCohesive;
589345480ffeSMatthew G. Knepley   void      *ctx;
589445480ffeSMatthew G. Knepley 
589545480ffeSMatthew G. Knepley   PetscFunctionBegin;
58969566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
58979566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
58989566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
58999566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
59009566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
59019566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
590245480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
59039566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
59049566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
59059566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
59069566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
59076a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
59086a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
590945480ffeSMatthew G. Knepley   }
591045480ffeSMatthew G. Knepley   if (Nf) {
59119566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
59129566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
591345480ffeSMatthew G. Knepley   }
59149566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
591545480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
59169566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
591745480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
59187a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
591945480ffeSMatthew G. Knepley   }
592045480ffeSMatthew G. Knepley 
59219566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
59229566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
592345480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
592445480ffeSMatthew G. Knepley }
592545480ffeSMatthew G. Knepley 
59267f96f943SMatthew G. Knepley /*@
5927bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
5928e5e52638SMatthew G. Knepley 
5929d083f849SBarry Smith   Collective on dm
5930e5e52638SMatthew G. Knepley 
5931e5e52638SMatthew G. Knepley   Input Parameter:
5932bb7acecfSBarry Smith . dm - The `DM`
5933e5e52638SMatthew G. Knepley 
5934e5e52638SMatthew G. Knepley   Output Parameter:
5935bb7acecfSBarry Smith . newdm - The `DM`
5936e5e52638SMatthew G. Knepley 
5937e5e52638SMatthew G. Knepley   Level: advanced
5938e5e52638SMatthew G. Knepley 
5939db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5940e5e52638SMatthew G. Knepley @*/
59419371c9d4SSatish Balay PetscErrorCode DMCopyDS(DM dm, DM newdm) {
5942e5e52638SMatthew G. Knepley   PetscInt Nds, s;
5943e5e52638SMatthew G. Knepley 
5944e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5945e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
59469566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59479566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
5948e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5949e5e52638SMatthew G. Knepley     DMLabel  label;
5950b3cf3223SMatthew G. Knepley     IS       fields;
595145480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5952783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5953e5e52638SMatthew G. Knepley 
59549566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
5955b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
59569566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
595745480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
59589566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
59599566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
5960783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5961b8025e53SMatthew G. Knepley       PetscWeakForm wf;
596245480ffeSMatthew G. Knepley       DMLabel       label;
5963783e2ec8SMatthew G. Knepley       PetscInt      field;
5964783e2ec8SMatthew G. Knepley 
59659566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
59669566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
5967783e2ec8SMatthew G. Knepley     }
5968e5e52638SMatthew G. Knepley   }
5969799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
5970e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5971e5e52638SMatthew G. Knepley }
5972e5e52638SMatthew G. Knepley 
5973e5e52638SMatthew G. Knepley /*@
5974bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
5975e5e52638SMatthew G. Knepley 
5976d083f849SBarry Smith   Collective on dm
5977e5e52638SMatthew G. Knepley 
5978e5e52638SMatthew G. Knepley   Input Parameter:
5979bb7acecfSBarry Smith . dm - The `DM`
5980e5e52638SMatthew G. Knepley 
5981e5e52638SMatthew G. Knepley   Output Parameter:
5982bb7acecfSBarry Smith . newdm - The `DM`
5983e5e52638SMatthew G. Knepley 
5984e5e52638SMatthew G. Knepley   Level: advanced
5985e5e52638SMatthew G. Knepley 
5986bb7acecfSBarry Smith   Developer Note:
5987bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
5988bb7acecfSBarry Smith 
5989db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
5990e5e52638SMatthew G. Knepley @*/
59919371c9d4SSatish Balay PetscErrorCode DMCopyDisc(DM dm, DM newdm) {
5992e5e52638SMatthew G. Knepley   PetscFunctionBegin;
59939566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
59949566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
5995e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5996e5e52638SMatthew G. Knepley }
5997e5e52638SMatthew G. Knepley 
5998c73cfb54SMatthew G. Knepley /*@
5999bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6000c73cfb54SMatthew G. Knepley 
6001c73cfb54SMatthew G. Knepley   Not collective
6002c73cfb54SMatthew G. Knepley 
6003c73cfb54SMatthew G. Knepley   Input Parameter:
6004bb7acecfSBarry Smith . dm - The `DM`
6005c73cfb54SMatthew G. Knepley 
6006c73cfb54SMatthew G. Knepley   Output Parameter:
6007c73cfb54SMatthew G. Knepley . dim - The topological dimension
6008c73cfb54SMatthew G. Knepley 
6009c73cfb54SMatthew G. Knepley   Level: beginner
6010c73cfb54SMatthew G. Knepley 
6011db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6012c73cfb54SMatthew G. Knepley @*/
60139371c9d4SSatish Balay PetscErrorCode DMGetDimension(DM dm, PetscInt *dim) {
6014c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6015c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6016534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6017c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6018c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6019c73cfb54SMatthew G. Knepley }
6020c73cfb54SMatthew G. Knepley 
6021c73cfb54SMatthew G. Knepley /*@
6022bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6023c73cfb54SMatthew G. Knepley 
6024c73cfb54SMatthew G. Knepley   Collective on dm
6025c73cfb54SMatthew G. Knepley 
6026c73cfb54SMatthew G. Knepley   Input Parameters:
6027bb7acecfSBarry Smith + dm - The `DM`
6028c73cfb54SMatthew G. Knepley - dim - The topological dimension
6029c73cfb54SMatthew G. Knepley 
6030c73cfb54SMatthew G. Knepley   Level: beginner
6031c73cfb54SMatthew G. Knepley 
6032db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6033c73cfb54SMatthew G. Knepley @*/
60349371c9d4SSatish Balay PetscErrorCode DMSetDimension(DM dm, PetscInt dim) {
6035e5e52638SMatthew G. Knepley   PetscDS  ds;
603645480ffeSMatthew G. Knepley   PetscInt Nds, n;
6037f17e8794SMatthew G. Knepley 
6038c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6039c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6040c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6041c73cfb54SMatthew G. Knepley   dm->dim = dim;
6042d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
60439566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
604445480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
60459566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
60469566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
604745480ffeSMatthew G. Knepley     }
6048d17bd122SMatthew G. Knepley   }
6049c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6050c73cfb54SMatthew G. Knepley }
6051c73cfb54SMatthew G. Knepley 
6052793f3fe5SMatthew G. Knepley /*@
6053793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6054793f3fe5SMatthew G. Knepley 
6055d083f849SBarry Smith   Collective on dm
6056793f3fe5SMatthew G. Knepley 
6057793f3fe5SMatthew G. Knepley   Input Parameters:
6058bb7acecfSBarry Smith + dm - the `DM`
6059793f3fe5SMatthew G. Knepley - dim - the dimension
6060793f3fe5SMatthew G. Knepley 
6061793f3fe5SMatthew G. Knepley   Output Parameters:
6062793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6063aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6064793f3fe5SMatthew G. Knepley 
6065793f3fe5SMatthew G. Knepley   Note:
6066793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6067a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6068793f3fe5SMatthew G. Knepley   then the interval is empty.
6069793f3fe5SMatthew G. Knepley 
6070793f3fe5SMatthew G. Knepley   Level: intermediate
6071793f3fe5SMatthew G. Knepley 
6072db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6073793f3fe5SMatthew G. Knepley @*/
60749371c9d4SSatish Balay PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd) {
6075793f3fe5SMatthew G. Knepley   PetscInt d;
6076793f3fe5SMatthew G. Knepley 
6077793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6078793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60799566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
60807a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6081dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
6082793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6083793f3fe5SMatthew G. Knepley }
6084793f3fe5SMatthew G. Knepley 
60856636e97aSMatthew G Knepley /*@
6086bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6087f4d763aaSMatthew G. Knepley 
60888f700142SStefano Zampini   Collective on dm
60898f700142SStefano Zampini 
6090f4d763aaSMatthew G. Knepley   Input Parameter:
6091bb7acecfSBarry Smith . dm - The original `DM`
6092f4d763aaSMatthew G. Knepley 
6093f4d763aaSMatthew G. Knepley   Output Parameter:
6094bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6095f4d763aaSMatthew G. Knepley 
6096f4d763aaSMatthew G. Knepley   Level: intermediate
6097f4d763aaSMatthew G. Knepley 
6098bb7acecfSBarry Smith   Note:
6099bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6100bb7acecfSBarry 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
6101bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6102bb7acecfSBarry Smith 
6103bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6104f4d763aaSMatthew G. Knepley @*/
61059371c9d4SSatish Balay PetscErrorCode DMGetOutputDM(DM dm, DM *odm) {
6106c26acbdeSMatthew G. Knepley   PetscSection section;
61072d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
610814f150ffSMatthew G. Knepley 
610914f150ffSMatthew G. Knepley   PetscFunctionBegin;
611014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
611114f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
61129566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
61139566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
61149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
61152d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6116c26acbdeSMatthew G. Knepley     *odm = dm;
6117c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6118c26acbdeSMatthew G. Knepley   }
611914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6120c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
612114f150ffSMatthew G. Knepley     PetscSF      sf;
612214f150ffSMatthew G. Knepley 
61239566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
61249566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
61259566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
61269566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
61279566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
61289566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
61299566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
61309566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
61319566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
613214f150ffSMatthew G. Knepley   }
613314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
613414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
613514f150ffSMatthew G. Knepley }
6136f4d763aaSMatthew G. Knepley 
6137f4d763aaSMatthew G. Knepley /*@
6138cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6139f4d763aaSMatthew G. Knepley 
6140f4d763aaSMatthew G. Knepley   Input Parameter:
6141bb7acecfSBarry Smith . dm - The original `DM`
6142f4d763aaSMatthew G. Knepley 
6143cdb7a50dSMatthew G. Knepley   Output Parameters:
6144cdb7a50dSMatthew G. Knepley + num - The output sequence number
6145cdb7a50dSMatthew G. Knepley - val - The output sequence value
6146f4d763aaSMatthew G. Knepley 
6147f4d763aaSMatthew G. Knepley   Level: intermediate
6148f4d763aaSMatthew G. Knepley 
6149bb7acecfSBarry Smith   Note:
6150bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6151bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6152bb7acecfSBarry Smith 
6153bb7acecfSBarry Smith   Developer Note:
6154bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6155bb7acecfSBarry Smith   not directly related to the `DM`.
6156f4d763aaSMatthew G. Knepley 
6157db781477SPatrick Sanan .seealso: `VecView()`
6158f4d763aaSMatthew G. Knepley @*/
61599371c9d4SSatish Balay PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val) {
6160f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6161f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61629371c9d4SSatish Balay   if (num) {
61639371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
61649371c9d4SSatish Balay     *num = dm->outputSequenceNum;
61659371c9d4SSatish Balay   }
61669371c9d4SSatish Balay   if (val) {
61679371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
61689371c9d4SSatish Balay     *val = dm->outputSequenceVal;
61699371c9d4SSatish Balay   }
6170f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6171f4d763aaSMatthew G. Knepley }
6172f4d763aaSMatthew G. Knepley 
6173f4d763aaSMatthew G. Knepley /*@
6174cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6175f4d763aaSMatthew G. Knepley 
6176f4d763aaSMatthew G. Knepley   Input Parameters:
6177bb7acecfSBarry Smith + dm - The original `DM`
6178cdb7a50dSMatthew G. Knepley . num - The output sequence number
6179cdb7a50dSMatthew G. Knepley - val - The output sequence value
6180f4d763aaSMatthew G. Knepley 
6181f4d763aaSMatthew G. Knepley   Level: intermediate
6182f4d763aaSMatthew G. Knepley 
6183bb7acecfSBarry Smith   Note:
6184bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6185bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6186f4d763aaSMatthew G. Knepley 
6187db781477SPatrick Sanan .seealso: `VecView()`
6188f4d763aaSMatthew G. Knepley @*/
61899371c9d4SSatish Balay PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val) {
6190f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6191f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6192f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6193cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6194cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6195cdb7a50dSMatthew G. Knepley }
6196cdb7a50dSMatthew G. Knepley 
6197cdb7a50dSMatthew G. Knepley /*@C
6198bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6199cdb7a50dSMatthew G. Knepley 
6200cdb7a50dSMatthew G. Knepley   Input Parameters:
6201bb7acecfSBarry Smith + dm   - The original `DM`
6202cdb7a50dSMatthew G. Knepley . name - The sequence name
6203cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6204cdb7a50dSMatthew G. Knepley 
6205cdb7a50dSMatthew G. Knepley   Output Parameter:
6206cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6207cdb7a50dSMatthew G. Knepley 
6208cdb7a50dSMatthew G. Knepley   Level: intermediate
6209cdb7a50dSMatthew G. Knepley 
6210bb7acecfSBarry Smith   Note:
6211bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6212bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6213bb7acecfSBarry Smith 
6214bb7acecfSBarry Smith   Developer Note:
6215bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6216cdb7a50dSMatthew G. Knepley 
6217db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6218cdb7a50dSMatthew G. Knepley @*/
62199371c9d4SSatish Balay PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val) {
6220cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6221cdb7a50dSMatthew G. Knepley 
6222cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6223cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6224cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6225064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
62269566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6227cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6228cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6229cdb7a50dSMatthew G. Knepley     PetscScalar value;
6230cdb7a50dSMatthew G. Knepley 
62319566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
62324aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6233cdb7a50dSMatthew G. Knepley #endif
6234cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6235f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6236f4d763aaSMatthew G. Knepley }
62378e4ac7eaSMatthew G. Knepley 
62388e4ac7eaSMatthew G. Knepley /*@
6239bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
62408e4ac7eaSMatthew G. Knepley 
62418e4ac7eaSMatthew G. Knepley   Not collective
62428e4ac7eaSMatthew G. Knepley 
62438e4ac7eaSMatthew G. Knepley   Input Parameter:
6244bb7acecfSBarry Smith . dm - The `DM`
62458e4ac7eaSMatthew G. Knepley 
62468e4ac7eaSMatthew G. Knepley   Output Parameter:
6247bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
62488e4ac7eaSMatthew G. Knepley 
62498e4ac7eaSMatthew G. Knepley   Level: beginner
62508e4ac7eaSMatthew G. Knepley 
6251db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
62528e4ac7eaSMatthew G. Knepley @*/
62539371c9d4SSatish Balay PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural) {
62548e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62558e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6256534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
62578e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
62588e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62598e4ac7eaSMatthew G. Knepley }
62608e4ac7eaSMatthew G. Knepley 
62618e4ac7eaSMatthew G. Knepley /*@
6262bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
62638e4ac7eaSMatthew G. Knepley 
62648e4ac7eaSMatthew G. Knepley   Collective on dm
62658e4ac7eaSMatthew G. Knepley 
62668e4ac7eaSMatthew G. Knepley   Input Parameters:
6267bb7acecfSBarry Smith  + dm - The `DM`
6268bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
62698e4ac7eaSMatthew G. Knepley 
6270bb7acecfSBarry Smith   Note:
6271bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
62725d3b26e6SMatthew G. Knepley 
62738e4ac7eaSMatthew G. Knepley   Level: beginner
62748e4ac7eaSMatthew G. Knepley 
6275db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
62768e4ac7eaSMatthew G. Knepley @*/
62779371c9d4SSatish Balay PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural) {
62788e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62798e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62808833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
62818e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
62828e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62838e4ac7eaSMatthew G. Knepley }
6284c58f1c22SToby Isaac 
6285c58f1c22SToby Isaac /*@C
6286bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6287c58f1c22SToby Isaac 
6288c58f1c22SToby Isaac   Not Collective
6289c58f1c22SToby Isaac 
6290c58f1c22SToby Isaac   Input Parameters:
6291bb7acecfSBarry Smith + dm   - The `DM` object
6292c58f1c22SToby Isaac - name - The label name
6293c58f1c22SToby Isaac 
6294c58f1c22SToby Isaac   Level: intermediate
6295c58f1c22SToby Isaac 
6296db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6297c58f1c22SToby Isaac @*/
62989371c9d4SSatish Balay PetscErrorCode DMCreateLabel(DM dm, const char name[]) {
62995d80c0bfSVaclav Hapla   PetscBool flg;
63005d80c0bfSVaclav Hapla   DMLabel   label;
6301c58f1c22SToby Isaac 
6302c58f1c22SToby Isaac   PetscFunctionBegin;
6303c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6304c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63059566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6306c58f1c22SToby Isaac   if (!flg) {
63079566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
63089566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
63099566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6310c58f1c22SToby Isaac   }
6311c58f1c22SToby Isaac   PetscFunctionReturn(0);
6312c58f1c22SToby Isaac }
6313c58f1c22SToby Isaac 
6314c58f1c22SToby Isaac /*@C
6315bb7acecfSBarry 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.
63160fdc7489SMatthew Knepley 
63170fdc7489SMatthew Knepley   Not Collective
63180fdc7489SMatthew Knepley 
63190fdc7489SMatthew Knepley   Input Parameters:
6320bb7acecfSBarry Smith + dm   - The `DM` object
63210fdc7489SMatthew Knepley . l    - The index for the label
63220fdc7489SMatthew Knepley - name - The label name
63230fdc7489SMatthew Knepley 
63240fdc7489SMatthew Knepley   Level: intermediate
63250fdc7489SMatthew Knepley 
6326db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
63270fdc7489SMatthew Knepley @*/
63289371c9d4SSatish Balay PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[]) {
63290fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
63300fdc7489SMatthew Knepley   DMLabel     label;
63310fdc7489SMatthew Knepley   PetscInt    Nl, m;
63320fdc7489SMatthew Knepley   PetscBool   flg, match;
63330fdc7489SMatthew Knepley   const char *lname;
63340fdc7489SMatthew Knepley 
63350fdc7489SMatthew Knepley   PetscFunctionBegin;
63360fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6337064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
63389566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
63390fdc7489SMatthew Knepley   if (!flg) {
63409566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
63419566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
63429566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
63430fdc7489SMatthew Knepley   }
63449566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
634563a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
63460fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
63479566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
63489566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
63490fdc7489SMatthew Knepley     if (match) break;
63500fdc7489SMatthew Knepley   }
63510fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
63520fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
63530fdc7489SMatthew Knepley   else prev->next = orig->next;
63540fdc7489SMatthew Knepley   if (!l) {
63550fdc7489SMatthew Knepley     orig->next = dm->labels;
63560fdc7489SMatthew Knepley     dm->labels = orig;
63570fdc7489SMatthew Knepley   } else {
63589371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
63599371c9d4SSatish Balay       ;
63600fdc7489SMatthew Knepley     orig->next = prev->next;
63610fdc7489SMatthew Knepley     prev->next = orig;
63620fdc7489SMatthew Knepley   }
63630fdc7489SMatthew Knepley   PetscFunctionReturn(0);
63640fdc7489SMatthew Knepley }
63650fdc7489SMatthew Knepley 
63660fdc7489SMatthew Knepley /*@C
6367bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6368c58f1c22SToby Isaac 
6369c58f1c22SToby Isaac   Not Collective
6370c58f1c22SToby Isaac 
6371c58f1c22SToby Isaac   Input Parameters:
6372bb7acecfSBarry Smith + dm   - The `DM` object
6373c58f1c22SToby Isaac . name - The label name
6374c58f1c22SToby Isaac - point - The mesh point
6375c58f1c22SToby Isaac 
6376c58f1c22SToby Isaac   Output Parameter:
6377c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6378c58f1c22SToby Isaac 
6379c58f1c22SToby Isaac   Level: beginner
6380c58f1c22SToby Isaac 
6381db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6382c58f1c22SToby Isaac @*/
63839371c9d4SSatish Balay PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value) {
6384c58f1c22SToby Isaac   DMLabel label;
6385c58f1c22SToby Isaac 
6386c58f1c22SToby Isaac   PetscFunctionBegin;
6387c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6388c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63899566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
63907a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
63919566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6392c58f1c22SToby Isaac   PetscFunctionReturn(0);
6393c58f1c22SToby Isaac }
6394c58f1c22SToby Isaac 
6395c58f1c22SToby Isaac /*@C
6396bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6397c58f1c22SToby Isaac 
6398c58f1c22SToby Isaac   Not Collective
6399c58f1c22SToby Isaac 
6400c58f1c22SToby Isaac   Input Parameters:
6401bb7acecfSBarry Smith + dm   - The `DM` object
6402c58f1c22SToby Isaac . name - The label name
6403c58f1c22SToby Isaac . point - The mesh point
6404c58f1c22SToby Isaac - value - The label value for this point
6405c58f1c22SToby Isaac 
6406c58f1c22SToby Isaac   Output Parameter:
6407c58f1c22SToby Isaac 
6408c58f1c22SToby Isaac   Level: beginner
6409c58f1c22SToby Isaac 
6410db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6411c58f1c22SToby Isaac @*/
64129371c9d4SSatish Balay PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value) {
6413c58f1c22SToby Isaac   DMLabel label;
6414c58f1c22SToby Isaac 
6415c58f1c22SToby Isaac   PetscFunctionBegin;
6416c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6417c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6419c58f1c22SToby Isaac   if (!label) {
64209566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
64219566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6422c58f1c22SToby Isaac   }
64239566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6424c58f1c22SToby Isaac   PetscFunctionReturn(0);
6425c58f1c22SToby Isaac }
6426c58f1c22SToby Isaac 
6427c58f1c22SToby Isaac /*@C
6428bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6429c58f1c22SToby Isaac 
6430c58f1c22SToby Isaac   Not Collective
6431c58f1c22SToby Isaac 
6432c58f1c22SToby Isaac   Input Parameters:
6433bb7acecfSBarry Smith + dm   - The `DM` object
6434c58f1c22SToby Isaac . name - The label name
6435c58f1c22SToby Isaac . point - The mesh point
6436c58f1c22SToby Isaac - value - The label value for this point
6437c58f1c22SToby Isaac 
6438c58f1c22SToby Isaac   Level: beginner
6439c58f1c22SToby Isaac 
6440db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6441c58f1c22SToby Isaac @*/
64429371c9d4SSatish Balay PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value) {
6443c58f1c22SToby Isaac   DMLabel label;
6444c58f1c22SToby Isaac 
6445c58f1c22SToby Isaac   PetscFunctionBegin;
6446c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6447c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64489566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6449c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64509566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6451c58f1c22SToby Isaac   PetscFunctionReturn(0);
6452c58f1c22SToby Isaac }
6453c58f1c22SToby Isaac 
6454c58f1c22SToby Isaac /*@C
6455bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6456c58f1c22SToby Isaac 
6457c58f1c22SToby Isaac   Not Collective
6458c58f1c22SToby Isaac 
6459c58f1c22SToby Isaac   Input Parameters:
6460bb7acecfSBarry Smith + dm   - The `DM` object
6461c58f1c22SToby Isaac - name - The label name
6462c58f1c22SToby Isaac 
6463c58f1c22SToby Isaac   Output Parameter:
6464c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6465c58f1c22SToby Isaac 
6466c58f1c22SToby Isaac   Level: beginner
6467c58f1c22SToby Isaac 
6468bb7acecfSBarry Smith   Developer Note:
6469bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6470bb7acecfSBarry Smith 
6471bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6472c58f1c22SToby Isaac @*/
64739371c9d4SSatish Balay PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size) {
6474c58f1c22SToby Isaac   DMLabel label;
6475c58f1c22SToby Isaac 
6476c58f1c22SToby Isaac   PetscFunctionBegin;
6477c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6478c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6479534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
64809566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6481c58f1c22SToby Isaac   *size = 0;
6482c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64839566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6484c58f1c22SToby Isaac   PetscFunctionReturn(0);
6485c58f1c22SToby Isaac }
6486c58f1c22SToby Isaac 
6487c58f1c22SToby Isaac /*@C
6488bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6489c58f1c22SToby Isaac 
6490c58f1c22SToby Isaac   Not Collective
6491c58f1c22SToby Isaac 
6492c58f1c22SToby Isaac   Input Parameters:
6493bb7acecfSBarry Smith + mesh - The `DM` object
6494c58f1c22SToby Isaac - name - The label name
6495c58f1c22SToby Isaac 
6496c58f1c22SToby Isaac   Output Parameter:
6497c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6498c58f1c22SToby Isaac 
6499c58f1c22SToby Isaac   Level: beginner
6500c58f1c22SToby Isaac 
6501db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6502c58f1c22SToby Isaac @*/
65039371c9d4SSatish Balay PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids) {
6504c58f1c22SToby Isaac   DMLabel label;
6505c58f1c22SToby Isaac 
6506c58f1c22SToby Isaac   PetscFunctionBegin;
6507c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6508c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6509c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
65109566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6511c58f1c22SToby Isaac   *ids = NULL;
6512dab2e251SBlaise Bourdin   if (label) {
65139566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6514dab2e251SBlaise Bourdin   } else {
6515dab2e251SBlaise Bourdin     /* returning an empty IS */
65169566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6517dab2e251SBlaise Bourdin   }
6518c58f1c22SToby Isaac   PetscFunctionReturn(0);
6519c58f1c22SToby Isaac }
6520c58f1c22SToby Isaac 
6521c58f1c22SToby Isaac /*@C
6522c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6523c58f1c22SToby Isaac 
6524c58f1c22SToby Isaac   Not Collective
6525c58f1c22SToby Isaac 
6526c58f1c22SToby Isaac   Input Parameters:
6527bb7acecfSBarry Smith + dm - The `DM` object
6528c58f1c22SToby Isaac . name - The label name
6529c58f1c22SToby Isaac - value - The stratum value
6530c58f1c22SToby Isaac 
6531c58f1c22SToby Isaac   Output Parameter:
6532bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6533c58f1c22SToby Isaac 
6534c58f1c22SToby Isaac   Level: beginner
6535c58f1c22SToby Isaac 
6536db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6537c58f1c22SToby Isaac @*/
65389371c9d4SSatish Balay PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size) {
6539c58f1c22SToby Isaac   DMLabel label;
6540c58f1c22SToby Isaac 
6541c58f1c22SToby Isaac   PetscFunctionBegin;
6542c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6543c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6544534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
65459566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6546c58f1c22SToby Isaac   *size = 0;
6547c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65489566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6549c58f1c22SToby Isaac   PetscFunctionReturn(0);
6550c58f1c22SToby Isaac }
6551c58f1c22SToby Isaac 
6552c58f1c22SToby Isaac /*@C
6553c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6554c58f1c22SToby Isaac 
6555c58f1c22SToby Isaac   Not Collective
6556c58f1c22SToby Isaac 
6557c58f1c22SToby Isaac   Input Parameters:
6558bb7acecfSBarry Smith + dm - The `DM` object
6559c58f1c22SToby Isaac . name - The label name
6560c58f1c22SToby Isaac - value - The stratum value
6561c58f1c22SToby Isaac 
6562c58f1c22SToby Isaac   Output Parameter:
6563c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6564c58f1c22SToby Isaac 
6565c58f1c22SToby Isaac   Level: beginner
6566c58f1c22SToby Isaac 
6567db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6568c58f1c22SToby Isaac @*/
65699371c9d4SSatish Balay PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points) {
6570c58f1c22SToby Isaac   DMLabel label;
6571c58f1c22SToby Isaac 
6572c58f1c22SToby Isaac   PetscFunctionBegin;
6573c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6574c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6575c58f1c22SToby Isaac   PetscValidPointer(points, 4);
65769566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6577c58f1c22SToby Isaac   *points = NULL;
6578c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65799566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6580c58f1c22SToby Isaac   PetscFunctionReturn(0);
6581c58f1c22SToby Isaac }
6582c58f1c22SToby Isaac 
65834de306b1SToby Isaac /*@C
65849044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
65854de306b1SToby Isaac 
65864de306b1SToby Isaac   Not Collective
65874de306b1SToby Isaac 
65884de306b1SToby Isaac   Input Parameters:
6589bb7acecfSBarry Smith + dm - The `DM` object
65904de306b1SToby Isaac . name - The label name
65914de306b1SToby Isaac . value - The stratum value
65924de306b1SToby Isaac - points - The stratum points
65934de306b1SToby Isaac 
65944de306b1SToby Isaac   Level: beginner
65954de306b1SToby Isaac 
6596bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
65974de306b1SToby Isaac @*/
65989371c9d4SSatish Balay PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points) {
65994de306b1SToby Isaac   DMLabel label;
66004de306b1SToby Isaac 
66014de306b1SToby Isaac   PetscFunctionBegin;
66024de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66034de306b1SToby Isaac   PetscValidCharPointer(name, 2);
66044de306b1SToby Isaac   PetscValidPointer(points, 4);
66059566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66064de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
66079566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
66084de306b1SToby Isaac   PetscFunctionReturn(0);
66094de306b1SToby Isaac }
66104de306b1SToby Isaac 
6611c58f1c22SToby Isaac /*@C
6612bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6613c58f1c22SToby Isaac 
6614c58f1c22SToby Isaac   Not Collective
6615c58f1c22SToby Isaac 
6616c58f1c22SToby Isaac   Input Parameters:
6617bb7acecfSBarry Smith + dm   - The `DM` object
6618c58f1c22SToby Isaac . name - The label name
6619c58f1c22SToby Isaac - value - The label value for this point
6620c58f1c22SToby Isaac 
6621c58f1c22SToby Isaac   Output Parameter:
6622c58f1c22SToby Isaac 
6623c58f1c22SToby Isaac   Level: beginner
6624c58f1c22SToby Isaac 
6625bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6626c58f1c22SToby Isaac @*/
66279371c9d4SSatish Balay PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value) {
6628c58f1c22SToby Isaac   DMLabel label;
6629c58f1c22SToby Isaac 
6630c58f1c22SToby Isaac   PetscFunctionBegin;
6631c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6632c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66339566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6634c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66359566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6636c58f1c22SToby Isaac   PetscFunctionReturn(0);
6637c58f1c22SToby Isaac }
6638c58f1c22SToby Isaac 
6639c58f1c22SToby Isaac /*@
6640bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6641c58f1c22SToby Isaac 
6642c58f1c22SToby Isaac   Not Collective
6643c58f1c22SToby Isaac 
6644c58f1c22SToby Isaac   Input Parameter:
6645bb7acecfSBarry Smith . dm   - The `DM` object
6646c58f1c22SToby Isaac 
6647c58f1c22SToby Isaac   Output Parameter:
6648c58f1c22SToby Isaac . numLabels - the number of Labels
6649c58f1c22SToby Isaac 
6650c58f1c22SToby Isaac   Level: intermediate
6651c58f1c22SToby Isaac 
6652bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6653c58f1c22SToby Isaac @*/
66549371c9d4SSatish Balay PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels) {
66555d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6656c58f1c22SToby Isaac   PetscInt    n    = 0;
6657c58f1c22SToby Isaac 
6658c58f1c22SToby Isaac   PetscFunctionBegin;
6659c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6660534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
66619371c9d4SSatish Balay   while (next) {
66629371c9d4SSatish Balay     ++n;
66639371c9d4SSatish Balay     next = next->next;
66649371c9d4SSatish Balay   }
6665c58f1c22SToby Isaac   *numLabels = n;
6666c58f1c22SToby Isaac   PetscFunctionReturn(0);
6667c58f1c22SToby Isaac }
6668c58f1c22SToby Isaac 
6669c58f1c22SToby Isaac /*@C
6670c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6671c58f1c22SToby Isaac 
6672c58f1c22SToby Isaac   Not Collective
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac   Input Parameters:
6675bb7acecfSBarry Smith + dm - The `DM` object
6676c58f1c22SToby Isaac - n  - the label number
6677c58f1c22SToby Isaac 
6678c58f1c22SToby Isaac   Output Parameter:
6679c58f1c22SToby Isaac . name - the label name
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac   Level: intermediate
6682c58f1c22SToby Isaac 
6683bb7acecfSBarry Smith   Developer Note:
6684bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6685bb7acecfSBarry Smith 
6686bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6687c58f1c22SToby Isaac @*/
66889371c9d4SSatish Balay PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name) {
66895d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6690c58f1c22SToby Isaac   PetscInt    l    = 0;
6691c58f1c22SToby Isaac 
6692c58f1c22SToby Isaac   PetscFunctionBegin;
6693c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6694c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6695c58f1c22SToby Isaac   while (next) {
6696c58f1c22SToby Isaac     if (l == n) {
66979566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
6698c58f1c22SToby Isaac       PetscFunctionReturn(0);
6699c58f1c22SToby Isaac     }
6700c58f1c22SToby Isaac     ++l;
6701c58f1c22SToby Isaac     next = next->next;
6702c58f1c22SToby Isaac   }
670363a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6704c58f1c22SToby Isaac }
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac /*@C
6707bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6708c58f1c22SToby Isaac 
6709c58f1c22SToby Isaac   Not Collective
6710c58f1c22SToby Isaac 
6711c58f1c22SToby Isaac   Input Parameters:
6712bb7acecfSBarry Smith + dm   - The `DM` object
6713c58f1c22SToby Isaac - name - The label name
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac   Output Parameter:
6716bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6717c58f1c22SToby Isaac 
6718c58f1c22SToby Isaac   Level: intermediate
6719c58f1c22SToby Isaac 
6720bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6721c58f1c22SToby Isaac @*/
67229371c9d4SSatish Balay PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel) {
67235d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6724d67d17b1SMatthew G. Knepley   const char *lname;
6725c58f1c22SToby Isaac 
6726c58f1c22SToby Isaac   PetscFunctionBegin;
6727c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6728c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6729534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6730c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6731c58f1c22SToby Isaac   while (next) {
67329566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
67339566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6734c58f1c22SToby Isaac     if (*hasLabel) break;
6735c58f1c22SToby Isaac     next = next->next;
6736c58f1c22SToby Isaac   }
6737c58f1c22SToby Isaac   PetscFunctionReturn(0);
6738c58f1c22SToby Isaac }
6739c58f1c22SToby Isaac 
6740c58f1c22SToby Isaac /*@C
6741bb7acecfSBarry Smith   DMGetLabel - Return the label of a given name, or NULL, from a `DM`
6742c58f1c22SToby Isaac 
6743c58f1c22SToby Isaac   Not Collective
6744c58f1c22SToby Isaac 
6745c58f1c22SToby Isaac   Input Parameters:
6746bb7acecfSBarry Smith + dm   - The `DM` object
6747c58f1c22SToby Isaac - name - The label name
6748c58f1c22SToby Isaac 
6749c58f1c22SToby Isaac   Output Parameter:
6750bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent
6751c58f1c22SToby Isaac 
6752bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6753bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6754bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6755bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6756bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6757bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6758bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
67596d7c9049SMatthew G. Knepley 
6760c58f1c22SToby Isaac   Level: intermediate
6761c58f1c22SToby Isaac 
6762bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6763c58f1c22SToby Isaac @*/
67649371c9d4SSatish Balay PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label) {
67655d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6766c58f1c22SToby Isaac   PetscBool   hasLabel;
6767d67d17b1SMatthew G. Knepley   const char *lname;
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   PetscFunctionBegin;
6770c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6771c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6772c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6773c58f1c22SToby Isaac   *label = NULL;
6774c58f1c22SToby Isaac   while (next) {
67759566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
67769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6777c58f1c22SToby Isaac     if (hasLabel) {
6778c58f1c22SToby Isaac       *label = next->label;
6779c58f1c22SToby Isaac       break;
6780c58f1c22SToby Isaac     }
6781c58f1c22SToby Isaac     next = next->next;
6782c58f1c22SToby Isaac   }
6783c58f1c22SToby Isaac   PetscFunctionReturn(0);
6784c58f1c22SToby Isaac }
6785c58f1c22SToby Isaac 
6786c58f1c22SToby Isaac /*@C
6787bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac   Not Collective
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac   Input Parameters:
6792bb7acecfSBarry Smith + dm - The `DM` object
6793c58f1c22SToby Isaac - n  - the label number
6794c58f1c22SToby Isaac 
6795c58f1c22SToby Isaac   Output Parameter:
6796c58f1c22SToby Isaac . label - the label
6797c58f1c22SToby Isaac 
6798c58f1c22SToby Isaac   Level: intermediate
6799c58f1c22SToby Isaac 
6800bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6801c58f1c22SToby Isaac @*/
68029371c9d4SSatish Balay PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label) {
68035d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6804c58f1c22SToby Isaac   PetscInt    l    = 0;
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   PetscFunctionBegin;
6807c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6808c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6809c58f1c22SToby Isaac   while (next) {
6810c58f1c22SToby Isaac     if (l == n) {
6811c58f1c22SToby Isaac       *label = next->label;
6812c58f1c22SToby Isaac       PetscFunctionReturn(0);
6813c58f1c22SToby Isaac     }
6814c58f1c22SToby Isaac     ++l;
6815c58f1c22SToby Isaac     next = next->next;
6816c58f1c22SToby Isaac   }
681763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6818c58f1c22SToby Isaac }
6819c58f1c22SToby Isaac 
6820c58f1c22SToby Isaac /*@C
6821bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
6822c58f1c22SToby Isaac 
6823c58f1c22SToby Isaac   Not Collective
6824c58f1c22SToby Isaac 
6825c58f1c22SToby Isaac   Input Parameters:
6826bb7acecfSBarry Smith + dm   - The `DM` object
6827bb7acecfSBarry Smith - label - The `DMLabel`
6828c58f1c22SToby Isaac 
6829c58f1c22SToby Isaac   Level: developer
6830c58f1c22SToby Isaac 
6831bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6832c58f1c22SToby Isaac @*/
68339371c9d4SSatish Balay PetscErrorCode DMAddLabel(DM dm, DMLabel label) {
68345d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
6835c58f1c22SToby Isaac   PetscBool   hasLabel;
6836d67d17b1SMatthew G. Knepley   const char *lname;
68375d80c0bfSVaclav Hapla   PetscBool   flg;
6838c58f1c22SToby Isaac 
6839c58f1c22SToby Isaac   PetscFunctionBegin;
6840c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68419566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
68429566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
68437a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
68449566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
6845c58f1c22SToby Isaac   tmpLabel->label  = label;
6846c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
68475d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
68485d80c0bfSVaclav Hapla   *p = tmpLabel;
68499566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
68509566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
68515d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
68529566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
6853ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
6854c58f1c22SToby Isaac   PetscFunctionReturn(0);
6855c58f1c22SToby Isaac }
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac /*@C
68584a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
68594a7ee7d0SMatthew G. Knepley 
68604a7ee7d0SMatthew G. Knepley   Not Collective
68614a7ee7d0SMatthew G. Knepley 
68624a7ee7d0SMatthew G. Knepley   Input Parameters:
6863bb7acecfSBarry Smith + dm    - The `DM` object
6864bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
68654a7ee7d0SMatthew G. Knepley 
6866bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6867bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
6868bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
6869bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6870bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6871bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6872bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
68734a7ee7d0SMatthew G. Knepley 
68744a7ee7d0SMatthew G. Knepley   Level: intermediate
68754a7ee7d0SMatthew G. Knepley 
6876bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
68774a7ee7d0SMatthew G. Knepley @*/
68789371c9d4SSatish Balay PetscErrorCode DMSetLabel(DM dm, DMLabel label) {
68794a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
68804a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
68814a7ee7d0SMatthew G. Knepley   const char *name, *lname;
68824a7ee7d0SMatthew G. Knepley 
68834a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
68844a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68854a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
68869566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
68874a7ee7d0SMatthew G. Knepley   while (next) {
68889566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
68899566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
68904a7ee7d0SMatthew G. Knepley     if (hasLabel) {
68919566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
68929566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
68934a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
68949566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
68954a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
68969566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
68974a7ee7d0SMatthew G. Knepley       next->label = label;
68984a7ee7d0SMatthew G. Knepley       break;
68994a7ee7d0SMatthew G. Knepley     }
69004a7ee7d0SMatthew G. Knepley     next = next->next;
69014a7ee7d0SMatthew G. Knepley   }
69024a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
69034a7ee7d0SMatthew G. Knepley }
69044a7ee7d0SMatthew G. Knepley 
69054a7ee7d0SMatthew G. Knepley /*@C
6906bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   Not Collective
6909c58f1c22SToby Isaac 
6910c58f1c22SToby Isaac   Input Parameters:
6911bb7acecfSBarry Smith + dm   - The `DM` object
6912c58f1c22SToby Isaac - name - The label name
6913c58f1c22SToby Isaac 
6914c58f1c22SToby Isaac   Output Parameter:
6915bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent. Pass in NULL to call `DMLabelDestroy()` on the label, otherwise the
6916bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
6917c58f1c22SToby Isaac 
6918c58f1c22SToby Isaac   Level: developer
6919c58f1c22SToby Isaac 
6920bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
6921c58f1c22SToby Isaac @*/
69229371c9d4SSatish Balay PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label) {
692395d578d6SVaclav Hapla   DMLabelLink link, *pnext;
6924c58f1c22SToby Isaac   PetscBool   hasLabel;
6925d67d17b1SMatthew G. Knepley   const char *lname;
6926c58f1c22SToby Isaac 
6927c58f1c22SToby Isaac   PetscFunctionBegin;
6928c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6929e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
6930e5472504SVaclav Hapla   if (label) {
6931e5472504SVaclav Hapla     PetscValidPointer(label, 3);
6932c58f1c22SToby Isaac     *label = NULL;
6933e5472504SVaclav Hapla   }
69345d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
69359566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
69369566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6937c58f1c22SToby Isaac     if (hasLabel) {
693895d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
69399566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
694095d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
69419566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
6942ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
694395d578d6SVaclav Hapla       if (label) *label = link->label;
69449566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
69459566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6946c58f1c22SToby Isaac       break;
6947c58f1c22SToby Isaac     }
6948c58f1c22SToby Isaac   }
6949c58f1c22SToby Isaac   PetscFunctionReturn(0);
6950c58f1c22SToby Isaac }
6951c58f1c22SToby Isaac 
6952306894acSVaclav Hapla /*@
6953bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
6954306894acSVaclav Hapla 
6955306894acSVaclav Hapla   Not Collective
6956306894acSVaclav Hapla 
6957306894acSVaclav Hapla   Input Parameters:
6958bb7acecfSBarry Smith + dm   - The `DM` object
6959bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
6960306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
6961306894acSVaclav Hapla 
6962306894acSVaclav Hapla   Level: developer
6963306894acSVaclav Hapla 
6964bb7acecfSBarry Smith   Note:
6965306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
6966bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
6967306894acSVaclav Hapla   *label nullified.
6968306894acSVaclav Hapla 
6969bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
6970306894acSVaclav Hapla @*/
69719371c9d4SSatish Balay PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound) {
697243e45a93SVaclav Hapla   DMLabelLink link, *pnext;
6973306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
6974306894acSVaclav Hapla 
6975306894acSVaclav Hapla   PetscFunctionBegin;
6976306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6977306894acSVaclav Hapla   PetscValidPointer(label, 2);
6978f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
6979306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
6980306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
69815d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
698243e45a93SVaclav Hapla     if (*label == link->label) {
6983306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
698443e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
6985306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
6986ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
698743e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
69889566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
69899566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6990306894acSVaclav Hapla       break;
6991306894acSVaclav Hapla     }
6992306894acSVaclav Hapla   }
69937a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
6994306894acSVaclav Hapla   PetscFunctionReturn(0);
6995306894acSVaclav Hapla }
6996306894acSVaclav Hapla 
6997c58f1c22SToby Isaac /*@C
6998c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6999c58f1c22SToby Isaac 
7000c58f1c22SToby Isaac   Not Collective
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   Input Parameters:
7003bb7acecfSBarry Smith + dm   - The `DM` object
7004c58f1c22SToby Isaac - name - The label name
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Output Parameter:
7007c58f1c22SToby Isaac . output - The flag for output
7008c58f1c22SToby Isaac 
7009c58f1c22SToby Isaac   Level: developer
7010c58f1c22SToby Isaac 
7011bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7012c58f1c22SToby Isaac @*/
70139371c9d4SSatish Balay PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output) {
70145d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7015d67d17b1SMatthew G. Knepley   const char *lname;
7016c58f1c22SToby Isaac 
7017c58f1c22SToby Isaac   PetscFunctionBegin;
7018c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7019dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7020dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7021c58f1c22SToby Isaac   while (next) {
7022c58f1c22SToby Isaac     PetscBool flg;
7023c58f1c22SToby Isaac 
70249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70259566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
70269371c9d4SSatish Balay     if (flg) {
70279371c9d4SSatish Balay       *output = next->output;
70289371c9d4SSatish Balay       PetscFunctionReturn(0);
70299371c9d4SSatish Balay     }
7030c58f1c22SToby Isaac     next = next->next;
7031c58f1c22SToby Isaac   }
703298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7033c58f1c22SToby Isaac }
7034c58f1c22SToby Isaac 
7035c58f1c22SToby Isaac /*@C
7036bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac   Not Collective
7039c58f1c22SToby Isaac 
7040c58f1c22SToby Isaac   Input Parameters:
7041bb7acecfSBarry Smith + dm     - The `DM` object
7042c58f1c22SToby Isaac . name   - The label name
7043bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7044c58f1c22SToby Isaac 
7045c58f1c22SToby Isaac   Level: developer
7046c58f1c22SToby Isaac 
7047bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7048c58f1c22SToby Isaac @*/
70499371c9d4SSatish Balay PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output) {
70505d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7051d67d17b1SMatthew G. Knepley   const char *lname;
7052c58f1c22SToby Isaac 
7053c58f1c22SToby Isaac   PetscFunctionBegin;
7054c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7055534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7056c58f1c22SToby Isaac   while (next) {
7057c58f1c22SToby Isaac     PetscBool flg;
7058c58f1c22SToby Isaac 
70599566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70609566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
70619371c9d4SSatish Balay     if (flg) {
70629371c9d4SSatish Balay       next->output = output;
70639371c9d4SSatish Balay       PetscFunctionReturn(0);
70649371c9d4SSatish Balay     }
7065c58f1c22SToby Isaac     next = next->next;
7066c58f1c22SToby Isaac   }
706798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7068c58f1c22SToby Isaac }
7069c58f1c22SToby Isaac 
7070c58f1c22SToby Isaac /*@
7071bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7072c58f1c22SToby Isaac 
7073d083f849SBarry Smith   Collective on dmA
7074c58f1c22SToby Isaac 
7075d8d19677SJose E. Roman   Input Parameters:
7076bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7077bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7078bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7079bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7080bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   Level: intermediate
7083c58f1c22SToby Isaac 
7084bb7acecfSBarry Smith   Note:
70852cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7086c58f1c22SToby Isaac 
7087bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7088c58f1c22SToby Isaac @*/
70899371c9d4SSatish Balay PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode) {
70902cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7091c58f1c22SToby Isaac   const char *name;
7092c58f1c22SToby Isaac   PetscBool   flg;
70935d80c0bfSVaclav Hapla   DMLabelLink link;
7094c58f1c22SToby Isaac 
70955d80c0bfSVaclav Hapla   PetscFunctionBegin;
70965d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
70975d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
70985d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
70995d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
71007a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
71015d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
71025d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
71035d80c0bfSVaclav Hapla     label = link->label;
71049566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
71055d80c0bfSVaclav Hapla     if (!all) {
71069566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7107c58f1c22SToby Isaac       if (flg) continue;
71089566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
71097d5acc75SStefano Zampini       if (flg) continue;
71109566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7111ba2698f1SMatthew G. Knepley       if (flg) continue;
71125d80c0bfSVaclav Hapla     }
71139566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
71142cbb9b06SVaclav Hapla     if (labelOld) {
71152cbb9b06SVaclav Hapla       switch (emode) {
71169371c9d4SSatish Balay       case DM_COPY_LABELS_KEEP: continue;
71179371c9d4SSatish Balay       case DM_COPY_LABELS_REPLACE: PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE)); break;
71189371c9d4SSatish Balay       case DM_COPY_LABELS_FAIL: SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
71199371c9d4SSatish Balay       default: SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
71202cbb9b06SVaclav Hapla       }
71212cbb9b06SVaclav Hapla     }
71225d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
71239566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
71245d80c0bfSVaclav Hapla     } else {
71255d80c0bfSVaclav Hapla       labelNew = label;
71265d80c0bfSVaclav Hapla     }
71279566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
71289566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7129c58f1c22SToby Isaac   }
7130c58f1c22SToby Isaac   PetscFunctionReturn(0);
7131c58f1c22SToby Isaac }
7132461a15a0SLisandro Dalcin 
7133609dae6eSVaclav Hapla /*@C
7134bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7135609dae6eSVaclav Hapla 
71365efe38ccSVaclav Hapla   Collective
7137609dae6eSVaclav Hapla 
7138609dae6eSVaclav Hapla   Input Parameters:
7139bb7acecfSBarry Smith + dm0 - First `DM` object
7140bb7acecfSBarry Smith - dm1 - Second `DM` object
7141609dae6eSVaclav Hapla 
7142609dae6eSVaclav Hapla   Output Parameters
71435efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7144609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7145609dae6eSVaclav Hapla 
7146609dae6eSVaclav Hapla   Level: intermediate
7147609dae6eSVaclav Hapla 
7148609dae6eSVaclav Hapla   Notes:
7149bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7150bb7acecfSBarry Smith 
7151bb7acecfSBarry Smith   If equal is passed as NULL and difference is found, an error is thrown on all processes.
7152bb7acecfSBarry Smith 
7153bb7acecfSBarry Smith   Make sure to pass equal is NULL on all processes or none of them.
7154609dae6eSVaclav Hapla 
71555efe38ccSVaclav Hapla   The output message is set independently on each rank.
7156bb7acecfSBarry Smith 
7157bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7158bb7acecfSBarry Smith 
7159bb7acecfSBarry Smith   If message is passed as NULL and a difference is found, the difference description is printed to stderr in synchronized manner.
7160bb7acecfSBarry Smith 
7161bb7acecfSBarry Smith   Make sure to pass message as NULL on all processes or no processes.
7162609dae6eSVaclav Hapla 
7163609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7164bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7165609dae6eSVaclav Hapla 
7166bb7acecfSBarry Smith   Fortran Note:
7167bb7acecfSBarry Smith   This function is not available from Fortran.
7168609dae6eSVaclav Hapla 
7169bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7170609dae6eSVaclav Hapla @*/
71719371c9d4SSatish Balay PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message) {
71725efe38ccSVaclav Hapla   PetscInt    n, i;
7173609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
71745efe38ccSVaclav Hapla   PetscBool   eq;
7175609dae6eSVaclav Hapla   MPI_Comm    comm;
71765efe38ccSVaclav Hapla   PetscMPIInt rank;
7177609dae6eSVaclav Hapla 
7178609dae6eSVaclav Hapla   PetscFunctionBegin;
7179609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7180609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7181609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
71825efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7183609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
71849566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
71859566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
71865efe38ccSVaclav Hapla   {
71875efe38ccSVaclav Hapla     PetscInt n1;
71885efe38ccSVaclav Hapla 
71899566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
71909566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
71915efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
7192*48a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
71939566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
71945efe38ccSVaclav Hapla     if (!eq) goto finish;
71955efe38ccSVaclav Hapla   }
71965efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7197609dae6eSVaclav Hapla     DMLabel     l0, l1;
7198609dae6eSVaclav Hapla     const char *name;
7199609dae6eSVaclav Hapla     char       *msgInner;
7200609dae6eSVaclav Hapla 
7201609dae6eSVaclav Hapla     /* Ignore label order */
72029566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
72039566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
72049566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7205609dae6eSVaclav Hapla     if (!l1) {
720663a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
72075efe38ccSVaclav Hapla       eq = PETSC_FALSE;
72085efe38ccSVaclav Hapla       break;
7209609dae6eSVaclav Hapla     }
72109566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
72119566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
72129566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
72135efe38ccSVaclav Hapla     if (!eq) break;
7214609dae6eSVaclav Hapla   }
72159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7216609dae6eSVaclav Hapla finish:
72175efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7218609dae6eSVaclav Hapla   if (message) {
7219609dae6eSVaclav Hapla     *message = NULL;
7220*48a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
72215efe38ccSVaclav Hapla   } else {
7222*48a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
72239566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
72245efe38ccSVaclav Hapla   }
72255efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
72265efe38ccSVaclav Hapla   if (equal) *equal = eq;
72277a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7228609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7229609dae6eSVaclav Hapla }
7230609dae6eSVaclav Hapla 
72319371c9d4SSatish Balay PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value) {
7232461a15a0SLisandro Dalcin   PetscFunctionBegin;
7233461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7234461a15a0SLisandro Dalcin   if (!*label) {
72359566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
72369566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7237461a15a0SLisandro Dalcin   }
72389566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7239461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7240461a15a0SLisandro Dalcin }
7241461a15a0SLisandro Dalcin 
72420fdc7489SMatthew Knepley /*
72430fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
72440fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
72450fdc7489SMatthew Knepley   (label, id) pair in the DM.
72460fdc7489SMatthew Knepley 
72470fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
72480fdc7489SMatthew Knepley   each label.
72490fdc7489SMatthew Knepley */
72509371c9d4SSatish Balay PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal) {
72510fdc7489SMatthew Knepley   DMUniversalLabel ul;
72520fdc7489SMatthew Knepley   PetscBool       *active;
72530fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
72540fdc7489SMatthew Knepley 
72550fdc7489SMatthew Knepley   PetscFunctionBegin;
72569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
72579566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
72589566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
72599566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
72600fdc7489SMatthew Knepley   ul->Nl = 0;
72610fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
72620fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
72630fdc7489SMatthew Knepley     const char *name;
72640fdc7489SMatthew Knepley 
72659566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72669566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
72679566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
72680fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
72690fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
72700fdc7489SMatthew Knepley   }
72719566063dSJacob 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));
72720fdc7489SMatthew Knepley   ul->Nv = 0;
72730fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
72740fdc7489SMatthew Knepley     DMLabel     label;
72750fdc7489SMatthew Knepley     PetscInt    nv;
72760fdc7489SMatthew Knepley     const char *name;
72770fdc7489SMatthew Knepley 
72780fdc7489SMatthew Knepley     if (!active[l]) continue;
72799566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72809566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
72819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
72829566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
72830fdc7489SMatthew Knepley     ul->indices[m] = l;
72840fdc7489SMatthew Knepley     ul->Nv += nv;
72850fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
72860fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
72870fdc7489SMatthew Knepley     ++m;
72880fdc7489SMatthew Knepley   }
72890fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
72900fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
72910fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
72920fdc7489SMatthew Knepley   }
72930fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
72940fdc7489SMatthew Knepley     PetscInt b;
72950fdc7489SMatthew Knepley 
72960fdc7489SMatthew Knepley     ul->masks[l] = 0;
72970fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
72980fdc7489SMatthew Knepley   }
72999566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
73000fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
73010fdc7489SMatthew Knepley     DMLabel         label;
73020fdc7489SMatthew Knepley     IS              valueIS;
73030fdc7489SMatthew Knepley     const PetscInt *varr;
73040fdc7489SMatthew Knepley     PetscInt        nv, v;
73050fdc7489SMatthew Knepley 
73060fdc7489SMatthew Knepley     if (!active[l]) continue;
73079566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
73089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
73099566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
73109566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
73119371c9d4SSatish Balay     for (v = 0; v < nv; ++v) { ul->values[ul->offsets[m] + v] = varr[v]; }
73129566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
73139566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
73149566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
73150fdc7489SMatthew Knepley     ++m;
73160fdc7489SMatthew Knepley   }
73179566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
73180fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
73190fdc7489SMatthew Knepley     PetscInt  uval   = 0;
73200fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
73210fdc7489SMatthew Knepley 
73220fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
73230fdc7489SMatthew Knepley       DMLabel  label;
73240649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
73250fdc7489SMatthew Knepley 
73260fdc7489SMatthew Knepley       if (!active[l]) continue;
73279566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
73289566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
73299566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
73309371c9d4SSatish Balay       if (val == defval) {
73319371c9d4SSatish Balay         ++m;
73329371c9d4SSatish Balay         continue;
73339371c9d4SSatish Balay       }
73340649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
73350fdc7489SMatthew Knepley       marked = PETSC_TRUE;
73369566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
733763a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
73380fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
73390fdc7489SMatthew Knepley       ++m;
73400fdc7489SMatthew Knepley     }
73419566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
73420fdc7489SMatthew Knepley   }
73439566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
73440fdc7489SMatthew Knepley   *universal = ul;
73450fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73460fdc7489SMatthew Knepley }
73470fdc7489SMatthew Knepley 
73489371c9d4SSatish Balay PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal) {
73490fdc7489SMatthew Knepley   PetscInt l;
73500fdc7489SMatthew Knepley 
73510fdc7489SMatthew Knepley   PetscFunctionBegin;
73529566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
73539566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
73549566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
73559566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
73569566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
73570fdc7489SMatthew Knepley   *universal = NULL;
73580fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73590fdc7489SMatthew Knepley }
73600fdc7489SMatthew Knepley 
73619371c9d4SSatish Balay PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel) {
73620fdc7489SMatthew Knepley   PetscFunctionBegin;
73630fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
73640fdc7489SMatthew Knepley   *ulabel = ul->label;
73650fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73660fdc7489SMatthew Knepley }
73670fdc7489SMatthew Knepley 
73689371c9d4SSatish Balay PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm) {
73690fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
73700fdc7489SMatthew Knepley 
73710fdc7489SMatthew Knepley   PetscFunctionBegin;
7372064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
73730fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
73749566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
73759566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
73760fdc7489SMatthew Knepley   }
73770fdc7489SMatthew Knepley   if (preserveOrder) {
73780fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
73790fdc7489SMatthew Knepley       const char *name;
73800fdc7489SMatthew Knepley       PetscBool   match;
73810fdc7489SMatthew Knepley 
73829566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
73839566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
738463a3b9bcSJacob 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]);
73850fdc7489SMatthew Knepley     }
73860fdc7489SMatthew Knepley   }
73870fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73880fdc7489SMatthew Knepley }
73890fdc7489SMatthew Knepley 
73909371c9d4SSatish Balay PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value) {
73910fdc7489SMatthew Knepley   PetscInt l;
73920fdc7489SMatthew Knepley 
73930fdc7489SMatthew Knepley   PetscFunctionBegin;
73940fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
73950fdc7489SMatthew Knepley     DMLabel  label;
73960fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
73970fdc7489SMatthew Knepley 
73980fdc7489SMatthew Knepley     if (lval) {
73999566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
74009566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
74019566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
74020fdc7489SMatthew Knepley     }
74030fdc7489SMatthew Knepley   }
74040fdc7489SMatthew Knepley   PetscFunctionReturn(0);
74050fdc7489SMatthew Knepley }
7406a8fb8f29SToby Isaac 
7407a8fb8f29SToby Isaac /*@
7408bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7409bb7acecfSBarry Smith 
7410bb7acecfSBarry Smith   Not collective
7411a8fb8f29SToby Isaac 
7412a8fb8f29SToby Isaac   Input Parameter:
7413bb7acecfSBarry Smith . dm - The `DM` object
7414a8fb8f29SToby Isaac 
7415a8fb8f29SToby Isaac   Output Parameter:
7416bb7acecfSBarry Smith . cdm - The coarse `DM`
7417a8fb8f29SToby Isaac 
7418a8fb8f29SToby Isaac   Level: intermediate
7419a8fb8f29SToby Isaac 
7420bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7421a8fb8f29SToby Isaac @*/
74229371c9d4SSatish Balay PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm) {
7423a8fb8f29SToby Isaac   PetscFunctionBegin;
7424a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7425a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7426a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7427a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7428a8fb8f29SToby Isaac }
7429a8fb8f29SToby Isaac 
7430a8fb8f29SToby Isaac /*@
7431bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7432a8fb8f29SToby Isaac 
7433a8fb8f29SToby Isaac   Input Parameters:
7434bb7acecfSBarry Smith + dm - The `DM` object
7435bb7acecfSBarry Smith - cdm - The coarse `DM`
7436a8fb8f29SToby Isaac 
7437a8fb8f29SToby Isaac   Level: intermediate
7438a8fb8f29SToby Isaac 
7439bb7acecfSBarry Smith   Note:
7440bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7441bb7acecfSBarry Smith 
7442bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7443a8fb8f29SToby Isaac @*/
74449371c9d4SSatish Balay PetscErrorCode DMSetCoarseDM(DM dm, DM cdm) {
7445a8fb8f29SToby Isaac   PetscFunctionBegin;
7446a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7447a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
74489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
74499566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7450a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7451a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7452a8fb8f29SToby Isaac }
7453a8fb8f29SToby Isaac 
745488bdff64SToby Isaac /*@
7455bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
745688bdff64SToby Isaac 
745788bdff64SToby Isaac   Input Parameter:
7458bb7acecfSBarry Smith . dm - The `DM` object
745988bdff64SToby Isaac 
746088bdff64SToby Isaac   Output Parameter:
7461bb7acecfSBarry Smith . fdm - The fine `DM`
746288bdff64SToby Isaac 
746388bdff64SToby Isaac   Level: intermediate
746488bdff64SToby Isaac 
7465bb7acecfSBarry Smith .seealso: `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
746688bdff64SToby Isaac @*/
74679371c9d4SSatish Balay PetscErrorCode DMGetFineDM(DM dm, DM *fdm) {
746888bdff64SToby Isaac   PetscFunctionBegin;
746988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
747088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
747188bdff64SToby Isaac   *fdm = dm->fineMesh;
747288bdff64SToby Isaac   PetscFunctionReturn(0);
747388bdff64SToby Isaac }
747488bdff64SToby Isaac 
747588bdff64SToby Isaac /*@
7476bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
747788bdff64SToby Isaac 
747888bdff64SToby Isaac   Input Parameters:
7479bb7acecfSBarry Smith + dm - The `DM` object
7480bb7acecfSBarry Smith - fdm - The fine `DM`
748188bdff64SToby Isaac 
7482bb7acecfSBarry Smith   Level: developer
748388bdff64SToby Isaac 
7484bb7acecfSBarry Smith   Note:
7485bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7486bb7acecfSBarry Smith 
7487bb7acecfSBarry Smith .seealso: `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
748888bdff64SToby Isaac @*/
74899371c9d4SSatish Balay PetscErrorCode DMSetFineDM(DM dm, DM fdm) {
749088bdff64SToby Isaac   PetscFunctionBegin;
749188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
749288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
74939566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
74949566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
749588bdff64SToby Isaac   dm->fineMesh = fdm;
749688bdff64SToby Isaac   PetscFunctionReturn(0);
749788bdff64SToby Isaac }
749888bdff64SToby Isaac 
7499a6ba4734SToby Isaac /*@C
7500bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7501a6ba4734SToby Isaac 
7502783e2ec8SMatthew G. Knepley   Collective on dm
7503783e2ec8SMatthew G. Knepley 
7504a6ba4734SToby Isaac   Input Parameters:
7505bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7506bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7507a6ba4734SToby Isaac . name     - The BC name
750845480ffeSMatthew G. Knepley . label    - The label defining constrained points
7509bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
751045480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7511a6ba4734SToby Isaac . field    - The field to constrain
751245480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7513a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7514a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
751556cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7516a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7517a6ba4734SToby Isaac 
751845480ffeSMatthew G. Knepley   Output Parameter:
751945480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
752045480ffeSMatthew G. Knepley 
7521a6ba4734SToby Isaac   Options Database Keys:
7522a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7523a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7524a6ba4734SToby Isaac 
7525bb7acecfSBarry Smith   Notes:
7526bb7acecfSBarry 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:
752756cf3b9cSMatthew G. Knepley 
752856cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
752956cf3b9cSMatthew G. Knepley 
7530bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
753156cf3b9cSMatthew G. Knepley 
753256cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
753356cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
753456cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
753556cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
753656cf3b9cSMatthew G. Knepley 
753756cf3b9cSMatthew G. Knepley + dim - the spatial dimension
753856cf3b9cSMatthew G. Knepley . Nf - the number of fields
753956cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
754056cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
754156cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
754256cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
754356cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
754456cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
754556cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
754656cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
754756cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
754856cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
754956cf3b9cSMatthew G. Knepley . t - current time
755056cf3b9cSMatthew G. Knepley . x - coordinates of the current point
755156cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
755256cf3b9cSMatthew G. Knepley . constants - constant parameters
755356cf3b9cSMatthew G. Knepley - bcval - output values at the current point
755456cf3b9cSMatthew G. Knepley 
7555ed808b8fSJed Brown   Level: intermediate
7556a6ba4734SToby Isaac 
7557db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7558a6ba4734SToby Isaac @*/
75599371c9d4SSatish Balay 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) {
7560e5e52638SMatthew G. Knepley   PetscDS ds;
7561a6ba4734SToby Isaac 
7562a6ba4734SToby Isaac   PetscFunctionBegin;
7563a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7564783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
756545480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
756645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
756745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
756845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
756901a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
75709566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7571799db056SMatthew G. Knepley   /* Complete label */
7572799db056SMatthew G. Knepley   if (label) {
7573799db056SMatthew G. Knepley     PetscObject  obj;
7574799db056SMatthew G. Knepley     PetscClassId id;
7575799db056SMatthew G. Knepley 
7576799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7577799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7578799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7579799db056SMatthew G. Knepley       DM plex;
7580799db056SMatthew G. Knepley 
7581799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7582799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7583799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7584799db056SMatthew G. Knepley     }
7585799db056SMatthew G. Knepley   }
75869566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7587a6ba4734SToby Isaac   PetscFunctionReturn(0);
7588a6ba4734SToby Isaac }
7589a6ba4734SToby Isaac 
759045480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
75919371c9d4SSatish Balay static PetscErrorCode DMPopulateBoundary(DM dm) {
7592e5e52638SMatthew G. Knepley   PetscDS     ds;
7593dff059c6SToby Isaac   DMBoundary *lastnext;
7594e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7595e6f8dbb6SToby Isaac 
7596e6f8dbb6SToby Isaac   PetscFunctionBegin;
75979566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7598e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
759947a1f5adSToby Isaac   if (dm->boundary) {
760047a1f5adSToby Isaac     DMBoundary next = dm->boundary;
760147a1f5adSToby Isaac 
760247a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
760347a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
760447a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
760547a1f5adSToby Isaac     while (next) {
760647a1f5adSToby Isaac       DMBoundary b = next;
760747a1f5adSToby Isaac 
760847a1f5adSToby Isaac       next = b->next;
76099566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7610a6ba4734SToby Isaac     }
761147a1f5adSToby Isaac     dm->boundary = NULL;
7612a6ba4734SToby Isaac   }
761347a1f5adSToby Isaac 
7614dff059c6SToby Isaac   lastnext = &(dm->boundary);
7615e6f8dbb6SToby Isaac   while (dsbound) {
7616e6f8dbb6SToby Isaac     DMBoundary dmbound;
7617e6f8dbb6SToby Isaac 
76189566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7619e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
762045480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
762147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7622dff059c6SToby Isaac     *lastnext           = dmbound;
7623dff059c6SToby Isaac     lastnext            = &(dmbound->next);
7624dff059c6SToby Isaac     dsbound             = dsbound->next;
7625a6ba4734SToby Isaac   }
7626a6ba4734SToby Isaac   PetscFunctionReturn(0);
7627a6ba4734SToby Isaac }
7628a6ba4734SToby Isaac 
7629bb7acecfSBarry Smith /* TODO: missing manual page */
76309371c9d4SSatish Balay PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd) {
7631b95f2879SToby Isaac   DMBoundary b;
7632a6ba4734SToby Isaac 
7633a6ba4734SToby Isaac   PetscFunctionBegin;
7634a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7635534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7636a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
76379566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7638b95f2879SToby Isaac   b = dm->boundary;
7639a6ba4734SToby Isaac   while (b && !(*isBd)) {
7640e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7641e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7642a6ba4734SToby Isaac     PetscInt   i;
7643a6ba4734SToby Isaac 
764445480ffeSMatthew G. Knepley     if (label) {
76459566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7646a6ba4734SToby Isaac     }
7647a6ba4734SToby Isaac     b = b->next;
7648a6ba4734SToby Isaac   }
7649a6ba4734SToby Isaac   PetscFunctionReturn(0);
7650a6ba4734SToby Isaac }
76514d6f44ffSToby Isaac 
76524d6f44ffSToby Isaac /*@C
7653bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7654a6e0b375SMatthew G. Knepley 
7655bb7acecfSBarry Smith   Collective on dm
76564d6f44ffSToby Isaac 
76574d6f44ffSToby Isaac   Input Parameters:
7658bb7acecfSBarry Smith + dm      - The `DM`
76590709b2feSToby Isaac . time    - The time
76604d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76614d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76624d6f44ffSToby Isaac - mode    - The insertion mode for values
76634d6f44ffSToby Isaac 
76644d6f44ffSToby Isaac   Output Parameter:
76654d6f44ffSToby Isaac . X - vector
76664d6f44ffSToby Isaac 
76674d6f44ffSToby Isaac    Calling sequence of func:
766877b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
76694d6f44ffSToby Isaac 
76704d6f44ffSToby Isaac +  dim - The spatial dimension
76718ec8862eSJed Brown .  time - The time at which to sample
76724d6f44ffSToby Isaac .  x   - The coordinates
767377b739a6SMatthew Knepley .  Nc  - The number of components
76744d6f44ffSToby Isaac .  u   - The output field values
76754d6f44ffSToby Isaac -  ctx - optional user-defined function context
76764d6f44ffSToby Isaac 
76774d6f44ffSToby Isaac   Level: developer
76784d6f44ffSToby Isaac 
7679bb7acecfSBarry Smith   Developer Notes:
7680bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7681bb7acecfSBarry Smith 
7682bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7683bb7acecfSBarry Smith 
7684db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
76854d6f44ffSToby Isaac @*/
76869371c9d4SSatish Balay PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X) {
76874d6f44ffSToby Isaac   Vec localX;
76884d6f44ffSToby Isaac 
76894d6f44ffSToby Isaac   PetscFunctionBegin;
76904d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76919566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
76929566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
76939566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
76949566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
76959566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
76964d6f44ffSToby Isaac   PetscFunctionReturn(0);
76974d6f44ffSToby Isaac }
76984d6f44ffSToby Isaac 
7699a6e0b375SMatthew G. Knepley /*@C
7700bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7701a6e0b375SMatthew G. Knepley 
7702a6e0b375SMatthew G. Knepley   Not collective
7703a6e0b375SMatthew G. Knepley 
7704a6e0b375SMatthew G. Knepley   Input Parameters:
7705bb7acecfSBarry Smith + dm      - The `DM`
7706a6e0b375SMatthew G. Knepley . time    - The time
7707a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7708a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7709a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7710a6e0b375SMatthew G. Knepley 
7711a6e0b375SMatthew G. Knepley   Output Parameter:
7712a6e0b375SMatthew G. Knepley . localX - vector
7713a6e0b375SMatthew G. Knepley 
7714a6e0b375SMatthew G. Knepley    Calling sequence of func:
771577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7716a6e0b375SMatthew G. Knepley 
7717a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7718a6e0b375SMatthew G. Knepley .  x   - The coordinates
771977b739a6SMatthew Knepley .  Nc  - The number of components
7720a6e0b375SMatthew G. Knepley .  u   - The output field values
7721a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7722a6e0b375SMatthew G. Knepley 
7723a6e0b375SMatthew G. Knepley   Level: developer
7724a6e0b375SMatthew G. Knepley 
7725bb7acecfSBarry Smith   Developer Notes:
7726bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7727bb7acecfSBarry Smith 
7728bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7729bb7acecfSBarry Smith 
7730db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7731a6e0b375SMatthew G. Knepley @*/
77329371c9d4SSatish Balay PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) {
77334d6f44ffSToby Isaac   PetscFunctionBegin;
77344d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7735064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
77369566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
77374d6f44ffSToby Isaac   PetscFunctionReturn(0);
77384d6f44ffSToby Isaac }
77394d6f44ffSToby Isaac 
7740a6e0b375SMatthew G. Knepley /*@C
7741bb7acecfSBarry 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.
7742a6e0b375SMatthew G. Knepley 
7743bb7acecfSBarry Smith   Collective on dm
7744a6e0b375SMatthew G. Knepley 
7745a6e0b375SMatthew G. Knepley   Input Parameters:
7746bb7acecfSBarry Smith + dm      - The `DM`
7747a6e0b375SMatthew G. Knepley . time    - The time
7748bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7749a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7750bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
7751a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7752a6e0b375SMatthew G. Knepley 
7753a6e0b375SMatthew G. Knepley   Output Parameter:
7754a6e0b375SMatthew G. Knepley . X - vector
7755a6e0b375SMatthew G. Knepley 
7756a6e0b375SMatthew G. Knepley    Calling sequence of func:
775777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7758a6e0b375SMatthew G. Knepley 
7759a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7760a6e0b375SMatthew G. Knepley .  x   - The coordinates
776177b739a6SMatthew Knepley .  Nc  - The number of components
7762a6e0b375SMatthew G. Knepley .  u   - The output field values
7763a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7764a6e0b375SMatthew G. Knepley 
7765a6e0b375SMatthew G. Knepley   Level: developer
7766a6e0b375SMatthew G. Knepley 
7767bb7acecfSBarry Smith   Developer Notes:
7768bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7769bb7acecfSBarry Smith 
7770bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7771bb7acecfSBarry Smith 
7772db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7773a6e0b375SMatthew G. Knepley @*/
77749371c9d4SSatish Balay 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) {
77752c53366bSMatthew G. Knepley   Vec localX;
77762c53366bSMatthew G. Knepley 
77772c53366bSMatthew G. Knepley   PetscFunctionBegin;
77782c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77799566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
77809566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
77819566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
77829566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
77839566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
77842c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
77852c53366bSMatthew G. Knepley }
77862c53366bSMatthew G. Knepley 
7787a6e0b375SMatthew G. Knepley /*@C
7788bb7acecfSBarry 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.
7789a6e0b375SMatthew G. Knepley 
7790a6e0b375SMatthew G. Knepley   Not collective
7791a6e0b375SMatthew G. Knepley 
7792a6e0b375SMatthew G. Knepley   Input Parameters:
7793bb7acecfSBarry Smith + dm      - The `DM`
7794a6e0b375SMatthew G. Knepley . time    - The time
7795bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7796a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7797a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7798a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7799a6e0b375SMatthew G. Knepley 
7800a6e0b375SMatthew G. Knepley   Output Parameter:
7801a6e0b375SMatthew G. Knepley . localX - vector
7802a6e0b375SMatthew G. Knepley 
7803a6e0b375SMatthew G. Knepley    Calling sequence of func:
780477b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7805a6e0b375SMatthew G. Knepley 
7806a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7807a6e0b375SMatthew G. Knepley .  x   - The coordinates
780877b739a6SMatthew Knepley .  Nc  - The number of components
7809a6e0b375SMatthew G. Knepley .  u   - The output field values
7810a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7811a6e0b375SMatthew G. Knepley 
7812a6e0b375SMatthew G. Knepley   Level: developer
7813a6e0b375SMatthew G. Knepley 
7814bb7acecfSBarry Smith   Developer Notes:
7815bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7816bb7acecfSBarry Smith 
7817bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7818bb7acecfSBarry Smith 
7819db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7820a6e0b375SMatthew G. Knepley @*/
78219371c9d4SSatish Balay 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) {
78224d6f44ffSToby Isaac   PetscFunctionBegin;
78234d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7824064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
78259566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
78264d6f44ffSToby Isaac   PetscFunctionReturn(0);
78274d6f44ffSToby Isaac }
78282716604bSToby Isaac 
7829a6e0b375SMatthew G. Knepley /*@C
7830bb7acecfSBarry 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.
7831a6e0b375SMatthew G. Knepley 
7832a6e0b375SMatthew G. Knepley   Not collective
7833a6e0b375SMatthew G. Knepley 
7834a6e0b375SMatthew G. Knepley   Input Parameters:
7835bb7acecfSBarry Smith + dm      - The `DM`
7836a6e0b375SMatthew G. Knepley . time    - The time
7837a6e0b375SMatthew G. Knepley . localU  - The input field vector
7838a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7839a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7840a6e0b375SMatthew G. Knepley 
7841a6e0b375SMatthew G. Knepley   Output Parameter:
7842a6e0b375SMatthew G. Knepley . localX  - The output vector
7843a6e0b375SMatthew G. Knepley 
7844a6e0b375SMatthew G. Knepley    Calling sequence of func:
7845a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7846a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7847a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7848a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7849a6e0b375SMatthew G. Knepley 
7850a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7851a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7852a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7853a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7854a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7855a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7856a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7857a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7858a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7859a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7860a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7861a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7862a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7863a6e0b375SMatthew G. Knepley .  t            - The current time
7864a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7865a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7866a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7867a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7868a6e0b375SMatthew G. Knepley 
7869bb7acecfSBarry Smith   Note:
7870bb7acecfSBarry 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.
7871bb7acecfSBarry 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
7872bb7acecfSBarry 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
7873a6e0b375SMatthew 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.
7874a6e0b375SMatthew G. Knepley 
7875a6e0b375SMatthew G. Knepley   Level: intermediate
7876a6e0b375SMatthew G. Knepley 
7877bb7acecfSBarry Smith   Developer Notes:
7878bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7879bb7acecfSBarry Smith 
7880bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7881bb7acecfSBarry Smith 
7882db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7883a6e0b375SMatthew G. Knepley @*/
78849371c9d4SSatish Balay 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) {
78858c6c5593SMatthew G. Knepley   PetscFunctionBegin;
78868c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78878c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
78888c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
78899566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
78908c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
78918c6c5593SMatthew G. Knepley }
78928c6c5593SMatthew G. Knepley 
7893a6e0b375SMatthew G. Knepley /*@C
7894a6e0b375SMatthew 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.
7895a6e0b375SMatthew G. Knepley 
7896a6e0b375SMatthew G. Knepley   Not collective
7897a6e0b375SMatthew G. Knepley 
7898a6e0b375SMatthew G. Knepley   Input Parameters:
7899bb7acecfSBarry Smith + dm      - The `DM`
7900a6e0b375SMatthew G. Knepley . time    - The time
7901bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
7902a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7903a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7904bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
7905a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7906a6e0b375SMatthew G. Knepley . localU  - The input field vector
7907a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7908a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7909a6e0b375SMatthew G. Knepley 
7910a6e0b375SMatthew G. Knepley   Output Parameter:
7911a6e0b375SMatthew G. Knepley . localX  - The output vector
7912a6e0b375SMatthew G. Knepley 
7913a6e0b375SMatthew G. Knepley    Calling sequence of func:
7914a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7915a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7916a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7917a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7918a6e0b375SMatthew G. Knepley 
7919a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7920a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7921a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7922a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7923a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7924a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7925a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7926a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7927a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7928a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7929a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7930a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7931a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7932a6e0b375SMatthew G. Knepley .  t            - The current time
7933a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7934a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7935a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7936a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7937a6e0b375SMatthew G. Knepley 
7938bb7acecfSBarry Smith   Note:
7939bb7acecfSBarry 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.
7940bb7acecfSBarry 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
7941bb7acecfSBarry 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
7942a6e0b375SMatthew 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.
7943a6e0b375SMatthew G. Knepley 
7944a6e0b375SMatthew G. Knepley   Level: intermediate
7945a6e0b375SMatthew G. Knepley 
7946bb7acecfSBarry Smith   Developer Notes:
7947bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7948bb7acecfSBarry Smith 
7949bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7950bb7acecfSBarry Smith 
7951d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7952a6e0b375SMatthew G. Knepley @*/
79539371c9d4SSatish Balay 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) {
79548c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79558c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7956064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
7957064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
79589566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
79598c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79608c6c5593SMatthew G. Knepley }
79618c6c5593SMatthew G. Knepley 
79622716604bSToby Isaac /*@C
7963d29d7c6eSMatthew 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.
7964d29d7c6eSMatthew G. Knepley 
7965d29d7c6eSMatthew G. Knepley   Not collective
7966d29d7c6eSMatthew G. Knepley 
7967d29d7c6eSMatthew G. Knepley   Input Parameters:
7968bb7acecfSBarry Smith + dm      - The `DM`
7969d29d7c6eSMatthew G. Knepley . time    - The time
7970bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
7971d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
7972d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
7973bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
7974d29d7c6eSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7975d29d7c6eSMatthew G. Knepley . U       - The input field vector
7976d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7977d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
7978d29d7c6eSMatthew G. Knepley 
7979d29d7c6eSMatthew G. Knepley   Output Parameter:
7980d29d7c6eSMatthew G. Knepley . X       - The output vector
7981d29d7c6eSMatthew G. Knepley 
7982d29d7c6eSMatthew G. Knepley    Calling sequence of func:
7983d29d7c6eSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7984d29d7c6eSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7985d29d7c6eSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7986d29d7c6eSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7987d29d7c6eSMatthew G. Knepley 
7988d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
7989d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
7990d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7991d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
7992d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7993d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
7994d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7995d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7996d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7997d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7998d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7999d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8000d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8001d29d7c6eSMatthew G. Knepley .  t            - The current time
8002d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8003d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8004d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8005d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8006d29d7c6eSMatthew G. Knepley 
8007bb7acecfSBarry Smith   Note:
8008bb7acecfSBarry 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.
8009bb7acecfSBarry 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
8010bb7acecfSBarry 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
8011d29d7c6eSMatthew 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.
8012d29d7c6eSMatthew G. Knepley 
8013d29d7c6eSMatthew G. Knepley   Level: intermediate
8014d29d7c6eSMatthew G. Knepley 
8015bb7acecfSBarry Smith   Developer Notes:
8016bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8017bb7acecfSBarry Smith 
8018bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8019bb7acecfSBarry Smith 
8020d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8021d29d7c6eSMatthew G. Knepley @*/
80229371c9d4SSatish Balay 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) {
8023d29d7c6eSMatthew G. Knepley   DM  dmIn;
8024d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8025d29d7c6eSMatthew G. Knepley 
8026d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8027d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8028d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8029d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8030d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8031d29d7c6eSMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dm, U, mode, localU));
8032d29d7c6eSMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dm, U, mode, localU));
8033d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8034d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8035d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8036d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8037d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
8038d29d7c6eSMatthew G. Knepley   PetscFunctionReturn(0);
8039d29d7c6eSMatthew G. Knepley }
8040d29d7c6eSMatthew G. Knepley 
8041d29d7c6eSMatthew G. Knepley /*@C
8042ece3a9fcSMatthew 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.
8043ece3a9fcSMatthew G. Knepley 
8044ece3a9fcSMatthew G. Knepley   Not collective
8045ece3a9fcSMatthew G. Knepley 
8046ece3a9fcSMatthew G. Knepley   Input Parameters:
8047bb7acecfSBarry Smith + dm      - The `DM`
8048ece3a9fcSMatthew G. Knepley . time    - The time
8049bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8050ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8051ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8052bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8053ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8054ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8055ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8056ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8057ece3a9fcSMatthew G. Knepley 
8058ece3a9fcSMatthew G. Knepley   Output Parameter:
8059ece3a9fcSMatthew G. Knepley . localX  - The output vector
8060ece3a9fcSMatthew G. Knepley 
8061ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8062ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8063ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8064ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8065ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8066ece3a9fcSMatthew G. Knepley 
8067ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8068ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8069ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8070ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8071ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8072ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8073ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8074ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8075ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8076ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8077ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8078ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8079ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8080ece3a9fcSMatthew G. Knepley .  t            - The current time
8081ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8082ece3a9fcSMatthew G. Knepley .  n            - The face normal
8083ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8084ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8085ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8086ece3a9fcSMatthew G. Knepley 
8087ece3a9fcSMatthew G. Knepley   Note:
8088bb7acecfSBarry 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.
8089bb7acecfSBarry 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
8090bb7acecfSBarry 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
8091ece3a9fcSMatthew 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.
8092ece3a9fcSMatthew G. Knepley 
8093ece3a9fcSMatthew G. Knepley   Level: intermediate
8094ece3a9fcSMatthew G. Knepley 
8095bb7acecfSBarry Smith   Developer Notes:
8096bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8097bb7acecfSBarry Smith 
8098bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8099bb7acecfSBarry Smith 
8100db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8101ece3a9fcSMatthew G. Knepley @*/
81029371c9d4SSatish Balay 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) {
8103ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8104ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8105064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8106064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
81079566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8108ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8109ece3a9fcSMatthew G. Knepley }
8110ece3a9fcSMatthew G. Knepley 
8111ece3a9fcSMatthew G. Knepley /*@C
81122716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
81132716604bSToby Isaac 
8114bb7acecfSBarry Smith   Collective on dm
8115bb7acecfSBarry Smith 
81162716604bSToby Isaac   Input Parameters:
8117bb7acecfSBarry Smith + dm    - The `DM`
81180709b2feSToby Isaac . time  - The time
81192716604bSToby Isaac . funcs - The functions to evaluate for each field component
81202716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8121574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
81222716604bSToby Isaac 
81232716604bSToby Isaac   Output Parameter:
81242716604bSToby Isaac . diff - The diff ||u - u_h||_2
81252716604bSToby Isaac 
81262716604bSToby Isaac   Level: developer
81272716604bSToby Isaac 
8128bb7acecfSBarry Smith   Developer Notes:
8129bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8130bb7acecfSBarry Smith 
8131bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8132bb7acecfSBarry Smith 
8133db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
81342716604bSToby Isaac @*/
81359371c9d4SSatish Balay PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) {
81362716604bSToby Isaac   PetscFunctionBegin;
81372716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8138b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
81399566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
81402716604bSToby Isaac   PetscFunctionReturn(0);
81412716604bSToby Isaac }
8142b698f381SToby Isaac 
8143b698f381SToby Isaac /*@C
8144b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8145b698f381SToby Isaac 
8146d083f849SBarry Smith   Collective on dm
8147d083f849SBarry Smith 
8148b698f381SToby Isaac   Input Parameters:
8149bb7acecfSBarry Smith + dm    - The `DM`
8150b698f381SToby Isaac , time  - The time
8151b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8152b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8153574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8154b698f381SToby Isaac - n     - The vector to project along
8155b698f381SToby Isaac 
8156b698f381SToby Isaac   Output Parameter:
8157b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8158b698f381SToby Isaac 
8159b698f381SToby Isaac   Level: developer
8160b698f381SToby Isaac 
8161bb7acecfSBarry Smith   Developer Notes:
8162bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8163bb7acecfSBarry Smith 
8164bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8165bb7acecfSBarry Smith 
8166bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8167b698f381SToby Isaac @*/
81689371c9d4SSatish Balay 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) {
8169b698f381SToby Isaac   PetscFunctionBegin;
8170b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8171b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
81729566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
8173b698f381SToby Isaac   PetscFunctionReturn(0);
8174b698f381SToby Isaac }
8175b698f381SToby Isaac 
81762a16baeaSToby Isaac /*@C
81772a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
81782a16baeaSToby Isaac 
8179d083f849SBarry Smith   Collective on dm
8180d083f849SBarry Smith 
81812a16baeaSToby Isaac   Input Parameters:
8182bb7acecfSBarry Smith + dm    - The `DM`
81832a16baeaSToby Isaac . time  - The time
81842a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
81852a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8186574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
81872a16baeaSToby Isaac 
81882a16baeaSToby Isaac   Output Parameter:
81892a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
81902a16baeaSToby Isaac 
81912a16baeaSToby Isaac   Level: developer
81922a16baeaSToby Isaac 
8193bb7acecfSBarry Smith   Developer Notes:
8194bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8195bb7acecfSBarry Smith 
8196bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8197bb7acecfSBarry Smith 
8198db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
81992a16baeaSToby Isaac @*/
82009371c9d4SSatish Balay PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) {
82012a16baeaSToby Isaac   PetscFunctionBegin;
82022a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82032a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
82049566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
82052a16baeaSToby Isaac   PetscFunctionReturn(0);
82062a16baeaSToby Isaac }
82072a16baeaSToby Isaac 
8208df0b854cSToby Isaac /*@C
8209bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8210502a2867SDave May 
8211502a2867SDave May  Not Collective
8212502a2867SDave May 
8213502a2867SDave May  Input Parameter:
8214bb7acecfSBarry Smith .  dm    - The `DM`
8215502a2867SDave May 
82160a19bb7dSprj-  Output Parameters:
82170a19bb7dSprj- +  nranks - the number of neighbours
82180a19bb7dSprj- -  ranks - the neighbors ranks
8219502a2867SDave May 
8220bb7acecfSBarry Smith  Note:
8221bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8222502a2867SDave May 
8223502a2867SDave May  Level: beginner
8224502a2867SDave May 
8225db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8226502a2867SDave May @*/
82279371c9d4SSatish Balay PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[]) {
8228502a2867SDave May   PetscFunctionBegin;
8229502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82309566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
8231502a2867SDave May   PetscFunctionReturn(0);
8232502a2867SDave May }
8233502a2867SDave May 
8234531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8235531c7667SBarry Smith 
8236531c7667SBarry Smith /*
8237531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8238531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8239531c7667SBarry Smith */
82409371c9d4SSatish Balay PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx) {
8241531c7667SBarry Smith   PetscFunctionBegin;
8242531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8243531c7667SBarry Smith     Vec x1local;
8244531c7667SBarry Smith     DM  dm;
82459566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
82467a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
82479566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
82489566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
82499566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8250531c7667SBarry Smith     x1 = x1local;
8251531c7667SBarry Smith   }
82529566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8253531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8254531c7667SBarry Smith     DM dm;
82559566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
82569566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8257531c7667SBarry Smith   }
8258531c7667SBarry Smith   PetscFunctionReturn(0);
8259531c7667SBarry Smith }
8260531c7667SBarry Smith 
8261531c7667SBarry Smith /*@
8262bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8263531c7667SBarry Smith 
8264531c7667SBarry Smith     Input Parameter:
8265bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8266531c7667SBarry Smith 
8267bb7acecfSBarry Smith     Developer Note:
8268bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8269531c7667SBarry Smith 
82701b266c99SBarry Smith     Level: advanced
82711b266c99SBarry Smith 
8272db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8273531c7667SBarry Smith @*/
82749371c9d4SSatish Balay PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring) {
8275531c7667SBarry Smith   PetscFunctionBegin;
8276531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8277531c7667SBarry Smith   PetscFunctionReturn(0);
8278531c7667SBarry Smith }
82798320bc6fSPatrick Sanan 
82808320bc6fSPatrick Sanan /*@
8281bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
82828320bc6fSPatrick Sanan 
82838320bc6fSPatrick Sanan     Collective
82848320bc6fSPatrick Sanan 
82858320bc6fSPatrick Sanan     Input Parameters:
8286bb7acecfSBarry Smith +    dm1 - the first `DM`
8287bb7acecfSBarry Smith -    dm2 - the second `DM`
82888320bc6fSPatrick Sanan 
82898320bc6fSPatrick Sanan     Output Parameters:
8290bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8291bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
82928320bc6fSPatrick Sanan 
82938320bc6fSPatrick Sanan     Notes:
8294bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
82953d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
82968320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8297bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
82983d862458SPatrick Sanan     decomposition, but hold different data.
82998320bc6fSPatrick Sanan 
83008320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8301bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
83028320bc6fSPatrick Sanan 
8303bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
83048320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
83058320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8306bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
83078320bc6fSPatrick Sanan 
8308bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8309bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
83108320bc6fSPatrick Sanan .vb
83118320bc6fSPatrick Sanan   ...
83129566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
83138320bc6fSPatrick Sanan   if (set && compatible)  {
83149566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
83159566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
83169566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
83178320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
83188320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
83198320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
83208320bc6fSPatrick Sanan       }
83218320bc6fSPatrick Sanan     }
83229566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
83239566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
83248320bc6fSPatrick Sanan   } else {
83258320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
83268320bc6fSPatrick Sanan   }
83278320bc6fSPatrick Sanan   ...
83288320bc6fSPatrick Sanan .ve
83298320bc6fSPatrick Sanan 
8330bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
83318320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
83328320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
83338320bc6fSPatrick Sanan     always check the "set" output parameter.
83348320bc6fSPatrick Sanan 
8335bb7acecfSBarry Smith     A `DM` is always compatible with itself.
83368320bc6fSPatrick Sanan 
8337bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
83388320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
83398320bc6fSPatrick Sanan     incompatible.
83408320bc6fSPatrick Sanan 
83418320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8342bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
83438320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
83448320bc6fSPatrick Sanan 
8345bb7acecfSBarry Smith     Developer Note:
8346bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
83473d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8348a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8349bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
83508320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
83513d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8352bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
83538320bc6fSPatrick Sanan 
83548320bc6fSPatrick Sanan     Level: advanced
83558320bc6fSPatrick Sanan 
8356db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
83578320bc6fSPatrick Sanan @*/
83589371c9d4SSatish Balay PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set) {
83598320bc6fSPatrick Sanan   PetscMPIInt compareResult;
83608320bc6fSPatrick Sanan   DMType      type, type2;
83618320bc6fSPatrick Sanan   PetscBool   sameType;
83628320bc6fSPatrick Sanan 
83638320bc6fSPatrick Sanan   PetscFunctionBegin;
8364a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
83658320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
83668320bc6fSPatrick Sanan 
83678320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8368a5bc1bf3SBarry Smith   if (dm1 == dm2) {
83698320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
83708320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
83718320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83728320bc6fSPatrick Sanan   }
83738320bc6fSPatrick Sanan 
83748320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
83758320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
83768320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
83778320bc6fSPatrick Sanan      determined by the implementation-specific logic */
83789566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
83798320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
83808320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
83818320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
83828320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83838320bc6fSPatrick Sanan   }
83848320bc6fSPatrick Sanan 
83858320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8386a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8387dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
8388b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
83898320bc6fSPatrick Sanan   }
83908320bc6fSPatrick Sanan 
8391a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
83928320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
83939566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
83949566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
83959566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
83968320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8397dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
83988320bc6fSPatrick Sanan   } else {
83998320bc6fSPatrick Sanan     *set = PETSC_FALSE;
84008320bc6fSPatrick Sanan   }
84018320bc6fSPatrick Sanan   PetscFunctionReturn(0);
84028320bc6fSPatrick Sanan }
8403c0f0dcc3SMatthew G. Knepley 
8404c0f0dcc3SMatthew G. Knepley /*@C
8405bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8406c0f0dcc3SMatthew G. Knepley 
8407bb7acecfSBarry Smith   Logically Collective on dm
8408c0f0dcc3SMatthew G. Knepley 
8409c0f0dcc3SMatthew G. Knepley   Input Parameters:
8410bb7acecfSBarry Smith + DM - the `DM`
8411c0f0dcc3SMatthew G. Knepley . f - the monitor function
8412c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8413c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8414c0f0dcc3SMatthew G. Knepley 
8415c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8416bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8417c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8418c0f0dcc3SMatthew G. Knepley 
8419bb7acecfSBarry Smith   Note:
8420c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8421bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8422c0f0dcc3SMatthew G. Knepley   order in which they were set.
8423c0f0dcc3SMatthew G. Knepley 
8424bb7acecfSBarry Smith   Fortran Note:
8425bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8426bb7acecfSBarry Smith 
8427bb7acecfSBarry Smith   Developer Note:
8428bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8429c0f0dcc3SMatthew G. Knepley 
8430c0f0dcc3SMatthew G. Knepley   Level: intermediate
8431c0f0dcc3SMatthew G. Knepley 
8432bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`,`DMMonitorSetFromOptions()`, `DMMonitor()`
8433c0f0dcc3SMatthew G. Knepley @*/
84349371c9d4SSatish Balay PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **)) {
8435c0f0dcc3SMatthew G. Knepley   PetscInt m;
8436c0f0dcc3SMatthew G. Knepley 
8437c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8438c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8439c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8440c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8441c0f0dcc3SMatthew G. Knepley 
84429566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8443c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8444c0f0dcc3SMatthew G. Knepley   }
84457a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8446c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8447c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8448c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
8449c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8450c0f0dcc3SMatthew G. Knepley }
8451c0f0dcc3SMatthew G. Knepley 
8452c0f0dcc3SMatthew G. Knepley /*@
8453bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8454c0f0dcc3SMatthew G. Knepley 
8455bb7acecfSBarry Smith   Logically Collective on dm
8456c0f0dcc3SMatthew G. Knepley 
8457c0f0dcc3SMatthew G. Knepley   Input Parameter:
8458c0f0dcc3SMatthew G. Knepley . dm - the DM
8459c0f0dcc3SMatthew G. Knepley 
8460c0f0dcc3SMatthew G. Knepley   Options Database Key:
8461c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8462bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8463c0f0dcc3SMatthew G. Knepley   set via the options database
8464c0f0dcc3SMatthew G. Knepley 
8465bb7acecfSBarry Smith   Note:
8466bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8467c0f0dcc3SMatthew G. Knepley 
8468c0f0dcc3SMatthew G. Knepley   Level: intermediate
8469c0f0dcc3SMatthew G. Knepley 
8470bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`,  `DMMonitor()`
8471c0f0dcc3SMatthew G. Knepley @*/
84729371c9d4SSatish Balay PetscErrorCode DMMonitorCancel(DM dm) {
8473c0f0dcc3SMatthew G. Knepley   PetscInt m;
8474c0f0dcc3SMatthew G. Knepley 
8475c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8476c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8477c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
84789566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8479c0f0dcc3SMatthew G. Knepley   }
8480c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8481c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8482c0f0dcc3SMatthew G. Knepley }
8483c0f0dcc3SMatthew G. Knepley 
8484c0f0dcc3SMatthew G. Knepley /*@C
8485c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8486c0f0dcc3SMatthew G. Knepley 
8487bb7acecfSBarry Smith   Collective on dm
8488c0f0dcc3SMatthew G. Knepley 
8489c0f0dcc3SMatthew G. Knepley   Input Parameters:
8490bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8491c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8492c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8493c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8494c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8495bb7acecfSBarry 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
8496c0f0dcc3SMatthew G. Knepley 
8497c0f0dcc3SMatthew G. Knepley   Output Parameter:
8498c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8499c0f0dcc3SMatthew G. Knepley 
8500c0f0dcc3SMatthew G. Knepley   Level: developer
8501c0f0dcc3SMatthew G. Knepley 
8502db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8503db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8504db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8505db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8506c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8507db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8508bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8509c0f0dcc3SMatthew G. Knepley @*/
85109371c9d4SSatish Balay PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg) {
8511c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8512c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8513c0f0dcc3SMatthew G. Knepley 
8514c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8515c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8517c0f0dcc3SMatthew G. Knepley   if (*flg) {
8518c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8519c0f0dcc3SMatthew G. Knepley 
85209566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
85219566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
85229566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
85239566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8524c0f0dcc3SMatthew G. Knepley   }
8525c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8526c0f0dcc3SMatthew G. Knepley }
8527c0f0dcc3SMatthew G. Knepley 
8528c0f0dcc3SMatthew G. Knepley /*@
8529c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8530c0f0dcc3SMatthew G. Knepley 
8531bb7acecfSBarry Smith    Collective on dm
8532c0f0dcc3SMatthew G. Knepley 
8533c0f0dcc3SMatthew G. Knepley    Input Parameters:
8534bb7acecfSBarry Smith .  dm - The `DM`
8535c0f0dcc3SMatthew G. Knepley 
8536c0f0dcc3SMatthew G. Knepley    Level: developer
8537c0f0dcc3SMatthew G. Knepley 
8538bb7acecfSBarry Smith    Question:
8539bb7acecfSBarry 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
8540bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8541bb7acecfSBarry Smith 
8542bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8543c0f0dcc3SMatthew G. Knepley @*/
85449371c9d4SSatish Balay PetscErrorCode DMMonitor(DM dm) {
8545c0f0dcc3SMatthew G. Knepley   PetscInt m;
8546c0f0dcc3SMatthew G. Knepley 
8547c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8548c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8549c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8550*48a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8551c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8552c0f0dcc3SMatthew G. Knepley }
85532e4af2aeSMatthew G. Knepley 
85542e4af2aeSMatthew G. Knepley /*@
8555bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
85562e4af2aeSMatthew G. Knepley 
8557bb7acecfSBarry Smith   Collective on dm
85582e4af2aeSMatthew G. Knepley 
85592e4af2aeSMatthew G. Knepley   Input Parameters:
8560bb7acecfSBarry Smith + dm     - The `DM`
85616b867d5aSJose E. Roman - sol    - The solution vector
85622e4af2aeSMatthew G. Knepley 
85636b867d5aSJose E. Roman   Input/Output Parameter:
85646b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
85656b867d5aSJose E. Roman            contains the error in each field
85666b867d5aSJose E. Roman 
85676b867d5aSJose E. Roman   Output Parameter:
85686b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
85692e4af2aeSMatthew G. Knepley 
8570bb7acecfSBarry Smith   Note:
8571bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
85722e4af2aeSMatthew G. Knepley 
85732e4af2aeSMatthew G. Knepley   Level: developer
85742e4af2aeSMatthew G. Knepley 
8575db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
85762e4af2aeSMatthew G. Knepley @*/
85779371c9d4SSatish Balay PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec) {
85782e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
85792e4af2aeSMatthew G. Knepley   void    **ctxs;
85802e4af2aeSMatthew G. Knepley   PetscReal time;
85812e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
85822e4af2aeSMatthew G. Knepley 
85832e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
85849566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
85859566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
85869566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
85872e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
85882e4af2aeSMatthew G. Knepley     PetscDS         ds;
85892e4af2aeSMatthew G. Knepley     DMLabel         label;
85902e4af2aeSMatthew G. Knepley     IS              fieldIS;
85912e4af2aeSMatthew G. Knepley     const PetscInt *fields;
85922e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
85932e4af2aeSMatthew G. Knepley 
85949566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
85959566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
85969566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
85972e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
85982e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
85999566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
86002e4af2aeSMatthew G. Knepley     }
86019566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
86022e4af2aeSMatthew G. Knepley   }
86039371c9d4SSatish Balay   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); }
86049566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
86059566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
86062e4af2aeSMatthew G. Knepley   if (errorVec) {
86072e4af2aeSMatthew G. Knepley     DM             edm;
86082e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
86092e4af2aeSMatthew G. Knepley     PetscBool      simplex;
86102e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
86112e4af2aeSMatthew G. Knepley 
86129566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
86139566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
86149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
86159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
86162e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
86179566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
86182e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
86192e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
86202e4af2aeSMatthew G. Knepley       PetscQuadrature q;
86212e4af2aeSMatthew G. Knepley       const char     *name;
86222e4af2aeSMatthew G. Knepley 
86239566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
86249566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
86259566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
86269566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
86279566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
86289566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
86299566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
86309566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
86312e4af2aeSMatthew G. Knepley     }
86329566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
86332e4af2aeSMatthew G. Knepley 
86349566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
86359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
86369566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
86379566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
86382e4af2aeSMatthew G. Knepley   }
86399566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
86402e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
86412e4af2aeSMatthew G. Knepley }
86429a2a23afSMatthew G. Knepley 
86439a2a23afSMatthew G. Knepley /*@
8644bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
86459a2a23afSMatthew G. Knepley 
86469a2a23afSMatthew G. Knepley   Not collective
86479a2a23afSMatthew G. Knepley 
86489a2a23afSMatthew G. Knepley   Input Parameter:
8649bb7acecfSBarry Smith . dm     - The `DM`
86509a2a23afSMatthew G. Knepley 
86519a2a23afSMatthew G. Knepley   Output Parameter:
8652a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
86539a2a23afSMatthew G. Knepley 
86549a2a23afSMatthew G. Knepley   Level: advanced
86559a2a23afSMatthew G. Knepley 
8656bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
86579a2a23afSMatthew G. Knepley @*/
86589371c9d4SSatish Balay PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux) {
86599a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86609a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86619566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
86629a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86639a2a23afSMatthew G. Knepley }
86649a2a23afSMatthew G. Knepley 
86659a2a23afSMatthew G. Knepley /*@
8666ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
86679a2a23afSMatthew G. Knepley 
86689a2a23afSMatthew G. Knepley   Not collective
86699a2a23afSMatthew G. Knepley 
86709a2a23afSMatthew G. Knepley   Input Parameters:
8671bb7acecfSBarry Smith + dm     - The `DM`
8672bb7acecfSBarry Smith . label  - The `DMLabel`
8673ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8674ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
86759a2a23afSMatthew G. Knepley 
86769a2a23afSMatthew G. Knepley   Output Parameter:
8677bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
86789a2a23afSMatthew G. Knepley 
8679bb7acecfSBarry Smith   Note:
8680bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
868104c51a94SMatthew G. Knepley 
86829a2a23afSMatthew G. Knepley   Level: advanced
86839a2a23afSMatthew G. Knepley 
8684bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
86859a2a23afSMatthew G. Knepley @*/
86869371c9d4SSatish Balay PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux) {
8687ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
868804c51a94SMatthew G. Knepley   PetscBool       has;
86899a2a23afSMatthew G. Knepley 
86909a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86919a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86929a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
86939a2a23afSMatthew G. Knepley   key.label = label;
86949a2a23afSMatthew G. Knepley   key.value = value;
8695ac17215fSMatthew G. Knepley   key.part  = part;
86969566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
86979566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
86989566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
86999a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
87009a2a23afSMatthew G. Knepley }
87019a2a23afSMatthew G. Knepley 
87029a2a23afSMatthew G. Knepley /*@
8703bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
87049a2a23afSMatthew G. Knepley 
8705bb7acecfSBarry Smith   Not collective because auxilary vectors are not parallel
87069a2a23afSMatthew G. Knepley 
87079a2a23afSMatthew G. Knepley   Input Parameters:
8708bb7acecfSBarry Smith + dm     - The `DM`
8709bb7acecfSBarry Smith . label  - The `DMLabel`
87109a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8711ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
8712bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
87139a2a23afSMatthew G. Knepley 
87149a2a23afSMatthew G. Knepley   Level: advanced
87159a2a23afSMatthew G. Knepley 
8716bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
87179a2a23afSMatthew G. Knepley @*/
87189371c9d4SSatish Balay PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux) {
87199a2a23afSMatthew G. Knepley   Vec             old;
87209a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
87219a2a23afSMatthew G. Knepley 
87229a2a23afSMatthew G. Knepley   PetscFunctionBegin;
87239a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87249a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
87259a2a23afSMatthew G. Knepley   key.label = label;
87269a2a23afSMatthew G. Knepley   key.value = value;
8727ac17215fSMatthew G. Knepley   key.part  = part;
87289566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
87299566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
87309566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
87319566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
87329566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
87339a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
87349a2a23afSMatthew G. Knepley }
87359a2a23afSMatthew G. Knepley 
87369a2a23afSMatthew G. Knepley /*@C
8737bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
87389a2a23afSMatthew G. Knepley 
87399a2a23afSMatthew G. Knepley   Not collective
87409a2a23afSMatthew G. Knepley 
87419a2a23afSMatthew G. Knepley   Input Parameter:
8742bb7acecfSBarry Smith . dm      - The `DM`
87439a2a23afSMatthew G. Knepley 
87449a2a23afSMatthew G. Knepley   Output Parameters:
8745bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
8746bb7acecfSBarry Smith . values  - The label values for each `Vec`
8747bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
87489a2a23afSMatthew G. Knepley 
8749bb7acecfSBarry Smith   Note:
8750bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
87519a2a23afSMatthew G. Knepley 
87529a2a23afSMatthew G. Knepley   Level: advanced
87539a2a23afSMatthew G. Knepley 
8754bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
87559a2a23afSMatthew G. Knepley @*/
87569371c9d4SSatish Balay PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[]) {
87579a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
87589a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
87599a2a23afSMatthew G. Knepley 
87609a2a23afSMatthew G. Knepley   PetscFunctionBegin;
87619a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87629a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
8763dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
8764dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
87659566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
87669566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
87679566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
87689371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
87699371c9d4SSatish Balay     labels[i] = keys[i].label;
87709371c9d4SSatish Balay     values[i] = keys[i].value;
87719371c9d4SSatish Balay     parts[i]  = keys[i].part;
87729371c9d4SSatish Balay   }
87739566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
87749a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
87759a2a23afSMatthew G. Knepley }
87769a2a23afSMatthew G. Knepley 
87779a2a23afSMatthew G. Knepley /*@
8778bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
87799a2a23afSMatthew G. Knepley 
87809a2a23afSMatthew G. Knepley   Not collective
87819a2a23afSMatthew G. Knepley 
87829a2a23afSMatthew G. Knepley   Input Parameter:
8783bb7acecfSBarry Smith . dm    - The `DM`
87849a2a23afSMatthew G. Knepley 
87859a2a23afSMatthew G. Knepley   Output Parameter:
8786bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
87879a2a23afSMatthew G. Knepley 
87889a2a23afSMatthew G. Knepley   Level: advanced
87899a2a23afSMatthew G. Knepley 
8790bb7acecfSBarry Smith   Note:
8791bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
8792bb7acecfSBarry Smith 
8793db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
87949a2a23afSMatthew G. Knepley @*/
87959371c9d4SSatish Balay PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew) {
87969a2a23afSMatthew G. Knepley   PetscFunctionBegin;
87979a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87989566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
87999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
88009a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
88019a2a23afSMatthew G. Knepley }
8802b5a892a1SMatthew G. Knepley 
8803b5a892a1SMatthew G. Knepley /*@C
8804bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
8805b5a892a1SMatthew G. Knepley 
8806b5a892a1SMatthew G. Knepley   Not collective
8807b5a892a1SMatthew G. Knepley 
8808b5a892a1SMatthew G. Knepley   Input Parameters:
8809bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8810b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8811b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8812b5a892a1SMatthew G. Knepley 
8813b5a892a1SMatthew G. Knepley   Output Parameters:
8814bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8815b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8816b5a892a1SMatthew G. Knepley 
8817b5a892a1SMatthew G. Knepley   Level: advanced
8818b5a892a1SMatthew G. Knepley 
8819bb7acecfSBarry Smith   Note:
8820bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
8821bb7acecfSBarry Smith 
8822bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
8823bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
8824bb7acecfSBarry Smith 
8825bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
8826bb7acecfSBarry Smith 
8827bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
8828b5a892a1SMatthew G. Knepley @*/
88299371c9d4SSatish Balay PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found) {
8830b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
8831b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
8832b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8833b5a892a1SMatthew G. Knepley 
8834b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
88359371c9d4SSatish Balay   if (!nO) {
88369371c9d4SSatish Balay     *ornt  = 0;
88379371c9d4SSatish Balay     *found = PETSC_TRUE;
88389371c9d4SSatish Balay     PetscFunctionReturn(0);
88399371c9d4SSatish Balay   }
8840b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8841b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
8842b5a892a1SMatthew G. Knepley 
88439371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
88449371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
88459371c9d4SSatish Balay     if (c == cS) {
88469371c9d4SSatish Balay       *ornt = o;
88479371c9d4SSatish Balay       break;
88489371c9d4SSatish Balay     }
8849b5a892a1SMatthew G. Knepley   }
8850b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8851b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8852b5a892a1SMatthew G. Knepley }
8853b5a892a1SMatthew G. Knepley 
8854b5a892a1SMatthew G. Knepley /*@C
8855bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
8856b5a892a1SMatthew G. Knepley 
8857b5a892a1SMatthew G. Knepley   Not collective
8858b5a892a1SMatthew G. Knepley 
8859b5a892a1SMatthew G. Knepley   Input Parameters:
8860bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8861b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8862b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8863b5a892a1SMatthew G. Knepley 
8864b5a892a1SMatthew G. Knepley   Output Parameters:
8865bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8866b5a892a1SMatthew G. Knepley 
8867b5a892a1SMatthew G. Knepley   Level: advanced
8868b5a892a1SMatthew G. Knepley 
8869bb7acecfSBarry Smith   Note:
8870bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
8871bb7acecfSBarry Smith 
8872bb7acecfSBarry Smith   Developer Note:
8873bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
8874bb7acecfSBarry Smith 
8875bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
8876b5a892a1SMatthew G. Knepley @*/
88779371c9d4SSatish Balay PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt) {
8878b5a892a1SMatthew G. Knepley   PetscBool found;
8879b5a892a1SMatthew G. Knepley 
8880b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
88819566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
88827a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8883b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8884b5a892a1SMatthew G. Knepley }
8885b5a892a1SMatthew G. Knepley 
8886b5a892a1SMatthew G. Knepley /*@C
8887bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
8888b5a892a1SMatthew G. Knepley 
8889b5a892a1SMatthew G. Knepley   Not collective
8890b5a892a1SMatthew G. Knepley 
8891b5a892a1SMatthew G. Knepley   Input Parameters:
8892bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8893b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
8894b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
8895b5a892a1SMatthew G. Knepley 
8896b5a892a1SMatthew G. Knepley   Output Parameters:
8897bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8898b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8899b5a892a1SMatthew G. Knepley 
8900b5a892a1SMatthew G. Knepley   Level: advanced
8901b5a892a1SMatthew G. Knepley 
8902bb7acecfSBarry Smith   Note:
8903bb7acecfSBarry Smith   An arrangement is a vertex order
8904bb7acecfSBarry Smith 
8905bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
8906bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
8907bb7acecfSBarry Smith 
8908bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
8909bb7acecfSBarry Smith 
8910bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
8911b5a892a1SMatthew G. Knepley @*/
89129371c9d4SSatish Balay PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found) {
8913b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
8914b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
8915b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8916b5a892a1SMatthew G. Knepley 
8917b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
89189371c9d4SSatish Balay   if (!nO) {
89199371c9d4SSatish Balay     *ornt  = 0;
89209371c9d4SSatish Balay     *found = PETSC_TRUE;
89219371c9d4SSatish Balay     PetscFunctionReturn(0);
89229371c9d4SSatish Balay   }
8923b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8924b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
8925b5a892a1SMatthew G. Knepley 
89269371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
89279371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
89289371c9d4SSatish Balay     if (c == cS) {
89299371c9d4SSatish Balay       *ornt = o;
89309371c9d4SSatish Balay       break;
89319371c9d4SSatish Balay     }
8932b5a892a1SMatthew G. Knepley   }
8933b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8934b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8935b5a892a1SMatthew G. Knepley }
8936b5a892a1SMatthew G. Knepley 
8937b5a892a1SMatthew G. Knepley /*@C
8938bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
8939b5a892a1SMatthew G. Knepley 
8940b5a892a1SMatthew G. Knepley   Not collective
8941b5a892a1SMatthew G. Knepley 
8942b5a892a1SMatthew G. Knepley   Input Parameters:
8943bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8944b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
8945b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
8946b5a892a1SMatthew G. Knepley 
8947b5a892a1SMatthew G. Knepley   Output Parameters:
8948bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8949b5a892a1SMatthew G. Knepley 
8950b5a892a1SMatthew G. Knepley   Level: advanced
8951b5a892a1SMatthew G. Knepley 
8952bb7acecfSBarry Smith   Note:
8953bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
8954bb7acecfSBarry Smith 
8955bb7acecfSBarry Smith   Developer Note:
8956bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
8957bb7acecfSBarry Smith 
8958bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
8959b5a892a1SMatthew G. Knepley @*/
89609371c9d4SSatish Balay PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt) {
8961b5a892a1SMatthew G. Knepley   PetscBool found;
8962b5a892a1SMatthew G. Knepley 
8963b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
89649566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
89657a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8966b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8967b5a892a1SMatthew G. Knepley }
8968012bc364SMatthew G. Knepley 
8969012bc364SMatthew G. Knepley /*@C
8970012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
8971012bc364SMatthew G. Knepley 
8972012bc364SMatthew G. Knepley   Not collective
8973012bc364SMatthew G. Knepley 
8974012bc364SMatthew G. Knepley   Input Parameters:
8975bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
8976012bc364SMatthew G. Knepley - point - Coordinates of the point
8977012bc364SMatthew G. Knepley 
8978012bc364SMatthew G. Knepley   Output Parameters:
8979012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
8980012bc364SMatthew G. Knepley 
8981012bc364SMatthew G. Knepley   Level: advanced
8982012bc364SMatthew G. Knepley 
8983bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
8984012bc364SMatthew G. Knepley @*/
89859371c9d4SSatish Balay PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside) {
8986012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
8987012bc364SMatthew G. Knepley   PetscInt  d;
8988012bc364SMatthew G. Knepley 
8989012bc364SMatthew G. Knepley   PetscFunctionBegin;
8990012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
8991012bc364SMatthew G. Knepley   switch (ct) {
8992012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
8993012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
8994012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
89959371c9d4SSatish Balay       if (point[d] < -1.0) {
89969371c9d4SSatish Balay         *inside = PETSC_FALSE;
89979371c9d4SSatish Balay         break;
89989371c9d4SSatish Balay       }
8999012bc364SMatthew G. Knepley       sum += point[d];
9000012bc364SMatthew G. Knepley     }
90019371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
90029371c9d4SSatish Balay       *inside = PETSC_FALSE;
90039371c9d4SSatish Balay       break;
90049371c9d4SSatish Balay     }
9005012bc364SMatthew G. Knepley     break;
9006012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9007012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9008012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
90099371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
90109371c9d4SSatish Balay         *inside = PETSC_FALSE;
9011012bc364SMatthew G. Knepley         break;
90129371c9d4SSatish Balay       }
90139371c9d4SSatish Balay     break;
90149371c9d4SSatish Balay   default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9015012bc364SMatthew G. Knepley   }
9016012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
9017012bc364SMatthew G. Knepley }
9018