xref: /petsc/src/dm/interface/dm.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
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 
71648a46eb9SPierre 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) {
73048a46eb9SPierre 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   }
74448a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7456eb26441SStefano Zampini 
7469566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
74748a46eb9SPierre 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 
76348a46eb9SPierre 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);
94248a46eb9SPierre 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;
1090ad540459SPierre Jolivet         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));
1120dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
112137d0c07bSMatthew G Knepley   }
11221411c6eeSJed Brown   *ltog = dm->ltogmap;
11231411c6eeSJed Brown   PetscFunctionReturn(0);
11241411c6eeSJed Brown }
11251411c6eeSJed Brown 
11261411c6eeSJed Brown /*@
1127bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11281411c6eeSJed Brown 
11291411c6eeSJed Brown    Not Collective
11301411c6eeSJed Brown 
11311411c6eeSJed Brown    Input Parameter:
1132bb7acecfSBarry Smith .  dm - the `DM` with block structure
11331411c6eeSJed Brown 
11341411c6eeSJed Brown    Output Parameter:
11351411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11361411c6eeSJed Brown 
11371411c6eeSJed Brown    Level: intermediate
11381411c6eeSJed Brown 
1139bb7acecfSBarry Smith    Note:
1140bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1141bb7acecfSBarry Smith 
1142bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1143bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1144bb7acecfSBarry Smith 
1145db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11461411c6eeSJed Brown @*/
11479371c9d4SSatish Balay PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs) {
11481411c6eeSJed Brown   PetscFunctionBegin;
11491411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1150534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11517a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11521411c6eeSJed Brown   *bs = dm->bs;
11531411c6eeSJed Brown   PetscFunctionReturn(0);
11541411c6eeSJed Brown }
11551411c6eeSJed Brown 
115648eeb7c8SBarry Smith /*@C
1157bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1158bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
115947c6ae99SBarry Smith 
1160a5bc1bf3SBarry Smith     Collective on dmc
116147c6ae99SBarry Smith 
1162d8d19677SJose E. Roman     Input Parameters:
1163bb7acecfSBarry Smith +   dmc - the `DM` object
1164bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
116547c6ae99SBarry Smith 
1166d8d19677SJose E. Roman     Output Parameters:
116747c6ae99SBarry Smith +  mat - the interpolation
1168bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
116947c6ae99SBarry Smith 
117047c6ae99SBarry Smith     Level: developer
117147c6ae99SBarry Smith 
117295452b02SPatrick Sanan     Notes:
1173bb7acecfSBarry 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
1174bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1175d52bd9f3SBarry Smith 
1176bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1177bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
117885afcc9aSBarry Smith 
1179bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
118047c6ae99SBarry Smith 
118147c6ae99SBarry Smith @*/
11829371c9d4SSatish Balay PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec) {
118347c6ae99SBarry Smith   PetscFunctionBegin;
1184a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1185a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1186c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
11879566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1188dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
11899566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
119047c6ae99SBarry Smith   PetscFunctionReturn(0);
119147c6ae99SBarry Smith }
119247c6ae99SBarry Smith 
11933ad4599aSBarry Smith /*@
1194bb7acecfSBarry 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`.
1195bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1196bb7acecfSBarry Smith     representation of xfine.
11972ed6491fSPatrick Sanan 
11982ed6491fSPatrick Sanan   Input Parameters:
1199bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1200bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12012ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12022ed6491fSPatrick Sanan 
12032ed6491fSPatrick Sanan   Output Parameter:
12042ed6491fSPatrick Sanan .    scale - the scaled vector
12052ed6491fSPatrick Sanan 
1206bb7acecfSBarry Smith   Level: advanced
12072ed6491fSPatrick Sanan 
1208e9c74fd6SRichard Tran Mills   Developer Notes:
1209bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1210e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1211e9c74fd6SRichard Tran Mills 
1212bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12132ed6491fSPatrick Sanan 
12142ed6491fSPatrick Sanan @*/
12159371c9d4SSatish Balay PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale) {
12162ed6491fSPatrick Sanan   Vec         fine;
12172ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12189704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1219e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12209704db99SRichard Tran Mills #endif
12212ed6491fSPatrick Sanan 
12222ed6491fSPatrick Sanan   PetscFunctionBegin;
12239566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12249566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12259566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12269704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12279704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12289704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12299704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12309566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1231e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12329566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12339566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12349566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
123583aa49f4SRichard Tran Mills   }
12369704db99SRichard Tran Mills #endif
12379566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12389566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12399566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12402ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12412ed6491fSPatrick Sanan }
12422ed6491fSPatrick Sanan 
12432ed6491fSPatrick Sanan /*@
1244bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1245bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12463ad4599aSBarry Smith 
1247a5bc1bf3SBarry Smith     Collective on dmc
12483ad4599aSBarry Smith 
1249d8d19677SJose E. Roman     Input Parameters:
1250bb7acecfSBarry Smith +   dmc - the `DM` object
1251bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12523ad4599aSBarry Smith 
12533ad4599aSBarry Smith     Output Parameter:
12543ad4599aSBarry Smith .  mat - the restriction
12553ad4599aSBarry Smith 
12563ad4599aSBarry Smith     Level: developer
12573ad4599aSBarry Smith 
1258bb7acecfSBarry Smith     Note:
1259bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1260bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12613ad4599aSBarry Smith 
1262bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12633ad4599aSBarry Smith 
12643ad4599aSBarry Smith @*/
12659371c9d4SSatish Balay PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat) {
12663ad4599aSBarry Smith   PetscFunctionBegin;
1267a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1268a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12695a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
12709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1271dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
12729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
12733ad4599aSBarry Smith   PetscFunctionReturn(0);
12743ad4599aSBarry Smith }
12753ad4599aSBarry Smith 
127647c6ae99SBarry Smith /*@
1277bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1278bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1279bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1280bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
128147c6ae99SBarry Smith 
1282a5bc1bf3SBarry Smith     Collective on dac
128347c6ae99SBarry Smith 
1284d8d19677SJose E. Roman     Input Parameters:
1285bb7acecfSBarry Smith +   dac - the `DM` object
1286bb7acecfSBarry Smith -   daf - the second, finer `DM` object
128747c6ae99SBarry Smith 
128847c6ae99SBarry Smith     Output Parameter:
12896dbf9973SLawrence Mitchell .   mat - the injection
129047c6ae99SBarry Smith 
129147c6ae99SBarry Smith     Level: developer
129247c6ae99SBarry Smith 
1293bb7acecfSBarry Smith    Note:
1294bb7acecfSBarry 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
1295bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
129685afcc9aSBarry Smith 
1297bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1298bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
129947c6ae99SBarry Smith 
130047c6ae99SBarry Smith @*/
13019371c9d4SSatish Balay PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat) {
130247c6ae99SBarry Smith   PetscFunctionBegin;
1303a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1304a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13055a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1307dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
130947c6ae99SBarry Smith   PetscFunctionReturn(0);
131047c6ae99SBarry Smith }
131147c6ae99SBarry Smith 
1312b412c318SBarry Smith /*@
1313bb7acecfSBarry 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
1314bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1315bd041c0cSMatthew G. Knepley 
1316a5bc1bf3SBarry Smith   Collective on dac
1317bd041c0cSMatthew G. Knepley 
1318d8d19677SJose E. Roman   Input Parameters:
1319bb7acecfSBarry Smith + dmc - the target `DM` object
1320bb7acecfSBarry Smith - dmf - the source `DM` object
1321bd041c0cSMatthew G. Knepley 
1322bd041c0cSMatthew G. Knepley   Output Parameter:
1323b4937a87SMatthew G. Knepley . mat - the mass matrix
1324bd041c0cSMatthew G. Knepley 
1325bd041c0cSMatthew G. Knepley   Level: developer
1326bd041c0cSMatthew G. Knepley 
1327bb7acecfSBarry Smith   Notes:
1328bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1329bb7acecfSBarry Smith 
1330bb7acecfSBarry 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()`
1331bb7acecfSBarry Smith 
1332bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1333bd041c0cSMatthew G. Knepley @*/
13349371c9d4SSatish Balay PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat) {
1335bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1336b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1337b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13385a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13395b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1340dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13415b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
1342b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1343b4937a87SMatthew G. Knepley }
1344b4937a87SMatthew G. Knepley 
1345b4937a87SMatthew G. Knepley /*@
1346bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1347b4937a87SMatthew G. Knepley 
1348b4937a87SMatthew G. Knepley   Collective on dm
1349b4937a87SMatthew G. Knepley 
1350b4937a87SMatthew G. Knepley   Input Parameter:
1351bb7acecfSBarry Smith . dm - the `DM` object
1352b4937a87SMatthew G. Knepley 
1353b4937a87SMatthew G. Knepley   Output Parameter:
1354bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1355b4937a87SMatthew G. Knepley 
1356b4937a87SMatthew G. Knepley   Level: developer
1357b4937a87SMatthew G. Knepley 
1358bb7acecfSBarry Smith   Note:
1359bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1360bb7acecfSBarry Smith 
1361bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1362b4937a87SMatthew G. Knepley @*/
13639371c9d4SSatish Balay PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm) {
1364b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1365b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1366b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1367dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
1368bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1369bd041c0cSMatthew G. Knepley }
1370bd041c0cSMatthew G. Knepley 
1371bd041c0cSMatthew G. Knepley /*@
1372bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1373bb7acecfSBarry Smith     of a PDE on the `DM`.
137447c6ae99SBarry Smith 
1375d083f849SBarry Smith     Collective on dm
137647c6ae99SBarry Smith 
1377d8d19677SJose E. Roman     Input Parameters:
1378bb7acecfSBarry Smith +   dm - the `DM` object
1379bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
138047c6ae99SBarry Smith 
138147c6ae99SBarry Smith     Output Parameter:
138247c6ae99SBarry Smith .   coloring - the coloring
138347c6ae99SBarry Smith 
1384ec5066bdSBarry Smith     Notes:
1385bb7acecfSBarry 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
1386bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1387ec5066bdSBarry Smith 
1388bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
1389ec5066bdSBarry Smith 
139047c6ae99SBarry Smith     Level: developer
139147c6ae99SBarry Smith 
1392bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
139347c6ae99SBarry Smith 
1394aab9d709SJed Brown @*/
13959371c9d4SSatish Balay PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring) {
139647c6ae99SBarry Smith   PetscFunctionBegin;
1397171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13985a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1399dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
140047c6ae99SBarry Smith   PetscFunctionReturn(0);
140147c6ae99SBarry Smith }
140247c6ae99SBarry Smith 
1403b412c318SBarry Smith /*@
1404bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
140547c6ae99SBarry Smith 
1406d083f849SBarry Smith     Collective on dm
140747c6ae99SBarry Smith 
140847c6ae99SBarry Smith     Input Parameter:
1409bb7acecfSBarry Smith .   dm - the `DM` object
141047c6ae99SBarry Smith 
141147c6ae99SBarry Smith     Output Parameter:
141247c6ae99SBarry Smith .   mat - the empty Jacobian
141347c6ae99SBarry Smith 
1414073dac72SJed Brown     Level: beginner
141547c6ae99SBarry Smith 
1416f27dd7c6SMatthew G. Knepley     Options Database Keys:
1417bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1418f27dd7c6SMatthew G. Knepley 
141995452b02SPatrick Sanan     Notes:
142095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
142194013140SBarry Smith     do not need to do it yourself.
142294013140SBarry Smith 
142394013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1424bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
142594013140SBarry Smith 
1426bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
142794013140SBarry Smith     internally by PETSc.
142894013140SBarry Smith 
1429bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1430bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
143194013140SBarry Smith 
1432bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
143347c6ae99SBarry Smith 
1434aab9d709SJed Brown @*/
14359371c9d4SSatish Balay PetscErrorCode DMCreateMatrix(DM dm, Mat *mat) {
143647c6ae99SBarry Smith   PetscFunctionBegin;
1437171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1438064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14399566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14409566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1441dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
144276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1443c6b011d8SStefano Zampini     DM mdm;
1444c6b011d8SStefano Zampini 
14459566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14467a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1447c6b011d8SStefano Zampini   }
1448e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1449e5e52638SMatthew G. Knepley   if (dm->Nf) {
1450e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1451649ef022SMatthew Knepley     PetscInt     Nf, f;
1452e571a35bSMatthew G. Knepley 
14539566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1454649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1455649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14569566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14579566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14589566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1459649ef022SMatthew Knepley         break;
1460e571a35bSMatthew G. Knepley       }
1461649ef022SMatthew Knepley     }
1462649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1463649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14649566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14659566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
14669566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1467e571a35bSMatthew G. Knepley       }
1468e571a35bSMatthew G. Knepley     }
1469e571a35bSMatthew G. Knepley   }
14709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
147147c6ae99SBarry Smith   PetscFunctionReturn(0);
147247c6ae99SBarry Smith }
147347c6ae99SBarry Smith 
1474732e2eb9SMatthew G Knepley /*@
1475bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1476bb7acecfSBarry 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
1477bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1478aa0f6e3cSJed Brown 
1479aa0f6e3cSJed Brown   Logically Collective on dm
1480aa0f6e3cSJed Brown 
1481aa0f6e3cSJed Brown   Input Parameters:
1482bb7acecfSBarry Smith + dm - the `DM`
1483bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1484aa0f6e3cSJed Brown 
1485aa0f6e3cSJed Brown   Level: developer
1486aa0f6e3cSJed Brown 
1487bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1488aa0f6e3cSJed Brown @*/
14899371c9d4SSatish Balay PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip) {
1490aa0f6e3cSJed Brown   PetscFunctionBegin;
1491aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1492aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1493aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1494aa0f6e3cSJed Brown }
1495aa0f6e3cSJed Brown 
1496aa0f6e3cSJed Brown /*@
1497bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1498732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1499732e2eb9SMatthew G Knepley 
1500d083f849SBarry Smith   Logically Collective on dm
1501732e2eb9SMatthew G Knepley 
1502d8d19677SJose E. Roman   Input Parameters:
1503bb7acecfSBarry Smith + dm - the `DM`
1504bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1505732e2eb9SMatthew G Knepley 
1506732e2eb9SMatthew G Knepley   Level: developer
1507f27dd7c6SMatthew G. Knepley 
1508f27dd7c6SMatthew G. Knepley   Options Database Keys:
1509bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1510f27dd7c6SMatthew G. Knepley 
1511bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1512732e2eb9SMatthew G Knepley @*/
15139371c9d4SSatish Balay PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only) {
1514732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1515732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1516732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1517732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1518732e2eb9SMatthew G Knepley }
1519732e2eb9SMatthew G Knepley 
1520b06ff27eSHong Zhang /*@
1521bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1522bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1523b06ff27eSHong Zhang 
1524d083f849SBarry Smith   Logically Collective on dm
1525b06ff27eSHong Zhang 
1526d8d19677SJose E. Roman   Input Parameters:
1527bb7acecfSBarry Smith + dm - the `DM`
1528bb7acecfSBarry Smith - only - `PETSC_TRUE` if you only want matrix stucture
1529b06ff27eSHong Zhang 
1530b06ff27eSHong Zhang   Level: developer
1531bb7acecfSBarry Smith 
1532bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1533b06ff27eSHong Zhang @*/
15349371c9d4SSatish Balay PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only) {
1535b06ff27eSHong Zhang   PetscFunctionBegin;
1536b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1537b06ff27eSHong Zhang   dm->structure_only = only;
1538b06ff27eSHong Zhang   PetscFunctionReturn(0);
1539b06ff27eSHong Zhang }
1540b06ff27eSHong Zhang 
1541a89ea682SMatthew G Knepley /*@C
1542bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1543a89ea682SMatthew G Knepley 
1544a89ea682SMatthew G Knepley   Not Collective
1545a89ea682SMatthew G Knepley 
1546a89ea682SMatthew G Knepley   Input Parameters:
1547bb7acecfSBarry Smith + dm - the `DM` object
1548a5b23f4aSJose E. Roman . count - The minimum size
1549bb7acecfSBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, or MPIU_INT)
1550a89ea682SMatthew G Knepley 
1551a89ea682SMatthew G Knepley   Output Parameter:
1552a89ea682SMatthew G Knepley . array - the work array
1553a89ea682SMatthew G Knepley 
1554a89ea682SMatthew G Knepley   Level: developer
1555a89ea682SMatthew G Knepley 
1556bb7acecfSBarry Smith   Note:
1557bb7acecfSBarry Smith   A `DM` may stash the array between instantations so using this routine may be more efficient than calling `PetscMalloc()`
1558bb7acecfSBarry Smith 
1559bb7acecfSBarry Smith   The array may contain nonzero values
1560bb7acecfSBarry Smith 
1561bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1562a89ea682SMatthew G Knepley @*/
15639371c9d4SSatish Balay PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem) {
1564aa1993deSMatthew G Knepley   DMWorkLink  link;
156569291d52SBarry Smith   PetscMPIInt dsize;
1566a89ea682SMatthew G Knepley 
1567a89ea682SMatthew G Knepley   PetscFunctionBegin;
1568a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1569aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1570aa1993deSMatthew G Knepley   if (dm->workin) {
1571aa1993deSMatthew G Knepley     link       = dm->workin;
1572aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1573aa1993deSMatthew G Knepley   } else {
1574*4dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1575a89ea682SMatthew G Knepley   }
15769566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype, &dsize));
15775056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
15789566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
15799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1580854ce69bSBarry Smith     link->bytes = dsize * count;
1581aa1993deSMatthew G Knepley   }
1582aa1993deSMatthew G Knepley   link->next  = dm->workout;
1583aa1993deSMatthew G Knepley   dm->workout = link;
1584cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
158500d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
158600d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
158700d952a4SJed Brown #endif
1588aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
1589a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1590a89ea682SMatthew G Knepley }
1591a89ea682SMatthew G Knepley 
1592aa1993deSMatthew G Knepley /*@C
1593bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1594aa1993deSMatthew G Knepley 
1595aa1993deSMatthew G Knepley   Not Collective
1596aa1993deSMatthew G Knepley 
1597aa1993deSMatthew G Knepley   Input Parameters:
1598bb7acecfSBarry Smith + dm - the `DM` object
1599a5b23f4aSJose E. Roman . count - The minimum size
160069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1601aa1993deSMatthew G Knepley 
1602aa1993deSMatthew G Knepley   Output Parameter:
1603aa1993deSMatthew G Knepley . array - the work array
1604aa1993deSMatthew G Knepley 
1605aa1993deSMatthew G Knepley   Level: developer
1606aa1993deSMatthew G Knepley 
160795452b02SPatrick Sanan   Developer Notes:
1608bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1609147403d9SBarry Smith 
1610bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1611aa1993deSMatthew G Knepley @*/
16129371c9d4SSatish Balay PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem) {
1613aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1614aa1993deSMatthew G Knepley 
1615aa1993deSMatthew G Knepley   PetscFunctionBegin;
1616aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1617aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1618aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1619aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1620aa1993deSMatthew G Knepley       *p            = link->next;
1621aa1993deSMatthew G Knepley       link->next    = dm->workin;
1622aa1993deSMatthew G Knepley       dm->workin    = link;
16230298fd71SBarry Smith       *(void **)mem = NULL;
1624aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1625aa1993deSMatthew G Knepley     }
1626aa1993deSMatthew G Knepley   }
1627aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1628aa1993deSMatthew G Knepley }
1629e7c4fc90SDmitry Karpeev 
16308cda7954SMatthew G. Knepley /*@C
1631bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1632bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
16338cda7954SMatthew G. Knepley 
1634bb7acecfSBarry Smith   Logically collective on dm
16358cda7954SMatthew G. Knepley 
16368cda7954SMatthew G. Knepley   Input Parameters:
1637bb7acecfSBarry Smith + dm     - The `DM`
16388cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16398cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16408cda7954SMatthew G. Knepley 
1641147403d9SBarry Smith   Calling sequence of nullsp:
1642147403d9SBarry Smith .vb
1643147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1644147403d9SBarry Smith .ve
1645bb7acecfSBarry Smith +  dm        - The present `DM`
1646bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1647147403d9SBarry Smith .  field     - The field number in dm
1648147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16498cda7954SMatthew G. Knepley 
165049762cbcSSatish Balay   Level: intermediate
165149762cbcSSatish Balay 
1652bb7acecfSBarry Smith   Fortran Notes:
1653bb7acecfSBarry Smith   This function is not available from Fortran.
1654bb7acecfSBarry Smith 
1655bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1656147403d9SBarry Smith @*/
16579371c9d4SSatish Balay PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
1658435a35e8SMatthew G Knepley   PetscFunctionBegin;
1659435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16607a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1661435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1662435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1663435a35e8SMatthew G Knepley }
1664435a35e8SMatthew G Knepley 
16658cda7954SMatthew G. Knepley /*@C
1666bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
16678cda7954SMatthew G. Knepley 
16688cda7954SMatthew G. Knepley   Not collective
16698cda7954SMatthew G. Knepley 
16708cda7954SMatthew G. Knepley   Input Parameters:
1671bb7acecfSBarry Smith + dm     - The `DM`
16728cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16738cda7954SMatthew G. Knepley 
16748cda7954SMatthew G. Knepley   Output Parameter:
16758cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16768cda7954SMatthew G. Knepley 
1677147403d9SBarry Smith   Calling sequence of nullsp:
1678147403d9SBarry Smith .vb
1679147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1680147403d9SBarry Smith .ve
1681147403d9SBarry Smith +  dm        - The present DM
1682147403d9SBarry Smith .  origField - The field number given above, in the original DM
1683147403d9SBarry Smith .  field     - The field number in dm
1684147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16858cda7954SMatthew G. Knepley 
1686bb7acecfSBarry Smith   Fortran Note:
1687bb7acecfSBarry Smith   This function is not available from Fortran.
16888cda7954SMatthew G. Knepley 
168949762cbcSSatish Balay    Level: intermediate
169049762cbcSSatish Balay 
1691bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1692147403d9SBarry Smith @*/
16939371c9d4SSatish Balay PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
16940a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16950a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1696f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16977a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
16980a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16990a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17000a50eb56SMatthew G. Knepley }
17010a50eb56SMatthew G. Knepley 
17028cda7954SMatthew G. Knepley /*@C
1703bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17048cda7954SMatthew G. Knepley 
1705bb7acecfSBarry Smith   Logically collective on dm
17068cda7954SMatthew G. Knepley 
17078cda7954SMatthew G. Knepley   Input Parameters:
1708bb7acecfSBarry Smith + dm     - The `DM`
17098cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17108cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17118cda7954SMatthew G. Knepley 
1712147403d9SBarry Smith   Calling sequence of nullsp:
1713147403d9SBarry Smith .vb
1714147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1715147403d9SBarry Smith .ve
1716bb7acecfSBarry Smith +  dm        - The present `DM`
1717bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1718147403d9SBarry Smith .  field     - The field number in dm
1719147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17208cda7954SMatthew G. Knepley 
1721bb7acecfSBarry Smith   Fortran Note:
1722bb7acecfSBarry Smith   This function is not available from Fortran.
17238cda7954SMatthew G. Knepley 
172449762cbcSSatish Balay    Level: intermediate
172549762cbcSSatish Balay 
1726bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1727bb7acecfSBarry Smith           `MatNullSpace`
1728147403d9SBarry Smith @*/
17299371c9d4SSatish Balay PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
1730f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1731f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17327a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1733f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1734f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1735f9d4088aSMatthew G. Knepley }
1736f9d4088aSMatthew G. Knepley 
17378cda7954SMatthew G. Knepley /*@C
1738bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17398cda7954SMatthew G. Knepley 
17408cda7954SMatthew G. Knepley   Not collective
17418cda7954SMatthew G. Knepley 
17428cda7954SMatthew G. Knepley   Input Parameters:
1743bb7acecfSBarry Smith + dm     - The `DM`
17448cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17458cda7954SMatthew G. Knepley 
17468cda7954SMatthew G. Knepley   Output Parameter:
17478cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17488cda7954SMatthew G. Knepley 
1749147403d9SBarry Smith   Calling sequence of nullsp:
1750147403d9SBarry Smith .vb
1751147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1752147403d9SBarry Smith .ve
1753bb7acecfSBarry Smith +  dm        - The present `DM`
1754bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1755147403d9SBarry Smith .  field     - The field number in dm
1756147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17578cda7954SMatthew G. Knepley 
1758bb7acecfSBarry Smith   Fortran Note:
1759bb7acecfSBarry Smith   This function is not available from Fortran.
17608cda7954SMatthew G. Knepley 
176149762cbcSSatish Balay    Level: intermediate
176249762cbcSSatish Balay 
1763bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1764bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1765147403d9SBarry Smith @*/
17669371c9d4SSatish Balay PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *)) {
1767f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1768f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1769f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17707a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1771f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1772f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1773f9d4088aSMatthew G. Knepley }
1774f9d4088aSMatthew G. Knepley 
17754f3b5142SJed Brown /*@C
1776bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
17774d343eeaSMatthew G Knepley 
17784d343eeaSMatthew G Knepley   Not collective
17794d343eeaSMatthew G Knepley 
17804d343eeaSMatthew G Knepley   Input Parameter:
1781bb7acecfSBarry Smith . dm - the `DM` object
17824d343eeaSMatthew G Knepley 
17834d343eeaSMatthew G Knepley   Output Parameters:
17840298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
1785bb7acecfSBarry Smith . fieldNames - The number of each field (or NULL if not requested)
17860298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17874d343eeaSMatthew G Knepley 
17884d343eeaSMatthew G Knepley   Level: intermediate
17894d343eeaSMatthew G Knepley 
1790bb7acecfSBarry Smith   Note:
179121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1792bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1793bb7acecfSBarry Smith   `PetscFree()`.
179421c9b008SJed Brown 
1795bb7acecfSBarry Smith   Fortran Note:
1796bb7acecfSBarry Smith   Not available in Fortran.
1797bb7acecfSBarry Smith 
1798bb7acecfSBarry Smith   Developer Note:
1799bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1800bb7acecfSBarry Smith   likely be removed.
1801bb7acecfSBarry Smith 
1802bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1803bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18044d343eeaSMatthew G Knepley @*/
18059371c9d4SSatish Balay PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields) {
180637d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18074d343eeaSMatthew G Knepley 
18084d343eeaSMatthew G Knepley   PetscFunctionBegin;
18094d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
181069ca1f37SDmitry Karpeev   if (numFields) {
1811534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
181269ca1f37SDmitry Karpeev     *numFields = 0;
181369ca1f37SDmitry Karpeev   }
181437d0c07bSMatthew G Knepley   if (fieldNames) {
181537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
18160298fd71SBarry Smith     *fieldNames = NULL;
181769ca1f37SDmitry Karpeev   }
181869ca1f37SDmitry Karpeev   if (fields) {
181969ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
18200298fd71SBarry Smith     *fields = NULL;
182169ca1f37SDmitry Karpeev   }
18229566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
182337d0c07bSMatthew G Knepley   if (section) {
18243a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
182537d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
182637d0c07bSMatthew G Knepley 
18279566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
18289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
18299566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
18309566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
183137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
183237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18339566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
183437d0c07bSMatthew G Knepley     }
183537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
183637d0c07bSMatthew G Knepley       PetscInt gdof;
183737d0c07bSMatthew G Knepley 
18389566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
183937d0c07bSMatthew G Knepley       if (gdof > 0) {
184037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18413a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
184237d0c07bSMatthew G Knepley 
18439566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18449566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18453a544194SStefano Zampini           fpdof = fdof - fcdof;
18463a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18473a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18483a544194SStefano Zampini             fieldNc[f] = 1;
18493a544194SStefano Zampini           }
18503a544194SStefano Zampini           fieldSizes[f] += fpdof;
185137d0c07bSMatthew G Knepley         }
185237d0c07bSMatthew G Knepley       }
185337d0c07bSMatthew G Knepley     }
185437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
18559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
185637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
185737d0c07bSMatthew G Knepley     }
185837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
185937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
186037d0c07bSMatthew G Knepley 
18619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
186237d0c07bSMatthew G Knepley       if (gdof > 0) {
18639566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
186437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
186537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
186637d0c07bSMatthew G Knepley 
18679566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
18689566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1869ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
187037d0c07bSMatthew G Knepley         }
187137d0c07bSMatthew G Knepley       }
187237d0c07bSMatthew G Knepley     }
18738865f1eaSKarl Rupp     if (numFields) *numFields = nF;
187437d0c07bSMatthew G Knepley     if (fieldNames) {
18759566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
187637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
187737d0c07bSMatthew G Knepley         const char *fieldName;
187837d0c07bSMatthew G Knepley 
18799566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
18809566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
188137d0c07bSMatthew G Knepley       }
188237d0c07bSMatthew G Knepley     }
188337d0c07bSMatthew G Knepley     if (fields) {
18849566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
188537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18863a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18873a544194SStefano Zampini 
18889566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
18893a544194SStefano Zampini         in[0] = -fieldNc[f];
18903a544194SStefano Zampini         in[1] = fieldNc[f];
18911c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
18923a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
18939566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
189437d0c07bSMatthew G Knepley       }
189537d0c07bSMatthew G Knepley     }
18969566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1897dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
18984d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18994d343eeaSMatthew G Knepley }
19004d343eeaSMatthew G Knepley 
190116621825SDmitry Karpeev /*@C
1902bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1903bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1904bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1905bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
1906e7c4fc90SDmitry Karpeev 
1907e7c4fc90SDmitry Karpeev   Not collective
1908e7c4fc90SDmitry Karpeev 
1909e7c4fc90SDmitry Karpeev   Input Parameter:
1910bb7acecfSBarry Smith . dm - the `DM` object
1911e7c4fc90SDmitry Karpeev 
1912e7c4fc90SDmitry Karpeev   Output Parameters:
1913bb7acecfSBarry Smith + len       - The number of fields (or NULL if not requested)
19140298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19150298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
1916bb7acecfSBarry Smith - dmlist    - The `DM`s for each field subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
1917e7c4fc90SDmitry Karpeev 
1918e7c4fc90SDmitry Karpeev   Level: intermediate
1919e7c4fc90SDmitry Karpeev 
1920bb7acecfSBarry Smith   Note:
1921e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1922bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
1923bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
1924e7c4fc90SDmitry Karpeev 
1925bb7acecfSBarry Smith   Fortran Note:
1926bb7acecfSBarry Smith   Not available in Fortran.
1927bb7acecfSBarry Smith 
1928bb7acecfSBarry Smith   Developer Note:
1929bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
1930bb7acecfSBarry Smith 
1931bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
1932e7c4fc90SDmitry Karpeev @*/
19339371c9d4SSatish Balay PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist) {
1934e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1935e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19368865f1eaSKarl Rupp   if (len) {
1937534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
19388865f1eaSKarl Rupp     *len = 0;
19398865f1eaSKarl Rupp   }
19408865f1eaSKarl Rupp   if (namelist) {
19418865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
1942ea78f98cSLisandro Dalcin     *namelist = NULL;
19438865f1eaSKarl Rupp   }
19448865f1eaSKarl Rupp   if (islist) {
19458865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
1946ea78f98cSLisandro Dalcin     *islist = NULL;
19478865f1eaSKarl Rupp   }
19488865f1eaSKarl Rupp   if (dmlist) {
19498865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
1950ea78f98cSLisandro Dalcin     *dmlist = NULL;
19518865f1eaSKarl Rupp   }
1952f3f0edfdSDmitry Karpeev   /*
1953f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1954f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1955f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1956f3f0edfdSDmitry Karpeev    */
19577a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
195816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1959435a35e8SMatthew G Knepley     PetscSection section;
1960435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1961435a35e8SMatthew G Knepley 
19629566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
19639566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
1964435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1965f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
19669566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
19679566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
19689566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
1969435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1970435a35e8SMatthew G Knepley         const char *fieldName;
1971435a35e8SMatthew G Knepley 
19729566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
197303dc3394SMatthew G. Knepley         if (namelist) {
19749566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19759566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
1976435a35e8SMatthew G Knepley         }
197703dc3394SMatthew G. Knepley       }
1978435a35e8SMatthew G Knepley     } else {
19799566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
1980e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19810298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1982e7c4fc90SDmitry Karpeev     }
1983dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
198416621825SDmitry Karpeev   PetscFunctionReturn(0);
198516621825SDmitry Karpeev }
198616621825SDmitry Karpeev 
1987412a4547SAlexis Marboeuf /*@C
1988412a4547SAlexis Marboeuf   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1989412a4547SAlexis Marboeuf                   The fields are defined by DMCreateFieldIS().
1990435a35e8SMatthew G Knepley 
1991435a35e8SMatthew G Knepley   Not collective
1992435a35e8SMatthew G Knepley 
1993435a35e8SMatthew G Knepley   Input Parameters:
1994bb7acecfSBarry Smith + dm        - The `DM `object
1995bb7acecfSBarry Smith . numFields - The number of fields to select
19962adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1997435a35e8SMatthew G Knepley 
1998435a35e8SMatthew G Knepley   Output Parameters:
1999bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2000bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2001435a35e8SMatthew G Knepley 
2002bb7acecfSBarry Smith   Note:
2003bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20045d3b26e6SMatthew G. Knepley 
2005435a35e8SMatthew G Knepley   Level: intermediate
2006435a35e8SMatthew G Knepley 
2007bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2008435a35e8SMatthew G Knepley @*/
20099371c9d4SSatish Balay PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm) {
2010435a35e8SMatthew G Knepley   PetscFunctionBegin;
2011435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2012dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
20138865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
20148865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2015dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
2016435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2017435a35e8SMatthew G Knepley }
2018435a35e8SMatthew G Knepley 
20192adcc780SMatthew G. Knepley /*@C
2020bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
20212adcc780SMatthew G. Knepley 
20222adcc780SMatthew G. Knepley   Not collective
20232adcc780SMatthew G. Knepley 
2024d8d19677SJose E. Roman   Input Parameters:
2025bb7acecfSBarry Smith + dms - The `DM` objects
2026bb7acecfSBarry Smith - n - The number of `DM`s
20272adcc780SMatthew G. Knepley 
20282adcc780SMatthew G. Knepley   Output Parameters:
2029bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2030bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
20312adcc780SMatthew G. Knepley 
2032bb7acecfSBarry Smith   Note:
2033bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20345d3b26e6SMatthew G. Knepley 
20352adcc780SMatthew G. Knepley   Level: intermediate
20362adcc780SMatthew G. Knepley 
2037bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
20382adcc780SMatthew G. Knepley @*/
20399371c9d4SSatish Balay PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm) {
20402adcc780SMatthew G. Knepley   PetscInt i;
20412adcc780SMatthew G. Knepley 
20422adcc780SMatthew G. Knepley   PetscFunctionBegin;
20432adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2044ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
20452adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2046a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2047bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2048bb7acecfSBarry Smith   if (n) {
2049b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2050dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
20512adcc780SMatthew G. Knepley   }
20522adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20532adcc780SMatthew G. Knepley }
20542adcc780SMatthew G. Knepley 
205516621825SDmitry Karpeev /*@C
2056bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2057bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2058bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2059bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2060bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
206116621825SDmitry Karpeev 
206216621825SDmitry Karpeev   Not collective
206316621825SDmitry Karpeev 
206416621825SDmitry Karpeev   Input Parameter:
2065bb7acecfSBarry Smith . dm - the `DM` object
206616621825SDmitry Karpeev 
206716621825SDmitry Karpeev   Output Parameters:
2068bb7acecfSBarry Smith + n            - The number of subproblems in the domain decomposition (or NULL if not requested)
20690298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20700298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20710298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2072bb7acecfSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
207316621825SDmitry Karpeev 
207416621825SDmitry Karpeev   Level: intermediate
207516621825SDmitry Karpeev 
2076bb7acecfSBarry Smith   Note:
207716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2078bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2079bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
208016621825SDmitry Karpeev 
2081bb7acecfSBarry Smith   Questions:
2082bb7acecfSBarry Smith   The dmlist is for the inner subdomains or the outer subdomains or all subdomains?
2083bb7acecfSBarry Smith 
2084bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
208516621825SDmitry Karpeev @*/
20869371c9d4SSatish Balay PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist) {
2087be081cd6SPeter Brune   DMSubDomainHookLink link;
2088be081cd6SPeter Brune   PetscInt            i, l;
208916621825SDmitry Karpeev 
209016621825SDmitry Karpeev   PetscFunctionBegin;
209116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20929371c9d4SSatish Balay   if (n) {
20939371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
20949371c9d4SSatish Balay     *n = 0;
20959371c9d4SSatish Balay   }
20969371c9d4SSatish Balay   if (namelist) {
20979371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
20989371c9d4SSatish Balay     *namelist = NULL;
20999371c9d4SSatish Balay   }
21009371c9d4SSatish Balay   if (innerislist) {
21019371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
21029371c9d4SSatish Balay     *innerislist = NULL;
21039371c9d4SSatish Balay   }
21049371c9d4SSatish Balay   if (outerislist) {
21059371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
21069371c9d4SSatish Balay     *outerislist = NULL;
21079371c9d4SSatish Balay   }
21089371c9d4SSatish Balay   if (dmlist) {
21099371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
21109371c9d4SSatish Balay     *dmlist = NULL;
21119371c9d4SSatish Balay   }
2112f3f0edfdSDmitry Karpeev   /*
2113f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2114f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2115f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2116f3f0edfdSDmitry Karpeev    */
21177a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
211816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2119dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
212014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2121f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2122be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2123be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
21249566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2125be081cd6SPeter Brune         }
2126648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2127e7c4fc90SDmitry Karpeev       }
212814a18fd3SPeter Brune     }
2129bb7acecfSBarry Smith     if (n) *n = l;
213014a18fd3SPeter Brune   }
2131e30e807fSPeter Brune   PetscFunctionReturn(0);
2132e30e807fSPeter Brune }
2133e30e807fSPeter Brune 
2134e30e807fSPeter Brune /*@C
2135e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2136e30e807fSPeter Brune 
2137e30e807fSPeter Brune   Not collective
2138e30e807fSPeter Brune 
2139e30e807fSPeter Brune   Input Parameters:
2140bb7acecfSBarry Smith + dm - the `DM` object
2141e30e807fSPeter Brune . n  - the number of subdomain scatters
2142e30e807fSPeter Brune - subdms - the local subdomains
2143e30e807fSPeter Brune 
2144e30e807fSPeter Brune   Output Parameters:
21456b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2146e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2147e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2148e30e807fSPeter Brune 
2149bb7acecfSBarry Smith   Note:
2150bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2151e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2152e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2153e30e807fSPeter Brune   solution and residual data.
2154e30e807fSPeter Brune 
2155bb7acecfSBarry Smith   Questions:
2156bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2157bb7acecfSBarry Smith 
2158e30e807fSPeter Brune   Level: developer
2159e30e807fSPeter Brune 
2160bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2161e30e807fSPeter Brune @*/
21629371c9d4SSatish Balay PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat) {
2163e30e807fSPeter Brune   PetscFunctionBegin;
2164e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2165e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2166dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
2167e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2168e7c4fc90SDmitry Karpeev }
2169e7c4fc90SDmitry Karpeev 
217047c6ae99SBarry Smith /*@
2171bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
217247c6ae99SBarry Smith 
2173d083f849SBarry Smith   Collective on dm
217447c6ae99SBarry Smith 
2175d8d19677SJose E. Roman   Input Parameters:
2176bb7acecfSBarry Smith + dm   - the `DM` object
2177bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
217847c6ae99SBarry Smith 
217947c6ae99SBarry Smith   Output Parameter:
2180bb7acecfSBarry Smith . dmf - the refined `D`M, or NULL
2181ae0a1c52SMatthew G Knepley 
2182f27dd7c6SMatthew G. Knepley   Options Database Keys:
2183412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2184412e9a14SMatthew G. Knepley 
2185bb7acecfSBarry Smith   Note:
2186bb7acecfSBarry Smith   If no refinement was done, the return value is NULL
218747c6ae99SBarry Smith 
218847c6ae99SBarry Smith   Level: developer
218947c6ae99SBarry Smith 
2190bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
219147c6ae99SBarry Smith @*/
21929371c9d4SSatish Balay PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf) {
2193c833c3b5SJed Brown   DMRefineHookLink link;
219447c6ae99SBarry Smith 
219547c6ae99SBarry Smith   PetscFunctionBegin;
2196732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21979566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2198dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
21994057135bSMatthew G Knepley   if (*dmf) {
220043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22018865f1eaSKarl Rupp 
22029566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
22038865f1eaSKarl Rupp 
2204644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22050598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2206656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22078865f1eaSKarl Rupp 
22089566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2209c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
22101baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2211c833c3b5SJed Brown     }
2212c833c3b5SJed Brown   }
22139566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
2214c833c3b5SJed Brown   PetscFunctionReturn(0);
2215c833c3b5SJed Brown }
2216c833c3b5SJed Brown 
2217bb9467b5SJed Brown /*@C
2218c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2219c833c3b5SJed Brown 
2220bb7acecfSBarry Smith    Logically Collective on coarse
2221c833c3b5SJed Brown 
22224165533cSJose E. Roman    Input Parameters:
2223bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2224bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2225bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
22260298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2227c833c3b5SJed Brown 
2228c833c3b5SJed Brown    Calling sequence of refinehook:
2229c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2230c833c3b5SJed Brown 
2231bb7acecfSBarry Smith +  coarse - coarse level `DM`
2232bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2233c833c3b5SJed Brown -  ctx - optional user-defined function context
2234c833c3b5SJed Brown 
2235c833c3b5SJed Brown    Calling sequence for interphook:
2236c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2237c833c3b5SJed Brown 
2238bb7acecfSBarry Smith +  coarse - coarse level `DM`
2239c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2240bb7acecfSBarry Smith .  fine - fine level `DM` to update
2241c833c3b5SJed Brown -  ctx - optional user-defined function context
2242c833c3b5SJed Brown 
2243c833c3b5SJed Brown    Level: advanced
2244c833c3b5SJed Brown 
2245c833c3b5SJed Brown    Notes:
2246bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2247bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2248bb7acecfSBarry Smith 
2249bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2250c833c3b5SJed Brown 
2251c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2252c833c3b5SJed Brown 
2253bb7acecfSBarry Smith    Fortran Note:
2254bb7acecfSBarry Smith    This function is not available from Fortran.
2255bb9467b5SJed Brown 
2256bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2257c833c3b5SJed Brown @*/
22589371c9d4SSatish Balay PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx) {
2259c833c3b5SJed Brown   DMRefineHookLink link, *p;
2260c833c3b5SJed Brown 
2261c833c3b5SJed Brown   PetscFunctionBegin;
2262c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
22633d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
22643d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22653d8e3701SJed Brown   }
22669566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2267c833c3b5SJed Brown   link->refinehook = refinehook;
2268c833c3b5SJed Brown   link->interphook = interphook;
2269c833c3b5SJed Brown   link->ctx        = ctx;
22700298fd71SBarry Smith   link->next       = NULL;
2271c833c3b5SJed Brown   *p               = link;
2272c833c3b5SJed Brown   PetscFunctionReturn(0);
2273c833c3b5SJed Brown }
2274c833c3b5SJed Brown 
22753d8e3701SJed Brown /*@C
2276bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2277bb7acecfSBarry Smith     a nonlinear problem to a finer grid
22783d8e3701SJed Brown 
2279bb7acecfSBarry Smith    Logically Collective on coarse
22803d8e3701SJed Brown 
22814165533cSJose E. Roman    Input Parameters:
2282bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2283bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2284bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
22853d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22863d8e3701SJed Brown 
22873d8e3701SJed Brown    Level: advanced
22883d8e3701SJed Brown 
2289bb7acecfSBarry Smith    Note:
22903d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22913d8e3701SJed Brown 
2292bb7acecfSBarry Smith    Fortran Note:
2293bb7acecfSBarry Smith    This function is not available from Fortran.
22943d8e3701SJed Brown 
2295bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
22963d8e3701SJed Brown @*/
22979371c9d4SSatish Balay PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx) {
22983d8e3701SJed Brown   DMRefineHookLink link, *p;
22993d8e3701SJed Brown 
23003d8e3701SJed Brown   PetscFunctionBegin;
23013d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23023d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23033d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23043d8e3701SJed Brown       link = *p;
23053d8e3701SJed Brown       *p   = link->next;
23069566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23073d8e3701SJed Brown       break;
23083d8e3701SJed Brown     }
23093d8e3701SJed Brown   }
23103d8e3701SJed Brown   PetscFunctionReturn(0);
23113d8e3701SJed Brown }
23123d8e3701SJed Brown 
2313c833c3b5SJed Brown /*@
2314bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2315c833c3b5SJed Brown 
2316c833c3b5SJed Brown    Collective if any hooks are
2317c833c3b5SJed Brown 
23184165533cSJose E. Roman    Input Parameters:
2319bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2320bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2321bb7acecfSBarry Smith -  fine - finer `DM` to update
2322c833c3b5SJed Brown 
2323c833c3b5SJed Brown    Level: developer
2324c833c3b5SJed Brown 
2325bb7acecfSBarry Smith    Developer Note:
2326bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2327bb7acecfSBarry Smith    an API with consistent terminology.
2328bb7acecfSBarry Smith 
2329bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2330c833c3b5SJed Brown @*/
23319371c9d4SSatish Balay PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine) {
2332c833c3b5SJed Brown   DMRefineHookLink link;
2333c833c3b5SJed Brown 
2334c833c3b5SJed Brown   PetscFunctionBegin;
2335c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
23361baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
23374057135bSMatthew G Knepley   }
233847c6ae99SBarry Smith   PetscFunctionReturn(0);
233947c6ae99SBarry Smith }
234047c6ae99SBarry Smith 
2341eb3f98d2SBarry Smith /*@
23421f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23431f3379b2SToby Isaac 
2344bb7acecfSBarry Smith    Collective on dm
23451f3379b2SToby Isaac 
23464165533cSJose E. Roman    Input Parameters:
2347bb7acecfSBarry Smith +  coarse - coarse `DM`
2348bb7acecfSBarry Smith .  fine   - fine `DM`
2349bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2350bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2351bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
23521f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23531f3379b2SToby Isaac 
23544165533cSJose E. Roman    Output Parameter:
23551f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23561f3379b2SToby Isaac 
23571f3379b2SToby Isaac    Level: developer
23581f3379b2SToby Isaac 
2359bb7acecfSBarry Smith    Note:
2360bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
23611f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23621f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23631f3379b2SToby Isaac    slope-limiting reconstruction.
23641f3379b2SToby Isaac 
2365bb7acecfSBarry Smith    Developer Note:
2366bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2367bb7acecfSBarry Smith 
2368bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
23691f3379b2SToby Isaac @*/
23709371c9d4SSatish Balay PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol) {
23711f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
23721f3379b2SToby Isaac 
23731f3379b2SToby Isaac   PetscFunctionBegin;
23741f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23751f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
23761f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
23771f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
23781f3379b2SToby Isaac 
23799566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
23801f3379b2SToby Isaac   if (interpsol) {
23819566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
23821f3379b2SToby Isaac   } else if (interp) {
23839566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
238498921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23851f3379b2SToby Isaac   PetscFunctionReturn(0);
23861f3379b2SToby Isaac }
23871f3379b2SToby Isaac 
23881f3379b2SToby Isaac /*@
2389bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2390eb3f98d2SBarry Smith 
2391eb3f98d2SBarry Smith     Not Collective
2392eb3f98d2SBarry Smith 
2393eb3f98d2SBarry Smith     Input Parameter:
2394bb7acecfSBarry Smith .   dm - the `DM` object
2395eb3f98d2SBarry Smith 
2396eb3f98d2SBarry Smith     Output Parameter:
2397eb3f98d2SBarry Smith .   level - number of refinements
2398eb3f98d2SBarry Smith 
2399eb3f98d2SBarry Smith     Level: developer
2400eb3f98d2SBarry Smith 
2401bb7acecfSBarry Smith     Note:
2402bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2403bb7acecfSBarry Smith 
2404bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2405eb3f98d2SBarry Smith 
2406eb3f98d2SBarry Smith @*/
24079371c9d4SSatish Balay PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level) {
2408eb3f98d2SBarry Smith   PetscFunctionBegin;
2409eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2410eb3f98d2SBarry Smith   *level = dm->levelup;
2411eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2412eb3f98d2SBarry Smith }
2413eb3f98d2SBarry Smith 
2414fef3a512SBarry Smith /*@
2415bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2416fef3a512SBarry Smith 
2417fef3a512SBarry Smith     Not Collective
2418fef3a512SBarry Smith 
2419d8d19677SJose E. Roman     Input Parameters:
2420bb7acecfSBarry Smith +   dm - the `DM` object
2421fef3a512SBarry Smith -   level - number of refinements
2422fef3a512SBarry Smith 
2423fef3a512SBarry Smith     Level: advanced
2424fef3a512SBarry Smith 
242595452b02SPatrick Sanan     Notes:
2426bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2427fef3a512SBarry Smith 
2428bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2429bb7acecfSBarry Smith 
2430bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2431fef3a512SBarry Smith 
2432fef3a512SBarry Smith @*/
24339371c9d4SSatish Balay PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level) {
2434fef3a512SBarry Smith   PetscFunctionBegin;
2435fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2436fef3a512SBarry Smith   dm->levelup = level;
2437fef3a512SBarry Smith   PetscFunctionReturn(0);
2438fef3a512SBarry Smith }
2439fef3a512SBarry Smith 
2440d410b0cfSMatthew G. Knepley /*@
2441bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2442d410b0cfSMatthew G. Knepley 
2443d410b0cfSMatthew G. Knepley   Collective on dm
2444d410b0cfSMatthew G. Knepley 
2445f1a722f8SMatthew G. Knepley   Input Parameters:
2446bb7acecfSBarry Smith + dm     - the `DM` object
2447d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2448d410b0cfSMatthew G. Knepley 
2449d410b0cfSMatthew G. Knepley   Output Parameter:
2450bb7acecfSBarry Smith . dme - the extruded `DM`, or NULL
2451d410b0cfSMatthew G. Knepley 
2452bb7acecfSBarry Smith   Note:
2453bb7acecfSBarry Smith   If no extrusion was done, the return value is NULL
2454d410b0cfSMatthew G. Knepley 
2455d410b0cfSMatthew G. Knepley   Level: developer
2456d410b0cfSMatthew G. Knepley 
2457bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2458d410b0cfSMatthew G. Knepley @*/
24599371c9d4SSatish Balay PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme) {
2460d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2461d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2462dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2463d410b0cfSMatthew G. Knepley   if (*dme) {
2464d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
24659566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2466d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
24679566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2468d410b0cfSMatthew G. Knepley   }
2469d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2470d410b0cfSMatthew G. Knepley }
2471d410b0cfSMatthew G. Knepley 
24729371c9d4SSatish Balay PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm) {
2473ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2474ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2475ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2476ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2477ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2478ca3d3a14SMatthew G. Knepley }
2479ca3d3a14SMatthew G. Knepley 
24809371c9d4SSatish Balay PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv) {
2481ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2482ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2483ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2484ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2485ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2486ca3d3a14SMatthew G. Knepley }
2487ca3d3a14SMatthew G. Knepley 
2488ca3d3a14SMatthew G. Knepley /*@
2489bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2490ca3d3a14SMatthew G. Knepley 
2491ca3d3a14SMatthew G. Knepley   Input Parameter:
2492ca3d3a14SMatthew G. Knepley . dm - The DM
2493ca3d3a14SMatthew G. Knepley 
2494ca3d3a14SMatthew G. Knepley   Output Parameter:
2495ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2496ca3d3a14SMatthew G. Knepley 
2497ca3d3a14SMatthew G. Knepley   Level: developer
2498ca3d3a14SMatthew G. Knepley 
2499bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2500ca3d3a14SMatthew G. Knepley @*/
25019371c9d4SSatish Balay PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg) {
2502ca3d3a14SMatthew G. Knepley   Vec tv;
2503ca3d3a14SMatthew G. Knepley 
2504ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2505ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2506534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
25079566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2508ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2509ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2510ca3d3a14SMatthew G. Knepley }
2511ca3d3a14SMatthew G. Knepley 
25129371c9d4SSatish Balay PetscErrorCode DMConstructBasisTransform_Internal(DM dm) {
2513ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2514ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2515ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2516ca3d3a14SMatthew G. Knepley 
2517ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
25189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
25199566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
25209566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
25219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
25229566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
25239566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
25249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
25259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2526ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
25279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2528ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2529ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2530ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2532ca3d3a14SMatthew G. Knepley       if (!dof) continue;
25339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
25349566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2535ca3d3a14SMatthew G. Knepley     }
2536ca3d3a14SMatthew G. Knepley   }
25379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
25389566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
25399566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2540ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2541ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
25429566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2543ca3d3a14SMatthew G. Knepley       if (dof) {
2544ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2545ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2546ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2547ca3d3a14SMatthew G. Knepley 
2548ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
25499566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
25509566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
25519566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2552ca3d3a14SMatthew G. Knepley       }
2553ca3d3a14SMatthew G. Knepley     }
2554ca3d3a14SMatthew G. Knepley   }
25559566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2556ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2557ca3d3a14SMatthew G. Knepley }
2558ca3d3a14SMatthew G. Knepley 
25599371c9d4SSatish Balay PetscErrorCode DMCopyTransform(DM dm, DM newdm) {
2560ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2561ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2562ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2563ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2564ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2565ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2566ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
25679566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2568ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2569ca3d3a14SMatthew G. Knepley }
2570ca3d3a14SMatthew G. Knepley 
2571bb9467b5SJed Brown /*@C
2572bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2573baf369e7SPeter Brune 
2574bb7acecfSBarry Smith    Logically Collective on dm
2575baf369e7SPeter Brune 
25764165533cSJose E. Roman    Input Parameters:
2577bb7acecfSBarry Smith +  dm - the `DM`
2578bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2579bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
25800298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2581baf369e7SPeter Brune 
2582baf369e7SPeter Brune    Calling sequence for beginhook:
2583baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2584baf369e7SPeter Brune 
2585baf369e7SPeter Brune +  dm - global DM
2586baf369e7SPeter Brune .  g - global vector
2587baf369e7SPeter Brune .  mode - mode
2588baf369e7SPeter Brune .  l - local vector
2589baf369e7SPeter Brune -  ctx - optional user-defined function context
2590baf369e7SPeter Brune 
2591baf369e7SPeter Brune    Calling sequence for endhook:
2592ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2593baf369e7SPeter Brune 
2594baf369e7SPeter Brune +  global - global DM
2595baf369e7SPeter Brune -  ctx - optional user-defined function context
2596baf369e7SPeter Brune 
2597baf369e7SPeter Brune    Level: advanced
2598baf369e7SPeter Brune 
2599bb7acecfSBarry Smith    Note:
2600bb7acecfSBarry 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.
2601bb7acecfSBarry Smith 
2602bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2603baf369e7SPeter Brune @*/
26049371c9d4SSatish Balay PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx) {
2605baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2606baf369e7SPeter Brune 
2607baf369e7SPeter Brune   PetscFunctionBegin;
2608baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2609baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
26109566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2611baf369e7SPeter Brune   link->beginhook = beginhook;
2612baf369e7SPeter Brune   link->endhook   = endhook;
2613baf369e7SPeter Brune   link->ctx       = ctx;
26140298fd71SBarry Smith   link->next      = NULL;
2615baf369e7SPeter Brune   *p              = link;
2616baf369e7SPeter Brune   PetscFunctionReturn(0);
2617baf369e7SPeter Brune }
2618baf369e7SPeter Brune 
26199371c9d4SSatish Balay static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx) {
26204c274da1SToby Isaac   Mat          cMat;
262179769bd5SJed Brown   Vec          cVec, cBias;
26224c274da1SToby Isaac   PetscSection section, cSec;
26234c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
26244c274da1SToby Isaac 
26254c274da1SToby Isaac   PetscFunctionBegin;
26264c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26279566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
26284c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
26295db9a05bSToby Isaac     PetscInt nRows;
26305db9a05bSToby Isaac 
26319566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
26325db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
26339566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
26349566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
26359566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
26369566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
26379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
26384c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
26404c274da1SToby Isaac       if (dof) {
26414c274da1SToby Isaac         PetscScalar *vals;
26429566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
26439566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
26444c274da1SToby Isaac       }
26454c274da1SToby Isaac     }
26469566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
26474c274da1SToby Isaac   }
26484c274da1SToby Isaac   PetscFunctionReturn(0);
26494c274da1SToby Isaac }
26504c274da1SToby Isaac 
265147c6ae99SBarry Smith /*@
265201729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
265301729b5cSPatrick Sanan 
2654d083f849SBarry Smith     Neighbor-wise Collective on dm
265501729b5cSPatrick Sanan 
265601729b5cSPatrick Sanan     Input Parameters:
2657bb7acecfSBarry Smith +   dm - the `DM` object
265801729b5cSPatrick Sanan .   g - the global vector
2659bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
266001729b5cSPatrick Sanan -   l - the local vector
266101729b5cSPatrick Sanan 
266201729b5cSPatrick Sanan     Notes:
2663bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2664bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2665bb7acecfSBarry Smith 
2666bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
266701729b5cSPatrick Sanan 
266801729b5cSPatrick Sanan     Level: beginner
266901729b5cSPatrick Sanan 
2670bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2671bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2672bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
267301729b5cSPatrick Sanan 
267401729b5cSPatrick Sanan @*/
26759371c9d4SSatish Balay PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l) {
267601729b5cSPatrick Sanan   PetscFunctionBegin;
26779566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
26789566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
267901729b5cSPatrick Sanan   PetscFunctionReturn(0);
268001729b5cSPatrick Sanan }
268101729b5cSPatrick Sanan 
268201729b5cSPatrick Sanan /*@
268347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
268447c6ae99SBarry Smith 
2685d083f849SBarry Smith     Neighbor-wise Collective on dm
268647c6ae99SBarry Smith 
268747c6ae99SBarry Smith     Input Parameters:
2688bb7acecfSBarry Smith +   dm - the `DM` object
268947c6ae99SBarry Smith .   g - the global vector
2690bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
269147c6ae99SBarry Smith -   l - the local vector
269247c6ae99SBarry Smith 
269301729b5cSPatrick Sanan     Level: intermediate
269447c6ae99SBarry Smith 
2695bb7acecfSBarry Smith     Notes:
2696bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2697bb7acecfSBarry Smith 
2698bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2699bb7acecfSBarry Smith 
2700bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2701bb7acecfSBarry Smith 
2702bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2703bb7acecfSBarry Smith 
2704bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
270547c6ae99SBarry Smith 
270647c6ae99SBarry Smith @*/
27079371c9d4SSatish Balay PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l) {
27087128ae9fSMatthew G Knepley   PetscSF                 sf;
2709baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
271047c6ae99SBarry Smith 
271147c6ae99SBarry Smith   PetscFunctionBegin;
2712171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2713baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
27141baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2715baf369e7SPeter Brune   }
27169566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27177128ae9fSMatthew G Knepley   if (sf) {
2718ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2719ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2720d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
27217128ae9fSMatthew G Knepley 
27227a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27239566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27249566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27259566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
27269566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27279566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27287128ae9fSMatthew G Knepley   } else {
27299566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
27307128ae9fSMatthew G Knepley   }
273147c6ae99SBarry Smith   PetscFunctionReturn(0);
273247c6ae99SBarry Smith }
273347c6ae99SBarry Smith 
273447c6ae99SBarry Smith /*@
273547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
273647c6ae99SBarry Smith 
2737d083f849SBarry Smith     Neighbor-wise Collective on dm
273847c6ae99SBarry Smith 
273947c6ae99SBarry Smith     Input Parameters:
2740bb7acecfSBarry Smith +   dm - the `DM` object
274147c6ae99SBarry Smith .   g - the global vector
2742bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
274347c6ae99SBarry Smith -   l - the local vector
274447c6ae99SBarry Smith 
274501729b5cSPatrick Sanan     Level: intermediate
274647c6ae99SBarry Smith 
2747bb7acecfSBarry Smith     Note:
2748bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2749bb7acecfSBarry Smith 
2750bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
275147c6ae99SBarry Smith 
275247c6ae99SBarry Smith @*/
27539371c9d4SSatish Balay PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l) {
27547128ae9fSMatthew G Knepley   PetscSF                 sf;
2755ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2756ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2757ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2758baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2759d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
276047c6ae99SBarry Smith 
276147c6ae99SBarry Smith   PetscFunctionBegin;
2762171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27639566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
27649566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
27657128ae9fSMatthew G Knepley   if (sf) {
27667a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27677128ae9fSMatthew G Knepley 
27689566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
27699566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
27709566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
27719566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
27729566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
27739566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
27747128ae9fSMatthew G Knepley   } else {
27759566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
27767128ae9fSMatthew G Knepley   }
27779566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2778baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
27799566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2780baf369e7SPeter Brune   }
278147c6ae99SBarry Smith   PetscFunctionReturn(0);
278247c6ae99SBarry Smith }
278347c6ae99SBarry Smith 
2784d4d07f1eSToby Isaac /*@C
2785d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2786d4d07f1eSToby Isaac 
2787bb7acecfSBarry Smith    Logically Collective on dm
2788d4d07f1eSToby Isaac 
27894165533cSJose E. Roman    Input Parameters:
2790bb7acecfSBarry Smith +  dm - the `DM`
2791bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2792bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2793d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2794d4d07f1eSToby Isaac 
2795d4d07f1eSToby Isaac    Calling sequence for beginhook:
2796d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2797d4d07f1eSToby Isaac 
2798bb7acecfSBarry Smith +  dm - global `DM`
2799d4d07f1eSToby Isaac .  l - local vector
2800d4d07f1eSToby Isaac .  mode - mode
2801d4d07f1eSToby Isaac .  g - global vector
2802d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2803d4d07f1eSToby Isaac 
2804d4d07f1eSToby Isaac    Calling sequence for endhook:
2805d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2806d4d07f1eSToby Isaac 
2807bb7acecfSBarry Smith +  global - global `DM`
2808d4d07f1eSToby Isaac .  l - local vector
2809d4d07f1eSToby Isaac .  mode - mode
2810d4d07f1eSToby Isaac .  g - global vector
2811d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2812d4d07f1eSToby Isaac 
2813d4d07f1eSToby Isaac    Level: advanced
2814d4d07f1eSToby Isaac 
2815bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2816d4d07f1eSToby Isaac @*/
28179371c9d4SSatish Balay PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx) {
2818d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2819d4d07f1eSToby Isaac 
2820d4d07f1eSToby Isaac   PetscFunctionBegin;
2821d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2822d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
28239566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2824d4d07f1eSToby Isaac   link->beginhook = beginhook;
2825d4d07f1eSToby Isaac   link->endhook   = endhook;
2826d4d07f1eSToby Isaac   link->ctx       = ctx;
2827d4d07f1eSToby Isaac   link->next      = NULL;
2828d4d07f1eSToby Isaac   *p              = link;
2829d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2830d4d07f1eSToby Isaac }
2831d4d07f1eSToby Isaac 
28329371c9d4SSatish Balay static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx) {
28334c274da1SToby Isaac   Mat          cMat;
28344c274da1SToby Isaac   Vec          cVec;
28354c274da1SToby Isaac   PetscSection section, cSec;
28364c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
28374c274da1SToby Isaac 
28384c274da1SToby Isaac   PetscFunctionBegin;
28394c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28409566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
28414c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
28425db9a05bSToby Isaac     PetscInt nRows;
28435db9a05bSToby Isaac 
28449566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
28455db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
28469566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
28479566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
28489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
28494c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
28509566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
28514c274da1SToby Isaac       if (dof) {
28524c274da1SToby Isaac         PetscInt     d;
28534c274da1SToby Isaac         PetscScalar *vals;
28549566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
28559566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
28564c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28574c274da1SToby Isaac          * we just extracted */
2858ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
28594c274da1SToby Isaac       }
28604c274da1SToby Isaac     }
28619566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
28629566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
28634c274da1SToby Isaac   }
28644c274da1SToby Isaac   PetscFunctionReturn(0);
28654c274da1SToby Isaac }
286601729b5cSPatrick Sanan /*@
286701729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
286801729b5cSPatrick Sanan 
2869d083f849SBarry Smith     Neighbor-wise Collective on dm
287001729b5cSPatrick Sanan 
287101729b5cSPatrick Sanan     Input Parameters:
2872bb7acecfSBarry Smith +   dm - the `DM` object
287301729b5cSPatrick Sanan .   l - the local vector
2874bb7acecfSBarry 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.
287501729b5cSPatrick Sanan -   g - the global vector
287601729b5cSPatrick Sanan 
287701729b5cSPatrick Sanan     Notes:
287801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2879bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
288001729b5cSPatrick Sanan 
2881bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2882bb7acecfSBarry Smith 
2883bb7acecfSBarry 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.
2884bb7acecfSBarry Smith 
2885bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
288601729b5cSPatrick Sanan 
288701729b5cSPatrick Sanan     Level: beginner
288801729b5cSPatrick Sanan 
2889bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`,  `DMLocalToGlobalHookAdd()`,  `DMGlobaToLocallHookAdd()`
289001729b5cSPatrick Sanan 
289101729b5cSPatrick Sanan @*/
28929371c9d4SSatish Balay PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g) {
289301729b5cSPatrick Sanan   PetscFunctionBegin;
28949566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
28959566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
289601729b5cSPatrick Sanan   PetscFunctionReturn(0);
289701729b5cSPatrick Sanan }
28984c274da1SToby Isaac 
289947c6ae99SBarry Smith /*@
290001729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
29019a42bb27SBarry Smith 
2902d083f849SBarry Smith     Neighbor-wise Collective on dm
29039a42bb27SBarry Smith 
29049a42bb27SBarry Smith     Input Parameters:
2905bb7acecfSBarry Smith +   dm - the `DM` object
2906f6813fd5SJed Brown .   l - the local vector
2907bb7acecfSBarry 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.
29081eb28f2eSBarry Smith -   g - the global vector
29099a42bb27SBarry Smith 
291095452b02SPatrick Sanan     Notes:
2911bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2912bb7acecfSBarry Smith 
2913bb7acecfSBarry 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.
2914bb7acecfSBarry Smith 
2915bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
2916bb7acecfSBarry Smith 
2917bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
2918bb7acecfSBarry Smith 
2919bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
29209a42bb27SBarry Smith 
292101729b5cSPatrick Sanan     Level: intermediate
29229a42bb27SBarry Smith 
2923bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
29249a42bb27SBarry Smith 
29259a42bb27SBarry Smith @*/
29269371c9d4SSatish Balay PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g) {
29277128ae9fSMatthew G Knepley   PetscSF                 sf;
292884330215SMatthew G. Knepley   PetscSection            s, gs;
2929d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2930ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2931ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2932ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2933fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2934d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
29359a42bb27SBarry Smith 
29369a42bb27SBarry Smith   PetscFunctionBegin;
2937171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2938d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
29391baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
2940d4d07f1eSToby Isaac   }
29419566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
29429566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
29439566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
29447128ae9fSMatthew G Knepley   switch (mode) {
29457128ae9fSMatthew G Knepley   case INSERT_VALUES:
29467128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
29479371c9d4SSatish Balay   case INSERT_BC_VALUES: isInsert = PETSC_TRUE; break;
29487128ae9fSMatthew G Knepley   case ADD_VALUES:
29497128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
29509371c9d4SSatish Balay   case ADD_BC_VALUES: isInsert = PETSC_FALSE; break;
29519371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
29527128ae9fSMatthew G Knepley   }
2953ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
29549566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
2955ca3d3a14SMatthew G. Knepley     if (transform) {
29569566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
29579566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
29589566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
29599566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
2960fa88e482SJed Brown     } else if (isInsert) {
29619566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
2962fa88e482SJed Brown     } else {
29639566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
2964fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2965ca3d3a14SMatthew G. Knepley     }
2966fa88e482SJed Brown     if (s && isInsert) {
29679566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
2968fa88e482SJed Brown     } else {
29699566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
2970fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2971fa88e482SJed Brown     }
2972ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
29739566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
297484330215SMatthew G. Knepley     } else if (s && isInsert) {
297584330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
297684330215SMatthew G. Knepley 
29779566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
29789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29799566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
298084330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2981b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
298284330215SMatthew G. Knepley 
29839566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
29849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
29859566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29869566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
29879566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
29889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
2989b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
299003442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
29917a8be351SBarry 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);
2992b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2993b3b16f48SMatthew G. Knepley         if (!gcdof) {
299484330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
2995b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2996b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
299784330215SMatthew G. Knepley           const PetscInt *cdofs;
299884330215SMatthew G. Knepley           PetscInt        cind = 0;
299984330215SMatthew G. Knepley 
30009566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3001b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
30029371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
30039371c9d4SSatish Balay               ++cind;
30049371c9d4SSatish Balay               continue;
30059371c9d4SSatish Balay             }
3006b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
300784330215SMatthew G. Knepley           }
30087a8be351SBarry 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);
300984330215SMatthew G. Knepley       }
3010ca3d3a14SMatthew G. Knepley     }
3011fa88e482SJed Brown     if (g_inplace) {
30129566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3013fa88e482SJed Brown     } else {
30149566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3015fa88e482SJed Brown     }
3016ca3d3a14SMatthew G. Knepley     if (transform) {
30179566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30189566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3019fa88e482SJed Brown     } else if (l_inplace) {
30209566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3021ca3d3a14SMatthew G. Knepley     } else {
30229566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3023ca3d3a14SMatthew G. Knepley     }
30247128ae9fSMatthew G Knepley   } else {
30259566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
30267128ae9fSMatthew G Knepley   }
30279a42bb27SBarry Smith   PetscFunctionReturn(0);
30289a42bb27SBarry Smith }
30299a42bb27SBarry Smith 
30309a42bb27SBarry Smith /*@
30319a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
303247c6ae99SBarry Smith 
3033d083f849SBarry Smith     Neighbor-wise Collective on dm
303447c6ae99SBarry Smith 
303547c6ae99SBarry Smith     Input Parameters:
3036bb7acecfSBarry Smith +   dm - the `DM` object
3037f6813fd5SJed Brown .   l - the local vector
3038bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3039f6813fd5SJed Brown -   g - the global vector
304047c6ae99SBarry Smith 
304101729b5cSPatrick Sanan     Level: intermediate
304247c6ae99SBarry Smith 
3043bb7acecfSBarry Smith     Note:
3044bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3045bb7acecfSBarry Smith 
3046bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
304747c6ae99SBarry Smith 
304847c6ae99SBarry Smith @*/
30499371c9d4SSatish Balay PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g) {
30507128ae9fSMatthew G Knepley   PetscSF                 sf;
305184330215SMatthew G. Knepley   PetscSection            s;
3052d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3053ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
305447c6ae99SBarry Smith 
305547c6ae99SBarry Smith   PetscFunctionBegin;
3056171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30579566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30589566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30597128ae9fSMatthew G Knepley   switch (mode) {
30607128ae9fSMatthew G Knepley   case INSERT_VALUES:
30619371c9d4SSatish Balay   case INSERT_ALL_VALUES: isInsert = PETSC_TRUE; break;
30627128ae9fSMatthew G Knepley   case ADD_VALUES:
30639371c9d4SSatish Balay   case ADD_ALL_VALUES: isInsert = PETSC_FALSE; break;
30649371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30657128ae9fSMatthew G Knepley   }
306684330215SMatthew G. Knepley   if (sf && !isInsert) {
3067ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3068ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3069ca3d3a14SMatthew G. Knepley     Vec                tmpl;
307084330215SMatthew G. Knepley 
30719566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3072ca3d3a14SMatthew G. Knepley     if (transform) {
30739566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30749566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3075ca3d3a14SMatthew G. Knepley     } else {
30769566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3077ca3d3a14SMatthew G. Knepley     }
30789566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
30799566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3080ca3d3a14SMatthew G. Knepley     if (transform) {
30819566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
30829566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3083ca3d3a14SMatthew G. Knepley     } else {
30849566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3085ca3d3a14SMatthew G. Knepley     }
30869566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
308784330215SMatthew G. Knepley   } else if (s && isInsert) {
30887128ae9fSMatthew G Knepley   } else {
30899566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
30907128ae9fSMatthew G Knepley   }
3091d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30929566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3093d4d07f1eSToby Isaac   }
309447c6ae99SBarry Smith   PetscFunctionReturn(0);
309547c6ae99SBarry Smith }
309647c6ae99SBarry Smith 
3097f089877aSRichard Tran Mills /*@
3098bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3099bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3100bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3101f089877aSRichard Tran Mills 
3102d083f849SBarry Smith    Neighbor-wise Collective on dm
3103f089877aSRichard Tran Mills 
3104f089877aSRichard Tran Mills    Input Parameters:
3105bb7acecfSBarry Smith +  dm - the `DM` object
3106bc0a1609SRichard Tran Mills .  g - the original local vector
3107bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3108f089877aSRichard Tran Mills 
3109bc0a1609SRichard Tran Mills    Output Parameter:
3110bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3111f089877aSRichard Tran Mills 
3112f089877aSRichard Tran Mills    Level: intermediate
3113f089877aSRichard Tran Mills 
3114bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3115f089877aSRichard Tran Mills 
3116f089877aSRichard Tran Mills @*/
31179371c9d4SSatish Balay PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l) {
3118f089877aSRichard Tran Mills   PetscFunctionBegin;
3119f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31209566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
3121f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3122f089877aSRichard Tran Mills }
3123f089877aSRichard Tran Mills 
3124f089877aSRichard Tran Mills /*@
3125bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3126bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3127f089877aSRichard Tran Mills 
3128d083f849SBarry Smith    Neighbor-wise Collective on dm
3129f089877aSRichard Tran Mills 
3130f089877aSRichard Tran Mills    Input Parameters:
3131bb7acecfSBarry Smith +  da - the `DM` object
3132bc0a1609SRichard Tran Mills .  g - the original local vector
3133bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3134f089877aSRichard Tran Mills 
3135bc0a1609SRichard Tran Mills    Output Parameter:
3136bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3137f089877aSRichard Tran Mills 
3138f089877aSRichard Tran Mills    Level: intermediate
3139f089877aSRichard Tran Mills 
3140bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3141f089877aSRichard Tran Mills 
3142f089877aSRichard Tran Mills @*/
31439371c9d4SSatish Balay PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l) {
3144f089877aSRichard Tran Mills   PetscFunctionBegin;
3145f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31469566063dSJacob Faibussowitsch   PetscCall((*dm->ops->localtolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
3147f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3148f089877aSRichard Tran Mills }
3149f089877aSRichard Tran Mills 
315047c6ae99SBarry Smith /*@
3151bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
315247c6ae99SBarry Smith 
3153d083f849SBarry Smith     Collective on dm
315447c6ae99SBarry Smith 
3155d8d19677SJose E. Roman     Input Parameters:
3156bb7acecfSBarry Smith +   dm - the `DM` object
3157bb7acecfSBarry Smith -   comm - the communicator to contain the new `DM` object (or MPI_COMM_NULL)
315847c6ae99SBarry Smith 
315947c6ae99SBarry Smith     Output Parameter:
3160bb7acecfSBarry Smith .   dmc - the coarsened `DM`
316147c6ae99SBarry Smith 
316247c6ae99SBarry Smith     Level: developer
316347c6ae99SBarry Smith 
3164bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
316547c6ae99SBarry Smith 
316647c6ae99SBarry Smith @*/
31679371c9d4SSatish Balay PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc) {
3168b17ce1afSJed Brown   DMCoarsenHookLink link;
316947c6ae99SBarry Smith 
317047c6ae99SBarry Smith   PetscFunctionBegin;
3171171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3173dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3174b9d85ea2SLisandro Dalcin   if (*dmc) {
3175a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
31769566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
317743842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31789566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3179644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
31800598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3181656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
31829566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3183b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
31849566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3185b17ce1afSJed Brown     }
3186b9d85ea2SLisandro Dalcin   }
31879566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
31887a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3189b17ce1afSJed Brown   PetscFunctionReturn(0);
3190b17ce1afSJed Brown }
3191b17ce1afSJed Brown 
3192bb9467b5SJed Brown /*@C
3193b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3194b17ce1afSJed Brown 
3195bb7acecfSBarry Smith    Logically Collective on fine
3196b17ce1afSJed Brown 
31974165533cSJose E. Roman    Input Parameters:
3198bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3199b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3200bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
32010298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3202b17ce1afSJed Brown 
3203b17ce1afSJed Brown    Calling sequence of coarsenhook:
3204b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3205b17ce1afSJed Brown 
3206bb7acecfSBarry Smith +  fine - fine level `DM`
3207bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3208b17ce1afSJed Brown -  ctx - optional user-defined function context
3209b17ce1afSJed Brown 
3210b17ce1afSJed Brown    Calling sequence for restricthook:
3211c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3212bb7acecfSBarry Smith $
3213bb7acecfSBarry Smith +  fine - fine level `DM`
3214bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3215c833c3b5SJed Brown .  rscale - scaling vector for restriction
3216c833c3b5SJed Brown .  inject - matrix restricting by injection
3217b17ce1afSJed Brown .  coarse - coarse level DM to update
3218b17ce1afSJed Brown -  ctx - optional user-defined function context
3219b17ce1afSJed Brown 
3220b17ce1afSJed Brown    Level: advanced
3221b17ce1afSJed Brown 
3222b17ce1afSJed Brown    Notes:
3223bb7acecfSBarry 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`.
3224b17ce1afSJed Brown 
3225b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3226b17ce1afSJed Brown 
3227b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3228bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3229b17ce1afSJed Brown 
3230bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3231bb7acecfSBarry Smith 
3232bb7acecfSBarry Smith    Fortran Note:
3233bb7acecfSBarry Smith    This function is not available from Fortran.
3234bb9467b5SJed Brown 
3235db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3236b17ce1afSJed Brown @*/
32379371c9d4SSatish Balay PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx) {
3238b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3239b17ce1afSJed Brown 
3240b17ce1afSJed Brown   PetscFunctionBegin;
3241b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
32421e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
32431e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32441e3d8eccSJed Brown   }
32459566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3246b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3247b17ce1afSJed Brown   link->restricthook = restricthook;
3248b17ce1afSJed Brown   link->ctx          = ctx;
32490298fd71SBarry Smith   link->next         = NULL;
3250b17ce1afSJed Brown   *p                 = link;
3251b17ce1afSJed Brown   PetscFunctionReturn(0);
3252b17ce1afSJed Brown }
3253b17ce1afSJed Brown 
3254dc822a44SJed Brown /*@C
3255bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3256dc822a44SJed Brown 
3257bb7acecfSBarry Smith    Logically Collective on fine
3258dc822a44SJed Brown 
32594165533cSJose E. Roman    Input Parameters:
3260bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3261dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3262bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3263dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3264dc822a44SJed Brown 
3265dc822a44SJed Brown    Level: advanced
3266dc822a44SJed Brown 
3267bb7acecfSBarry Smith    Note:
3268dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3269dc822a44SJed Brown 
3270bb7acecfSBarry Smith    Fortran Note:
3271bb7acecfSBarry Smith    This function is not available from Fortran.
3272dc822a44SJed Brown 
3273db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3274dc822a44SJed Brown @*/
32759371c9d4SSatish Balay PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx) {
3276dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3277dc822a44SJed Brown 
3278dc822a44SJed Brown   PetscFunctionBegin;
3279dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3280dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3281dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3282dc822a44SJed Brown       link = *p;
3283dc822a44SJed Brown       *p   = link->next;
32849566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3285dc822a44SJed Brown       break;
3286dc822a44SJed Brown     }
3287dc822a44SJed Brown   }
3288dc822a44SJed Brown   PetscFunctionReturn(0);
3289dc822a44SJed Brown }
3290dc822a44SJed Brown 
3291b17ce1afSJed Brown /*@
3292bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3293b17ce1afSJed Brown 
3294b17ce1afSJed Brown    Collective if any hooks are
3295b17ce1afSJed Brown 
32964165533cSJose E. Roman    Input Parameters:
3297bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3298bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3299e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3300bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3301e91eccc2SStefano Zampini -  coarse - coarser DM to update
3302b17ce1afSJed Brown 
3303b17ce1afSJed Brown    Level: developer
3304b17ce1afSJed Brown 
3305bb7acecfSBarry Smith    Developer Note:
3306bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3307bb7acecfSBarry Smith 
3308bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3309b17ce1afSJed Brown @*/
33109371c9d4SSatish Balay PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse) {
3311b17ce1afSJed Brown   DMCoarsenHookLink link;
3312b17ce1afSJed Brown 
3313b17ce1afSJed Brown   PetscFunctionBegin;
3314b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
33151baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3316b17ce1afSJed Brown   }
331747c6ae99SBarry Smith   PetscFunctionReturn(0);
331847c6ae99SBarry Smith }
331947c6ae99SBarry Smith 
3320bb9467b5SJed Brown /*@C
3321be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
33225dbd56e3SPeter Brune 
3323d083f849SBarry Smith    Logically Collective on global
33245dbd56e3SPeter Brune 
33254165533cSJose E. Roman    Input Parameters:
3326bb7acecfSBarry Smith +  global - global `DM`
3327bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
33285dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
33290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
33305dbd56e3SPeter Brune 
3331ec4806b8SPeter Brune    Calling sequence for ddhook:
3332ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3333ec4806b8SPeter Brune 
3334bb7acecfSBarry Smith +  global - global `DM`
3335bb7acecfSBarry Smith .  block  - block `DM`
3336ec4806b8SPeter Brune -  ctx - optional user-defined function context
3337ec4806b8SPeter Brune 
33385dbd56e3SPeter Brune    Calling sequence for restricthook:
3339ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33405dbd56e3SPeter Brune 
3341bb7acecfSBarry Smith +  global - global `DM`
33425dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33435dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3344bb7acecfSBarry Smith .  block  - block `DM`
33455dbd56e3SPeter Brune -  ctx - optional user-defined function context
33465dbd56e3SPeter Brune 
33475dbd56e3SPeter Brune    Level: advanced
33485dbd56e3SPeter Brune 
33495dbd56e3SPeter Brune    Notes:
3350bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
33515dbd56e3SPeter Brune 
33525dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33535dbd56e3SPeter Brune 
33545dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3355bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
33565dbd56e3SPeter Brune 
3357bb7acecfSBarry Smith    Fortran Note:
3358bb7acecfSBarry Smith    This function is not available from Fortran.
3359bb9467b5SJed Brown 
3360bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
33615dbd56e3SPeter Brune @*/
33629371c9d4SSatish Balay PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx) {
3363be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
33645dbd56e3SPeter Brune 
33655dbd56e3SPeter Brune   PetscFunctionBegin;
33665dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3367b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
3368b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3369b3a6b972SJed Brown   }
33709566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
33715dbd56e3SPeter Brune   link->restricthook = restricthook;
3372be081cd6SPeter Brune   link->ddhook       = ddhook;
33735dbd56e3SPeter Brune   link->ctx          = ctx;
33740298fd71SBarry Smith   link->next         = NULL;
33755dbd56e3SPeter Brune   *p                 = link;
33765dbd56e3SPeter Brune   PetscFunctionReturn(0);
33775dbd56e3SPeter Brune }
33785dbd56e3SPeter Brune 
3379b3a6b972SJed Brown /*@C
3380b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3381b3a6b972SJed Brown 
3382bb7acecfSBarry Smith    Logically Collective on global
3383b3a6b972SJed Brown 
33844165533cSJose E. Roman    Input Parameters:
3385bb7acecfSBarry Smith +  global - global `DM`
3386bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3387b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3388b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3389b3a6b972SJed Brown 
3390b3a6b972SJed Brown    Level: advanced
3391b3a6b972SJed Brown 
3392bb7acecfSBarry Smith    Fortran Note:
3393bb7acecfSBarry Smith    This function is not available from Fortran.
3394b3a6b972SJed Brown 
3395db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3396b3a6b972SJed Brown @*/
33979371c9d4SSatish Balay PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx) {
3398b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3399b3a6b972SJed Brown 
3400b3a6b972SJed Brown   PetscFunctionBegin;
3401b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3402b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3403b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3404b3a6b972SJed Brown       link = *p;
3405b3a6b972SJed Brown       *p   = link->next;
34069566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3407b3a6b972SJed Brown       break;
3408b3a6b972SJed Brown     }
3409b3a6b972SJed Brown   }
3410b3a6b972SJed Brown   PetscFunctionReturn(0);
3411b3a6b972SJed Brown }
3412b3a6b972SJed Brown 
34135dbd56e3SPeter Brune /*@
3414bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
34155dbd56e3SPeter Brune 
34165dbd56e3SPeter Brune    Collective if any hooks are
34175dbd56e3SPeter Brune 
34184165533cSJose E. Roman    Input Parameters:
3419bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3420be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3421be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3422bb7acecfSBarry Smith -  coarse - coarser `DM` to update
34235dbd56e3SPeter Brune 
34245dbd56e3SPeter Brune    Level: developer
34255dbd56e3SPeter Brune 
3426db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
34275dbd56e3SPeter Brune @*/
34289371c9d4SSatish Balay PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm) {
3429be081cd6SPeter Brune   DMSubDomainHookLink link;
34305dbd56e3SPeter Brune 
34315dbd56e3SPeter Brune   PetscFunctionBegin;
3432be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
34331baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
34345dbd56e3SPeter Brune   }
34355dbd56e3SPeter Brune   PetscFunctionReturn(0);
34365dbd56e3SPeter Brune }
34375dbd56e3SPeter Brune 
34385fe1f584SPeter Brune /*@
3439bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
34405fe1f584SPeter Brune 
34415fe1f584SPeter Brune     Not Collective
34425fe1f584SPeter Brune 
34435fe1f584SPeter Brune     Input Parameter:
3444bb7acecfSBarry Smith .   dm - the `DM` object
34455fe1f584SPeter Brune 
34465fe1f584SPeter Brune     Output Parameter:
34476a7d9d85SPeter Brune .   level - number of coarsenings
34485fe1f584SPeter Brune 
34495fe1f584SPeter Brune     Level: developer
34505fe1f584SPeter Brune 
3451bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34525fe1f584SPeter Brune 
34535fe1f584SPeter Brune @*/
34549371c9d4SSatish Balay PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level) {
34555fe1f584SPeter Brune   PetscFunctionBegin;
34565fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3457b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
34585fe1f584SPeter Brune   *level = dm->leveldown;
34595fe1f584SPeter Brune   PetscFunctionReturn(0);
34605fe1f584SPeter Brune }
34615fe1f584SPeter Brune 
34629a64c4a8SMatthew G. Knepley /*@
3463bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
34649a64c4a8SMatthew G. Knepley 
3465bb7acecfSBarry Smith     Collective on dm
34669a64c4a8SMatthew G. Knepley 
34679a64c4a8SMatthew G. Knepley     Input Parameters:
3468bb7acecfSBarry Smith +   dm - the `DM` object
34699a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34709a64c4a8SMatthew G. Knepley 
34719a64c4a8SMatthew G. Knepley     Level: developer
34729a64c4a8SMatthew G. Knepley 
3473bb7acecfSBarry Smith     Note:
3474bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3475bb7acecfSBarry Smith 
3476bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
34779a64c4a8SMatthew G. Knepley @*/
34789371c9d4SSatish Balay PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level) {
34799a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34809a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34819a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34829a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34839a64c4a8SMatthew G. Knepley }
34849a64c4a8SMatthew G. Knepley 
348547c6ae99SBarry Smith /*@C
3486bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
348747c6ae99SBarry Smith 
3488d083f849SBarry Smith     Collective on dm
348947c6ae99SBarry Smith 
3490d8d19677SJose E. Roman     Input Parameters:
3491bb7acecfSBarry Smith +   dm - the `DM` object
349247c6ae99SBarry Smith -   nlevels - the number of levels of refinement
349347c6ae99SBarry Smith 
349447c6ae99SBarry Smith     Output Parameter:
3495bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
349647c6ae99SBarry Smith 
349747c6ae99SBarry Smith     Level: developer
349847c6ae99SBarry Smith 
3499bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
350047c6ae99SBarry Smith 
350147c6ae99SBarry Smith @*/
35029371c9d4SSatish Balay PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[]) {
350347c6ae99SBarry Smith   PetscFunctionBegin;
3504171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35057a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
350647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3507b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
350847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3509dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
351047c6ae99SBarry Smith   } else if (dm->ops->refine) {
351147c6ae99SBarry Smith     PetscInt i;
351247c6ae99SBarry Smith 
35139566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
351448a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3515ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
351647c6ae99SBarry Smith   PetscFunctionReturn(0);
351747c6ae99SBarry Smith }
351847c6ae99SBarry Smith 
351947c6ae99SBarry Smith /*@C
3520bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
352147c6ae99SBarry Smith 
3522d083f849SBarry Smith     Collective on dm
352347c6ae99SBarry Smith 
3524d8d19677SJose E. Roman     Input Parameters:
3525bb7acecfSBarry Smith +   dm - the `DM` object
352647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
352747c6ae99SBarry Smith 
352847c6ae99SBarry Smith     Output Parameter:
3529bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
353047c6ae99SBarry Smith 
353147c6ae99SBarry Smith     Level: developer
353247c6ae99SBarry Smith 
3533bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
353447c6ae99SBarry Smith 
353547c6ae99SBarry Smith @*/
35369371c9d4SSatish Balay PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[]) {
353747c6ae99SBarry Smith   PetscFunctionBegin;
3538171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35397a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
354047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
354147c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
354247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3543dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
354447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
354547c6ae99SBarry Smith     PetscInt i;
354647c6ae99SBarry Smith 
35479566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
354848a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3549ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
355047c6ae99SBarry Smith   PetscFunctionReturn(0);
355147c6ae99SBarry Smith }
355247c6ae99SBarry Smith 
35531a266240SBarry Smith /*@C
3554bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
35551a266240SBarry Smith 
3556bb7acecfSBarry Smith     Logically Collective if the function is collective
35571a266240SBarry Smith 
35581a266240SBarry Smith     Input Parameters:
3559bb7acecfSBarry Smith +   dm - the `DM` object
35601a266240SBarry Smith -   destroy - the destroy function
35611a266240SBarry Smith 
35621a266240SBarry Smith     Level: intermediate
35631a266240SBarry Smith 
3564bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
35651a266240SBarry Smith 
3566f07f9ceaSJed Brown @*/
35679371c9d4SSatish Balay PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **)) {
35681a266240SBarry Smith   PetscFunctionBegin;
3569171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35701a266240SBarry Smith   dm->ctxdestroy = destroy;
35711a266240SBarry Smith   PetscFunctionReturn(0);
35721a266240SBarry Smith }
35731a266240SBarry Smith 
3574b07ff414SBarry Smith /*@
3575bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
357647c6ae99SBarry Smith 
357747c6ae99SBarry Smith     Not Collective
357847c6ae99SBarry Smith 
357947c6ae99SBarry Smith     Input Parameters:
3580bb7acecfSBarry Smith +   dm - the `DM` object
358147c6ae99SBarry Smith -   ctx - the user context
358247c6ae99SBarry Smith 
358347c6ae99SBarry Smith     Level: intermediate
358447c6ae99SBarry Smith 
3585bb7acecfSBarry Smith     Note:
3586bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3587bb7acecfSBarry Smith 
3588bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
358947c6ae99SBarry Smith 
359047c6ae99SBarry Smith @*/
35919371c9d4SSatish Balay PetscErrorCode DMSetApplicationContext(DM dm, void *ctx) {
359247c6ae99SBarry Smith   PetscFunctionBegin;
3593171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
359447c6ae99SBarry Smith   dm->ctx = ctx;
359547c6ae99SBarry Smith   PetscFunctionReturn(0);
359647c6ae99SBarry Smith }
359747c6ae99SBarry Smith 
359847c6ae99SBarry Smith /*@
3599bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
360047c6ae99SBarry Smith 
360147c6ae99SBarry Smith     Not Collective
360247c6ae99SBarry Smith 
360347c6ae99SBarry Smith     Input Parameter:
3604bb7acecfSBarry Smith .   dm - the `DM` object
360547c6ae99SBarry Smith 
360647c6ae99SBarry Smith     Output Parameter:
360747c6ae99SBarry Smith .   ctx - the user context
360847c6ae99SBarry Smith 
360947c6ae99SBarry Smith     Level: intermediate
361047c6ae99SBarry Smith 
3611bb7acecfSBarry Smith     Note:
3612bb7acecfSBarry Smith     A user context is a way to pass problem specific information that is accessable whenever the `DM` is available
3613bb7acecfSBarry Smith 
3614bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
361547c6ae99SBarry Smith 
361647c6ae99SBarry Smith @*/
36179371c9d4SSatish Balay PetscErrorCode DMGetApplicationContext(DM dm, void *ctx) {
361847c6ae99SBarry Smith   PetscFunctionBegin;
3619171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36201b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
362147c6ae99SBarry Smith   PetscFunctionReturn(0);
362247c6ae99SBarry Smith }
362347c6ae99SBarry Smith 
362408da532bSDmitry Karpeev /*@C
3625bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
362608da532bSDmitry Karpeev 
3627d083f849SBarry Smith     Logically Collective on dm
362808da532bSDmitry Karpeev 
3629d8d19677SJose E. Roman     Input Parameters:
363008da532bSDmitry Karpeev +   dm - the DM object
36310298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
363208da532bSDmitry Karpeev 
363308da532bSDmitry Karpeev     Level: intermediate
363408da532bSDmitry Karpeev 
3635bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3636db781477SPatrick Sanan          `DMSetJacobian()`
363708da532bSDmitry Karpeev 
363808da532bSDmitry Karpeev @*/
36399371c9d4SSatish Balay PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec)) {
364008da532bSDmitry Karpeev   PetscFunctionBegin;
36415a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
364208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
364308da532bSDmitry Karpeev   PetscFunctionReturn(0);
364408da532bSDmitry Karpeev }
364508da532bSDmitry Karpeev 
364608da532bSDmitry Karpeev /*@
3647bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
364808da532bSDmitry Karpeev 
364908da532bSDmitry Karpeev     Not Collective
365008da532bSDmitry Karpeev 
365108da532bSDmitry Karpeev     Input Parameter:
3652bb7acecfSBarry Smith .   dm - the `DM` object to destroy
365308da532bSDmitry Karpeev 
365408da532bSDmitry Karpeev     Output Parameter:
3655bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
365608da532bSDmitry Karpeev 
365708da532bSDmitry Karpeev     Level: developer
365808da532bSDmitry Karpeev 
3659bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
366008da532bSDmitry Karpeev 
366108da532bSDmitry Karpeev @*/
36629371c9d4SSatish Balay PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg) {
366308da532bSDmitry Karpeev   PetscFunctionBegin;
36645a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3665534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
366608da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
366708da532bSDmitry Karpeev   PetscFunctionReturn(0);
366808da532bSDmitry Karpeev }
366908da532bSDmitry Karpeev 
367008da532bSDmitry Karpeev /*@C
3671bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
367208da532bSDmitry Karpeev 
3673d083f849SBarry Smith     Logically Collective on dm
367408da532bSDmitry Karpeev 
3675f899ff85SJose E. Roman     Input Parameter:
3676bb7acecfSBarry Smith .   dm - the `DM` object
367708da532bSDmitry Karpeev 
367808da532bSDmitry Karpeev     Output parameters:
367908da532bSDmitry Karpeev +   xl - lower bound
368008da532bSDmitry Karpeev -   xu - upper bound
368108da532bSDmitry Karpeev 
3682907376e6SBarry Smith     Level: advanced
3683907376e6SBarry Smith 
368495452b02SPatrick Sanan     Notes:
368595452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
368608da532bSDmitry Karpeev 
3687bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
368808da532bSDmitry Karpeev 
368908da532bSDmitry Karpeev @*/
36909371c9d4SSatish Balay PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu) {
369108da532bSDmitry Karpeev   PetscFunctionBegin;
36925a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
36945a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3695dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
369608da532bSDmitry Karpeev   PetscFunctionReturn(0);
369708da532bSDmitry Karpeev }
369808da532bSDmitry Karpeev 
3699b0ae01b7SPeter Brune /*@
3700bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3701b0ae01b7SPeter Brune 
3702b0ae01b7SPeter Brune     Not Collective
3703b0ae01b7SPeter Brune 
3704b0ae01b7SPeter Brune     Input Parameter:
3705b0ae01b7SPeter Brune .   dm - the DM object
3706b0ae01b7SPeter Brune 
3707b0ae01b7SPeter Brune     Output Parameter:
3708bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3709b0ae01b7SPeter Brune 
3710b0ae01b7SPeter Brune     Level: developer
3711b0ae01b7SPeter Brune 
3712bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3713b0ae01b7SPeter Brune 
3714b0ae01b7SPeter Brune @*/
37159371c9d4SSatish Balay PetscErrorCode DMHasColoring(DM dm, PetscBool *flg) {
3716b0ae01b7SPeter Brune   PetscFunctionBegin;
37175a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3718534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3719b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3720b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3721b0ae01b7SPeter Brune }
3722b0ae01b7SPeter Brune 
37233ad4599aSBarry Smith /*@
3724bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
37253ad4599aSBarry Smith 
37263ad4599aSBarry Smith     Not Collective
37273ad4599aSBarry Smith 
37283ad4599aSBarry Smith     Input Parameter:
3729bb7acecfSBarry Smith .   dm - the `DM` object
37303ad4599aSBarry Smith 
37313ad4599aSBarry Smith     Output Parameter:
3732bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
37333ad4599aSBarry Smith 
37343ad4599aSBarry Smith     Level: developer
37353ad4599aSBarry Smith 
3736bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
37373ad4599aSBarry Smith 
37383ad4599aSBarry Smith @*/
37399371c9d4SSatish Balay PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg) {
37403ad4599aSBarry Smith   PetscFunctionBegin;
37415a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3742534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
37433ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37443ad4599aSBarry Smith   PetscFunctionReturn(0);
37453ad4599aSBarry Smith }
37463ad4599aSBarry Smith 
3747a7058e45SLawrence Mitchell /*@
3748bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3749a7058e45SLawrence Mitchell 
3750a7058e45SLawrence Mitchell     Not Collective
3751a7058e45SLawrence Mitchell 
3752a7058e45SLawrence Mitchell     Input Parameter:
3753bb7acecfSBarry Smith .   dm - the `DM` object
3754a7058e45SLawrence Mitchell 
3755a7058e45SLawrence Mitchell     Output Parameter:
3756bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3757a7058e45SLawrence Mitchell 
3758a7058e45SLawrence Mitchell     Level: developer
3759a7058e45SLawrence Mitchell 
3760bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3761a7058e45SLawrence Mitchell 
3762a7058e45SLawrence Mitchell @*/
37639371c9d4SSatish Balay PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg) {
3764a7058e45SLawrence Mitchell   PetscFunctionBegin;
37655a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3766534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3767dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3768ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
3769a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3770a7058e45SLawrence Mitchell }
3771a7058e45SLawrence Mitchell 
37720298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3773264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3774264ace61SBarry Smith 
3775264ace61SBarry Smith /*@C
3776bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3777264ace61SBarry Smith 
3778d083f849SBarry Smith   Collective on dm
3779264ace61SBarry Smith 
3780264ace61SBarry Smith   Input Parameters:
3781bb7acecfSBarry Smith + dm     - The `DM` object
3782bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3783264ace61SBarry Smith 
3784264ace61SBarry Smith   Options Database Key:
3785bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3786264ace61SBarry Smith 
3787264ace61SBarry Smith   Level: intermediate
3788264ace61SBarry Smith 
3789bb7acecfSBarry Smith   Note:
3790bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3791bb7acecfSBarry Smith 
3792bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3793264ace61SBarry Smith @*/
37949371c9d4SSatish Balay PetscErrorCode DMSetType(DM dm, DMType method) {
3795264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3796264ace61SBarry Smith   PetscBool match;
3797264ace61SBarry Smith 
3798264ace61SBarry Smith   PetscFunctionBegin;
3799264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38009566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
3801264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3802264ace61SBarry Smith 
38039566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
38049566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
38057a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3806264ace61SBarry Smith 
3807dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
38089566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
38099566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
38109566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3811264ace61SBarry Smith   PetscFunctionReturn(0);
3812264ace61SBarry Smith }
3813264ace61SBarry Smith 
3814264ace61SBarry Smith /*@C
3815bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3816264ace61SBarry Smith 
3817264ace61SBarry Smith   Not Collective
3818264ace61SBarry Smith 
3819264ace61SBarry Smith   Input Parameter:
3820bb7acecfSBarry Smith . dm  - The `DM`
3821264ace61SBarry Smith 
3822264ace61SBarry Smith   Output Parameter:
3823bb7acecfSBarry Smith . type - The `DMType` name
3824264ace61SBarry Smith 
3825264ace61SBarry Smith   Level: intermediate
3826264ace61SBarry Smith 
3827bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3828264ace61SBarry Smith @*/
38299371c9d4SSatish Balay PetscErrorCode DMGetType(DM dm, DMType *type) {
3830264ace61SBarry Smith   PetscFunctionBegin;
3831264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3832c959eef4SJed Brown   PetscValidPointer(type, 2);
38339566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3834264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3835264ace61SBarry Smith   PetscFunctionReturn(0);
3836264ace61SBarry Smith }
3837264ace61SBarry Smith 
383867a56275SMatthew G Knepley /*@C
3839bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
384067a56275SMatthew G Knepley 
3841d083f849SBarry Smith   Collective on dm
384267a56275SMatthew G Knepley 
384367a56275SMatthew G Knepley   Input Parameters:
3844bb7acecfSBarry Smith + dm - the `DM`
3845bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
384667a56275SMatthew G Knepley 
384767a56275SMatthew G Knepley   Output Parameter:
3848bb7acecfSBarry Smith . M - pointer to new `DM`
384967a56275SMatthew G Knepley 
385067a56275SMatthew G Knepley   Notes:
3851bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3852bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3853bb7acecfSBarry Smith   of the input `DM`.
385467a56275SMatthew G Knepley 
385567a56275SMatthew G Knepley   Level: intermediate
385667a56275SMatthew G Knepley 
3857bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
385867a56275SMatthew G Knepley @*/
38599371c9d4SSatish Balay PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M) {
386067a56275SMatthew G Knepley   DM        B;
386167a56275SMatthew G Knepley   char      convname[256];
3862c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
386367a56275SMatthew G Knepley 
386467a56275SMatthew G Knepley   PetscFunctionBegin;
386567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
386667a56275SMatthew G Knepley   PetscValidType(dm, 1);
386767a56275SMatthew G Knepley   PetscValidPointer(M, 3);
38689566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
38699566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3870c067b6caSMatthew G. Knepley   if (sametype) {
3871c067b6caSMatthew G. Knepley     *M = dm;
38729566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
3873c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3874c067b6caSMatthew G. Knepley   } else {
38750298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
387667a56275SMatthew G Knepley 
387767a56275SMatthew G Knepley     /*
387867a56275SMatthew G Knepley        Order of precedence:
387967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
388067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
388167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
388267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
388367a56275SMatthew G Knepley        5) Use a really basic converter.
388467a56275SMatthew G Knepley     */
388567a56275SMatthew G Knepley 
388667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
38879566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
38889566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
38899566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
38909566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
38919566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
38929566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
389367a56275SMatthew G Knepley     if (conv) goto foundconv;
389467a56275SMatthew G Knepley 
389567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
38969566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
38979566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
38989566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
38999566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39009566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39019566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39029566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39039566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
390467a56275SMatthew G Knepley     if (conv) {
39059566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
390667a56275SMatthew G Knepley       goto foundconv;
390767a56275SMatthew G Knepley     }
390867a56275SMatthew G Knepley 
390967a56275SMatthew G Knepley #if 0
391067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
391167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
39129566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
391367a56275SMatthew G Knepley     if (conv) goto foundconv;
391467a56275SMatthew G Knepley 
391567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
391667a56275SMatthew G Knepley     if (dm->ops->convert) {
391767a56275SMatthew G Knepley       conv = dm->ops->convert;
391867a56275SMatthew G Knepley     }
391967a56275SMatthew G Knepley     if (conv) goto foundconv;
392067a56275SMatthew G Knepley #endif
392167a56275SMatthew G Knepley 
392267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
392398921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
392467a56275SMatthew G Knepley 
392567a56275SMatthew G Knepley   foundconv:
39269566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
39279566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
392812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
392990b157c4SStefano Zampini     {
39304fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
39316858538eSMatthew G. Knepley 
39324fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
39334fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
3934c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
39359566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
39369566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
39379566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
39389566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
393912fa691eSMatthew G. Knepley     }
39409566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
394167a56275SMatthew G Knepley   }
39429566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
394367a56275SMatthew G Knepley   PetscFunctionReturn(0);
394467a56275SMatthew G Knepley }
3945264ace61SBarry Smith 
3946264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3947264ace61SBarry Smith 
3948264ace61SBarry Smith /*@C
3949bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
39501c84c290SBarry Smith 
39511c84c290SBarry Smith   Not Collective
39521c84c290SBarry Smith 
39531c84c290SBarry Smith   Input Parameters:
39541c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39551c84c290SBarry Smith - create_func - The creation routine itself
39561c84c290SBarry Smith 
39571c84c290SBarry Smith   Notes:
3958bb7acecfSBarry Smith   DMRegister() may be called multiple times to add several user-defined `DM`s
39591c84c290SBarry Smith 
39601c84c290SBarry Smith   Sample usage:
39611c84c290SBarry Smith .vb
3962bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39631c84c290SBarry Smith .ve
39641c84c290SBarry Smith 
39651c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39661c84c290SBarry Smith .vb
39671c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39681c84c290SBarry Smith     DMSetType(DM,"my_da");
39691c84c290SBarry Smith .ve
39701c84c290SBarry Smith    or at runtime via the option
39711c84c290SBarry Smith .vb
39721c84c290SBarry Smith     -da_type my_da
39731c84c290SBarry Smith .ve
3974264ace61SBarry Smith 
3975264ace61SBarry Smith   Level: advanced
39761c84c290SBarry Smith 
3977bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
39781c84c290SBarry Smith 
3979264ace61SBarry Smith @*/
39809371c9d4SSatish Balay PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM)) {
3981264ace61SBarry Smith   PetscFunctionBegin;
39829566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
39839566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
3984264ace61SBarry Smith   PetscFunctionReturn(0);
3985264ace61SBarry Smith }
3986264ace61SBarry Smith 
3987b859378eSBarry Smith /*@C
3988bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
3989b859378eSBarry Smith 
3990d083f849SBarry Smith   Collective on viewer
3991b859378eSBarry Smith 
3992b859378eSBarry Smith   Input Parameters:
3993bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
3994bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
3995bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
3996bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
3997b859378eSBarry Smith 
3998b859378eSBarry Smith    Level: intermediate
3999b859378eSBarry Smith 
4000b859378eSBarry Smith   Notes:
400155849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4002b859378eSBarry Smith 
4003bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4004bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4005bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4006cd7e8a5eSksagiyam 
4007b859378eSBarry Smith   Notes for advanced users:
4008b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4009bb7acecfSBarry Smith   format, since `DMLoad()` and `DMView()` completely hide these details.
4010b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4011b859378eSBarry Smith   format is
4012b859378eSBarry Smith .vb
4013b859378eSBarry Smith      has not yet been determined
4014b859378eSBarry Smith .ve
4015b859378eSBarry Smith 
4016db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4017b859378eSBarry Smith @*/
40189371c9d4SSatish Balay PetscErrorCode DMLoad(DM newdm, PetscViewer viewer) {
40199331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4020b859378eSBarry Smith 
4021b859378eSBarry Smith   PetscFunctionBegin;
4022b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4023b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
40249566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
40259566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
40269566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
40279566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
40289331c7a4SMatthew G. Knepley   if (isbinary) {
40299331c7a4SMatthew G. Knepley     PetscInt classid;
40309331c7a4SMatthew G. Knepley     char     type[256];
4031b859378eSBarry Smith 
40329566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
40337a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
40349566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
40359566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4036dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
40379331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4038dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
40399331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
40409566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
4041b859378eSBarry Smith   PetscFunctionReturn(0);
4042b859378eSBarry Smith }
4043b859378eSBarry Smith 
40447da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40457da65231SMatthew G Knepley 
40469371c9d4SSatish Balay PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
40471d47ebbbSSatish Balay   PetscInt f;
40481b30c384SMatthew G Knepley 
40497da65231SMatthew G Knepley   PetscFunctionBegin;
405063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
405148a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
40527da65231SMatthew G Knepley   PetscFunctionReturn(0);
40537da65231SMatthew G Knepley }
40547da65231SMatthew G Knepley 
40559371c9d4SSatish Balay PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
40561b30c384SMatthew G Knepley   PetscInt f, g;
40577da65231SMatthew G Knepley 
40587da65231SMatthew G Knepley   PetscFunctionBegin;
405963a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
40601d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
40619566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
406248a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
40639566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
40647da65231SMatthew G Knepley   }
40657da65231SMatthew G Knepley   PetscFunctionReturn(0);
40667da65231SMatthew G Knepley }
4067e7c4fc90SDmitry Karpeev 
40689371c9d4SSatish Balay PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X) {
40690c5b8624SToby Isaac   PetscInt           localSize, bs;
40700c5b8624SToby Isaac   PetscMPIInt        size;
40710c5b8624SToby Isaac   Vec                x, xglob;
40720c5b8624SToby Isaac   const PetscScalar *xarray;
4073e759306cSMatthew G. Knepley 
4074e759306cSMatthew G. Knepley   PetscFunctionBegin;
40759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
40769566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
40779566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
40789566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
40799566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
40800c5b8624SToby Isaac   if (size > 1) {
40819566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
40829566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
40839566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
40849566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
40850c5b8624SToby Isaac   } else {
40860c5b8624SToby Isaac     xglob = x;
40870c5b8624SToby Isaac   }
40889566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
40890c5b8624SToby Isaac   if (size > 1) {
40909566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
40919566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
40920c5b8624SToby Isaac   }
40939566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4094e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4095e759306cSMatthew G. Knepley }
4096e759306cSMatthew G. Knepley 
409788ed4aceSMatthew G Knepley /*@
4098bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4099061576a5SJed Brown 
4100061576a5SJed Brown   Input Parameter:
4101bb7acecfSBarry Smith . dm - The `DM`
4102061576a5SJed Brown 
4103061576a5SJed Brown   Output Parameter:
4104bb7acecfSBarry Smith . section - The `PetscSection`
4105061576a5SJed Brown 
4106061576a5SJed Brown   Options Database Keys:
4107bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4108061576a5SJed Brown 
4109061576a5SJed Brown   Level: advanced
4110061576a5SJed Brown 
4111061576a5SJed Brown   Notes:
4112bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4113061576a5SJed Brown 
4114bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4115061576a5SJed Brown 
4116db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4117061576a5SJed Brown @*/
41189371c9d4SSatish Balay PetscErrorCode DMGetSection(DM dm, PetscSection *section) {
4119061576a5SJed Brown   PetscFunctionBegin;
41209566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
4121061576a5SJed Brown   PetscFunctionReturn(0);
4122061576a5SJed Brown }
4123061576a5SJed Brown 
4124061576a5SJed Brown /*@
4125bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
412688ed4aceSMatthew G Knepley 
412788ed4aceSMatthew G Knepley   Input Parameter:
4128bb7acecfSBarry Smith . dm - The `DM`
412988ed4aceSMatthew G Knepley 
413088ed4aceSMatthew G Knepley   Output Parameter:
4131bb7acecfSBarry Smith . section - The `PetscSection`
413288ed4aceSMatthew G Knepley 
4133e5893cccSMatthew G. Knepley   Options Database Keys:
4134bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4135e5893cccSMatthew G. Knepley 
413688ed4aceSMatthew G Knepley   Level: intermediate
413788ed4aceSMatthew G Knepley 
4138bb7acecfSBarry Smith   Note:
4139bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
414088ed4aceSMatthew G Knepley 
4141db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
414288ed4aceSMatthew G Knepley @*/
41439371c9d4SSatish Balay PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section) {
414488ed4aceSMatthew G Knepley   PetscFunctionBegin;
414588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
414688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41471bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4148e5e52638SMatthew G. Knepley     PetscInt d;
4149e5e52638SMatthew G. Knepley 
415045480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
415145480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
415245480ffeSMatthew G. Knepley       PetscViewer       viewer;
415345480ffeSMatthew G. Knepley       PetscViewerFormat format;
415445480ffeSMatthew G. Knepley       PetscBool         flg;
415545480ffeSMatthew G. Knepley 
41569566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
41579566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
415845480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
41599566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
41609566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
416145480ffeSMatthew G. Knepley       }
416245480ffeSMatthew G. Knepley       if (flg) {
41639566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
41649566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
41659566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
416645480ffeSMatthew G. Knepley       }
416745480ffeSMatthew G. Knepley     }
4168dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
41699566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
41702f0f8703SMatthew G. Knepley   }
41711bb6d2a8SBarry Smith   *section = dm->localSection;
417288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
417388ed4aceSMatthew G Knepley }
417488ed4aceSMatthew G Knepley 
417588ed4aceSMatthew G Knepley /*@
4176bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4177061576a5SJed Brown 
4178061576a5SJed Brown   Input Parameters:
4179bb7acecfSBarry Smith + dm - The `DM`
4180bb7acecfSBarry Smith - section - The `PetscSection`
4181061576a5SJed Brown 
4182061576a5SJed Brown   Level: advanced
4183061576a5SJed Brown 
4184061576a5SJed Brown   Notes:
4185bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4186061576a5SJed Brown 
4187bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4188061576a5SJed Brown 
4189db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4190061576a5SJed Brown @*/
41919371c9d4SSatish Balay PetscErrorCode DMSetSection(DM dm, PetscSection section) {
4192061576a5SJed Brown   PetscFunctionBegin;
41939566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
4194061576a5SJed Brown   PetscFunctionReturn(0);
4195061576a5SJed Brown }
4196061576a5SJed Brown 
4197061576a5SJed Brown /*@
4198bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
419988ed4aceSMatthew G Knepley 
420088ed4aceSMatthew G Knepley   Input Parameters:
4201bb7acecfSBarry Smith + dm - The `DM`
4202bb7acecfSBarry Smith - section - The `PetscSection`
420388ed4aceSMatthew G Knepley 
420488ed4aceSMatthew G Knepley   Level: intermediate
420588ed4aceSMatthew G Knepley 
4206bb7acecfSBarry Smith   Note:
4207bb7acecfSBarry Smith   Any existing Section will be destroyed
420888ed4aceSMatthew G Knepley 
4209bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
421088ed4aceSMatthew G Knepley @*/
42119371c9d4SSatish Balay PetscErrorCode DMSetLocalSection(DM dm, PetscSection section) {
4212c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4213af122d2aSMatthew G Knepley   PetscInt f;
421488ed4aceSMatthew G Knepley 
421588ed4aceSMatthew G Knepley   PetscFunctionBegin;
421688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4217b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
42199566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
42201bb6d2a8SBarry Smith   dm->localSection = section;
42219566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4222af122d2aSMatthew G Knepley   if (numFields) {
42239566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4224af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
42250f21e855SMatthew G. Knepley       PetscObject disc;
4226af122d2aSMatthew G Knepley       const char *name;
4227af122d2aSMatthew G Knepley 
42289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
42299566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
42309566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4231af122d2aSMatthew G Knepley     }
4232af122d2aSMatthew G Knepley   }
4233e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
42349566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
423588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
423688ed4aceSMatthew G Knepley }
423788ed4aceSMatthew G Knepley 
42389435951eSToby Isaac /*@
4239bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
42409435951eSToby Isaac 
4241e228b242SToby Isaac   not collective
4242e228b242SToby Isaac 
42439435951eSToby Isaac   Input Parameter:
4244bb7acecfSBarry Smith . dm - The `DM`
42459435951eSToby Isaac 
4246d8d19677SJose E. Roman   Output Parameters:
4247bb7acecfSBarry 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.
4248bb7acecfSBarry 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.
424979769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
42509435951eSToby Isaac 
42519435951eSToby Isaac   Level: advanced
42529435951eSToby Isaac 
4253bb7acecfSBarry Smith   Note:
4254bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
42559435951eSToby Isaac 
4256db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
42579435951eSToby Isaac @*/
42589371c9d4SSatish Balay PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias) {
42599435951eSToby Isaac   PetscFunctionBegin;
42609435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4261dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
42623b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
42633b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
426479769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
42659435951eSToby Isaac   PetscFunctionReturn(0);
42669435951eSToby Isaac }
42679435951eSToby Isaac 
42689435951eSToby Isaac /*@
4269bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
42709435951eSToby Isaac 
4271bb7acecfSBarry 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()`.
42729435951eSToby Isaac 
4273bb7acecfSBarry 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.
42749435951eSToby Isaac 
4275e228b242SToby Isaac   collective on dm
4276e228b242SToby Isaac 
42779435951eSToby Isaac   Input Parameters:
4278bb7acecfSBarry Smith + dm - The `DM`
4279bb7acecfSBarry 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).
4280bb7acecfSBarry 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).
4281bb7acecfSBarry 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).
42829435951eSToby Isaac 
42839435951eSToby Isaac   Level: advanced
42849435951eSToby Isaac 
4285bb7acecfSBarry Smith   Note:
4286bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
42879435951eSToby Isaac 
4288db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
42899435951eSToby Isaac @*/
42909371c9d4SSatish Balay PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias) {
4291e228b242SToby Isaac   PetscMPIInt result;
42929435951eSToby Isaac 
42939435951eSToby Isaac   PetscFunctionBegin;
42949435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4295e228b242SToby Isaac   if (section) {
4296e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42979566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
42987a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4299e228b242SToby Isaac   }
4300e228b242SToby Isaac   if (mat) {
4301e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
43029566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
43037a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4304e228b242SToby Isaac   }
430579769bd5SJed Brown   if (bias) {
430679769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
43079566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
430879769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
430979769bd5SJed Brown   }
43109566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43119566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
43123b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
43139566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
43149566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
43153b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
43169566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
43179566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
431879769bd5SJed Brown   dm->defaultConstraint.bias = bias;
43199435951eSToby Isaac   PetscFunctionReturn(0);
43209435951eSToby Isaac }
43219435951eSToby Isaac 
4322497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4323507e4973SMatthew G. Knepley /*
4324bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4325507e4973SMatthew G. Knepley 
4326507e4973SMatthew G. Knepley   Input Parameters:
4327bb7acecfSBarry Smith + dm - The `DM`
4328bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4329bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4330507e4973SMatthew G. Knepley 
4331507e4973SMatthew G. Knepley   Level: intermediate
4332507e4973SMatthew G. Knepley 
4333db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4334507e4973SMatthew G. Knepley */
43359371c9d4SSatish Balay static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection) {
4336507e4973SMatthew G. Knepley   MPI_Comm        comm;
4337507e4973SMatthew G. Knepley   PetscLayout     layout;
4338507e4973SMatthew G. Knepley   const PetscInt *ranges;
4339507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4340507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4341507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4342507e4973SMatthew G. Knepley 
4343507e4973SMatthew G. Knepley   PetscFunctionBegin;
43449566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4345507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
43479566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
43489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
43499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
43509566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
43519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
43529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
43539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
43549566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4355507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4356f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4357507e4973SMatthew G. Knepley 
43589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
43599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
43609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
43619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
43629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
43639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4364507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
43659371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
43669371c9d4SSatish 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));
43679371c9d4SSatish Balay       valid = PETSC_FALSE;
43689371c9d4SSatish Balay     }
43699371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
43709371c9d4SSatish 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));
43719371c9d4SSatish Balay       valid = PETSC_FALSE;
43729371c9d4SSatish Balay     }
4373507e4973SMatthew G. Knepley     if (gdof < 0) {
4374507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4375507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4376507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4377507e4973SMatthew G. Knepley 
43789566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4379507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
43809371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
43819371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
43829371c9d4SSatish Balay           valid = PETSC_FALSE;
43839371c9d4SSatish Balay           break;
43849371c9d4SSatish Balay         }
4385507e4973SMatthew G. Knepley       }
4386507e4973SMatthew G. Knepley     }
4387507e4973SMatthew G. Knepley   }
43889566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
43899566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
43901c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4391507e4973SMatthew G. Knepley   if (!gvalid) {
43929566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4393507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4394507e4973SMatthew G. Knepley   }
4395507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4396507e4973SMatthew G. Knepley }
4397f741bcd2SMatthew G. Knepley #endif
4398507e4973SMatthew G. Knepley 
439988ed4aceSMatthew G Knepley /*@
4400bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
440188ed4aceSMatthew G Knepley 
4402d083f849SBarry Smith   Collective on dm
44038b1ab98fSJed Brown 
440488ed4aceSMatthew G Knepley   Input Parameter:
4405bb7acecfSBarry Smith . dm - The `DM`
440688ed4aceSMatthew G Knepley 
440788ed4aceSMatthew G Knepley   Output Parameter:
4408bb7acecfSBarry Smith . section - The `PetscSection`
440988ed4aceSMatthew G Knepley 
441088ed4aceSMatthew G Knepley   Level: intermediate
441188ed4aceSMatthew G Knepley 
4412bb7acecfSBarry Smith   Note:
4413bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
441488ed4aceSMatthew G Knepley 
4415db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
441688ed4aceSMatthew G Knepley @*/
44179371c9d4SSatish Balay PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section) {
441888ed4aceSMatthew G Knepley   PetscFunctionBegin;
441988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44211bb6d2a8SBarry Smith   if (!dm->globalSection) {
4422fd59a867SMatthew G. Knepley     PetscSection s;
4423fd59a867SMatthew G. Knepley 
44249566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
44257a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
44267a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
44279566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
44289566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
44299566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
44309566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
443188ed4aceSMatthew G Knepley   }
44321bb6d2a8SBarry Smith   *section = dm->globalSection;
443388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
443488ed4aceSMatthew G Knepley }
443588ed4aceSMatthew G Knepley 
4436b21d0597SMatthew G Knepley /*@
4437bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4438b21d0597SMatthew G Knepley 
4439b21d0597SMatthew G Knepley   Input Parameters:
4440bb7acecfSBarry Smith + dm - The `DM`
44415080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4442b21d0597SMatthew G Knepley 
4443b21d0597SMatthew G Knepley   Level: intermediate
4444b21d0597SMatthew G Knepley 
4445bb7acecfSBarry Smith   Note:
4446bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4447b21d0597SMatthew G Knepley 
4448db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4449b21d0597SMatthew G Knepley @*/
44509371c9d4SSatish Balay PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section) {
4451b21d0597SMatthew G Knepley   PetscFunctionBegin;
4452b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44535080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44549566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44559566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
44561bb6d2a8SBarry Smith   dm->globalSection = section;
4457497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
44589566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4459507e4973SMatthew G. Knepley #endif
4460b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4461b21d0597SMatthew G Knepley }
4462b21d0597SMatthew G Knepley 
446388ed4aceSMatthew G Knepley /*@
4464bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4465bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
446688ed4aceSMatthew G Knepley 
446788ed4aceSMatthew G Knepley   Input Parameter:
4468bb7acecfSBarry Smith . dm - The `DM`
446988ed4aceSMatthew G Knepley 
447088ed4aceSMatthew G Knepley   Output Parameter:
4471bb7acecfSBarry Smith . sf - The `PetscSF`
447288ed4aceSMatthew G Knepley 
447388ed4aceSMatthew G Knepley   Level: intermediate
447488ed4aceSMatthew G Knepley 
4475bb7acecfSBarry Smith   Note:
4476bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
447788ed4aceSMatthew G Knepley 
4478db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
447988ed4aceSMatthew G Knepley @*/
44809371c9d4SSatish Balay PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf) {
448188ed4aceSMatthew G Knepley   PetscInt nroots;
448288ed4aceSMatthew G Knepley 
448388ed4aceSMatthew G Knepley   PetscFunctionBegin;
448488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
448588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
448648a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
44879566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
448888ed4aceSMatthew G Knepley   if (nroots < 0) {
448988ed4aceSMatthew G Knepley     PetscSection section, gSection;
449088ed4aceSMatthew G Knepley 
44919566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
449231ea6d37SMatthew G Knepley     if (section) {
44939566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
44949566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
449531ea6d37SMatthew G Knepley     } else {
44960298fd71SBarry Smith       *sf = NULL;
449731ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
449831ea6d37SMatthew G Knepley     }
449988ed4aceSMatthew G Knepley   }
45001bb6d2a8SBarry Smith   *sf = dm->sectionSF;
450188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
450288ed4aceSMatthew G Knepley }
450388ed4aceSMatthew G Knepley 
450488ed4aceSMatthew G Knepley /*@
4505bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
450688ed4aceSMatthew G Knepley 
450788ed4aceSMatthew G Knepley   Input Parameters:
4508bb7acecfSBarry Smith + dm - The `DM`
4509bb7acecfSBarry Smith - sf - The `PetscSF`
451088ed4aceSMatthew G Knepley 
451188ed4aceSMatthew G Knepley   Level: intermediate
451288ed4aceSMatthew G Knepley 
4513bb7acecfSBarry Smith   Note:
4514bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
451588ed4aceSMatthew G Knepley 
4516db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
451788ed4aceSMatthew G Knepley @*/
45189371c9d4SSatish Balay PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf) {
451988ed4aceSMatthew G Knepley   PetscFunctionBegin;
452088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4521b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45229566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
45239566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
45241bb6d2a8SBarry Smith   dm->sectionSF = sf;
452588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
452688ed4aceSMatthew G Knepley }
452788ed4aceSMatthew G Knepley 
452888ed4aceSMatthew G Knepley /*@C
4529bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
453088ed4aceSMatthew G Knepley   describing the data layout.
453188ed4aceSMatthew G Knepley 
453288ed4aceSMatthew G Knepley   Input Parameters:
4533bb7acecfSBarry Smith + dm - The `DM`
4534bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4535bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
453688ed4aceSMatthew G Knepley 
45371bb6d2a8SBarry Smith   Level: developer
45381bb6d2a8SBarry Smith 
4539bb7acecfSBarry Smith   Note:
4540bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4541bb7acecfSBarry Smith 
4542bb7acecfSBarry Smith   Developer Note:
4543bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4544bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4545bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
45461bb6d2a8SBarry Smith 
4547db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
454888ed4aceSMatthew G Knepley @*/
45499371c9d4SSatish Balay PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection) {
455088ed4aceSMatthew G Knepley   PetscFunctionBegin;
455188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45529566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
455388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
455488ed4aceSMatthew G Knepley }
4555af122d2aSMatthew G Knepley 
4556b21d0597SMatthew G Knepley /*@
4557bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4558bb7acecfSBarry Smith 
4559bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4560b21d0597SMatthew G Knepley 
4561b21d0597SMatthew G Knepley   Input Parameter:
4562bb7acecfSBarry Smith . dm - The `DM`
4563b21d0597SMatthew G Knepley 
4564b21d0597SMatthew G Knepley   Output Parameter:
4565bb7acecfSBarry Smith . sf - The `PetscSF`
4566b21d0597SMatthew G Knepley 
4567b21d0597SMatthew G Knepley   Level: intermediate
4568b21d0597SMatthew G Knepley 
4569bb7acecfSBarry Smith   Note:
4570bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4571b21d0597SMatthew G Knepley 
4572db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4573b21d0597SMatthew G Knepley @*/
45749371c9d4SSatish Balay PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
4575b21d0597SMatthew G Knepley   PetscFunctionBegin;
4576b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4577b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4578b21d0597SMatthew G Knepley   *sf = dm->sf;
4579b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4580b21d0597SMatthew G Knepley }
4581b21d0597SMatthew G Knepley 
4582057b4bcdSMatthew G Knepley /*@
4583bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4584bb7acecfSBarry Smith 
4585bb7acecfSBarry Smith   Collective on dm
4586057b4bcdSMatthew G Knepley 
4587057b4bcdSMatthew G Knepley   Input Parameters:
4588bb7acecfSBarry Smith + dm - The `DM`
4589bb7acecfSBarry Smith - sf - The` PetscSF`
4590057b4bcdSMatthew G Knepley 
4591057b4bcdSMatthew G Knepley   Level: intermediate
4592057b4bcdSMatthew G Knepley 
4593db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4594057b4bcdSMatthew G Knepley @*/
45959371c9d4SSatish Balay PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) {
4596057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4597057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4598b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
45999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46009566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4601057b4bcdSMatthew G Knepley   dm->sf = sf;
4602057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4603057b4bcdSMatthew G Knepley }
4604057b4bcdSMatthew G Knepley 
46054f37162bSMatthew G. Knepley /*@
4606bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
46074f37162bSMatthew G. Knepley 
46084f37162bSMatthew G. Knepley   Input Parameter:
4609bb7acecfSBarry Smith . dm - The `DM`
46104f37162bSMatthew G. Knepley 
46114f37162bSMatthew G. Knepley   Output Parameter:
4612bb7acecfSBarry Smith . sf - The `PetscSF`
46134f37162bSMatthew G. Knepley 
46144f37162bSMatthew G. Knepley   Level: intermediate
46154f37162bSMatthew G. Knepley 
4616bb7acecfSBarry Smith   Note:
4617bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
46184f37162bSMatthew G. Knepley 
4619db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46204f37162bSMatthew G. Knepley @*/
46219371c9d4SSatish Balay PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf) {
46224f37162bSMatthew G. Knepley   PetscFunctionBegin;
46234f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46244f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
46254f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
46264f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46274f37162bSMatthew G. Knepley }
46284f37162bSMatthew G. Knepley 
46294f37162bSMatthew G. Knepley /*@
46304f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
46314f37162bSMatthew G. Knepley 
46324f37162bSMatthew G. Knepley   Input Parameters:
46334f37162bSMatthew G. Knepley + dm - The DM
46344f37162bSMatthew G. Knepley - sf - The PetscSF
46354f37162bSMatthew G. Knepley 
46364f37162bSMatthew G. Knepley   Level: intermediate
46374f37162bSMatthew G. Knepley 
4638db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
46394f37162bSMatthew G. Knepley @*/
46409371c9d4SSatish Balay PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf) {
46414f37162bSMatthew G. Knepley   PetscFunctionBegin;
46424f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46434f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46459566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
46464f37162bSMatthew G. Knepley   dm->sfNatural = sf;
46474f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46484f37162bSMatthew G. Knepley }
46494f37162bSMatthew G. Knepley 
46509371c9d4SSatish Balay static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc) {
465134aa8a36SMatthew G. Knepley   PetscClassId id;
465234aa8a36SMatthew G. Knepley 
465334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
46549566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
465534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
46569566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
465734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
46589566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
465917c1d62eSMatthew G. Knepley   } else {
46609566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
466134aa8a36SMatthew G. Knepley   }
466234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
466334aa8a36SMatthew G. Knepley }
466434aa8a36SMatthew G. Knepley 
46659371c9d4SSatish Balay static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew) {
466644a7f3ddSMatthew G. Knepley   RegionField *tmpr;
466744a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
466844a7f3ddSMatthew G. Knepley 
466944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
467044a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
46719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
467244a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
46739371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
46749371c9d4SSatish Balay     tmpr[f].disc        = NULL;
46759371c9d4SSatish Balay     tmpr[f].label       = NULL;
46769371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
46779371c9d4SSatish Balay   }
46789566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
467944a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
468044a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
468144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
468244a7f3ddSMatthew G. Knepley }
468344a7f3ddSMatthew G. Knepley 
468444a7f3ddSMatthew G. Knepley /*@
468544a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
468644a7f3ddSMatthew G. Knepley 
4687d083f849SBarry Smith   Logically collective on dm
468844a7f3ddSMatthew G. Knepley 
468944a7f3ddSMatthew G. Knepley   Input Parameter:
469044a7f3ddSMatthew G. Knepley . dm - The DM
469144a7f3ddSMatthew G. Knepley 
469244a7f3ddSMatthew G. Knepley   Level: intermediate
469344a7f3ddSMatthew G. Knepley 
4694db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
469544a7f3ddSMatthew G. Knepley @*/
46969371c9d4SSatish Balay PetscErrorCode DMClearFields(DM dm) {
469744a7f3ddSMatthew G. Knepley   PetscInt f;
469844a7f3ddSMatthew G. Knepley 
469944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
470044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
470144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
47029566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
47039566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
470444a7f3ddSMatthew G. Knepley   }
47059566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
470644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
470744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
470844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
470944a7f3ddSMatthew G. Knepley }
471044a7f3ddSMatthew G. Knepley 
4711689b5837SMatthew G. Knepley /*@
4712689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4713689b5837SMatthew G. Knepley 
4714689b5837SMatthew G. Knepley   Not collective
4715689b5837SMatthew G. Knepley 
4716689b5837SMatthew G. Knepley   Input Parameter:
4717689b5837SMatthew G. Knepley . dm - The DM
4718689b5837SMatthew G. Knepley 
4719689b5837SMatthew G. Knepley   Output Parameter:
4720689b5837SMatthew G. Knepley . Nf - The number of fields
4721689b5837SMatthew G. Knepley 
4722689b5837SMatthew G. Knepley   Level: intermediate
4723689b5837SMatthew G. Knepley 
4724db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4725689b5837SMatthew G. Knepley @*/
47269371c9d4SSatish Balay PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields) {
47270f21e855SMatthew G. Knepley   PetscFunctionBegin;
47280f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4729534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
473044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4731af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4732af122d2aSMatthew G Knepley }
4733af122d2aSMatthew G Knepley 
4734689b5837SMatthew G. Knepley /*@
4735689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4736689b5837SMatthew G. Knepley 
4737d083f849SBarry Smith   Logically collective on dm
4738689b5837SMatthew G. Knepley 
4739689b5837SMatthew G. Knepley   Input Parameters:
4740689b5837SMatthew G. Knepley + dm - The DM
4741689b5837SMatthew G. Knepley - Nf - The number of fields
4742689b5837SMatthew G. Knepley 
4743689b5837SMatthew G. Knepley   Level: intermediate
4744689b5837SMatthew G. Knepley 
4745db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4746689b5837SMatthew G. Knepley @*/
47479371c9d4SSatish Balay PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields) {
47480f21e855SMatthew G. Knepley   PetscInt Nf, f;
4749af122d2aSMatthew G Knepley 
4750af122d2aSMatthew G Knepley   PetscFunctionBegin;
4751af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47529566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
47530f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47540f21e855SMatthew G. Knepley     PetscContainer obj;
47550f21e855SMatthew G. Knepley 
47569566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
47579566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
47589566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4759af122d2aSMatthew G Knepley   }
4760af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4761af122d2aSMatthew G Knepley }
4762af122d2aSMatthew G Knepley 
4763c1929be8SMatthew G. Knepley /*@
4764bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4765c1929be8SMatthew G. Knepley 
4766c1929be8SMatthew G. Knepley   Not collective
4767c1929be8SMatthew G. Knepley 
4768c1929be8SMatthew G. Knepley   Input Parameters:
4769bb7acecfSBarry Smith + dm - The `DM`
4770c1929be8SMatthew G. Knepley - f  - The field number
4771c1929be8SMatthew G. Knepley 
477244a7f3ddSMatthew G. Knepley   Output Parameters:
4773bb7acecfSBarry Smith + label - The label indicating the support of the field, or NULL for the entire mesh (pass in NULL if not needed)
4774bb7acecfSBarry Smith - disc - The discretization object (pass in NULL if not needed)
4775c1929be8SMatthew G. Knepley 
477644a7f3ddSMatthew G. Knepley   Level: intermediate
4777c1929be8SMatthew G. Knepley 
4778db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4779c1929be8SMatthew G. Knepley @*/
47809371c9d4SSatish Balay PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc) {
4781af122d2aSMatthew G Knepley   PetscFunctionBegin;
4782af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4783bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
47847a8be351SBarry 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);
478544a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4786bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
4787decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4788decb47aaSMatthew G. Knepley }
4789decb47aaSMatthew G. Knepley 
4790083401c6SMatthew G. Knepley /* Does not clear the DS */
47919371c9d4SSatish Balay PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc) {
4792083401c6SMatthew G. Knepley   PetscFunctionBegin;
47939566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
47949566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
47959566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4796083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4797bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
47989566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4799bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4800083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4801083401c6SMatthew G. Knepley }
4802083401c6SMatthew G. Knepley 
4803c1929be8SMatthew G. Knepley /*@
4804bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4805bb7acecfSBarry Smith   the field numbering.
4806c1929be8SMatthew G. Knepley 
4807d083f849SBarry Smith   Logically collective on dm
4808c1929be8SMatthew G. Knepley 
4809c1929be8SMatthew G. Knepley   Input Parameters:
4810bb7acecfSBarry Smith + dm    - The `DM`
4811c1929be8SMatthew G. Knepley . f     - The field number
481244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4813bb7acecfSBarry Smith - disc - The discretization object
4814c1929be8SMatthew G. Knepley 
481544a7f3ddSMatthew G. Knepley   Level: intermediate
4816c1929be8SMatthew G. Knepley 
4817db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4818c1929be8SMatthew G. Knepley @*/
48199371c9d4SSatish Balay PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc) {
4820decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4821decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4822e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4823bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
48247a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4825bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4826bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
48279566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
482844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
482944a7f3ddSMatthew G. Knepley }
483044a7f3ddSMatthew G. Knepley 
483144a7f3ddSMatthew G. Knepley /*@
4832bb7acecfSBarry 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)
4833bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
483444a7f3ddSMatthew G. Knepley 
4835d083f849SBarry Smith   Logically collective on dm
483644a7f3ddSMatthew G. Knepley 
483744a7f3ddSMatthew G. Knepley   Input Parameters:
4838bb7acecfSBarry Smith + dm    - The `DM`
483944a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4840bb7acecfSBarry Smith - disc - The discretization object
484144a7f3ddSMatthew G. Knepley 
484244a7f3ddSMatthew G. Knepley   Level: intermediate
484344a7f3ddSMatthew G. Knepley 
4844bb7acecfSBarry Smith   Notes:
4845bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4846bb7acecfSBarry Smith 
4847bb7acecfSBarry Smith   For example, a piecewise continous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4848bb7acecfSBarry 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
4849bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4850bb7acecfSBarry Smith 
4851bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
485244a7f3ddSMatthew G. Knepley @*/
48539371c9d4SSatish Balay PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc) {
485444a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
485544a7f3ddSMatthew G. Knepley 
485644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
485744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4858064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4859bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
48609566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
486144a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
4862bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
48639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4864bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4865bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
48669566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
4867af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4868af122d2aSMatthew G Knepley }
48696636e97aSMatthew G Knepley 
4870e5e52638SMatthew G. Knepley /*@
4871e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4872e0b68406SMatthew Knepley 
4873e0b68406SMatthew Knepley   Logically collective on dm
4874e0b68406SMatthew Knepley 
4875e0b68406SMatthew Knepley   Input Parameters:
4876bb7acecfSBarry Smith + dm          - The `DM`
4877e0b68406SMatthew Knepley . f           - The field index
4878bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
4879e0b68406SMatthew Knepley 
4880e0b68406SMatthew Knepley   Level: intermediate
4881e0b68406SMatthew Knepley 
4882db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4883e0b68406SMatthew Knepley @*/
48849371c9d4SSatish Balay PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor) {
4885e0b68406SMatthew Knepley   PetscFunctionBegin;
488663a3b9bcSJacob 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);
4887e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4888e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4889e0b68406SMatthew Knepley }
4890e0b68406SMatthew Knepley 
4891e0b68406SMatthew Knepley /*@
4892e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4893e0b68406SMatthew Knepley 
4894bb7acecfSBarry Smith   Not collective
4895e0b68406SMatthew Knepley 
4896e0b68406SMatthew Knepley   Input Parameters:
4897bb7acecfSBarry Smith + dm          - The `DM`
4898e0b68406SMatthew Knepley - f           - The field index
4899e0b68406SMatthew Knepley 
4900e0b68406SMatthew Knepley   Output Parameter:
4901e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4902e0b68406SMatthew Knepley 
4903e0b68406SMatthew Knepley   Level: intermediate
4904e0b68406SMatthew Knepley 
4905bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
4906e0b68406SMatthew Knepley @*/
49079371c9d4SSatish Balay PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor) {
4908e0b68406SMatthew Knepley   PetscFunctionBegin;
490963a3b9bcSJacob 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);
4910e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4911e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4912e0b68406SMatthew Knepley }
4913e0b68406SMatthew Knepley 
4914e0b68406SMatthew Knepley /*@
4915bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
4916e5e52638SMatthew G. Knepley 
4917d083f849SBarry Smith   Collective on dm
4918e5e52638SMatthew G. Knepley 
4919e5e52638SMatthew G. Knepley   Input Parameter:
4920bb7acecfSBarry Smith . dm - The `DM`
4921e5e52638SMatthew G. Knepley 
4922e5e52638SMatthew G. Knepley   Output Parameter:
4923bb7acecfSBarry Smith . newdm - The `DM`
4924e5e52638SMatthew G. Knepley 
4925e5e52638SMatthew G. Knepley   Level: advanced
4926e5e52638SMatthew G. Knepley 
4927db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
4928e5e52638SMatthew G. Knepley @*/
49299371c9d4SSatish Balay PetscErrorCode DMCopyFields(DM dm, DM newdm) {
4930e5e52638SMatthew G. Knepley   PetscInt Nf, f;
4931e5e52638SMatthew G. Knepley 
4932e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4933e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
49349566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49359566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
4936e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4937e5e52638SMatthew G. Knepley     DMLabel     label;
4938e5e52638SMatthew G. Knepley     PetscObject field;
493934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4940e5e52638SMatthew G. Knepley 
49419566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
49429566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
49439566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
49449566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
494534aa8a36SMatthew G. Knepley   }
494634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
494734aa8a36SMatthew G. Knepley }
494834aa8a36SMatthew G. Knepley 
494934aa8a36SMatthew G. Knepley /*@
495034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
495134aa8a36SMatthew G. Knepley 
495234aa8a36SMatthew G. Knepley   Not collective
495334aa8a36SMatthew G. Knepley 
495434aa8a36SMatthew G. Knepley   Input Parameters:
495534aa8a36SMatthew G. Knepley + dm - The DM object
495634aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
495734aa8a36SMatthew G. Knepley 
4958d8d19677SJose E. Roman   Output Parameters:
495934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
496034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
496134aa8a36SMatthew G. Knepley 
496234aa8a36SMatthew G. Knepley   Notes:
496334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
496434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
496534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4966979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
496734aa8a36SMatthew G. Knepley 
496834aa8a36SMatthew G. Knepley   Level: developer
496934aa8a36SMatthew G. Knepley 
4970db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
497134aa8a36SMatthew G. Knepley @*/
49729371c9d4SSatish Balay PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure) {
497334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
497434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4975534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
4976534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
497734aa8a36SMatthew G. Knepley   if (f < 0) {
497834aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
497934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
498034aa8a36SMatthew G. Knepley   } else {
498134aa8a36SMatthew G. Knepley     PetscInt Nf;
498234aa8a36SMatthew G. Knepley 
49839566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
49847a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
498534aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
498634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
498734aa8a36SMatthew G. Knepley   }
498834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
498934aa8a36SMatthew G. Knepley }
499034aa8a36SMatthew G. Knepley 
499134aa8a36SMatthew G. Knepley /*@
499234aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
499334aa8a36SMatthew G. Knepley 
499434aa8a36SMatthew G. Knepley   Not collective
499534aa8a36SMatthew G. Knepley 
499634aa8a36SMatthew G. Knepley   Input Parameters:
499734aa8a36SMatthew G. Knepley + dm         - The DM object
499834aa8a36SMatthew G. Knepley . f          - The field number
499934aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
500034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
500134aa8a36SMatthew G. Knepley 
500234aa8a36SMatthew G. Knepley   Notes:
500334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
500434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
500534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5006979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
500734aa8a36SMatthew G. Knepley 
500834aa8a36SMatthew G. Knepley   Level: developer
500934aa8a36SMatthew G. Knepley 
5010db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
501134aa8a36SMatthew G. Knepley @*/
50129371c9d4SSatish Balay PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure) {
501334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
501434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
501534aa8a36SMatthew G. Knepley   if (f < 0) {
501634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
501734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
501834aa8a36SMatthew G. Knepley   } else {
501934aa8a36SMatthew G. Knepley     PetscInt Nf;
502034aa8a36SMatthew G. Knepley 
50219566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
50227a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
502334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
502434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5025e5e52638SMatthew G. Knepley   }
5026e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5027e5e52638SMatthew G. Knepley }
5028e5e52638SMatthew G. Knepley 
5029b0441da4SMatthew G. Knepley /*@
5030b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5031b0441da4SMatthew G. Knepley 
5032b0441da4SMatthew G. Knepley   Not collective
5033b0441da4SMatthew G. Knepley 
5034f899ff85SJose E. Roman   Input Parameter:
5035b0441da4SMatthew G. Knepley . dm - The DM object
5036b0441da4SMatthew G. Knepley 
5037d8d19677SJose E. Roman   Output Parameters:
5038b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5039b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5040b0441da4SMatthew G. Knepley 
5041b0441da4SMatthew G. Knepley   Notes:
5042b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5043b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5044b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5045b0441da4SMatthew G. Knepley 
5046b0441da4SMatthew G. Knepley   Level: developer
5047b0441da4SMatthew G. Knepley 
5048db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5049b0441da4SMatthew G. Knepley @*/
50509371c9d4SSatish Balay PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure) {
5051b0441da4SMatthew G. Knepley   PetscInt Nf;
5052b0441da4SMatthew G. Knepley 
5053b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5054b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5055064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5056064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
50579566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5058b0441da4SMatthew G. Knepley   if (!Nf) {
50599566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5060b0441da4SMatthew G. Knepley   } else {
50619566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5062b0441da4SMatthew G. Knepley   }
5063b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5064b0441da4SMatthew G. Knepley }
5065b0441da4SMatthew G. Knepley 
5066b0441da4SMatthew G. Knepley /*@
5067b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5068b0441da4SMatthew G. Knepley 
5069b0441da4SMatthew G. Knepley   Not collective
5070b0441da4SMatthew G. Knepley 
5071b0441da4SMatthew G. Knepley   Input Parameters:
5072b0441da4SMatthew G. Knepley + dm         - The DM object
5073b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5074b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5075b0441da4SMatthew G. Knepley 
5076b0441da4SMatthew G. Knepley   Notes:
5077b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5078b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5079b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5080b0441da4SMatthew G. Knepley 
5081b0441da4SMatthew G. Knepley   Level: developer
5082b0441da4SMatthew G. Knepley 
5083db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5084b0441da4SMatthew G. Knepley @*/
50859371c9d4SSatish Balay PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure) {
5086b0441da4SMatthew G. Knepley   PetscInt Nf;
5087b0441da4SMatthew G. Knepley 
5088b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5089b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50909566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5091b0441da4SMatthew G. Knepley   if (!Nf) {
50929566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5093b0441da4SMatthew G. Knepley   } else {
50949566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5095e5e52638SMatthew G. Knepley   }
5096e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5097e5e52638SMatthew G. Knepley }
5098e5e52638SMatthew G. Knepley 
50999371c9d4SSatish Balay PetscErrorCode DMCompleteBCLabels_Internal(DM dm) {
5100799db056SMatthew G. Knepley   DM           plex;
5101799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5102799db056SMatthew G. Knepley   const char **names;
5103799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5104799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5105799db056SMatthew G. Knepley   size_t       len;
5106799db056SMatthew G. Knepley   MPI_Comm     comm;
5107799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5108783e2ec8SMatthew G. Knepley 
5109783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5110799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5111799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5112799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5113799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5114799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5115799db056SMatthew G. Knepley     PetscDS  dsBC;
5116799db056SMatthew G. Knepley     PetscInt numBd;
5117799db056SMatthew G. Knepley 
5118799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5119799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5120799db056SMatthew G. Knepley     maxLabels += numBd;
5121799db056SMatthew G. Knepley   }
5122799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5123799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5124799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5125799db056SMatthew G. Knepley     PetscDS  dsBC;
5126799db056SMatthew G. Knepley     PetscInt numBd, bd;
5127799db056SMatthew G. Knepley 
5128799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5129799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5130799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5131799db056SMatthew G. Knepley       DMLabel      label;
5132799db056SMatthew G. Knepley       PetscInt     field;
5133799db056SMatthew G. Knepley       PetscObject  obj;
5134799db056SMatthew G. Knepley       PetscClassId id;
5135799db056SMatthew G. Knepley 
5136799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
51379566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
51389566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5139799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
51409371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
51419371c9d4SSatish Balay         if (labels[l] == label) break;
5142799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5143783e2ec8SMatthew G. Knepley     }
5144799db056SMatthew G. Knepley   }
5145799db056SMatthew G. Knepley   /* Get label names */
5146799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5147799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
51489371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
51499371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
51509371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
51519371c9d4SSatish Balay   }
5152799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5153799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5154799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5155799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen * l], names[l]));
5156799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5157799db056SMatthew G. Knepley   /* Put all names on all processes */
5158799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5159799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5160799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5161799db056SMatthew G. Knepley   gNl = displs[size];
51629371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
51639371c9d4SSatish Balay     counts[p] *= gmaxLen;
51649371c9d4SSatish Balay     displs[p] *= gmaxLen;
51659371c9d4SSatish Balay   }
5166799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5167799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5168799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5169799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5170799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5171799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5172799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
51739371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
51749371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
51759566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5176799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
51779566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5178799db056SMatthew G. Knepley     ++gl;
5179783e2ec8SMatthew G. Knepley   }
5180799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5181783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5182783e2ec8SMatthew G. Knepley }
5183783e2ec8SMatthew G. Knepley 
51849371c9d4SSatish Balay static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew) {
5185e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5186e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5187e5e52638SMatthew G. Knepley 
5188e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5189e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
51909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5191e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
51929371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
51939371c9d4SSatish Balay     tmpd[s].ds     = NULL;
51949371c9d4SSatish Balay     tmpd[s].label  = NULL;
51959371c9d4SSatish Balay     tmpd[s].fields = NULL;
51969371c9d4SSatish Balay   }
51979566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5198e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5199e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5200e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5201e5e52638SMatthew G. Knepley }
5202e5e52638SMatthew G. Knepley 
5203e5e52638SMatthew G. Knepley /*@
5204e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5205e5e52638SMatthew G. Knepley 
5206e5e52638SMatthew G. Knepley   Not collective
5207e5e52638SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley   Input Parameter:
5209e5e52638SMatthew G. Knepley . dm - The DM
5210e5e52638SMatthew G. Knepley 
5211e5e52638SMatthew G. Knepley   Output Parameter:
5212e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5213e5e52638SMatthew G. Knepley 
5214e5e52638SMatthew G. Knepley   Level: intermediate
5215e5e52638SMatthew G. Knepley 
5216db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5217e5e52638SMatthew G. Knepley @*/
52189371c9d4SSatish Balay PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds) {
5219e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5220e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5221534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5222e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5223e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5224e5e52638SMatthew G. Knepley }
5225e5e52638SMatthew G. Knepley 
5226e5e52638SMatthew G. Knepley /*@
5227e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5228e5e52638SMatthew G. Knepley 
5229d083f849SBarry Smith   Logically collective on dm
5230e5e52638SMatthew G. Knepley 
5231e5e52638SMatthew G. Knepley   Input Parameter:
5232e5e52638SMatthew G. Knepley . dm - The DM
5233e5e52638SMatthew G. Knepley 
5234e5e52638SMatthew G. Knepley   Level: intermediate
5235e5e52638SMatthew G. Knepley 
5236db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5237e5e52638SMatthew G. Knepley @*/
52389371c9d4SSatish Balay PetscErrorCode DMClearDS(DM dm) {
5239e5e52638SMatthew G. Knepley   PetscInt s;
5240e5e52638SMatthew G. Knepley 
5241e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5242e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5243e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
52449566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
52459566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
52469566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5247e5e52638SMatthew G. Knepley   }
52489566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5249e5e52638SMatthew G. Knepley   dm->probs = NULL;
5250e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5251e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5252e5e52638SMatthew G. Knepley }
5253e5e52638SMatthew G. Knepley 
5254e5e52638SMatthew G. Knepley /*@
5255e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5256e5e52638SMatthew G. Knepley 
5257e5e52638SMatthew G. Knepley   Not collective
5258e5e52638SMatthew G. Knepley 
5259e5e52638SMatthew G. Knepley   Input Parameter:
5260e5e52638SMatthew G. Knepley . dm    - The DM
5261e5e52638SMatthew G. Knepley 
5262e5e52638SMatthew G. Knepley   Output Parameter:
5263e5e52638SMatthew G. Knepley . prob - The default PetscDS
5264e5e52638SMatthew G. Knepley 
5265e5e52638SMatthew G. Knepley   Level: intermediate
5266e5e52638SMatthew G. Knepley 
5267db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5268e5e52638SMatthew G. Knepley @*/
52699371c9d4SSatish Balay PetscErrorCode DMGetDS(DM dm, PetscDS *prob) {
5270e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5271e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5272e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5273b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5274b0143b4dSMatthew G. Knepley     PetscDS ds;
5275b0143b4dSMatthew G. Knepley 
52769566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
52779566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
52789566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5279b0143b4dSMatthew G. Knepley   }
5280b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5281e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5282e5e52638SMatthew G. Knepley }
5283e5e52638SMatthew G. Knepley 
5284e5e52638SMatthew G. Knepley /*@
5285e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5286e5e52638SMatthew G. Knepley 
5287e5e52638SMatthew G. Knepley   Not collective
5288e5e52638SMatthew G. Knepley 
5289e5e52638SMatthew G. Knepley   Input Parameters:
5290e5e52638SMatthew G. Knepley + dm    - The DM
5291e5e52638SMatthew G. Knepley - point - Cell for the DS
5292e5e52638SMatthew G. Knepley 
5293e5e52638SMatthew G. Knepley   Output Parameter:
5294e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5295e5e52638SMatthew G. Knepley 
5296e5e52638SMatthew G. Knepley   Level: developer
5297e5e52638SMatthew G. Knepley 
5298db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5299e5e52638SMatthew G. Knepley @*/
53009371c9d4SSatish Balay PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob) {
5301e5e52638SMatthew G. Knepley   PetscDS  probDef = NULL;
5302e5e52638SMatthew G. Knepley   PetscInt s;
5303e5e52638SMatthew G. Knepley 
5304e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5305e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5306e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
530763a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5308e5e52638SMatthew G. Knepley   *prob = NULL;
5309e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5310e5e52638SMatthew G. Knepley     PetscInt val;
5311e5e52638SMatthew G. Knepley 
53129371c9d4SSatish Balay     if (!dm->probs[s].label) {
53139371c9d4SSatish Balay       probDef = dm->probs[s].ds;
53149371c9d4SSatish Balay     } else {
53159566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
53169371c9d4SSatish Balay       if (val >= 0) {
53179371c9d4SSatish Balay         *prob = dm->probs[s].ds;
53189371c9d4SSatish Balay         break;
53199371c9d4SSatish Balay       }
5320e5e52638SMatthew G. Knepley     }
5321e5e52638SMatthew G. Knepley   }
5322e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5323e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5324e5e52638SMatthew G. Knepley }
5325e5e52638SMatthew G. Knepley 
5326e5e52638SMatthew G. Knepley /*@
5327e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5328e5e52638SMatthew G. Knepley 
5329e5e52638SMatthew G. Knepley   Not collective
5330e5e52638SMatthew G. Knepley 
5331e5e52638SMatthew G. Knepley   Input Parameters:
5332e5e52638SMatthew G. Knepley + dm    - The DM
5333e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5334e5e52638SMatthew G. Knepley 
5335b3cf3223SMatthew G. Knepley   Output Parameters:
5336b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5337b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5338e5e52638SMatthew G. Knepley 
5339154ca461SJed Brown   Note:
5340154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5341154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5342154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5343e5e52638SMatthew G. Knepley 
5344e5e52638SMatthew G. Knepley   Level: advanced
5345e5e52638SMatthew G. Knepley 
5346db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5347e5e52638SMatthew G. Knepley @*/
53489371c9d4SSatish Balay PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds) {
5349e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5350e5e52638SMatthew G. Knepley 
5351e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5352e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5353e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
53549371c9d4SSatish Balay   if (fields) {
53559371c9d4SSatish Balay     PetscValidPointer(fields, 3);
53569371c9d4SSatish Balay     *fields = NULL;
53579371c9d4SSatish Balay   }
53589371c9d4SSatish Balay   if (ds) {
53599371c9d4SSatish Balay     PetscValidPointer(ds, 4);
53609371c9d4SSatish Balay     *ds = NULL;
53619371c9d4SSatish Balay   }
5362e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5363154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5364b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5365b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
5366154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5367b3cf3223SMatthew G. Knepley     }
5368e5e52638SMatthew G. Knepley   }
53692df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5370e5e52638SMatthew G. Knepley }
5371e5e52638SMatthew G. Knepley 
5372e5e52638SMatthew G. Knepley /*@
5373bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5374083401c6SMatthew G. Knepley 
5375083401c6SMatthew G. Knepley   Collective on dm
5376083401c6SMatthew G. Knepley 
5377083401c6SMatthew G. Knepley   Input Parameters:
5378bb7acecfSBarry Smith + dm     - The `DM`
5379bb7acecfSBarry Smith . label  - The `DMLabel` defining the mesh region, or NULL for the entire mesh
5380bb7acecfSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or NULL for all fields
5381bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5382083401c6SMatthew G. Knepley 
5383bb7acecfSBarry Smith   Note:
5384bb7acecfSBarry 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,
5385083401c6SMatthew G. Knepley   the fields argument is ignored.
5386083401c6SMatthew G. Knepley 
5387083401c6SMatthew G. Knepley   Level: advanced
5388083401c6SMatthew G. Knepley 
5389db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5390083401c6SMatthew G. Knepley @*/
53919371c9d4SSatish Balay PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds) {
5392083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5393083401c6SMatthew G. Knepley 
5394083401c6SMatthew G. Knepley   PetscFunctionBegin;
5395083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5396083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5397064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5398083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5399083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
54009566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5401083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5402083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5403083401c6SMatthew G. Knepley     }
5404083401c6SMatthew G. Knepley   }
54059566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
54069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
54079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
54089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
5409083401c6SMatthew G. Knepley   if (!label) {
5410083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5411083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5412083401c6SMatthew G. Knepley     Nds = 0;
5413083401c6SMatthew G. Knepley   }
5414083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5415083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5416083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5417083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5418083401c6SMatthew G. Knepley }
5419083401c6SMatthew G. Knepley 
5420083401c6SMatthew G. Knepley /*@
5421e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5422e5e52638SMatthew G. Knepley 
5423e5e52638SMatthew G. Knepley   Not collective
5424e5e52638SMatthew G. Knepley 
5425e5e52638SMatthew G. Knepley   Input Parameters:
5426e5e52638SMatthew G. Knepley + dm  - The DM
5427e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5428e5e52638SMatthew G. Knepley 
5429e5e52638SMatthew G. Knepley   Output Parameters:
5430b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5431b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5432083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5433e5e52638SMatthew G. Knepley 
5434e5e52638SMatthew G. Knepley   Level: advanced
5435e5e52638SMatthew G. Knepley 
5436db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5437e5e52638SMatthew G. Knepley @*/
54389371c9d4SSatish Balay PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds) {
5439e5e52638SMatthew G. Knepley   PetscInt Nds;
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5442e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54439566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
544463a3b9bcSJacob 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);
5445e5e52638SMatthew G. Knepley   if (label) {
5446e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5447e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5448e5e52638SMatthew G. Knepley   }
5449b3cf3223SMatthew G. Knepley   if (fields) {
5450b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5451b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5452b3cf3223SMatthew G. Knepley   }
5453e5e52638SMatthew G. Knepley   if (ds) {
5454b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5455e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5456e5e52638SMatthew G. Knepley   }
5457e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5458e5e52638SMatthew G. Knepley }
5459e5e52638SMatthew G. Knepley 
5460e5e52638SMatthew G. Knepley /*@
5461083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5462e5e52638SMatthew G. Knepley 
5463083401c6SMatthew G. Knepley   Not collective
5464e5e52638SMatthew G. Knepley 
5465e5e52638SMatthew G. Knepley   Input Parameters:
5466e5e52638SMatthew G. Knepley + dm     - The DM
5467083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5468083401c6SMatthew G. Knepley . label  - The region label, or NULL
5469083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5470083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5471e5e52638SMatthew G. Knepley 
5472e5e52638SMatthew G. Knepley   Level: advanced
5473e5e52638SMatthew G. Knepley 
5474db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5475e5e52638SMatthew G. Knepley @*/
54769371c9d4SSatish Balay PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds) {
5477083401c6SMatthew G. Knepley   PetscInt Nds;
5478e5e52638SMatthew G. Knepley 
5479e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5480e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5481ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
54829566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
548363a3b9bcSJacob 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);
54849566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
54859566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5486083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5487083401c6SMatthew G. Knepley   if (fields) {
5488083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
54899566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
54909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5491083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5492e5e52638SMatthew G. Knepley   }
5493083401c6SMatthew G. Knepley   if (ds) {
5494083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
54959566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
54969566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5497083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5498083401c6SMatthew G. Knepley   }
5499e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5500e5e52638SMatthew G. Knepley }
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley /*@
55031d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55041d3af9e0SMatthew G. Knepley 
55051d3af9e0SMatthew G. Knepley   Not collective
55061d3af9e0SMatthew G. Knepley 
55071d3af9e0SMatthew G. Knepley   Input Parameters:
55081d3af9e0SMatthew G. Knepley + dm  - The DM
55091d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55101d3af9e0SMatthew G. Knepley 
55111d3af9e0SMatthew G. Knepley   Output Parameter:
55121d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55131d3af9e0SMatthew G. Knepley 
55141d3af9e0SMatthew G. Knepley   Level: advanced
55151d3af9e0SMatthew G. Knepley 
5516db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
55171d3af9e0SMatthew G. Knepley @*/
55189371c9d4SSatish Balay PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num) {
55191d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
55201d3af9e0SMatthew G. Knepley 
55211d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55221d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55231d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5524dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
55259566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
55269371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
55279371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
55281d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55291d3af9e0SMatthew G. Knepley   else *num = n;
55301d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55311d3af9e0SMatthew G. Knepley }
55321d3af9e0SMatthew G. Knepley 
55332df84da0SMatthew G. Knepley /*@C
5534bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
55352df84da0SMatthew G. Knepley 
55362df84da0SMatthew G. Knepley   Not collective
55372df84da0SMatthew G. Knepley 
5538f1a722f8SMatthew G. Knepley   Input Parameters:
5539bb7acecfSBarry Smith + dm     - The `DM`
55402df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5541bb7acecfSBarry Smith . prefix - The options prefix for the output `PetscFE`, or NULL
5542bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
55432df84da0SMatthew G. Knepley 
55442df84da0SMatthew G. Knepley   Output Parameter:
5545bb7acecfSBarry Smith . fem - The `PetscFE`
55462df84da0SMatthew G. Knepley 
5547bb7acecfSBarry Smith   Note:
5548bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
55492df84da0SMatthew G. Knepley 
55502df84da0SMatthew G. Knepley   Level: intermediate
55512df84da0SMatthew G. Knepley 
5552db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
55532df84da0SMatthew G. Knepley @*/
55549371c9d4SSatish Balay PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem) {
55552df84da0SMatthew G. Knepley   DMPolytopeType ct;
55562df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
55572df84da0SMatthew G. Knepley 
55582df84da0SMatthew G. Knepley   PetscFunctionBegin;
55592df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55602df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
55612df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
55622df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
55632df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
55649566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
55659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
55669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
55679566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
55682df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
55692df84da0SMatthew G. Knepley }
55702df84da0SMatthew G. Knepley 
55711d3af9e0SMatthew G. Knepley /*@
5572bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5573e5e52638SMatthew G. Knepley 
5574d083f849SBarry Smith   Collective on dm
5575e5e52638SMatthew G. Knepley 
5576e5e52638SMatthew G. Knepley   Input Parameter:
5577bb7acecfSBarry Smith . dm - The `DM`
5578e5e52638SMatthew G. Knepley 
557945480ffeSMatthew G. Knepley   Options Database Keys:
5580bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
558145480ffeSMatthew G. Knepley 
5582bb7acecfSBarry Smith   Note:
5583bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5584e5e52638SMatthew G. Knepley 
5585e5e52638SMatthew G. Knepley   Level: intermediate
5586e5e52638SMatthew G. Knepley 
5587db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5588e5e52638SMatthew G. Knepley @*/
55899371c9d4SSatish Balay PetscErrorCode DMCreateDS(DM dm) {
5590e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5591083401c6SMatthew G. Knepley   PetscDS   dsDef;
5592083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5593f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5594f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5595e5e52638SMatthew G. Knepley 
5596e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5597e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5598e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
55999566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
56009566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5601083401c6SMatthew G. Knepley   /* Determine how many regions we have */
56029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5603083401c6SMatthew G. Knepley   Nl   = 0;
5604083401c6SMatthew G. Knepley   Ndef = 0;
5605083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5606083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5607083401c6SMatthew G. Knepley     PetscInt l;
5608083401c6SMatthew G. Knepley 
5609f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5610f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5611f918ec44SMatthew G. Knepley     {
5612f918ec44SMatthew G. Knepley       PetscClassId id;
5613f918ec44SMatthew G. Knepley 
56149566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5615f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5616f918ec44SMatthew G. Knepley         Ceed ceed;
5617f918ec44SMatthew G. Knepley 
56189566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
56199566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5620f918ec44SMatthew G. Knepley       }
5621f918ec44SMatthew G. Knepley     }
5622f918ec44SMatthew G. Knepley #endif
56239371c9d4SSatish Balay     if (!label) {
56249371c9d4SSatish Balay       ++Ndef;
56259371c9d4SSatish Balay       continue;
56269371c9d4SSatish Balay     }
56279371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
56289371c9d4SSatish Balay       if (label == labelSet[l]) break;
5629083401c6SMatthew G. Knepley     if (l < Nl) continue;
5630083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5631083401c6SMatthew G. Knepley   }
5632083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
56339566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5634083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5635b3cf3223SMatthew G. Knepley     IS        fields;
5636b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5637b3cf3223SMatthew G. Knepley 
56389371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
56399371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
56407a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
56419566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
56429371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
56439371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
56449566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
56459566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
56469566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
56479566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
564888f0c812SMatthew G. Knepley 
56499566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56509566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
56519566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
56529566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
56532df9ee95SMatthew G. Knepley   }
56549566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
56559566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5656083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5657083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56580122748bSMatthew G. Knepley     DM              plex;
5659083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5660083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5661083401c6SMatthew G. Knepley     PetscInt       *fields;
5662083401c6SMatthew G. Knepley     const PetscInt *cells;
5663083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56640122748bSMatthew G. Knepley 
56659566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
56669566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
56679566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
56689566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
56695fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5670083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5671083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5672083401c6SMatthew G. Knepley       IS      pointIS;
5673083401c6SMatthew G. Knepley 
56749566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
56759566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
56769566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
56779566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5678083401c6SMatthew G. Knepley     }
56799566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5680083401c6SMatthew G. Knepley 
56819566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
56829566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
56839566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
56849566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
56859566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
56869566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
56879566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5688083401c6SMatthew G. Knepley 
56899566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
56909371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
56919371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
56929566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
56939566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
56949566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
56959566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5696083401c6SMatthew G. Knepley 
56979566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
56989566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
56999566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
57009566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
57019566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
57029566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
57039566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5704083401c6SMatthew G. Knepley   }
5705083401c6SMatthew G. Knepley   /* Create label DSes
5706083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5707083401c6SMatthew G. Knepley   */
5708083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5709083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5710083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5711083401c6SMatthew G. Knepley     PetscDS   ds;
5712083401c6SMatthew G. Knepley     IS        fields;
5713083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5714083401c6SMatthew G. Knepley 
57159566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
57169371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57179371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
57189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
57199371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57209371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
57219566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57229566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
57239566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
57249566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
57259566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
57269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
57279566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5728083401c6SMatthew G. Knepley     {
5729083401c6SMatthew G. Knepley       DMPolytopeType ct;
5730083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
57315fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
57320122748bSMatthew G. Knepley 
57339566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5734665f567fSMatthew G. Knepley       if (lStart >= 0) {
57359566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5736412e9a14SMatthew G. Knepley         switch (ct) {
5737412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5738412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5739412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
57409371c9d4SSatish Balay         case DM_POLYTOPE_QUAD_PRISM_TENSOR: isCohesiveLocal = PETSC_TRUE; break;
5741083401c6SMatthew G. Knepley         default: break;
5742412e9a14SMatthew G. Knepley         }
5743665f567fSMatthew G. Knepley       }
57449566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
57455fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
57465fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
57475fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
57489566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
57499566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
57505fedec97SMatthew G. Knepley           }
57515fedec97SMatthew G. Knepley           ++nf;
57525fedec97SMatthew G. Knepley         }
57535fedec97SMatthew G. Knepley       }
5754e5e52638SMatthew G. Knepley     }
57559566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5756e5e52638SMatthew G. Knepley   }
57579566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5758e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5759083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5760083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5761083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5762083401c6SMatthew G. Knepley     const PetscInt *fld;
57635fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
57645fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5765e5e52638SMatthew G. Knepley 
57669566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
57679566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
57689566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
57699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5770083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5771083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
57725fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5773e5e52638SMatthew G. Knepley       PetscClassId id;
5774e5e52638SMatthew G. Knepley 
57755fedec97SMatthew G. Knepley       /* Handle DS with no fields */
57769566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
57775fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
57789566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&disc));
57799566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5780083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
57819566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5782e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5783e5e52638SMatthew G. Knepley     }
57849566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5785e5e52638SMatthew G. Knepley   }
5786f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
57879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5788f9244615SMatthew G. Knepley   if (flg) {
57893b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
57903b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
57913b4aee56SMatthew G. Knepley       PetscInt Nf, f;
57923b4aee56SMatthew G. Knepley 
57939566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
57949566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
57953b4aee56SMatthew G. Knepley     }
5796f9244615SMatthew G. Knepley   }
5797e5e52638SMatthew G. Knepley   /* Setup DSes */
5798e5e52638SMatthew G. Knepley   if (doSetup) {
57999566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5800e5e52638SMatthew G. Knepley   }
5801e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5802e5e52638SMatthew G. Knepley }
5803e5e52638SMatthew G. Knepley 
5804e5e52638SMatthew G. Knepley /*@
5805bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
58067f96f943SMatthew G. Knepley 
5807bb7acecfSBarry Smith   Collective on `DM`
5808f2cacb80SMatthew G. Knepley 
58097f96f943SMatthew G. Knepley   Input Parameters:
5810bb7acecfSBarry Smith + dm   - The `DM`
58117f96f943SMatthew G. Knepley - time - The time
58127f96f943SMatthew G. Knepley 
58137f96f943SMatthew G. Knepley   Output Parameters:
5814f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5815f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
58167f96f943SMatthew G. Knepley 
5817bb7acecfSBarry Smith   Note:
5818bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
58197f96f943SMatthew G. Knepley 
58207f96f943SMatthew G. Knepley   Level: developer
58217f96f943SMatthew G. Knepley 
5822db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
58237f96f943SMatthew G. Knepley @*/
58249371c9d4SSatish Balay PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t) {
58257f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
58267f96f943SMatthew G. Knepley   void   **ectxs;
58277f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
58287f96f943SMatthew G. Knepley 
58297f96f943SMatthew G. Knepley   PetscFunctionBegin;
5830f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5831f2cacb80SMatthew G. Knepley   if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5832f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
58339566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
58349566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
58359566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58367f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
58377f96f943SMatthew G. Knepley     PetscDS         ds;
58387f96f943SMatthew G. Knepley     DMLabel         label;
58397f96f943SMatthew G. Knepley     IS              fieldIS;
58407f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58417f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58427f96f943SMatthew G. Knepley 
58439566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
58449566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
58459566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
58469566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
58479566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
5848f2cacb80SMatthew G. Knepley     if (u) {
58497f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58507f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58519566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
58527f96f943SMatthew G. Knepley       }
58539566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
58547f96f943SMatthew G. Knepley       if (label) {
58559566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
58567f96f943SMatthew G. Knepley       } else {
58579566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
58587f96f943SMatthew G. Knepley       }
58597f96f943SMatthew G. Knepley     }
5860f2cacb80SMatthew G. Knepley     if (u_t) {
58619566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
58629566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
5863f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5864f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
58659566063dSJacob Faibussowitsch         PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
5866f2cacb80SMatthew G. Knepley       }
58679566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(fieldIS, &fields));
5868f2cacb80SMatthew G. Knepley       if (label) {
58699566063dSJacob Faibussowitsch         PetscCall(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5870f2cacb80SMatthew G. Knepley       } else {
58719566063dSJacob Faibussowitsch         PetscCall(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5872f2cacb80SMatthew G. Knepley       }
5873f2cacb80SMatthew G. Knepley     }
5874f2cacb80SMatthew G. Knepley   }
5875f2cacb80SMatthew G. Knepley   if (u) {
58769566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
58779566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
5878f2cacb80SMatthew G. Knepley   }
5879f2cacb80SMatthew G. Knepley   if (u_t) {
58809566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
58819566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
5882f2cacb80SMatthew G. Knepley   }
58839566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
58847f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58857f96f943SMatthew G. Knepley }
58867f96f943SMatthew G. Knepley 
58879371c9d4SSatish Balay PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds) {
588845480ffeSMatthew G. Knepley   PetscDS    dsNew;
588945480ffeSMatthew G. Knepley   DSBoundary b;
58906a02485aSMatthew G. Knepley   PetscInt   cdim, Nf, f, d;
58915fedec97SMatthew G. Knepley   PetscBool  isCohesive;
589245480ffeSMatthew G. Knepley   void      *ctx;
589345480ffeSMatthew G. Knepley 
589445480ffeSMatthew G. Knepley   PetscFunctionBegin;
58959566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
58969566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
58979566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
58989566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
58999566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
59009566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
590145480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
59029566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
59039566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
59049566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
59059566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
59066a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
59076a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
590845480ffeSMatthew G. Knepley   }
590945480ffeSMatthew G. Knepley   if (Nf) {
59109566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
59119566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
591245480ffeSMatthew G. Knepley   }
59139566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
591445480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
59159566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
591645480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
59177a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
591845480ffeSMatthew G. Knepley   }
591945480ffeSMatthew G. Knepley 
59209566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
59219566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
592245480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
592345480ffeSMatthew G. Knepley }
592445480ffeSMatthew G. Knepley 
59257f96f943SMatthew G. Knepley /*@
5926bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
5927e5e52638SMatthew G. Knepley 
5928d083f849SBarry Smith   Collective on dm
5929e5e52638SMatthew G. Knepley 
5930e5e52638SMatthew G. Knepley   Input Parameter:
5931bb7acecfSBarry Smith . dm - The `DM`
5932e5e52638SMatthew G. Knepley 
5933e5e52638SMatthew G. Knepley   Output Parameter:
5934bb7acecfSBarry Smith . newdm - The `DM`
5935e5e52638SMatthew G. Knepley 
5936e5e52638SMatthew G. Knepley   Level: advanced
5937e5e52638SMatthew G. Knepley 
5938db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5939e5e52638SMatthew G. Knepley @*/
59409371c9d4SSatish Balay PetscErrorCode DMCopyDS(DM dm, DM newdm) {
5941e5e52638SMatthew G. Knepley   PetscInt Nds, s;
5942e5e52638SMatthew G. Knepley 
5943e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5944e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
59459566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
59469566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
5947e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5948e5e52638SMatthew G. Knepley     DMLabel  label;
5949b3cf3223SMatthew G. Knepley     IS       fields;
595045480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5951783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5952e5e52638SMatthew G. Knepley 
59539566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
5954b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
59559566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
595645480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
59579566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
59589566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
5959783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5960b8025e53SMatthew G. Knepley       PetscWeakForm wf;
596145480ffeSMatthew G. Knepley       DMLabel       label;
5962783e2ec8SMatthew G. Knepley       PetscInt      field;
5963783e2ec8SMatthew G. Knepley 
59649566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
59659566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
5966783e2ec8SMatthew G. Knepley     }
5967e5e52638SMatthew G. Knepley   }
5968799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
5969e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5970e5e52638SMatthew G. Knepley }
5971e5e52638SMatthew G. Knepley 
5972e5e52638SMatthew G. Knepley /*@
5973bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
5974e5e52638SMatthew G. Knepley 
5975d083f849SBarry Smith   Collective on dm
5976e5e52638SMatthew G. Knepley 
5977e5e52638SMatthew G. Knepley   Input Parameter:
5978bb7acecfSBarry Smith . dm - The `DM`
5979e5e52638SMatthew G. Knepley 
5980e5e52638SMatthew G. Knepley   Output Parameter:
5981bb7acecfSBarry Smith . newdm - The `DM`
5982e5e52638SMatthew G. Knepley 
5983e5e52638SMatthew G. Knepley   Level: advanced
5984e5e52638SMatthew G. Knepley 
5985bb7acecfSBarry Smith   Developer Note:
5986bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
5987bb7acecfSBarry Smith 
5988db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
5989e5e52638SMatthew G. Knepley @*/
59909371c9d4SSatish Balay PetscErrorCode DMCopyDisc(DM dm, DM newdm) {
5991e5e52638SMatthew G. Knepley   PetscFunctionBegin;
59929566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
59939566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
5994e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5995e5e52638SMatthew G. Knepley }
5996e5e52638SMatthew G. Knepley 
5997c73cfb54SMatthew G. Knepley /*@
5998bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
5999c73cfb54SMatthew G. Knepley 
6000c73cfb54SMatthew G. Knepley   Not collective
6001c73cfb54SMatthew G. Knepley 
6002c73cfb54SMatthew G. Knepley   Input Parameter:
6003bb7acecfSBarry Smith . dm - The `DM`
6004c73cfb54SMatthew G. Knepley 
6005c73cfb54SMatthew G. Knepley   Output Parameter:
6006c73cfb54SMatthew G. Knepley . dim - The topological dimension
6007c73cfb54SMatthew G. Knepley 
6008c73cfb54SMatthew G. Knepley   Level: beginner
6009c73cfb54SMatthew G. Knepley 
6010db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6011c73cfb54SMatthew G. Knepley @*/
60129371c9d4SSatish Balay PetscErrorCode DMGetDimension(DM dm, PetscInt *dim) {
6013c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6014c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6015534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6016c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6017c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6018c73cfb54SMatthew G. Knepley }
6019c73cfb54SMatthew G. Knepley 
6020c73cfb54SMatthew G. Knepley /*@
6021bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6022c73cfb54SMatthew G. Knepley 
6023c73cfb54SMatthew G. Knepley   Collective on dm
6024c73cfb54SMatthew G. Knepley 
6025c73cfb54SMatthew G. Knepley   Input Parameters:
6026bb7acecfSBarry Smith + dm - The `DM`
6027c73cfb54SMatthew G. Knepley - dim - The topological dimension
6028c73cfb54SMatthew G. Knepley 
6029c73cfb54SMatthew G. Knepley   Level: beginner
6030c73cfb54SMatthew G. Knepley 
6031db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6032c73cfb54SMatthew G. Knepley @*/
60339371c9d4SSatish Balay PetscErrorCode DMSetDimension(DM dm, PetscInt dim) {
6034e5e52638SMatthew G. Knepley   PetscDS  ds;
603545480ffeSMatthew G. Knepley   PetscInt Nds, n;
6036f17e8794SMatthew G. Knepley 
6037c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6038c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6039c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6040c73cfb54SMatthew G. Knepley   dm->dim = dim;
6041d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
60429566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
604345480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
60449566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
60459566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
604645480ffeSMatthew G. Knepley     }
6047d17bd122SMatthew G. Knepley   }
6048c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6049c73cfb54SMatthew G. Knepley }
6050c73cfb54SMatthew G. Knepley 
6051793f3fe5SMatthew G. Knepley /*@
6052793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6053793f3fe5SMatthew G. Knepley 
6054d083f849SBarry Smith   Collective on dm
6055793f3fe5SMatthew G. Knepley 
6056793f3fe5SMatthew G. Knepley   Input Parameters:
6057bb7acecfSBarry Smith + dm - the `DM`
6058793f3fe5SMatthew G. Knepley - dim - the dimension
6059793f3fe5SMatthew G. Knepley 
6060793f3fe5SMatthew G. Knepley   Output Parameters:
6061793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6062aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6063793f3fe5SMatthew G. Knepley 
6064793f3fe5SMatthew G. Knepley   Note:
6065793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6066a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6067793f3fe5SMatthew G. Knepley   then the interval is empty.
6068793f3fe5SMatthew G. Knepley 
6069793f3fe5SMatthew G. Knepley   Level: intermediate
6070793f3fe5SMatthew G. Knepley 
6071db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6072793f3fe5SMatthew G. Knepley @*/
60739371c9d4SSatish Balay PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd) {
6074793f3fe5SMatthew G. Knepley   PetscInt d;
6075793f3fe5SMatthew G. Knepley 
6076793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6077793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
60797a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6080dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
6081793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6082793f3fe5SMatthew G. Knepley }
6083793f3fe5SMatthew G. Knepley 
60846636e97aSMatthew G Knepley /*@
6085bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6086f4d763aaSMatthew G. Knepley 
60878f700142SStefano Zampini   Collective on dm
60888f700142SStefano Zampini 
6089f4d763aaSMatthew G. Knepley   Input Parameter:
6090bb7acecfSBarry Smith . dm - The original `DM`
6091f4d763aaSMatthew G. Knepley 
6092f4d763aaSMatthew G. Knepley   Output Parameter:
6093bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6094f4d763aaSMatthew G. Knepley 
6095f4d763aaSMatthew G. Knepley   Level: intermediate
6096f4d763aaSMatthew G. Knepley 
6097bb7acecfSBarry Smith   Note:
6098bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6099bb7acecfSBarry 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
6100bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6101bb7acecfSBarry Smith 
6102bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6103f4d763aaSMatthew G. Knepley @*/
61049371c9d4SSatish Balay PetscErrorCode DMGetOutputDM(DM dm, DM *odm) {
6105c26acbdeSMatthew G. Knepley   PetscSection section;
61062d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
610714f150ffSMatthew G. Knepley 
610814f150ffSMatthew G. Knepley   PetscFunctionBegin;
610914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
611014f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
61119566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
61129566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
61139566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
61142d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6115c26acbdeSMatthew G. Knepley     *odm = dm;
6116c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6117c26acbdeSMatthew G. Knepley   }
611814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6119c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
612014f150ffSMatthew G. Knepley     PetscSF      sf;
612114f150ffSMatthew G. Knepley 
61229566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
61239566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
61249566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
61259566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
61269566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
61279566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
61289566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
61299566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
61309566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
613114f150ffSMatthew G. Knepley   }
613214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
613314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
613414f150ffSMatthew G. Knepley }
6135f4d763aaSMatthew G. Knepley 
6136f4d763aaSMatthew G. Knepley /*@
6137cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6138f4d763aaSMatthew G. Knepley 
6139f4d763aaSMatthew G. Knepley   Input Parameter:
6140bb7acecfSBarry Smith . dm - The original `DM`
6141f4d763aaSMatthew G. Knepley 
6142cdb7a50dSMatthew G. Knepley   Output Parameters:
6143cdb7a50dSMatthew G. Knepley + num - The output sequence number
6144cdb7a50dSMatthew G. Knepley - val - The output sequence value
6145f4d763aaSMatthew G. Knepley 
6146f4d763aaSMatthew G. Knepley   Level: intermediate
6147f4d763aaSMatthew G. Knepley 
6148bb7acecfSBarry Smith   Note:
6149bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6150bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6151bb7acecfSBarry Smith 
6152bb7acecfSBarry Smith   Developer Note:
6153bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6154bb7acecfSBarry Smith   not directly related to the `DM`.
6155f4d763aaSMatthew G. Knepley 
6156db781477SPatrick Sanan .seealso: `VecView()`
6157f4d763aaSMatthew G. Knepley @*/
61589371c9d4SSatish Balay PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val) {
6159f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6160f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61619371c9d4SSatish Balay   if (num) {
61629371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
61639371c9d4SSatish Balay     *num = dm->outputSequenceNum;
61649371c9d4SSatish Balay   }
61659371c9d4SSatish Balay   if (val) {
61669371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
61679371c9d4SSatish Balay     *val = dm->outputSequenceVal;
61689371c9d4SSatish Balay   }
6169f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6170f4d763aaSMatthew G. Knepley }
6171f4d763aaSMatthew G. Knepley 
6172f4d763aaSMatthew G. Knepley /*@
6173cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6174f4d763aaSMatthew G. Knepley 
6175f4d763aaSMatthew G. Knepley   Input Parameters:
6176bb7acecfSBarry Smith + dm - The original `DM`
6177cdb7a50dSMatthew G. Knepley . num - The output sequence number
6178cdb7a50dSMatthew G. Knepley - val - The output sequence value
6179f4d763aaSMatthew G. Knepley 
6180f4d763aaSMatthew G. Knepley   Level: intermediate
6181f4d763aaSMatthew G. Knepley 
6182bb7acecfSBarry Smith   Note:
6183bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6184bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6185f4d763aaSMatthew G. Knepley 
6186db781477SPatrick Sanan .seealso: `VecView()`
6187f4d763aaSMatthew G. Knepley @*/
61889371c9d4SSatish Balay PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val) {
6189f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6190f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6191f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6192cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6193cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6194cdb7a50dSMatthew G. Knepley }
6195cdb7a50dSMatthew G. Knepley 
6196cdb7a50dSMatthew G. Knepley /*@C
6197bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6198cdb7a50dSMatthew G. Knepley 
6199cdb7a50dSMatthew G. Knepley   Input Parameters:
6200bb7acecfSBarry Smith + dm   - The original `DM`
6201cdb7a50dSMatthew G. Knepley . name - The sequence name
6202cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6203cdb7a50dSMatthew G. Knepley 
6204cdb7a50dSMatthew G. Knepley   Output Parameter:
6205cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6206cdb7a50dSMatthew G. Knepley 
6207cdb7a50dSMatthew G. Knepley   Level: intermediate
6208cdb7a50dSMatthew G. Knepley 
6209bb7acecfSBarry Smith   Note:
6210bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6211bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6212bb7acecfSBarry Smith 
6213bb7acecfSBarry Smith   Developer Note:
6214bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6215cdb7a50dSMatthew G. Knepley 
6216db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6217cdb7a50dSMatthew G. Knepley @*/
62189371c9d4SSatish Balay PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val) {
6219cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6220cdb7a50dSMatthew G. Knepley 
6221cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6222cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6223cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6224064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
62259566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6226cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6227cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6228cdb7a50dSMatthew G. Knepley     PetscScalar value;
6229cdb7a50dSMatthew G. Knepley 
62309566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
62314aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6232cdb7a50dSMatthew G. Knepley #endif
6233cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6234f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6235f4d763aaSMatthew G. Knepley }
62368e4ac7eaSMatthew G. Knepley 
62378e4ac7eaSMatthew G. Knepley /*@
6238bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
62398e4ac7eaSMatthew G. Knepley 
62408e4ac7eaSMatthew G. Knepley   Not collective
62418e4ac7eaSMatthew G. Knepley 
62428e4ac7eaSMatthew G. Knepley   Input Parameter:
6243bb7acecfSBarry Smith . dm - The `DM`
62448e4ac7eaSMatthew G. Knepley 
62458e4ac7eaSMatthew G. Knepley   Output Parameter:
6246bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
62478e4ac7eaSMatthew G. Knepley 
62488e4ac7eaSMatthew G. Knepley   Level: beginner
62498e4ac7eaSMatthew G. Knepley 
6250db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
62518e4ac7eaSMatthew G. Knepley @*/
62529371c9d4SSatish Balay PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural) {
62538e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62548e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6255534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
62568e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
62578e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62588e4ac7eaSMatthew G. Knepley }
62598e4ac7eaSMatthew G. Knepley 
62608e4ac7eaSMatthew G. Knepley /*@
6261bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
62628e4ac7eaSMatthew G. Knepley 
62638e4ac7eaSMatthew G. Knepley   Collective on dm
62648e4ac7eaSMatthew G. Knepley 
62658e4ac7eaSMatthew G. Knepley   Input Parameters:
6266bb7acecfSBarry Smith  + dm - The `DM`
6267bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
62688e4ac7eaSMatthew G. Knepley 
6269bb7acecfSBarry Smith   Note:
6270bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
62715d3b26e6SMatthew G. Knepley 
62728e4ac7eaSMatthew G. Knepley   Level: beginner
62738e4ac7eaSMatthew G. Knepley 
6274db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
62758e4ac7eaSMatthew G. Knepley @*/
62769371c9d4SSatish Balay PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural) {
62778e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
62788e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62798833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
62808e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
62818e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
62828e4ac7eaSMatthew G. Knepley }
6283c58f1c22SToby Isaac 
6284c58f1c22SToby Isaac /*@C
6285bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6286c58f1c22SToby Isaac 
6287c58f1c22SToby Isaac   Not Collective
6288c58f1c22SToby Isaac 
6289c58f1c22SToby Isaac   Input Parameters:
6290bb7acecfSBarry Smith + dm   - The `DM` object
6291c58f1c22SToby Isaac - name - The label name
6292c58f1c22SToby Isaac 
6293c58f1c22SToby Isaac   Level: intermediate
6294c58f1c22SToby Isaac 
6295db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6296c58f1c22SToby Isaac @*/
62979371c9d4SSatish Balay PetscErrorCode DMCreateLabel(DM dm, const char name[]) {
62985d80c0bfSVaclav Hapla   PetscBool flg;
62995d80c0bfSVaclav Hapla   DMLabel   label;
6300c58f1c22SToby Isaac 
6301c58f1c22SToby Isaac   PetscFunctionBegin;
6302c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6303c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63049566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6305c58f1c22SToby Isaac   if (!flg) {
63069566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
63079566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
63089566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6309c58f1c22SToby Isaac   }
6310c58f1c22SToby Isaac   PetscFunctionReturn(0);
6311c58f1c22SToby Isaac }
6312c58f1c22SToby Isaac 
6313c58f1c22SToby Isaac /*@C
6314bb7acecfSBarry 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.
63150fdc7489SMatthew Knepley 
63160fdc7489SMatthew Knepley   Not Collective
63170fdc7489SMatthew Knepley 
63180fdc7489SMatthew Knepley   Input Parameters:
6319bb7acecfSBarry Smith + dm   - The `DM` object
63200fdc7489SMatthew Knepley . l    - The index for the label
63210fdc7489SMatthew Knepley - name - The label name
63220fdc7489SMatthew Knepley 
63230fdc7489SMatthew Knepley   Level: intermediate
63240fdc7489SMatthew Knepley 
6325db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
63260fdc7489SMatthew Knepley @*/
63279371c9d4SSatish Balay PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[]) {
63280fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
63290fdc7489SMatthew Knepley   DMLabel     label;
63300fdc7489SMatthew Knepley   PetscInt    Nl, m;
63310fdc7489SMatthew Knepley   PetscBool   flg, match;
63320fdc7489SMatthew Knepley   const char *lname;
63330fdc7489SMatthew Knepley 
63340fdc7489SMatthew Knepley   PetscFunctionBegin;
63350fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6336064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
63379566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
63380fdc7489SMatthew Knepley   if (!flg) {
63399566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
63409566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
63419566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
63420fdc7489SMatthew Knepley   }
63439566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
634463a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
63450fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
63469566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
63479566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
63480fdc7489SMatthew Knepley     if (match) break;
63490fdc7489SMatthew Knepley   }
63500fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
63510fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
63520fdc7489SMatthew Knepley   else prev->next = orig->next;
63530fdc7489SMatthew Knepley   if (!l) {
63540fdc7489SMatthew Knepley     orig->next = dm->labels;
63550fdc7489SMatthew Knepley     dm->labels = orig;
63560fdc7489SMatthew Knepley   } else {
63579371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
63589371c9d4SSatish Balay       ;
63590fdc7489SMatthew Knepley     orig->next = prev->next;
63600fdc7489SMatthew Knepley     prev->next = orig;
63610fdc7489SMatthew Knepley   }
63620fdc7489SMatthew Knepley   PetscFunctionReturn(0);
63630fdc7489SMatthew Knepley }
63640fdc7489SMatthew Knepley 
63650fdc7489SMatthew Knepley /*@C
6366bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6367c58f1c22SToby Isaac 
6368c58f1c22SToby Isaac   Not Collective
6369c58f1c22SToby Isaac 
6370c58f1c22SToby Isaac   Input Parameters:
6371bb7acecfSBarry Smith + dm   - The `DM` object
6372c58f1c22SToby Isaac . name - The label name
6373c58f1c22SToby Isaac - point - The mesh point
6374c58f1c22SToby Isaac 
6375c58f1c22SToby Isaac   Output Parameter:
6376c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6377c58f1c22SToby Isaac 
6378c58f1c22SToby Isaac   Level: beginner
6379c58f1c22SToby Isaac 
6380db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6381c58f1c22SToby Isaac @*/
63829371c9d4SSatish Balay PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value) {
6383c58f1c22SToby Isaac   DMLabel label;
6384c58f1c22SToby Isaac 
6385c58f1c22SToby Isaac   PetscFunctionBegin;
6386c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6387c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
63889566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
63897a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
63909566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6391c58f1c22SToby Isaac   PetscFunctionReturn(0);
6392c58f1c22SToby Isaac }
6393c58f1c22SToby Isaac 
6394c58f1c22SToby Isaac /*@C
6395bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6396c58f1c22SToby Isaac 
6397c58f1c22SToby Isaac   Not Collective
6398c58f1c22SToby Isaac 
6399c58f1c22SToby Isaac   Input Parameters:
6400bb7acecfSBarry Smith + dm   - The `DM` object
6401c58f1c22SToby Isaac . name - The label name
6402c58f1c22SToby Isaac . point - The mesh point
6403c58f1c22SToby Isaac - value - The label value for this point
6404c58f1c22SToby Isaac 
6405c58f1c22SToby Isaac   Output Parameter:
6406c58f1c22SToby Isaac 
6407c58f1c22SToby Isaac   Level: beginner
6408c58f1c22SToby Isaac 
6409db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6410c58f1c22SToby Isaac @*/
64119371c9d4SSatish Balay PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value) {
6412c58f1c22SToby Isaac   DMLabel label;
6413c58f1c22SToby Isaac 
6414c58f1c22SToby Isaac   PetscFunctionBegin;
6415c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6416c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64179566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6418c58f1c22SToby Isaac   if (!label) {
64199566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
64209566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6421c58f1c22SToby Isaac   }
64229566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6423c58f1c22SToby Isaac   PetscFunctionReturn(0);
6424c58f1c22SToby Isaac }
6425c58f1c22SToby Isaac 
6426c58f1c22SToby Isaac /*@C
6427bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6428c58f1c22SToby Isaac 
6429c58f1c22SToby Isaac   Not Collective
6430c58f1c22SToby Isaac 
6431c58f1c22SToby Isaac   Input Parameters:
6432bb7acecfSBarry Smith + dm   - The `DM` object
6433c58f1c22SToby Isaac . name - The label name
6434c58f1c22SToby Isaac . point - The mesh point
6435c58f1c22SToby Isaac - value - The label value for this point
6436c58f1c22SToby Isaac 
6437c58f1c22SToby Isaac   Level: beginner
6438c58f1c22SToby Isaac 
6439db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6440c58f1c22SToby Isaac @*/
64419371c9d4SSatish Balay PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value) {
6442c58f1c22SToby Isaac   DMLabel label;
6443c58f1c22SToby Isaac 
6444c58f1c22SToby Isaac   PetscFunctionBegin;
6445c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6446c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64479566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6448c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64499566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6450c58f1c22SToby Isaac   PetscFunctionReturn(0);
6451c58f1c22SToby Isaac }
6452c58f1c22SToby Isaac 
6453c58f1c22SToby Isaac /*@C
6454bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6455c58f1c22SToby Isaac 
6456c58f1c22SToby Isaac   Not Collective
6457c58f1c22SToby Isaac 
6458c58f1c22SToby Isaac   Input Parameters:
6459bb7acecfSBarry Smith + dm   - The `DM` object
6460c58f1c22SToby Isaac - name - The label name
6461c58f1c22SToby Isaac 
6462c58f1c22SToby Isaac   Output Parameter:
6463c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6464c58f1c22SToby Isaac 
6465c58f1c22SToby Isaac   Level: beginner
6466c58f1c22SToby Isaac 
6467bb7acecfSBarry Smith   Developer Note:
6468bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6469bb7acecfSBarry Smith 
6470bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6471c58f1c22SToby Isaac @*/
64729371c9d4SSatish Balay PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size) {
6473c58f1c22SToby Isaac   DMLabel label;
6474c58f1c22SToby Isaac 
6475c58f1c22SToby Isaac   PetscFunctionBegin;
6476c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6477c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6478534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
64799566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6480c58f1c22SToby Isaac   *size = 0;
6481c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
64829566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6483c58f1c22SToby Isaac   PetscFunctionReturn(0);
6484c58f1c22SToby Isaac }
6485c58f1c22SToby Isaac 
6486c58f1c22SToby Isaac /*@C
6487bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6488c58f1c22SToby Isaac 
6489c58f1c22SToby Isaac   Not Collective
6490c58f1c22SToby Isaac 
6491c58f1c22SToby Isaac   Input Parameters:
6492bb7acecfSBarry Smith + mesh - The `DM` object
6493c58f1c22SToby Isaac - name - The label name
6494c58f1c22SToby Isaac 
6495c58f1c22SToby Isaac   Output Parameter:
6496c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6497c58f1c22SToby Isaac 
6498c58f1c22SToby Isaac   Level: beginner
6499c58f1c22SToby Isaac 
6500db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6501c58f1c22SToby Isaac @*/
65029371c9d4SSatish Balay PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids) {
6503c58f1c22SToby Isaac   DMLabel label;
6504c58f1c22SToby Isaac 
6505c58f1c22SToby Isaac   PetscFunctionBegin;
6506c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6507c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6508c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
65099566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6510c58f1c22SToby Isaac   *ids = NULL;
6511dab2e251SBlaise Bourdin   if (label) {
65129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6513dab2e251SBlaise Bourdin   } else {
6514dab2e251SBlaise Bourdin     /* returning an empty IS */
65159566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6516dab2e251SBlaise Bourdin   }
6517c58f1c22SToby Isaac   PetscFunctionReturn(0);
6518c58f1c22SToby Isaac }
6519c58f1c22SToby Isaac 
6520c58f1c22SToby Isaac /*@C
6521c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6522c58f1c22SToby Isaac 
6523c58f1c22SToby Isaac   Not Collective
6524c58f1c22SToby Isaac 
6525c58f1c22SToby Isaac   Input Parameters:
6526bb7acecfSBarry Smith + dm - The `DM` object
6527c58f1c22SToby Isaac . name - The label name
6528c58f1c22SToby Isaac - value - The stratum value
6529c58f1c22SToby Isaac 
6530c58f1c22SToby Isaac   Output Parameter:
6531bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6532c58f1c22SToby Isaac 
6533c58f1c22SToby Isaac   Level: beginner
6534c58f1c22SToby Isaac 
6535db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6536c58f1c22SToby Isaac @*/
65379371c9d4SSatish Balay PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size) {
6538c58f1c22SToby Isaac   DMLabel label;
6539c58f1c22SToby Isaac 
6540c58f1c22SToby Isaac   PetscFunctionBegin;
6541c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6542c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6543534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
65449566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6545c58f1c22SToby Isaac   *size = 0;
6546c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65479566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6548c58f1c22SToby Isaac   PetscFunctionReturn(0);
6549c58f1c22SToby Isaac }
6550c58f1c22SToby Isaac 
6551c58f1c22SToby Isaac /*@C
6552c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6553c58f1c22SToby Isaac 
6554c58f1c22SToby Isaac   Not Collective
6555c58f1c22SToby Isaac 
6556c58f1c22SToby Isaac   Input Parameters:
6557bb7acecfSBarry Smith + dm - The `DM` object
6558c58f1c22SToby Isaac . name - The label name
6559c58f1c22SToby Isaac - value - The stratum value
6560c58f1c22SToby Isaac 
6561c58f1c22SToby Isaac   Output Parameter:
6562c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6563c58f1c22SToby Isaac 
6564c58f1c22SToby Isaac   Level: beginner
6565c58f1c22SToby Isaac 
6566db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6567c58f1c22SToby Isaac @*/
65689371c9d4SSatish Balay PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points) {
6569c58f1c22SToby Isaac   DMLabel label;
6570c58f1c22SToby Isaac 
6571c58f1c22SToby Isaac   PetscFunctionBegin;
6572c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6573c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6574c58f1c22SToby Isaac   PetscValidPointer(points, 4);
65759566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6576c58f1c22SToby Isaac   *points = NULL;
6577c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
65789566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6579c58f1c22SToby Isaac   PetscFunctionReturn(0);
6580c58f1c22SToby Isaac }
6581c58f1c22SToby Isaac 
65824de306b1SToby Isaac /*@C
65839044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
65844de306b1SToby Isaac 
65854de306b1SToby Isaac   Not Collective
65864de306b1SToby Isaac 
65874de306b1SToby Isaac   Input Parameters:
6588bb7acecfSBarry Smith + dm - The `DM` object
65894de306b1SToby Isaac . name - The label name
65904de306b1SToby Isaac . value - The stratum value
65914de306b1SToby Isaac - points - The stratum points
65924de306b1SToby Isaac 
65934de306b1SToby Isaac   Level: beginner
65944de306b1SToby Isaac 
6595bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
65964de306b1SToby Isaac @*/
65979371c9d4SSatish Balay PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points) {
65984de306b1SToby Isaac   DMLabel label;
65994de306b1SToby Isaac 
66004de306b1SToby Isaac   PetscFunctionBegin;
66014de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66024de306b1SToby Isaac   PetscValidCharPointer(name, 2);
66034de306b1SToby Isaac   PetscValidPointer(points, 4);
66049566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66054de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
66069566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
66074de306b1SToby Isaac   PetscFunctionReturn(0);
66084de306b1SToby Isaac }
66094de306b1SToby Isaac 
6610c58f1c22SToby Isaac /*@C
6611bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6612c58f1c22SToby Isaac 
6613c58f1c22SToby Isaac   Not Collective
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac   Input Parameters:
6616bb7acecfSBarry Smith + dm   - The `DM` object
6617c58f1c22SToby Isaac . name - The label name
6618c58f1c22SToby Isaac - value - The label value for this point
6619c58f1c22SToby Isaac 
6620c58f1c22SToby Isaac   Output Parameter:
6621c58f1c22SToby Isaac 
6622c58f1c22SToby Isaac   Level: beginner
6623c58f1c22SToby Isaac 
6624bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6625c58f1c22SToby Isaac @*/
66269371c9d4SSatish Balay PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value) {
6627c58f1c22SToby Isaac   DMLabel label;
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   PetscFunctionBegin;
6630c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6631c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66329566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6633c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66349566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6635c58f1c22SToby Isaac   PetscFunctionReturn(0);
6636c58f1c22SToby Isaac }
6637c58f1c22SToby Isaac 
6638c58f1c22SToby Isaac /*@
6639bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6640c58f1c22SToby Isaac 
6641c58f1c22SToby Isaac   Not Collective
6642c58f1c22SToby Isaac 
6643c58f1c22SToby Isaac   Input Parameter:
6644bb7acecfSBarry Smith . dm   - The `DM` object
6645c58f1c22SToby Isaac 
6646c58f1c22SToby Isaac   Output Parameter:
6647c58f1c22SToby Isaac . numLabels - the number of Labels
6648c58f1c22SToby Isaac 
6649c58f1c22SToby Isaac   Level: intermediate
6650c58f1c22SToby Isaac 
6651bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6652c58f1c22SToby Isaac @*/
66539371c9d4SSatish Balay PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels) {
66545d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6655c58f1c22SToby Isaac   PetscInt    n    = 0;
6656c58f1c22SToby Isaac 
6657c58f1c22SToby Isaac   PetscFunctionBegin;
6658c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6659534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
66609371c9d4SSatish Balay   while (next) {
66619371c9d4SSatish Balay     ++n;
66629371c9d4SSatish Balay     next = next->next;
66639371c9d4SSatish Balay   }
6664c58f1c22SToby Isaac   *numLabels = n;
6665c58f1c22SToby Isaac   PetscFunctionReturn(0);
6666c58f1c22SToby Isaac }
6667c58f1c22SToby Isaac 
6668c58f1c22SToby Isaac /*@C
6669c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac   Not Collective
6672c58f1c22SToby Isaac 
6673c58f1c22SToby Isaac   Input Parameters:
6674bb7acecfSBarry Smith + dm - The `DM` object
6675c58f1c22SToby Isaac - n  - the label number
6676c58f1c22SToby Isaac 
6677c58f1c22SToby Isaac   Output Parameter:
6678c58f1c22SToby Isaac . name - the label name
6679c58f1c22SToby Isaac 
6680c58f1c22SToby Isaac   Level: intermediate
6681c58f1c22SToby Isaac 
6682bb7acecfSBarry Smith   Developer Note:
6683bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6684bb7acecfSBarry Smith 
6685bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6686c58f1c22SToby Isaac @*/
66879371c9d4SSatish Balay PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name) {
66885d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6689c58f1c22SToby Isaac   PetscInt    l    = 0;
6690c58f1c22SToby Isaac 
6691c58f1c22SToby Isaac   PetscFunctionBegin;
6692c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6693c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6694c58f1c22SToby Isaac   while (next) {
6695c58f1c22SToby Isaac     if (l == n) {
66969566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
6697c58f1c22SToby Isaac       PetscFunctionReturn(0);
6698c58f1c22SToby Isaac     }
6699c58f1c22SToby Isaac     ++l;
6700c58f1c22SToby Isaac     next = next->next;
6701c58f1c22SToby Isaac   }
670263a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6703c58f1c22SToby Isaac }
6704c58f1c22SToby Isaac 
6705c58f1c22SToby Isaac /*@C
6706bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac   Not Collective
6709c58f1c22SToby Isaac 
6710c58f1c22SToby Isaac   Input Parameters:
6711bb7acecfSBarry Smith + dm   - The `DM` object
6712c58f1c22SToby Isaac - name - The label name
6713c58f1c22SToby Isaac 
6714c58f1c22SToby Isaac   Output Parameter:
6715bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6716c58f1c22SToby Isaac 
6717c58f1c22SToby Isaac   Level: intermediate
6718c58f1c22SToby Isaac 
6719bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6720c58f1c22SToby Isaac @*/
67219371c9d4SSatish Balay PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel) {
67225d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6723d67d17b1SMatthew G. Knepley   const char *lname;
6724c58f1c22SToby Isaac 
6725c58f1c22SToby Isaac   PetscFunctionBegin;
6726c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6727c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6728534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6729c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6730c58f1c22SToby Isaac   while (next) {
67319566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
67329566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6733c58f1c22SToby Isaac     if (*hasLabel) break;
6734c58f1c22SToby Isaac     next = next->next;
6735c58f1c22SToby Isaac   }
6736c58f1c22SToby Isaac   PetscFunctionReturn(0);
6737c58f1c22SToby Isaac }
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac /*@C
6740bb7acecfSBarry Smith   DMGetLabel - Return the label of a given name, or NULL, from a `DM`
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac   Not Collective
6743c58f1c22SToby Isaac 
6744c58f1c22SToby Isaac   Input Parameters:
6745bb7acecfSBarry Smith + dm   - The `DM` object
6746c58f1c22SToby Isaac - name - The label name
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   Output Parameter:
6749bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent
6750c58f1c22SToby Isaac 
6751bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6752bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6753bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6754bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6755bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6756bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6757bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
67586d7c9049SMatthew G. Knepley 
6759c58f1c22SToby Isaac   Level: intermediate
6760c58f1c22SToby Isaac 
6761bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6762c58f1c22SToby Isaac @*/
67639371c9d4SSatish Balay PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label) {
67645d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6765c58f1c22SToby Isaac   PetscBool   hasLabel;
6766d67d17b1SMatthew G. Knepley   const char *lname;
6767c58f1c22SToby Isaac 
6768c58f1c22SToby Isaac   PetscFunctionBegin;
6769c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6770c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6771c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6772c58f1c22SToby Isaac   *label = NULL;
6773c58f1c22SToby Isaac   while (next) {
67749566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
67759566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6776c58f1c22SToby Isaac     if (hasLabel) {
6777c58f1c22SToby Isaac       *label = next->label;
6778c58f1c22SToby Isaac       break;
6779c58f1c22SToby Isaac     }
6780c58f1c22SToby Isaac     next = next->next;
6781c58f1c22SToby Isaac   }
6782c58f1c22SToby Isaac   PetscFunctionReturn(0);
6783c58f1c22SToby Isaac }
6784c58f1c22SToby Isaac 
6785c58f1c22SToby Isaac /*@C
6786bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
6787c58f1c22SToby Isaac 
6788c58f1c22SToby Isaac   Not Collective
6789c58f1c22SToby Isaac 
6790c58f1c22SToby Isaac   Input Parameters:
6791bb7acecfSBarry Smith + dm - The `DM` object
6792c58f1c22SToby Isaac - n  - the label number
6793c58f1c22SToby Isaac 
6794c58f1c22SToby Isaac   Output Parameter:
6795c58f1c22SToby Isaac . label - the label
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Level: intermediate
6798c58f1c22SToby Isaac 
6799bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6800c58f1c22SToby Isaac @*/
68019371c9d4SSatish Balay PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label) {
68025d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6803c58f1c22SToby Isaac   PetscInt    l    = 0;
6804c58f1c22SToby Isaac 
6805c58f1c22SToby Isaac   PetscFunctionBegin;
6806c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6807c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6808c58f1c22SToby Isaac   while (next) {
6809c58f1c22SToby Isaac     if (l == n) {
6810c58f1c22SToby Isaac       *label = next->label;
6811c58f1c22SToby Isaac       PetscFunctionReturn(0);
6812c58f1c22SToby Isaac     }
6813c58f1c22SToby Isaac     ++l;
6814c58f1c22SToby Isaac     next = next->next;
6815c58f1c22SToby Isaac   }
681663a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6817c58f1c22SToby Isaac }
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac /*@C
6820bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
6821c58f1c22SToby Isaac 
6822c58f1c22SToby Isaac   Not Collective
6823c58f1c22SToby Isaac 
6824c58f1c22SToby Isaac   Input Parameters:
6825bb7acecfSBarry Smith + dm   - The `DM` object
6826bb7acecfSBarry Smith - label - The `DMLabel`
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac   Level: developer
6829c58f1c22SToby Isaac 
6830bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6831c58f1c22SToby Isaac @*/
68329371c9d4SSatish Balay PetscErrorCode DMAddLabel(DM dm, DMLabel label) {
68335d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
6834c58f1c22SToby Isaac   PetscBool   hasLabel;
6835d67d17b1SMatthew G. Knepley   const char *lname;
68365d80c0bfSVaclav Hapla   PetscBool   flg;
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac   PetscFunctionBegin;
6839c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68409566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
68419566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
68427a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
68439566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
6844c58f1c22SToby Isaac   tmpLabel->label  = label;
6845c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
68465d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
68475d80c0bfSVaclav Hapla   *p = tmpLabel;
68489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
68499566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
68505d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
68519566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
6852ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
6853c58f1c22SToby Isaac   PetscFunctionReturn(0);
6854c58f1c22SToby Isaac }
6855c58f1c22SToby Isaac 
6856c58f1c22SToby Isaac /*@C
68574a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
68584a7ee7d0SMatthew G. Knepley 
68594a7ee7d0SMatthew G. Knepley   Not Collective
68604a7ee7d0SMatthew G. Knepley 
68614a7ee7d0SMatthew G. Knepley   Input Parameters:
6862bb7acecfSBarry Smith + dm    - The `DM` object
6863bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
68644a7ee7d0SMatthew G. Knepley 
6865bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6866bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
6867bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
6868bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6869bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6870bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6871bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
68724a7ee7d0SMatthew G. Knepley 
68734a7ee7d0SMatthew G. Knepley   Level: intermediate
68744a7ee7d0SMatthew G. Knepley 
6875bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
68764a7ee7d0SMatthew G. Knepley @*/
68779371c9d4SSatish Balay PetscErrorCode DMSetLabel(DM dm, DMLabel label) {
68784a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
68794a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
68804a7ee7d0SMatthew G. Knepley   const char *name, *lname;
68814a7ee7d0SMatthew G. Knepley 
68824a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
68834a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68844a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
68859566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
68864a7ee7d0SMatthew G. Knepley   while (next) {
68879566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
68889566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
68894a7ee7d0SMatthew G. Knepley     if (hasLabel) {
68909566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
68919566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
68924a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
68939566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
68944a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
68959566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
68964a7ee7d0SMatthew G. Knepley       next->label = label;
68974a7ee7d0SMatthew G. Knepley       break;
68984a7ee7d0SMatthew G. Knepley     }
68994a7ee7d0SMatthew G. Knepley     next = next->next;
69004a7ee7d0SMatthew G. Knepley   }
69014a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
69024a7ee7d0SMatthew G. Knepley }
69034a7ee7d0SMatthew G. Knepley 
69044a7ee7d0SMatthew G. Knepley /*@C
6905bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac   Not Collective
6908c58f1c22SToby Isaac 
6909c58f1c22SToby Isaac   Input Parameters:
6910bb7acecfSBarry Smith + dm   - The `DM` object
6911c58f1c22SToby Isaac - name - The label name
6912c58f1c22SToby Isaac 
6913c58f1c22SToby Isaac   Output Parameter:
6914bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent. Pass in NULL to call `DMLabelDestroy()` on the label, otherwise the
6915bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
6916c58f1c22SToby Isaac 
6917c58f1c22SToby Isaac   Level: developer
6918c58f1c22SToby Isaac 
6919bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
6920c58f1c22SToby Isaac @*/
69219371c9d4SSatish Balay PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label) {
692295d578d6SVaclav Hapla   DMLabelLink link, *pnext;
6923c58f1c22SToby Isaac   PetscBool   hasLabel;
6924d67d17b1SMatthew G. Knepley   const char *lname;
6925c58f1c22SToby Isaac 
6926c58f1c22SToby Isaac   PetscFunctionBegin;
6927c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6928e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
6929e5472504SVaclav Hapla   if (label) {
6930e5472504SVaclav Hapla     PetscValidPointer(label, 3);
6931c58f1c22SToby Isaac     *label = NULL;
6932e5472504SVaclav Hapla   }
69335d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
69349566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
69359566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6936c58f1c22SToby Isaac     if (hasLabel) {
693795d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
69389566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
693995d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
69409566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
6941ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
694295d578d6SVaclav Hapla       if (label) *label = link->label;
69439566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
69449566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6945c58f1c22SToby Isaac       break;
6946c58f1c22SToby Isaac     }
6947c58f1c22SToby Isaac   }
6948c58f1c22SToby Isaac   PetscFunctionReturn(0);
6949c58f1c22SToby Isaac }
6950c58f1c22SToby Isaac 
6951306894acSVaclav Hapla /*@
6952bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
6953306894acSVaclav Hapla 
6954306894acSVaclav Hapla   Not Collective
6955306894acSVaclav Hapla 
6956306894acSVaclav Hapla   Input Parameters:
6957bb7acecfSBarry Smith + dm   - The `DM` object
6958bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
6959306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
6960306894acSVaclav Hapla 
6961306894acSVaclav Hapla   Level: developer
6962306894acSVaclav Hapla 
6963bb7acecfSBarry Smith   Note:
6964306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
6965bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
6966306894acSVaclav Hapla   *label nullified.
6967306894acSVaclav Hapla 
6968bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
6969306894acSVaclav Hapla @*/
69709371c9d4SSatish Balay PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound) {
697143e45a93SVaclav Hapla   DMLabelLink link, *pnext;
6972306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
6973306894acSVaclav Hapla 
6974306894acSVaclav Hapla   PetscFunctionBegin;
6975306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6976306894acSVaclav Hapla   PetscValidPointer(label, 2);
6977f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
6978306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
6979306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
69805d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
698143e45a93SVaclav Hapla     if (*label == link->label) {
6982306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
698343e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
6984306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
6985ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
698643e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
69879566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
69889566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
6989306894acSVaclav Hapla       break;
6990306894acSVaclav Hapla     }
6991306894acSVaclav Hapla   }
69927a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
6993306894acSVaclav Hapla   PetscFunctionReturn(0);
6994306894acSVaclav Hapla }
6995306894acSVaclav Hapla 
6996c58f1c22SToby Isaac /*@C
6997c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6998c58f1c22SToby Isaac 
6999c58f1c22SToby Isaac   Not Collective
7000c58f1c22SToby Isaac 
7001c58f1c22SToby Isaac   Input Parameters:
7002bb7acecfSBarry Smith + dm   - The `DM` object
7003c58f1c22SToby Isaac - name - The label name
7004c58f1c22SToby Isaac 
7005c58f1c22SToby Isaac   Output Parameter:
7006c58f1c22SToby Isaac . output - The flag for output
7007c58f1c22SToby Isaac 
7008c58f1c22SToby Isaac   Level: developer
7009c58f1c22SToby Isaac 
7010bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7011c58f1c22SToby Isaac @*/
70129371c9d4SSatish Balay PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output) {
70135d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7014d67d17b1SMatthew G. Knepley   const char *lname;
7015c58f1c22SToby Isaac 
7016c58f1c22SToby Isaac   PetscFunctionBegin;
7017c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7018dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7019dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7020c58f1c22SToby Isaac   while (next) {
7021c58f1c22SToby Isaac     PetscBool flg;
7022c58f1c22SToby Isaac 
70239566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70249566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
70259371c9d4SSatish Balay     if (flg) {
70269371c9d4SSatish Balay       *output = next->output;
70279371c9d4SSatish Balay       PetscFunctionReturn(0);
70289371c9d4SSatish Balay     }
7029c58f1c22SToby Isaac     next = next->next;
7030c58f1c22SToby Isaac   }
703198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7032c58f1c22SToby Isaac }
7033c58f1c22SToby Isaac 
7034c58f1c22SToby Isaac /*@C
7035bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7036c58f1c22SToby Isaac 
7037c58f1c22SToby Isaac   Not Collective
7038c58f1c22SToby Isaac 
7039c58f1c22SToby Isaac   Input Parameters:
7040bb7acecfSBarry Smith + dm     - The `DM` object
7041c58f1c22SToby Isaac . name   - The label name
7042bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7043c58f1c22SToby Isaac 
7044c58f1c22SToby Isaac   Level: developer
7045c58f1c22SToby Isaac 
7046bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7047c58f1c22SToby Isaac @*/
70489371c9d4SSatish Balay PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output) {
70495d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7050d67d17b1SMatthew G. Knepley   const char *lname;
7051c58f1c22SToby Isaac 
7052c58f1c22SToby Isaac   PetscFunctionBegin;
7053c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7054534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7055c58f1c22SToby Isaac   while (next) {
7056c58f1c22SToby Isaac     PetscBool flg;
7057c58f1c22SToby Isaac 
70589566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70599566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
70609371c9d4SSatish Balay     if (flg) {
70619371c9d4SSatish Balay       next->output = output;
70629371c9d4SSatish Balay       PetscFunctionReturn(0);
70639371c9d4SSatish Balay     }
7064c58f1c22SToby Isaac     next = next->next;
7065c58f1c22SToby Isaac   }
706698921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7067c58f1c22SToby Isaac }
7068c58f1c22SToby Isaac 
7069c58f1c22SToby Isaac /*@
7070bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7071c58f1c22SToby Isaac 
7072d083f849SBarry Smith   Collective on dmA
7073c58f1c22SToby Isaac 
7074d8d19677SJose E. Roman   Input Parameters:
7075bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7076bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7077bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7078bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7079bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7080c58f1c22SToby Isaac 
7081c58f1c22SToby Isaac   Level: intermediate
7082c58f1c22SToby Isaac 
7083bb7acecfSBarry Smith   Note:
70842cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7085c58f1c22SToby Isaac 
7086bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7087c58f1c22SToby Isaac @*/
70889371c9d4SSatish Balay PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode) {
70892cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7090c58f1c22SToby Isaac   const char *name;
7091c58f1c22SToby Isaac   PetscBool   flg;
70925d80c0bfSVaclav Hapla   DMLabelLink link;
7093c58f1c22SToby Isaac 
70945d80c0bfSVaclav Hapla   PetscFunctionBegin;
70955d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
70965d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
70975d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
70985d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
70997a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
71005d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
71015d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
71025d80c0bfSVaclav Hapla     label = link->label;
71039566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
71045d80c0bfSVaclav Hapla     if (!all) {
71059566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7106c58f1c22SToby Isaac       if (flg) continue;
71079566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
71087d5acc75SStefano Zampini       if (flg) continue;
71099566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7110ba2698f1SMatthew G. Knepley       if (flg) continue;
71115d80c0bfSVaclav Hapla     }
71129566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
71132cbb9b06SVaclav Hapla     if (labelOld) {
71142cbb9b06SVaclav Hapla       switch (emode) {
71159371c9d4SSatish Balay       case DM_COPY_LABELS_KEEP: continue;
71169371c9d4SSatish Balay       case DM_COPY_LABELS_REPLACE: PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE)); break;
71179371c9d4SSatish Balay       case DM_COPY_LABELS_FAIL: SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
71189371c9d4SSatish Balay       default: SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
71192cbb9b06SVaclav Hapla       }
71202cbb9b06SVaclav Hapla     }
71215d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
71229566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
71235d80c0bfSVaclav Hapla     } else {
71245d80c0bfSVaclav Hapla       labelNew = label;
71255d80c0bfSVaclav Hapla     }
71269566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
71279566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7128c58f1c22SToby Isaac   }
7129c58f1c22SToby Isaac   PetscFunctionReturn(0);
7130c58f1c22SToby Isaac }
7131461a15a0SLisandro Dalcin 
7132609dae6eSVaclav Hapla /*@C
7133bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7134609dae6eSVaclav Hapla 
71355efe38ccSVaclav Hapla   Collective
7136609dae6eSVaclav Hapla 
7137609dae6eSVaclav Hapla   Input Parameters:
7138bb7acecfSBarry Smith + dm0 - First `DM` object
7139bb7acecfSBarry Smith - dm1 - Second `DM` object
7140609dae6eSVaclav Hapla 
7141609dae6eSVaclav Hapla   Output Parameters
71425efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7143609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7144609dae6eSVaclav Hapla 
7145609dae6eSVaclav Hapla   Level: intermediate
7146609dae6eSVaclav Hapla 
7147609dae6eSVaclav Hapla   Notes:
7148bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7149bb7acecfSBarry Smith 
7150bb7acecfSBarry Smith   If equal is passed as NULL and difference is found, an error is thrown on all processes.
7151bb7acecfSBarry Smith 
7152bb7acecfSBarry Smith   Make sure to pass equal is NULL on all processes or none of them.
7153609dae6eSVaclav Hapla 
71545efe38ccSVaclav Hapla   The output message is set independently on each rank.
7155bb7acecfSBarry Smith 
7156bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7157bb7acecfSBarry Smith 
7158bb7acecfSBarry Smith   If message is passed as NULL and a difference is found, the difference description is printed to stderr in synchronized manner.
7159bb7acecfSBarry Smith 
7160bb7acecfSBarry Smith   Make sure to pass message as NULL on all processes or no processes.
7161609dae6eSVaclav Hapla 
7162609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7163bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7164609dae6eSVaclav Hapla 
7165bb7acecfSBarry Smith   Fortran Note:
7166bb7acecfSBarry Smith   This function is not available from Fortran.
7167609dae6eSVaclav Hapla 
7168bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7169609dae6eSVaclav Hapla @*/
71709371c9d4SSatish Balay PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message) {
71715efe38ccSVaclav Hapla   PetscInt    n, i;
7172609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
71735efe38ccSVaclav Hapla   PetscBool   eq;
7174609dae6eSVaclav Hapla   MPI_Comm    comm;
71755efe38ccSVaclav Hapla   PetscMPIInt rank;
7176609dae6eSVaclav Hapla 
7177609dae6eSVaclav Hapla   PetscFunctionBegin;
7178609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7179609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7180609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
71815efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7182609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
71839566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
71849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
71855efe38ccSVaclav Hapla   {
71865efe38ccSVaclav Hapla     PetscInt n1;
71875efe38ccSVaclav Hapla 
71889566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
71899566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
71905efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
719148a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
71929566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
71935efe38ccSVaclav Hapla     if (!eq) goto finish;
71945efe38ccSVaclav Hapla   }
71955efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7196609dae6eSVaclav Hapla     DMLabel     l0, l1;
7197609dae6eSVaclav Hapla     const char *name;
7198609dae6eSVaclav Hapla     char       *msgInner;
7199609dae6eSVaclav Hapla 
7200609dae6eSVaclav Hapla     /* Ignore label order */
72019566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
72029566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
72039566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7204609dae6eSVaclav Hapla     if (!l1) {
720563a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
72065efe38ccSVaclav Hapla       eq = PETSC_FALSE;
72075efe38ccSVaclav Hapla       break;
7208609dae6eSVaclav Hapla     }
72099566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
72109566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
72119566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
72125efe38ccSVaclav Hapla     if (!eq) break;
7213609dae6eSVaclav Hapla   }
72149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7215609dae6eSVaclav Hapla finish:
72165efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7217609dae6eSVaclav Hapla   if (message) {
7218609dae6eSVaclav Hapla     *message = NULL;
721948a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
72205efe38ccSVaclav Hapla   } else {
722148a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
72229566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
72235efe38ccSVaclav Hapla   }
72245efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
72255efe38ccSVaclav Hapla   if (equal) *equal = eq;
72267a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7227609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7228609dae6eSVaclav Hapla }
7229609dae6eSVaclav Hapla 
72309371c9d4SSatish Balay PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value) {
7231461a15a0SLisandro Dalcin   PetscFunctionBegin;
7232461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7233461a15a0SLisandro Dalcin   if (!*label) {
72349566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
72359566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7236461a15a0SLisandro Dalcin   }
72379566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7238461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7239461a15a0SLisandro Dalcin }
7240461a15a0SLisandro Dalcin 
72410fdc7489SMatthew Knepley /*
72420fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
72430fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
72440fdc7489SMatthew Knepley   (label, id) pair in the DM.
72450fdc7489SMatthew Knepley 
72460fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
72470fdc7489SMatthew Knepley   each label.
72480fdc7489SMatthew Knepley */
72499371c9d4SSatish Balay PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal) {
72500fdc7489SMatthew Knepley   DMUniversalLabel ul;
72510fdc7489SMatthew Knepley   PetscBool       *active;
72520fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
72530fdc7489SMatthew Knepley 
72540fdc7489SMatthew Knepley   PetscFunctionBegin;
72559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
72569566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
72579566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
72589566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
72590fdc7489SMatthew Knepley   ul->Nl = 0;
72600fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
72610fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
72620fdc7489SMatthew Knepley     const char *name;
72630fdc7489SMatthew Knepley 
72649566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72659566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
72669566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
72670fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
72680fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
72690fdc7489SMatthew Knepley   }
72709566063dSJacob 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));
72710fdc7489SMatthew Knepley   ul->Nv = 0;
72720fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
72730fdc7489SMatthew Knepley     DMLabel     label;
72740fdc7489SMatthew Knepley     PetscInt    nv;
72750fdc7489SMatthew Knepley     const char *name;
72760fdc7489SMatthew Knepley 
72770fdc7489SMatthew Knepley     if (!active[l]) continue;
72789566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
72799566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
72809566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
72819566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
72820fdc7489SMatthew Knepley     ul->indices[m] = l;
72830fdc7489SMatthew Knepley     ul->Nv += nv;
72840fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
72850fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
72860fdc7489SMatthew Knepley     ++m;
72870fdc7489SMatthew Knepley   }
72880fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
72890fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
72900fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
72910fdc7489SMatthew Knepley   }
72920fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
72930fdc7489SMatthew Knepley     PetscInt b;
72940fdc7489SMatthew Knepley 
72950fdc7489SMatthew Knepley     ul->masks[l] = 0;
72960fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
72970fdc7489SMatthew Knepley   }
72989566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
72990fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
73000fdc7489SMatthew Knepley     DMLabel         label;
73010fdc7489SMatthew Knepley     IS              valueIS;
73020fdc7489SMatthew Knepley     const PetscInt *varr;
73030fdc7489SMatthew Knepley     PetscInt        nv, v;
73040fdc7489SMatthew Knepley 
73050fdc7489SMatthew Knepley     if (!active[l]) continue;
73069566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
73079566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
73089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
73099566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7310ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
73119566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
73129566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
73139566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
73140fdc7489SMatthew Knepley     ++m;
73150fdc7489SMatthew Knepley   }
73169566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
73170fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
73180fdc7489SMatthew Knepley     PetscInt  uval   = 0;
73190fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
73200fdc7489SMatthew Knepley 
73210fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
73220fdc7489SMatthew Knepley       DMLabel  label;
73230649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
73240fdc7489SMatthew Knepley 
73250fdc7489SMatthew Knepley       if (!active[l]) continue;
73269566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
73279566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
73289566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
73299371c9d4SSatish Balay       if (val == defval) {
73309371c9d4SSatish Balay         ++m;
73319371c9d4SSatish Balay         continue;
73329371c9d4SSatish Balay       }
73330649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
73340fdc7489SMatthew Knepley       marked = PETSC_TRUE;
73359566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
733663a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
73370fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
73380fdc7489SMatthew Knepley       ++m;
73390fdc7489SMatthew Knepley     }
73409566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
73410fdc7489SMatthew Knepley   }
73429566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
73430fdc7489SMatthew Knepley   *universal = ul;
73440fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73450fdc7489SMatthew Knepley }
73460fdc7489SMatthew Knepley 
73479371c9d4SSatish Balay PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal) {
73480fdc7489SMatthew Knepley   PetscInt l;
73490fdc7489SMatthew Knepley 
73500fdc7489SMatthew Knepley   PetscFunctionBegin;
73519566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
73529566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
73539566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
73549566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
73559566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
73560fdc7489SMatthew Knepley   *universal = NULL;
73570fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73580fdc7489SMatthew Knepley }
73590fdc7489SMatthew Knepley 
73609371c9d4SSatish Balay PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel) {
73610fdc7489SMatthew Knepley   PetscFunctionBegin;
73620fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
73630fdc7489SMatthew Knepley   *ulabel = ul->label;
73640fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73650fdc7489SMatthew Knepley }
73660fdc7489SMatthew Knepley 
73679371c9d4SSatish Balay PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm) {
73680fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
73690fdc7489SMatthew Knepley 
73700fdc7489SMatthew Knepley   PetscFunctionBegin;
7371064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
73720fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
73739566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
73749566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
73750fdc7489SMatthew Knepley   }
73760fdc7489SMatthew Knepley   if (preserveOrder) {
73770fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
73780fdc7489SMatthew Knepley       const char *name;
73790fdc7489SMatthew Knepley       PetscBool   match;
73800fdc7489SMatthew Knepley 
73819566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
73829566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
738363a3b9bcSJacob 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]);
73840fdc7489SMatthew Knepley     }
73850fdc7489SMatthew Knepley   }
73860fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73870fdc7489SMatthew Knepley }
73880fdc7489SMatthew Knepley 
73899371c9d4SSatish Balay PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value) {
73900fdc7489SMatthew Knepley   PetscInt l;
73910fdc7489SMatthew Knepley 
73920fdc7489SMatthew Knepley   PetscFunctionBegin;
73930fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
73940fdc7489SMatthew Knepley     DMLabel  label;
73950fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
73960fdc7489SMatthew Knepley 
73970fdc7489SMatthew Knepley     if (lval) {
73989566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
73999566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
74009566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
74010fdc7489SMatthew Knepley     }
74020fdc7489SMatthew Knepley   }
74030fdc7489SMatthew Knepley   PetscFunctionReturn(0);
74040fdc7489SMatthew Knepley }
7405a8fb8f29SToby Isaac 
7406a8fb8f29SToby Isaac /*@
7407bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7408bb7acecfSBarry Smith 
7409bb7acecfSBarry Smith   Not collective
7410a8fb8f29SToby Isaac 
7411a8fb8f29SToby Isaac   Input Parameter:
7412bb7acecfSBarry Smith . dm - The `DM` object
7413a8fb8f29SToby Isaac 
7414a8fb8f29SToby Isaac   Output Parameter:
7415bb7acecfSBarry Smith . cdm - The coarse `DM`
7416a8fb8f29SToby Isaac 
7417a8fb8f29SToby Isaac   Level: intermediate
7418a8fb8f29SToby Isaac 
7419bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7420a8fb8f29SToby Isaac @*/
74219371c9d4SSatish Balay PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm) {
7422a8fb8f29SToby Isaac   PetscFunctionBegin;
7423a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7424a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7425a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7426a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7427a8fb8f29SToby Isaac }
7428a8fb8f29SToby Isaac 
7429a8fb8f29SToby Isaac /*@
7430bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7431a8fb8f29SToby Isaac 
7432a8fb8f29SToby Isaac   Input Parameters:
7433bb7acecfSBarry Smith + dm - The `DM` object
7434bb7acecfSBarry Smith - cdm - The coarse `DM`
7435a8fb8f29SToby Isaac 
7436a8fb8f29SToby Isaac   Level: intermediate
7437a8fb8f29SToby Isaac 
7438bb7acecfSBarry Smith   Note:
7439bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7440bb7acecfSBarry Smith 
7441bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7442a8fb8f29SToby Isaac @*/
74439371c9d4SSatish Balay PetscErrorCode DMSetCoarseDM(DM dm, DM cdm) {
7444a8fb8f29SToby Isaac   PetscFunctionBegin;
7445a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7446a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
744789d734beSBarry Smith   if (dm == cdm) cdm = NULL;
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);
749389d734beSBarry Smith   if (dm == fdm) fdm = NULL;
74949566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
74959566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
749688bdff64SToby Isaac   dm->fineMesh = fdm;
749788bdff64SToby Isaac   PetscFunctionReturn(0);
749888bdff64SToby Isaac }
749988bdff64SToby Isaac 
7500a6ba4734SToby Isaac /*@C
7501bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7502a6ba4734SToby Isaac 
7503783e2ec8SMatthew G. Knepley   Collective on dm
7504783e2ec8SMatthew G. Knepley 
7505a6ba4734SToby Isaac   Input Parameters:
7506bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7507bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7508a6ba4734SToby Isaac . name     - The BC name
750945480ffeSMatthew G. Knepley . label    - The label defining constrained points
7510bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
751145480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7512a6ba4734SToby Isaac . field    - The field to constrain
751345480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7514a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7515a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
751656cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7517a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7518a6ba4734SToby Isaac 
751945480ffeSMatthew G. Knepley   Output Parameter:
752045480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
752145480ffeSMatthew G. Knepley 
7522a6ba4734SToby Isaac   Options Database Keys:
7523a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7524a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7525a6ba4734SToby Isaac 
7526bb7acecfSBarry Smith   Notes:
7527bb7acecfSBarry 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:
752856cf3b9cSMatthew G. Knepley 
752956cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
753056cf3b9cSMatthew G. Knepley 
7531bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
753256cf3b9cSMatthew G. Knepley 
753356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
753456cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
753556cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
753656cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
753756cf3b9cSMatthew G. Knepley 
753856cf3b9cSMatthew G. Knepley + dim - the spatial dimension
753956cf3b9cSMatthew G. Knepley . Nf - the number of fields
754056cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
754156cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
754256cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
754356cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
754456cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
754556cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
754656cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
754756cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
754856cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
754956cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
755056cf3b9cSMatthew G. Knepley . t - current time
755156cf3b9cSMatthew G. Knepley . x - coordinates of the current point
755256cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
755356cf3b9cSMatthew G. Knepley . constants - constant parameters
755456cf3b9cSMatthew G. Knepley - bcval - output values at the current point
755556cf3b9cSMatthew G. Knepley 
7556ed808b8fSJed Brown   Level: intermediate
7557a6ba4734SToby Isaac 
7558db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7559a6ba4734SToby Isaac @*/
75609371c9d4SSatish 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) {
7561e5e52638SMatthew G. Knepley   PetscDS ds;
7562a6ba4734SToby Isaac 
7563a6ba4734SToby Isaac   PetscFunctionBegin;
7564a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7565783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
756645480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
756745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
756845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
756945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
757001a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
75719566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7572799db056SMatthew G. Knepley   /* Complete label */
7573799db056SMatthew G. Knepley   if (label) {
7574799db056SMatthew G. Knepley     PetscObject  obj;
7575799db056SMatthew G. Knepley     PetscClassId id;
7576799db056SMatthew G. Knepley 
7577799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7578799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7579799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7580799db056SMatthew G. Knepley       DM plex;
7581799db056SMatthew G. Knepley 
7582799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7583799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7584799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7585799db056SMatthew G. Knepley     }
7586799db056SMatthew G. Knepley   }
75879566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7588a6ba4734SToby Isaac   PetscFunctionReturn(0);
7589a6ba4734SToby Isaac }
7590a6ba4734SToby Isaac 
759145480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
75929371c9d4SSatish Balay static PetscErrorCode DMPopulateBoundary(DM dm) {
7593e5e52638SMatthew G. Knepley   PetscDS     ds;
7594dff059c6SToby Isaac   DMBoundary *lastnext;
7595e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7596e6f8dbb6SToby Isaac 
7597e6f8dbb6SToby Isaac   PetscFunctionBegin;
75989566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7599e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
760047a1f5adSToby Isaac   if (dm->boundary) {
760147a1f5adSToby Isaac     DMBoundary next = dm->boundary;
760247a1f5adSToby Isaac 
760347a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
760447a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
760547a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
760647a1f5adSToby Isaac     while (next) {
760747a1f5adSToby Isaac       DMBoundary b = next;
760847a1f5adSToby Isaac 
760947a1f5adSToby Isaac       next = b->next;
76109566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7611a6ba4734SToby Isaac     }
761247a1f5adSToby Isaac     dm->boundary = NULL;
7613a6ba4734SToby Isaac   }
761447a1f5adSToby Isaac 
7615dff059c6SToby Isaac   lastnext = &(dm->boundary);
7616e6f8dbb6SToby Isaac   while (dsbound) {
7617e6f8dbb6SToby Isaac     DMBoundary dmbound;
7618e6f8dbb6SToby Isaac 
76199566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7620e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
762145480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
762247a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7623dff059c6SToby Isaac     *lastnext           = dmbound;
7624dff059c6SToby Isaac     lastnext            = &(dmbound->next);
7625dff059c6SToby Isaac     dsbound             = dsbound->next;
7626a6ba4734SToby Isaac   }
7627a6ba4734SToby Isaac   PetscFunctionReturn(0);
7628a6ba4734SToby Isaac }
7629a6ba4734SToby Isaac 
7630bb7acecfSBarry Smith /* TODO: missing manual page */
76319371c9d4SSatish Balay PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd) {
7632b95f2879SToby Isaac   DMBoundary b;
7633a6ba4734SToby Isaac 
7634a6ba4734SToby Isaac   PetscFunctionBegin;
7635a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7636534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7637a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
76389566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7639b95f2879SToby Isaac   b = dm->boundary;
7640a6ba4734SToby Isaac   while (b && !(*isBd)) {
7641e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7642e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7643a6ba4734SToby Isaac     PetscInt   i;
7644a6ba4734SToby Isaac 
764545480ffeSMatthew G. Knepley     if (label) {
76469566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7647a6ba4734SToby Isaac     }
7648a6ba4734SToby Isaac     b = b->next;
7649a6ba4734SToby Isaac   }
7650a6ba4734SToby Isaac   PetscFunctionReturn(0);
7651a6ba4734SToby Isaac }
76524d6f44ffSToby Isaac 
76534d6f44ffSToby Isaac /*@C
7654bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7655a6e0b375SMatthew G. Knepley 
7656bb7acecfSBarry Smith   Collective on dm
76574d6f44ffSToby Isaac 
76584d6f44ffSToby Isaac   Input Parameters:
7659bb7acecfSBarry Smith + dm      - The `DM`
76600709b2feSToby Isaac . time    - The time
76614d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76624d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76634d6f44ffSToby Isaac - mode    - The insertion mode for values
76644d6f44ffSToby Isaac 
76654d6f44ffSToby Isaac   Output Parameter:
76664d6f44ffSToby Isaac . X - vector
76674d6f44ffSToby Isaac 
76684d6f44ffSToby Isaac    Calling sequence of func:
766977b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
76704d6f44ffSToby Isaac 
76714d6f44ffSToby Isaac +  dim - The spatial dimension
76728ec8862eSJed Brown .  time - The time at which to sample
76734d6f44ffSToby Isaac .  x   - The coordinates
767477b739a6SMatthew Knepley .  Nc  - The number of components
76754d6f44ffSToby Isaac .  u   - The output field values
76764d6f44ffSToby Isaac -  ctx - optional user-defined function context
76774d6f44ffSToby Isaac 
76784d6f44ffSToby Isaac   Level: developer
76794d6f44ffSToby Isaac 
7680bb7acecfSBarry Smith   Developer Notes:
7681bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7682bb7acecfSBarry Smith 
7683bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7684bb7acecfSBarry Smith 
7685db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
76864d6f44ffSToby Isaac @*/
76879371c9d4SSatish Balay PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X) {
76884d6f44ffSToby Isaac   Vec localX;
76894d6f44ffSToby Isaac 
76904d6f44ffSToby Isaac   PetscFunctionBegin;
76914d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76929566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
76939566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
76949566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
76959566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
76969566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
76974d6f44ffSToby Isaac   PetscFunctionReturn(0);
76984d6f44ffSToby Isaac }
76994d6f44ffSToby Isaac 
7700a6e0b375SMatthew G. Knepley /*@C
7701bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7702a6e0b375SMatthew G. Knepley 
7703a6e0b375SMatthew G. Knepley   Not collective
7704a6e0b375SMatthew G. Knepley 
7705a6e0b375SMatthew G. Knepley   Input Parameters:
7706bb7acecfSBarry Smith + dm      - The `DM`
7707a6e0b375SMatthew G. Knepley . time    - The time
7708a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7709a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7710a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7711a6e0b375SMatthew G. Knepley 
7712a6e0b375SMatthew G. Knepley   Output Parameter:
7713a6e0b375SMatthew G. Knepley . localX - vector
7714a6e0b375SMatthew G. Knepley 
7715a6e0b375SMatthew G. Knepley    Calling sequence of func:
771677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7717a6e0b375SMatthew G. Knepley 
7718a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7719a6e0b375SMatthew G. Knepley .  x   - The coordinates
772077b739a6SMatthew Knepley .  Nc  - The number of components
7721a6e0b375SMatthew G. Knepley .  u   - The output field values
7722a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7723a6e0b375SMatthew G. Knepley 
7724a6e0b375SMatthew G. Knepley   Level: developer
7725a6e0b375SMatthew G. Knepley 
7726bb7acecfSBarry Smith   Developer Notes:
7727bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7728bb7acecfSBarry Smith 
7729bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7730bb7acecfSBarry Smith 
7731db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7732a6e0b375SMatthew G. Knepley @*/
77339371c9d4SSatish Balay PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) {
77344d6f44ffSToby Isaac   PetscFunctionBegin;
77354d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7736064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
77379566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
77384d6f44ffSToby Isaac   PetscFunctionReturn(0);
77394d6f44ffSToby Isaac }
77404d6f44ffSToby Isaac 
7741a6e0b375SMatthew G. Knepley /*@C
7742bb7acecfSBarry 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.
7743a6e0b375SMatthew G. Knepley 
7744bb7acecfSBarry Smith   Collective on dm
7745a6e0b375SMatthew G. Knepley 
7746a6e0b375SMatthew G. Knepley   Input Parameters:
7747bb7acecfSBarry Smith + dm      - The `DM`
7748a6e0b375SMatthew G. Knepley . time    - The time
7749bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7750a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7751bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
7752a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7753a6e0b375SMatthew G. Knepley 
7754a6e0b375SMatthew G. Knepley   Output Parameter:
7755a6e0b375SMatthew G. Knepley . X - vector
7756a6e0b375SMatthew G. Knepley 
7757a6e0b375SMatthew G. Knepley    Calling sequence of func:
775877b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7759a6e0b375SMatthew G. Knepley 
7760a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7761a6e0b375SMatthew G. Knepley .  x   - The coordinates
776277b739a6SMatthew Knepley .  Nc  - The number of components
7763a6e0b375SMatthew G. Knepley .  u   - The output field values
7764a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7765a6e0b375SMatthew G. Knepley 
7766a6e0b375SMatthew G. Knepley   Level: developer
7767a6e0b375SMatthew G. Knepley 
7768bb7acecfSBarry Smith   Developer Notes:
7769bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7770bb7acecfSBarry Smith 
7771bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7772bb7acecfSBarry Smith 
7773db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7774a6e0b375SMatthew G. Knepley @*/
77759371c9d4SSatish 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) {
77762c53366bSMatthew G. Knepley   Vec localX;
77772c53366bSMatthew G. Knepley 
77782c53366bSMatthew G. Knepley   PetscFunctionBegin;
77792c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77809566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
77819566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
77829566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
77839566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
77849566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
77852c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
77862c53366bSMatthew G. Knepley }
77872c53366bSMatthew G. Knepley 
7788a6e0b375SMatthew G. Knepley /*@C
7789bb7acecfSBarry 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.
7790a6e0b375SMatthew G. Knepley 
7791a6e0b375SMatthew G. Knepley   Not collective
7792a6e0b375SMatthew G. Knepley 
7793a6e0b375SMatthew G. Knepley   Input Parameters:
7794bb7acecfSBarry Smith + dm      - The `DM`
7795a6e0b375SMatthew G. Knepley . time    - The time
7796bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7797a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7798a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7799a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7800a6e0b375SMatthew G. Knepley 
7801a6e0b375SMatthew G. Knepley   Output Parameter:
7802a6e0b375SMatthew G. Knepley . localX - vector
7803a6e0b375SMatthew G. Knepley 
7804a6e0b375SMatthew G. Knepley    Calling sequence of func:
780577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7806a6e0b375SMatthew G. Knepley 
7807a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7808a6e0b375SMatthew G. Knepley .  x   - The coordinates
780977b739a6SMatthew Knepley .  Nc  - The number of components
7810a6e0b375SMatthew G. Knepley .  u   - The output field values
7811a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7812a6e0b375SMatthew G. Knepley 
7813a6e0b375SMatthew G. Knepley   Level: developer
7814a6e0b375SMatthew G. Knepley 
7815bb7acecfSBarry Smith   Developer Notes:
7816bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7817bb7acecfSBarry Smith 
7818bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7819bb7acecfSBarry Smith 
7820db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7821a6e0b375SMatthew G. Knepley @*/
78229371c9d4SSatish 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) {
78234d6f44ffSToby Isaac   PetscFunctionBegin;
78244d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7825064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
78269566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
78274d6f44ffSToby Isaac   PetscFunctionReturn(0);
78284d6f44ffSToby Isaac }
78292716604bSToby Isaac 
7830a6e0b375SMatthew G. Knepley /*@C
7831bb7acecfSBarry 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.
7832a6e0b375SMatthew G. Knepley 
7833a6e0b375SMatthew G. Knepley   Not collective
7834a6e0b375SMatthew G. Knepley 
7835a6e0b375SMatthew G. Knepley   Input Parameters:
7836bb7acecfSBarry Smith + dm      - The `DM`
7837a6e0b375SMatthew G. Knepley . time    - The time
7838a6e0b375SMatthew G. Knepley . localU  - The input field vector
7839a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7840a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7841a6e0b375SMatthew G. Knepley 
7842a6e0b375SMatthew G. Knepley   Output Parameter:
7843a6e0b375SMatthew G. Knepley . localX  - The output vector
7844a6e0b375SMatthew G. Knepley 
7845a6e0b375SMatthew G. Knepley    Calling sequence of func:
7846a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7847a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7848a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7849a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7850a6e0b375SMatthew G. Knepley 
7851a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7852a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7853a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7854a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7855a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7856a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7857a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7858a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7859a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7860a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7861a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7862a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7863a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7864a6e0b375SMatthew G. Knepley .  t            - The current time
7865a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7866a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7867a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7868a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7869a6e0b375SMatthew G. Knepley 
7870bb7acecfSBarry Smith   Note:
7871bb7acecfSBarry 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.
7872bb7acecfSBarry 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
7873bb7acecfSBarry 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
7874a6e0b375SMatthew 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.
7875a6e0b375SMatthew G. Knepley 
7876a6e0b375SMatthew G. Knepley   Level: intermediate
7877a6e0b375SMatthew G. Knepley 
7878bb7acecfSBarry Smith   Developer Notes:
7879bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7880bb7acecfSBarry Smith 
7881bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7882bb7acecfSBarry Smith 
7883db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7884a6e0b375SMatthew G. Knepley @*/
78859371c9d4SSatish 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) {
78868c6c5593SMatthew G. Knepley   PetscFunctionBegin;
78878c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78888c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
78898c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
78909566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
78918c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
78928c6c5593SMatthew G. Knepley }
78938c6c5593SMatthew G. Knepley 
7894a6e0b375SMatthew G. Knepley /*@C
7895a6e0b375SMatthew 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.
7896a6e0b375SMatthew G. Knepley 
7897a6e0b375SMatthew G. Knepley   Not collective
7898a6e0b375SMatthew G. Knepley 
7899a6e0b375SMatthew G. Knepley   Input Parameters:
7900bb7acecfSBarry Smith + dm      - The `DM`
7901a6e0b375SMatthew G. Knepley . time    - The time
7902bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
7903a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7904a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7905bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
7906a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7907a6e0b375SMatthew G. Knepley . localU  - The input field vector
7908a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7909a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7910a6e0b375SMatthew G. Knepley 
7911a6e0b375SMatthew G. Knepley   Output Parameter:
7912a6e0b375SMatthew G. Knepley . localX  - The output vector
7913a6e0b375SMatthew G. Knepley 
7914a6e0b375SMatthew G. Knepley    Calling sequence of func:
7915a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7916a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7917a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7918a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7919a6e0b375SMatthew G. Knepley 
7920a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7921a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7922a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7923a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7924a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7925a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7926a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7927a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7928a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7929a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7930a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7931a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7932a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7933a6e0b375SMatthew G. Knepley .  t            - The current time
7934a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7935a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7936a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7937a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7938a6e0b375SMatthew G. Knepley 
7939bb7acecfSBarry Smith   Note:
7940bb7acecfSBarry 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.
7941bb7acecfSBarry 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
7942bb7acecfSBarry 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
7943a6e0b375SMatthew 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.
7944a6e0b375SMatthew G. Knepley 
7945a6e0b375SMatthew G. Knepley   Level: intermediate
7946a6e0b375SMatthew G. Knepley 
7947bb7acecfSBarry Smith   Developer Notes:
7948bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7949bb7acecfSBarry Smith 
7950bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7951bb7acecfSBarry Smith 
7952d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
7953a6e0b375SMatthew G. Knepley @*/
79549371c9d4SSatish 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) {
79558c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7957064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
7958064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
79599566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
79608c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79618c6c5593SMatthew G. Knepley }
79628c6c5593SMatthew G. Knepley 
79632716604bSToby Isaac /*@C
7964d29d7c6eSMatthew 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.
7965d29d7c6eSMatthew G. Knepley 
7966d29d7c6eSMatthew G. Knepley   Not collective
7967d29d7c6eSMatthew G. Knepley 
7968d29d7c6eSMatthew G. Knepley   Input Parameters:
7969bb7acecfSBarry Smith + dm      - The `DM`
7970d29d7c6eSMatthew G. Knepley . time    - The time
7971bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
7972d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
7973d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
7974bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
7975d29d7c6eSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7976d29d7c6eSMatthew G. Knepley . U       - The input field vector
7977d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7978d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
7979d29d7c6eSMatthew G. Knepley 
7980d29d7c6eSMatthew G. Knepley   Output Parameter:
7981d29d7c6eSMatthew G. Knepley . X       - The output vector
7982d29d7c6eSMatthew G. Knepley 
7983d29d7c6eSMatthew G. Knepley    Calling sequence of func:
7984d29d7c6eSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7985d29d7c6eSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7986d29d7c6eSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7987d29d7c6eSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7988d29d7c6eSMatthew G. Knepley 
7989d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
7990d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
7991d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7992d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
7993d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7994d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
7995d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7996d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7997d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7998d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7999d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8000d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8001d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8002d29d7c6eSMatthew G. Knepley .  t            - The current time
8003d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8004d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8005d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8006d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8007d29d7c6eSMatthew G. Knepley 
8008bb7acecfSBarry Smith   Note:
8009bb7acecfSBarry 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.
8010bb7acecfSBarry 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
8011bb7acecfSBarry 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
8012d29d7c6eSMatthew 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.
8013d29d7c6eSMatthew G. Knepley 
8014d29d7c6eSMatthew G. Knepley   Level: intermediate
8015d29d7c6eSMatthew G. Knepley 
8016bb7acecfSBarry Smith   Developer Notes:
8017bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8018bb7acecfSBarry Smith 
8019bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8020bb7acecfSBarry Smith 
8021d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8022d29d7c6eSMatthew G. Knepley @*/
80239371c9d4SSatish 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) {
8024d29d7c6eSMatthew G. Knepley   DM  dmIn;
8025d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8026d29d7c6eSMatthew G. Knepley 
8027d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8028d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8029d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8030d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8031d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
803272fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
803372fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8034d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8035d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8036d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8037d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8038d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
8039d29d7c6eSMatthew G. Knepley   PetscFunctionReturn(0);
8040d29d7c6eSMatthew G. Knepley }
8041d29d7c6eSMatthew G. Knepley 
8042d29d7c6eSMatthew G. Knepley /*@C
8043ece3a9fcSMatthew 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.
8044ece3a9fcSMatthew G. Knepley 
8045ece3a9fcSMatthew G. Knepley   Not collective
8046ece3a9fcSMatthew G. Knepley 
8047ece3a9fcSMatthew G. Knepley   Input Parameters:
8048bb7acecfSBarry Smith + dm      - The `DM`
8049ece3a9fcSMatthew G. Knepley . time    - The time
8050bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8051ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8052ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8053bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8054ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8055ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8056ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8057ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8058ece3a9fcSMatthew G. Knepley 
8059ece3a9fcSMatthew G. Knepley   Output Parameter:
8060ece3a9fcSMatthew G. Knepley . localX  - The output vector
8061ece3a9fcSMatthew G. Knepley 
8062ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8063ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8064ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8065ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8066ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8067ece3a9fcSMatthew G. Knepley 
8068ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8069ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8070ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8071ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8072ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8073ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8074ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8075ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8076ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8077ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8078ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8079ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8080ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8081ece3a9fcSMatthew G. Knepley .  t            - The current time
8082ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8083ece3a9fcSMatthew G. Knepley .  n            - The face normal
8084ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8085ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8086ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8087ece3a9fcSMatthew G. Knepley 
8088ece3a9fcSMatthew G. Knepley   Note:
8089bb7acecfSBarry 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.
8090bb7acecfSBarry 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
8091bb7acecfSBarry 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
8092ece3a9fcSMatthew 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.
8093ece3a9fcSMatthew G. Knepley 
8094ece3a9fcSMatthew G. Knepley   Level: intermediate
8095ece3a9fcSMatthew G. Knepley 
8096bb7acecfSBarry Smith   Developer Notes:
8097bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8098bb7acecfSBarry Smith 
8099bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8100bb7acecfSBarry Smith 
8101db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8102ece3a9fcSMatthew G. Knepley @*/
81039371c9d4SSatish 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) {
8104ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8105ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8106064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8107064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
81089566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8109ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8110ece3a9fcSMatthew G. Knepley }
8111ece3a9fcSMatthew G. Knepley 
8112ece3a9fcSMatthew G. Knepley /*@C
81132716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
81142716604bSToby Isaac 
8115bb7acecfSBarry Smith   Collective on dm
8116bb7acecfSBarry Smith 
81172716604bSToby Isaac   Input Parameters:
8118bb7acecfSBarry Smith + dm    - The `DM`
81190709b2feSToby Isaac . time  - The time
81202716604bSToby Isaac . funcs - The functions to evaluate for each field component
81212716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8122574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
81232716604bSToby Isaac 
81242716604bSToby Isaac   Output Parameter:
81252716604bSToby Isaac . diff - The diff ||u - u_h||_2
81262716604bSToby Isaac 
81272716604bSToby Isaac   Level: developer
81282716604bSToby Isaac 
8129bb7acecfSBarry Smith   Developer Notes:
8130bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8131bb7acecfSBarry Smith 
8132bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8133bb7acecfSBarry Smith 
8134db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
81352716604bSToby Isaac @*/
81369371c9d4SSatish Balay PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) {
81372716604bSToby Isaac   PetscFunctionBegin;
81382716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8139b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
81409566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
81412716604bSToby Isaac   PetscFunctionReturn(0);
81422716604bSToby Isaac }
8143b698f381SToby Isaac 
8144b698f381SToby Isaac /*@C
8145b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8146b698f381SToby Isaac 
8147d083f849SBarry Smith   Collective on dm
8148d083f849SBarry Smith 
8149b698f381SToby Isaac   Input Parameters:
8150bb7acecfSBarry Smith + dm    - The `DM`
8151b698f381SToby Isaac , time  - The time
8152b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8153b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8154574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8155b698f381SToby Isaac - n     - The vector to project along
8156b698f381SToby Isaac 
8157b698f381SToby Isaac   Output Parameter:
8158b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8159b698f381SToby Isaac 
8160b698f381SToby Isaac   Level: developer
8161b698f381SToby Isaac 
8162bb7acecfSBarry Smith   Developer Notes:
8163bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8164bb7acecfSBarry Smith 
8165bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8166bb7acecfSBarry Smith 
8167bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8168b698f381SToby Isaac @*/
81699371c9d4SSatish 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) {
8170b698f381SToby Isaac   PetscFunctionBegin;
8171b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8172b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
81739566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
8174b698f381SToby Isaac   PetscFunctionReturn(0);
8175b698f381SToby Isaac }
8176b698f381SToby Isaac 
81772a16baeaSToby Isaac /*@C
81782a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
81792a16baeaSToby Isaac 
8180d083f849SBarry Smith   Collective on dm
8181d083f849SBarry Smith 
81822a16baeaSToby Isaac   Input Parameters:
8183bb7acecfSBarry Smith + dm    - The `DM`
81842a16baeaSToby Isaac . time  - The time
81852a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
81862a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8187574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
81882a16baeaSToby Isaac 
81892a16baeaSToby Isaac   Output Parameter:
81902a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
81912a16baeaSToby Isaac 
81922a16baeaSToby Isaac   Level: developer
81932a16baeaSToby Isaac 
8194bb7acecfSBarry Smith   Developer Notes:
8195bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8196bb7acecfSBarry Smith 
8197bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8198bb7acecfSBarry Smith 
8199db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
82002a16baeaSToby Isaac @*/
82019371c9d4SSatish Balay PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) {
82022a16baeaSToby Isaac   PetscFunctionBegin;
82032a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82042a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
82059566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
82062a16baeaSToby Isaac   PetscFunctionReturn(0);
82072a16baeaSToby Isaac }
82082a16baeaSToby Isaac 
8209df0b854cSToby Isaac /*@C
8210bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8211502a2867SDave May 
8212502a2867SDave May  Not Collective
8213502a2867SDave May 
8214502a2867SDave May  Input Parameter:
8215bb7acecfSBarry Smith .  dm    - The `DM`
8216502a2867SDave May 
82170a19bb7dSprj-  Output Parameters:
82180a19bb7dSprj- +  nranks - the number of neighbours
82190a19bb7dSprj- -  ranks - the neighbors ranks
8220502a2867SDave May 
8221bb7acecfSBarry Smith  Note:
8222bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8223502a2867SDave May 
8224502a2867SDave May  Level: beginner
8225502a2867SDave May 
8226db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8227502a2867SDave May @*/
82289371c9d4SSatish Balay PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[]) {
8229502a2867SDave May   PetscFunctionBegin;
8230502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82319566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
8232502a2867SDave May   PetscFunctionReturn(0);
8233502a2867SDave May }
8234502a2867SDave May 
8235531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8236531c7667SBarry Smith 
8237531c7667SBarry Smith /*
8238531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8239531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8240531c7667SBarry Smith */
82419371c9d4SSatish Balay PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx) {
8242531c7667SBarry Smith   PetscFunctionBegin;
8243531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8244531c7667SBarry Smith     Vec x1local;
8245531c7667SBarry Smith     DM  dm;
82469566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
82477a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
82489566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
82499566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
82509566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8251531c7667SBarry Smith     x1 = x1local;
8252531c7667SBarry Smith   }
82539566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8254531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8255531c7667SBarry Smith     DM dm;
82569566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
82579566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8258531c7667SBarry Smith   }
8259531c7667SBarry Smith   PetscFunctionReturn(0);
8260531c7667SBarry Smith }
8261531c7667SBarry Smith 
8262531c7667SBarry Smith /*@
8263bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8264531c7667SBarry Smith 
8265531c7667SBarry Smith     Input Parameter:
8266bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8267531c7667SBarry Smith 
8268bb7acecfSBarry Smith     Developer Note:
8269bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8270531c7667SBarry Smith 
82711b266c99SBarry Smith     Level: advanced
82721b266c99SBarry Smith 
8273db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8274531c7667SBarry Smith @*/
82759371c9d4SSatish Balay PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring) {
8276531c7667SBarry Smith   PetscFunctionBegin;
8277531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8278531c7667SBarry Smith   PetscFunctionReturn(0);
8279531c7667SBarry Smith }
82808320bc6fSPatrick Sanan 
82818320bc6fSPatrick Sanan /*@
8282bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
82838320bc6fSPatrick Sanan 
82848320bc6fSPatrick Sanan     Collective
82858320bc6fSPatrick Sanan 
82868320bc6fSPatrick Sanan     Input Parameters:
8287bb7acecfSBarry Smith +    dm1 - the first `DM`
8288bb7acecfSBarry Smith -    dm2 - the second `DM`
82898320bc6fSPatrick Sanan 
82908320bc6fSPatrick Sanan     Output Parameters:
8291bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8292bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
82938320bc6fSPatrick Sanan 
82948320bc6fSPatrick Sanan     Notes:
8295bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
82963d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
82978320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8298bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
82993d862458SPatrick Sanan     decomposition, but hold different data.
83008320bc6fSPatrick Sanan 
83018320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8302bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
83038320bc6fSPatrick Sanan 
8304bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
83058320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
83068320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8307bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
83088320bc6fSPatrick Sanan 
8309bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8310bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
83118320bc6fSPatrick Sanan .vb
83128320bc6fSPatrick Sanan   ...
83139566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
83148320bc6fSPatrick Sanan   if (set && compatible)  {
83159566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
83169566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
83179566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
83188320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
83198320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
83208320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
83218320bc6fSPatrick Sanan       }
83228320bc6fSPatrick Sanan     }
83239566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
83249566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
83258320bc6fSPatrick Sanan   } else {
83268320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
83278320bc6fSPatrick Sanan   }
83288320bc6fSPatrick Sanan   ...
83298320bc6fSPatrick Sanan .ve
83308320bc6fSPatrick Sanan 
8331bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
83328320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
83338320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
83348320bc6fSPatrick Sanan     always check the "set" output parameter.
83358320bc6fSPatrick Sanan 
8336bb7acecfSBarry Smith     A `DM` is always compatible with itself.
83378320bc6fSPatrick Sanan 
8338bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
83398320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
83408320bc6fSPatrick Sanan     incompatible.
83418320bc6fSPatrick Sanan 
83428320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8343bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
83448320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
83458320bc6fSPatrick Sanan 
8346bb7acecfSBarry Smith     Developer Note:
8347bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
83483d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8349a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8350bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
83518320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
83523d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8353bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
83548320bc6fSPatrick Sanan 
83558320bc6fSPatrick Sanan     Level: advanced
83568320bc6fSPatrick Sanan 
8357db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
83588320bc6fSPatrick Sanan @*/
83599371c9d4SSatish Balay PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set) {
83608320bc6fSPatrick Sanan   PetscMPIInt compareResult;
83618320bc6fSPatrick Sanan   DMType      type, type2;
83628320bc6fSPatrick Sanan   PetscBool   sameType;
83638320bc6fSPatrick Sanan 
83648320bc6fSPatrick Sanan   PetscFunctionBegin;
8365a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
83668320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
83678320bc6fSPatrick Sanan 
83688320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8369a5bc1bf3SBarry Smith   if (dm1 == dm2) {
83708320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
83718320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
83728320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83738320bc6fSPatrick Sanan   }
83748320bc6fSPatrick Sanan 
83758320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
83768320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
83778320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
83788320bc6fSPatrick Sanan      determined by the implementation-specific logic */
83799566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
83808320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
83818320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
83828320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
83838320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83848320bc6fSPatrick Sanan   }
83858320bc6fSPatrick Sanan 
83868320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8387a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8388dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
8389b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
83908320bc6fSPatrick Sanan   }
83918320bc6fSPatrick Sanan 
8392a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
83938320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
83949566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
83959566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
83969566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
83978320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8398dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
83998320bc6fSPatrick Sanan   } else {
84008320bc6fSPatrick Sanan     *set = PETSC_FALSE;
84018320bc6fSPatrick Sanan   }
84028320bc6fSPatrick Sanan   PetscFunctionReturn(0);
84038320bc6fSPatrick Sanan }
8404c0f0dcc3SMatthew G. Knepley 
8405c0f0dcc3SMatthew G. Knepley /*@C
8406bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8407c0f0dcc3SMatthew G. Knepley 
8408bb7acecfSBarry Smith   Logically Collective on dm
8409c0f0dcc3SMatthew G. Knepley 
8410c0f0dcc3SMatthew G. Knepley   Input Parameters:
8411bb7acecfSBarry Smith + DM - the `DM`
8412c0f0dcc3SMatthew G. Knepley . f - the monitor function
8413c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8414c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8415c0f0dcc3SMatthew G. Knepley 
8416c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8417bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8418c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8419c0f0dcc3SMatthew G. Knepley 
8420bb7acecfSBarry Smith   Note:
8421c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8422bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8423c0f0dcc3SMatthew G. Knepley   order in which they were set.
8424c0f0dcc3SMatthew G. Knepley 
8425bb7acecfSBarry Smith   Fortran Note:
8426bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8427bb7acecfSBarry Smith 
8428bb7acecfSBarry Smith   Developer Note:
8429bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8430c0f0dcc3SMatthew G. Knepley 
8431c0f0dcc3SMatthew G. Knepley   Level: intermediate
8432c0f0dcc3SMatthew G. Knepley 
8433bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`,`DMMonitorSetFromOptions()`, `DMMonitor()`
8434c0f0dcc3SMatthew G. Knepley @*/
84359371c9d4SSatish Balay PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **)) {
8436c0f0dcc3SMatthew G. Knepley   PetscInt m;
8437c0f0dcc3SMatthew G. Knepley 
8438c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8439c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8440c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8441c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8442c0f0dcc3SMatthew G. Knepley 
84439566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8444c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8445c0f0dcc3SMatthew G. Knepley   }
84467a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8447c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8448c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8449c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
8450c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8451c0f0dcc3SMatthew G. Knepley }
8452c0f0dcc3SMatthew G. Knepley 
8453c0f0dcc3SMatthew G. Knepley /*@
8454bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8455c0f0dcc3SMatthew G. Knepley 
8456bb7acecfSBarry Smith   Logically Collective on dm
8457c0f0dcc3SMatthew G. Knepley 
8458c0f0dcc3SMatthew G. Knepley   Input Parameter:
8459c0f0dcc3SMatthew G. Knepley . dm - the DM
8460c0f0dcc3SMatthew G. Knepley 
8461c0f0dcc3SMatthew G. Knepley   Options Database Key:
8462c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8463bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8464c0f0dcc3SMatthew G. Knepley   set via the options database
8465c0f0dcc3SMatthew G. Knepley 
8466bb7acecfSBarry Smith   Note:
8467bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8468c0f0dcc3SMatthew G. Knepley 
8469c0f0dcc3SMatthew G. Knepley   Level: intermediate
8470c0f0dcc3SMatthew G. Knepley 
8471bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`,  `DMMonitor()`
8472c0f0dcc3SMatthew G. Knepley @*/
84739371c9d4SSatish Balay PetscErrorCode DMMonitorCancel(DM dm) {
8474c0f0dcc3SMatthew G. Knepley   PetscInt m;
8475c0f0dcc3SMatthew G. Knepley 
8476c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8477c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8478c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
84799566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8480c0f0dcc3SMatthew G. Knepley   }
8481c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8482c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8483c0f0dcc3SMatthew G. Knepley }
8484c0f0dcc3SMatthew G. Knepley 
8485c0f0dcc3SMatthew G. Knepley /*@C
8486c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8487c0f0dcc3SMatthew G. Knepley 
8488bb7acecfSBarry Smith   Collective on dm
8489c0f0dcc3SMatthew G. Knepley 
8490c0f0dcc3SMatthew G. Knepley   Input Parameters:
8491bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8492c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8493c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8494c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8495c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8496bb7acecfSBarry 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
8497c0f0dcc3SMatthew G. Knepley 
8498c0f0dcc3SMatthew G. Knepley   Output Parameter:
8499c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8500c0f0dcc3SMatthew G. Knepley 
8501c0f0dcc3SMatthew G. Knepley   Level: developer
8502c0f0dcc3SMatthew G. Knepley 
8503db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8504db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8505db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8506db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8507c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8508db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8509bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8510c0f0dcc3SMatthew G. Knepley @*/
85119371c9d4SSatish Balay PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg) {
8512c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8513c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8514c0f0dcc3SMatthew G. Knepley 
8515c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8516c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8518c0f0dcc3SMatthew G. Knepley   if (*flg) {
8519c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8520c0f0dcc3SMatthew G. Knepley 
85219566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
85229566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
85239566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
85249566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8525c0f0dcc3SMatthew G. Knepley   }
8526c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8527c0f0dcc3SMatthew G. Knepley }
8528c0f0dcc3SMatthew G. Knepley 
8529c0f0dcc3SMatthew G. Knepley /*@
8530c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8531c0f0dcc3SMatthew G. Knepley 
8532bb7acecfSBarry Smith    Collective on dm
8533c0f0dcc3SMatthew G. Knepley 
8534c0f0dcc3SMatthew G. Knepley    Input Parameters:
8535bb7acecfSBarry Smith .  dm - The `DM`
8536c0f0dcc3SMatthew G. Knepley 
8537c0f0dcc3SMatthew G. Knepley    Level: developer
8538c0f0dcc3SMatthew G. Knepley 
8539bb7acecfSBarry Smith    Question:
8540bb7acecfSBarry 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
8541bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8542bb7acecfSBarry Smith 
8543bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8544c0f0dcc3SMatthew G. Knepley @*/
85459371c9d4SSatish Balay PetscErrorCode DMMonitor(DM dm) {
8546c0f0dcc3SMatthew G. Knepley   PetscInt m;
8547c0f0dcc3SMatthew G. Knepley 
8548c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8549c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8550c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
855148a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8552c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8553c0f0dcc3SMatthew G. Knepley }
85542e4af2aeSMatthew G. Knepley 
85552e4af2aeSMatthew G. Knepley /*@
8556bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
85572e4af2aeSMatthew G. Knepley 
8558bb7acecfSBarry Smith   Collective on dm
85592e4af2aeSMatthew G. Knepley 
85602e4af2aeSMatthew G. Knepley   Input Parameters:
8561bb7acecfSBarry Smith + dm     - The `DM`
85626b867d5aSJose E. Roman - sol    - The solution vector
85632e4af2aeSMatthew G. Knepley 
85646b867d5aSJose E. Roman   Input/Output Parameter:
85656b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
85666b867d5aSJose E. Roman            contains the error in each field
85676b867d5aSJose E. Roman 
85686b867d5aSJose E. Roman   Output Parameter:
85696b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
85702e4af2aeSMatthew G. Knepley 
8571bb7acecfSBarry Smith   Note:
8572bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
85732e4af2aeSMatthew G. Knepley 
85742e4af2aeSMatthew G. Knepley   Level: developer
85752e4af2aeSMatthew G. Knepley 
8576db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
85772e4af2aeSMatthew G. Knepley @*/
85789371c9d4SSatish Balay PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec) {
85792e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
85802e4af2aeSMatthew G. Knepley   void    **ctxs;
85812e4af2aeSMatthew G. Knepley   PetscReal time;
85822e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
85832e4af2aeSMatthew G. Knepley 
85842e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
85859566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
85869566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
85879566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
85882e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
85892e4af2aeSMatthew G. Knepley     PetscDS         ds;
85902e4af2aeSMatthew G. Knepley     DMLabel         label;
85912e4af2aeSMatthew G. Knepley     IS              fieldIS;
85922e4af2aeSMatthew G. Knepley     const PetscInt *fields;
85932e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
85942e4af2aeSMatthew G. Knepley 
85959566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
85969566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
85979566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
85982e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
85992e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
86009566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
86012e4af2aeSMatthew G. Knepley     }
86029566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
86032e4af2aeSMatthew G. Knepley   }
8604ad540459SPierre Jolivet   for (f = 0; f < Nf; ++f) PetscCheck(exactSol[f], PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "DS must contain exact solution functions in order to calculate error, missing for field %" PetscInt_FMT, f);
86059566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
86069566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
86072e4af2aeSMatthew G. Knepley   if (errorVec) {
86082e4af2aeSMatthew G. Knepley     DM             edm;
86092e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
86102e4af2aeSMatthew G. Knepley     PetscBool      simplex;
86112e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
86122e4af2aeSMatthew G. Knepley 
86139566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
86149566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
86159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
86169566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
86172e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
86189566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
86192e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
86202e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
86212e4af2aeSMatthew G. Knepley       PetscQuadrature q;
86222e4af2aeSMatthew G. Knepley       const char     *name;
86232e4af2aeSMatthew G. Knepley 
86249566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
86259566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
86269566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
86279566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
86289566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
86299566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
86309566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
86319566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
86322e4af2aeSMatthew G. Knepley     }
86339566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
86342e4af2aeSMatthew G. Knepley 
86359566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
86369566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
86379566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
86389566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
86392e4af2aeSMatthew G. Knepley   }
86409566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
86412e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
86422e4af2aeSMatthew G. Knepley }
86439a2a23afSMatthew G. Knepley 
86449a2a23afSMatthew G. Knepley /*@
8645bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
86469a2a23afSMatthew G. Knepley 
86479a2a23afSMatthew G. Knepley   Not collective
86489a2a23afSMatthew G. Knepley 
86499a2a23afSMatthew G. Knepley   Input Parameter:
8650bb7acecfSBarry Smith . dm     - The `DM`
86519a2a23afSMatthew G. Knepley 
86529a2a23afSMatthew G. Knepley   Output Parameter:
8653a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
86549a2a23afSMatthew G. Knepley 
86559a2a23afSMatthew G. Knepley   Level: advanced
86569a2a23afSMatthew G. Knepley 
8657bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
86589a2a23afSMatthew G. Knepley @*/
86599371c9d4SSatish Balay PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux) {
86609a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86619a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86629566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
86639a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
86649a2a23afSMatthew G. Knepley }
86659a2a23afSMatthew G. Knepley 
86669a2a23afSMatthew G. Knepley /*@
8667ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
86689a2a23afSMatthew G. Knepley 
86699a2a23afSMatthew G. Knepley   Not collective
86709a2a23afSMatthew G. Knepley 
86719a2a23afSMatthew G. Knepley   Input Parameters:
8672bb7acecfSBarry Smith + dm     - The `DM`
8673bb7acecfSBarry Smith . label  - The `DMLabel`
8674ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8675ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
86769a2a23afSMatthew G. Knepley 
86779a2a23afSMatthew G. Knepley   Output Parameter:
8678bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
86799a2a23afSMatthew G. Knepley 
8680bb7acecfSBarry Smith   Note:
8681bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
868204c51a94SMatthew G. Knepley 
86839a2a23afSMatthew G. Knepley   Level: advanced
86849a2a23afSMatthew G. Knepley 
8685bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
86869a2a23afSMatthew G. Knepley @*/
86879371c9d4SSatish Balay PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux) {
8688ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
868904c51a94SMatthew G. Knepley   PetscBool       has;
86909a2a23afSMatthew G. Knepley 
86919a2a23afSMatthew G. Knepley   PetscFunctionBegin;
86929a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86939a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
86949a2a23afSMatthew G. Knepley   key.label = label;
86959a2a23afSMatthew G. Knepley   key.value = value;
8696ac17215fSMatthew G. Knepley   key.part  = part;
86979566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
86989566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
86999566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
87009a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
87019a2a23afSMatthew G. Knepley }
87029a2a23afSMatthew G. Knepley 
87039a2a23afSMatthew G. Knepley /*@
8704bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
87059a2a23afSMatthew G. Knepley 
8706bb7acecfSBarry Smith   Not collective because auxilary vectors are not parallel
87079a2a23afSMatthew G. Knepley 
87089a2a23afSMatthew G. Knepley   Input Parameters:
8709bb7acecfSBarry Smith + dm     - The `DM`
8710bb7acecfSBarry Smith . label  - The `DMLabel`
87119a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8712ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
8713bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
87149a2a23afSMatthew G. Knepley 
87159a2a23afSMatthew G. Knepley   Level: advanced
87169a2a23afSMatthew G. Knepley 
8717bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
87189a2a23afSMatthew G. Knepley @*/
87199371c9d4SSatish Balay PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux) {
87209a2a23afSMatthew G. Knepley   Vec             old;
87219a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
87229a2a23afSMatthew G. Knepley 
87239a2a23afSMatthew G. Knepley   PetscFunctionBegin;
87249a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87259a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
87269a2a23afSMatthew G. Knepley   key.label = label;
87279a2a23afSMatthew G. Knepley   key.value = value;
8728ac17215fSMatthew G. Knepley   key.part  = part;
87299566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
87309566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
87319566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
87329566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
87339566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
87349a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
87359a2a23afSMatthew G. Knepley }
87369a2a23afSMatthew G. Knepley 
87379a2a23afSMatthew G. Knepley /*@C
8738bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
87399a2a23afSMatthew G. Knepley 
87409a2a23afSMatthew G. Knepley   Not collective
87419a2a23afSMatthew G. Knepley 
87429a2a23afSMatthew G. Knepley   Input Parameter:
8743bb7acecfSBarry Smith . dm      - The `DM`
87449a2a23afSMatthew G. Knepley 
87459a2a23afSMatthew G. Knepley   Output Parameters:
8746bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
8747bb7acecfSBarry Smith . values  - The label values for each `Vec`
8748bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
87499a2a23afSMatthew G. Knepley 
8750bb7acecfSBarry Smith   Note:
8751bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
87529a2a23afSMatthew G. Knepley 
87539a2a23afSMatthew G. Knepley   Level: advanced
87549a2a23afSMatthew G. Knepley 
8755bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
87569a2a23afSMatthew G. Knepley @*/
87579371c9d4SSatish Balay PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[]) {
87589a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
87599a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
87609a2a23afSMatthew G. Knepley 
87619a2a23afSMatthew G. Knepley   PetscFunctionBegin;
87629a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87639a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
8764dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
8765dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
87669566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
87679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
87689566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
87699371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
87709371c9d4SSatish Balay     labels[i] = keys[i].label;
87719371c9d4SSatish Balay     values[i] = keys[i].value;
87729371c9d4SSatish Balay     parts[i]  = keys[i].part;
87739371c9d4SSatish Balay   }
87749566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
87759a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
87769a2a23afSMatthew G. Knepley }
87779a2a23afSMatthew G. Knepley 
87789a2a23afSMatthew G. Knepley /*@
8779bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
87809a2a23afSMatthew G. Knepley 
87819a2a23afSMatthew G. Knepley   Not collective
87829a2a23afSMatthew G. Knepley 
87839a2a23afSMatthew G. Knepley   Input Parameter:
8784bb7acecfSBarry Smith . dm    - The `DM`
87859a2a23afSMatthew G. Knepley 
87869a2a23afSMatthew G. Knepley   Output Parameter:
8787bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
87889a2a23afSMatthew G. Knepley 
87899a2a23afSMatthew G. Knepley   Level: advanced
87909a2a23afSMatthew G. Knepley 
8791bb7acecfSBarry Smith   Note:
8792bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
8793bb7acecfSBarry Smith 
8794db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
87959a2a23afSMatthew G. Knepley @*/
87969371c9d4SSatish Balay PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew) {
87979a2a23afSMatthew G. Knepley   PetscFunctionBegin;
87989a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
88009566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
88019a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
88029a2a23afSMatthew G. Knepley }
8803b5a892a1SMatthew G. Knepley 
8804b5a892a1SMatthew G. Knepley /*@C
8805bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
8806b5a892a1SMatthew G. Knepley 
8807b5a892a1SMatthew G. Knepley   Not collective
8808b5a892a1SMatthew G. Knepley 
8809b5a892a1SMatthew G. Knepley   Input Parameters:
8810bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8811b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8812b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8813b5a892a1SMatthew G. Knepley 
8814b5a892a1SMatthew G. Knepley   Output Parameters:
8815bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8816b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8817b5a892a1SMatthew G. Knepley 
8818b5a892a1SMatthew G. Knepley   Level: advanced
8819b5a892a1SMatthew G. Knepley 
8820bb7acecfSBarry Smith   Note:
8821bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
8822bb7acecfSBarry Smith 
8823bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
8824bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
8825bb7acecfSBarry Smith 
8826bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
8827bb7acecfSBarry Smith 
8828bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
8829b5a892a1SMatthew G. Knepley @*/
88309371c9d4SSatish Balay PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found) {
8831b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
8832b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
8833b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8834b5a892a1SMatthew G. Knepley 
8835b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
88369371c9d4SSatish Balay   if (!nO) {
88379371c9d4SSatish Balay     *ornt  = 0;
88389371c9d4SSatish Balay     *found = PETSC_TRUE;
88399371c9d4SSatish Balay     PetscFunctionReturn(0);
88409371c9d4SSatish Balay   }
8841b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8842b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
8843b5a892a1SMatthew G. Knepley 
88449371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
88459371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
88469371c9d4SSatish Balay     if (c == cS) {
88479371c9d4SSatish Balay       *ornt = o;
88489371c9d4SSatish Balay       break;
88499371c9d4SSatish Balay     }
8850b5a892a1SMatthew G. Knepley   }
8851b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8852b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8853b5a892a1SMatthew G. Knepley }
8854b5a892a1SMatthew G. Knepley 
8855b5a892a1SMatthew G. Knepley /*@C
8856bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
8857b5a892a1SMatthew G. Knepley 
8858b5a892a1SMatthew G. Knepley   Not collective
8859b5a892a1SMatthew G. Knepley 
8860b5a892a1SMatthew G. Knepley   Input Parameters:
8861bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8862b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
8863b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
8864b5a892a1SMatthew G. Knepley 
8865b5a892a1SMatthew G. Knepley   Output Parameters:
8866bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8867b5a892a1SMatthew G. Knepley 
8868b5a892a1SMatthew G. Knepley   Level: advanced
8869b5a892a1SMatthew G. Knepley 
8870bb7acecfSBarry Smith   Note:
8871bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
8872bb7acecfSBarry Smith 
8873bb7acecfSBarry Smith   Developer Note:
8874bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
8875bb7acecfSBarry Smith 
8876bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
8877b5a892a1SMatthew G. Knepley @*/
88789371c9d4SSatish Balay PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt) {
8879b5a892a1SMatthew G. Knepley   PetscBool found;
8880b5a892a1SMatthew G. Knepley 
8881b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
88829566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
88837a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8884b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8885b5a892a1SMatthew G. Knepley }
8886b5a892a1SMatthew G. Knepley 
8887b5a892a1SMatthew G. Knepley /*@C
8888bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
8889b5a892a1SMatthew G. Knepley 
8890b5a892a1SMatthew G. Knepley   Not collective
8891b5a892a1SMatthew G. Knepley 
8892b5a892a1SMatthew G. Knepley   Input Parameters:
8893bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8894b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
8895b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
8896b5a892a1SMatthew G. Knepley 
8897b5a892a1SMatthew G. Knepley   Output Parameters:
8898bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8899b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
8900b5a892a1SMatthew G. Knepley 
8901b5a892a1SMatthew G. Knepley   Level: advanced
8902b5a892a1SMatthew G. Knepley 
8903bb7acecfSBarry Smith   Note:
8904bb7acecfSBarry Smith   An arrangement is a vertex order
8905bb7acecfSBarry Smith 
8906bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
8907bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
8908bb7acecfSBarry Smith 
8909bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
8910bb7acecfSBarry Smith 
8911bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
8912b5a892a1SMatthew G. Knepley @*/
89139371c9d4SSatish Balay PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found) {
8914b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
8915b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
8916b5a892a1SMatthew G. Knepley   PetscInt       o, c;
8917b5a892a1SMatthew G. Knepley 
8918b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
89199371c9d4SSatish Balay   if (!nO) {
89209371c9d4SSatish Balay     *ornt  = 0;
89219371c9d4SSatish Balay     *found = PETSC_TRUE;
89229371c9d4SSatish Balay     PetscFunctionReturn(0);
89239371c9d4SSatish Balay   }
8924b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
8925b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
8926b5a892a1SMatthew G. Knepley 
89279371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
89289371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
89299371c9d4SSatish Balay     if (c == cS) {
89309371c9d4SSatish Balay       *ornt = o;
89319371c9d4SSatish Balay       break;
89329371c9d4SSatish Balay     }
8933b5a892a1SMatthew G. Knepley   }
8934b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
8935b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8936b5a892a1SMatthew G. Knepley }
8937b5a892a1SMatthew G. Knepley 
8938b5a892a1SMatthew G. Knepley /*@C
8939bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
8940b5a892a1SMatthew G. Knepley 
8941b5a892a1SMatthew G. Knepley   Not collective
8942b5a892a1SMatthew G. Knepley 
8943b5a892a1SMatthew G. Knepley   Input Parameters:
8944bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
8945b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
8946b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
8947b5a892a1SMatthew G. Knepley 
8948b5a892a1SMatthew G. Knepley   Output Parameters:
8949bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
8950b5a892a1SMatthew G. Knepley 
8951b5a892a1SMatthew G. Knepley   Level: advanced
8952b5a892a1SMatthew G. Knepley 
8953bb7acecfSBarry Smith   Note:
8954bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
8955bb7acecfSBarry Smith 
8956bb7acecfSBarry Smith   Developer Note:
8957bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
8958bb7acecfSBarry Smith 
8959bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
8960b5a892a1SMatthew G. Knepley @*/
89619371c9d4SSatish Balay PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt) {
8962b5a892a1SMatthew G. Knepley   PetscBool found;
8963b5a892a1SMatthew G. Knepley 
8964b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
89659566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
89667a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
8967b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
8968b5a892a1SMatthew G. Knepley }
8969012bc364SMatthew G. Knepley 
8970012bc364SMatthew G. Knepley /*@C
8971012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
8972012bc364SMatthew G. Knepley 
8973012bc364SMatthew G. Knepley   Not collective
8974012bc364SMatthew G. Knepley 
8975012bc364SMatthew G. Knepley   Input Parameters:
8976bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
8977012bc364SMatthew G. Knepley - point - Coordinates of the point
8978012bc364SMatthew G. Knepley 
8979012bc364SMatthew G. Knepley   Output Parameters:
8980012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
8981012bc364SMatthew G. Knepley 
8982012bc364SMatthew G. Knepley   Level: advanced
8983012bc364SMatthew G. Knepley 
8984bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
8985012bc364SMatthew G. Knepley @*/
89869371c9d4SSatish Balay PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside) {
8987012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
8988012bc364SMatthew G. Knepley   PetscInt  d;
8989012bc364SMatthew G. Knepley 
8990012bc364SMatthew G. Knepley   PetscFunctionBegin;
8991012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
8992012bc364SMatthew G. Knepley   switch (ct) {
8993012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
8994012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
8995012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
89969371c9d4SSatish Balay       if (point[d] < -1.0) {
89979371c9d4SSatish Balay         *inside = PETSC_FALSE;
89989371c9d4SSatish Balay         break;
89999371c9d4SSatish Balay       }
9000012bc364SMatthew G. Knepley       sum += point[d];
9001012bc364SMatthew G. Knepley     }
90029371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
90039371c9d4SSatish Balay       *inside = PETSC_FALSE;
90049371c9d4SSatish Balay       break;
90059371c9d4SSatish Balay     }
9006012bc364SMatthew G. Knepley     break;
9007012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9008012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9009012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
90109371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
90119371c9d4SSatish Balay         *inside = PETSC_FALSE;
9012012bc364SMatthew G. Knepley         break;
90139371c9d4SSatish Balay       }
90149371c9d4SSatish Balay     break;
90159371c9d4SSatish Balay   default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9016012bc364SMatthew G. Knepley   }
9017012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
9018012bc364SMatthew G. Knepley }
9019