xref: /petsc/src/dm/interface/dm.c (revision a4e35b1925eceef64945ea472b84f2bf06a67b5e)
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;
20708be2fdSJed Brown 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, DM_ProjectFunction;
2167a56275SMatthew G Knepley 
22ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
23d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "INVALID", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "INVALID", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType", "DM_BC_", NULL};
240e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
259371c9d4SSatish Balay const char *const DMPolytopeTypes[]   = {"vertex",  "segment",       "tensor_segment",      "triangle", "quadrilateral", "tensor_quad",    "tetrahedron",  "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism",
269371c9d4SSatish Balay                                          "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown",  "invalid",       "DMPolytopeType", "DM_POLYTOPE_", NULL};
272cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2860c22052SBarry Smith 
29a4121054SBarry Smith /*@
30bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
31bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
32a4121054SBarry Smith 
33d083f849SBarry Smith   Collective
34a4121054SBarry Smith 
35a4121054SBarry Smith   Input Parameter:
36bb7acecfSBarry Smith . comm - The communicator for the `DM` object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Output Parameter:
39bb7acecfSBarry Smith . dm - The `DM` object
40a4121054SBarry Smith 
41a4121054SBarry Smith   Level: beginner
42a4121054SBarry Smith 
43bb7acecfSBarry Smith   Notes:
44bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
45bb7acecfSBarry Smith 
46bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
47bb7acecfSBarry Smith   error when you try to use the dm.
48bb7acecfSBarry Smith 
491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
50a4121054SBarry Smith @*/
51d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
52d71ae5a4SJacob Faibussowitsch {
53a4121054SBarry Smith   DM      v;
54e5e52638SMatthew G. Knepley   PetscDS ds;
55a4121054SBarry Smith 
56a4121054SBarry Smith   PetscFunctionBegin;
574f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
580298fd71SBarry Smith   *dm = NULL;
599566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
60a4121054SBarry Smith 
619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
62e7c4fc90SDmitry Karpeev 
6362e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6462e5d2d2SJDBetteridge 
6549be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6649be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
670298fd71SBarry Smith   v->ltogmap              = NULL;
68a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
691411c6eeSJed Brown   v->bs                   = 1;
70171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
719566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
729566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
73c58f1c22SToby Isaac   v->labels                    = NULL;
7434aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7534aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
76c58f1c22SToby Isaac   v->depthLabel                = NULL;
77ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
781bb6d2a8SBarry Smith   v->localSection              = NULL;
791bb6d2a8SBarry Smith   v->globalSection             = NULL;
803b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
813b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8279769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
836858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
846858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
856858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8696173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
87435a35e8SMatthew G Knepley   {
88435a35e8SMatthew G Knepley     PetscInt i;
89435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
900298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
91f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
92435a35e8SMatthew G Knepley     }
93435a35e8SMatthew G Knepley   }
949566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9507218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
969566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
979566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9814f150ffSMatthew G. Knepley   v->dmBC              = NULL;
99a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
100f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
101cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1029566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1039566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1044a7a4c06SLawrence Mitchell 
1051411c6eeSJed Brown   *dm = v;
1063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
107a4121054SBarry Smith }
108a4121054SBarry Smith 
10938221697SMatthew G. Knepley /*@
110bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11138221697SMatthew G. Knepley 
112d083f849SBarry Smith   Collective
11338221697SMatthew G. Knepley 
11438221697SMatthew G. Knepley   Input Parameter:
115bb7acecfSBarry Smith . dm - The original `DM` object
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   Output Parameter:
118bb7acecfSBarry Smith . newdm - The new `DM` object
11938221697SMatthew G. Knepley 
12038221697SMatthew G. Knepley   Level: beginner
12138221697SMatthew G. Knepley 
1221cb8cacdSPatrick Sanan   Notes:
123bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
124bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
125bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1261bb6d2a8SBarry Smith 
127bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12889706ed2SPatrick Sanan 
129bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
130bb7acecfSBarry Smith 
13160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
13238221697SMatthew G. Knepley @*/
133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
134d71ae5a4SJacob Faibussowitsch {
13538221697SMatthew G. Knepley   PetscSF  sf;
13638221697SMatthew G. Knepley   Vec      coords;
13738221697SMatthew G. Knepley   void    *ctx;
1386858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
13938221697SMatthew G. Knepley 
14038221697SMatthew G. Knepley   PetscFunctionBegin;
14138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1424f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1439566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1449566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
145ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
146ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
147c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
148fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1499566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1509566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1519566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1529566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1539566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1549566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
155dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1563f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1579566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1589566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1599566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1609566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1616858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1626858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
163be4c1c3eSMatthew G. Knepley       DM           ncdm;
164be4c1c3eSMatthew G. Knepley       PetscSection cs;
1655a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
166be4c1c3eSMatthew G. Knepley 
1676858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1689566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
169712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1705a0206caSToby Isaac       if (pEndMax >= 0) {
1716858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1726858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1739566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1746858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1756858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1769566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
177be4c1c3eSMatthew G. Knepley       }
178be4c1c3eSMatthew G. Knepley     }
1796858538eSMatthew G. Knepley   }
1809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1819566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1829566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18338221697SMatthew G. Knepley   if (coords) {
1849566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18538221697SMatthew G. Knepley   } else {
1869566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1879566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18838221697SMatthew G. Knepley   }
1896858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1906858538eSMatthew G. Knepley   if (coords) {
1916858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1926858538eSMatthew G. Knepley   } else {
1936858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1946858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1956858538eSMatthew G. Knepley   }
19690b157c4SStefano Zampini   {
1974fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1986858538eSMatthew G. Knepley 
1994fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2004fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
201c6b900c6SMatthew G. Knepley   }
20234aa8a36SMatthew G. Knepley   {
20334aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20434aa8a36SMatthew G. Knepley 
2059566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2069566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20734aa8a36SMatthew G. Knepley   }
2083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20938221697SMatthew G. Knepley }
21038221697SMatthew G. Knepley 
2119a42bb27SBarry Smith /*@C
212bb7acecfSBarry Smith   DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2139a42bb27SBarry Smith 
21420f4b53cSBarry Smith   Logically Collective
2159a42bb27SBarry Smith 
216147403d9SBarry Smith   Input Parameters:
2179a42bb27SBarry Smith + da    - initial distributed array
218bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2199a42bb27SBarry Smith 
22020f4b53cSBarry Smith   Options Database Key:
221147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2229a42bb27SBarry Smith 
2239a42bb27SBarry Smith   Level: intermediate
2249a42bb27SBarry Smith 
22560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
226bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2279a42bb27SBarry Smith @*/
228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype)
229d71ae5a4SJacob Faibussowitsch {
2309a42bb27SBarry Smith   PetscFunctionBegin;
2319a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2329566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2339566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
2343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2359a42bb27SBarry Smith }
2369a42bb27SBarry Smith 
237c0dedaeaSBarry Smith /*@C
238bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
239c0dedaeaSBarry Smith 
24020f4b53cSBarry Smith   Logically Collective
241c0dedaeaSBarry Smith 
242c0dedaeaSBarry Smith   Input Parameter:
243c0dedaeaSBarry Smith . da - initial distributed array
244c0dedaeaSBarry Smith 
245c0dedaeaSBarry Smith   Output Parameter:
246c0dedaeaSBarry Smith . ctype - the vector type
247c0dedaeaSBarry Smith 
248c0dedaeaSBarry Smith   Level: intermediate
249c0dedaeaSBarry Smith 
25060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
251c0dedaeaSBarry Smith @*/
252d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
253d71ae5a4SJacob Faibussowitsch {
254c0dedaeaSBarry Smith   PetscFunctionBegin;
255c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
256c0dedaeaSBarry Smith   *ctype = da->vectype;
2573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
258c0dedaeaSBarry Smith }
259c0dedaeaSBarry Smith 
2605f1ad066SMatthew G Knepley /*@
261bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2625f1ad066SMatthew G Knepley 
26320f4b53cSBarry Smith   Not Collective
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley   Input Parameter:
266bb7acecfSBarry Smith . v - The `Vec`
2675f1ad066SMatthew G Knepley 
2685f1ad066SMatthew G Knepley   Output Parameter:
269bb7acecfSBarry Smith . dm - The `DM`
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley   Level: intermediate
2725f1ad066SMatthew G Knepley 
273bb7acecfSBarry Smith   Note:
274bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
275bb7acecfSBarry Smith 
2761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2775f1ad066SMatthew G Knepley @*/
278d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
279d71ae5a4SJacob Faibussowitsch {
2805f1ad066SMatthew G Knepley   PetscFunctionBegin;
2815f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2824f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2839566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2855f1ad066SMatthew G Knepley }
2865f1ad066SMatthew G Knepley 
2875f1ad066SMatthew G Knepley /*@
288bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2895f1ad066SMatthew G Knepley 
29020f4b53cSBarry Smith   Not Collective
2915f1ad066SMatthew G Knepley 
2925f1ad066SMatthew G Knepley   Input Parameters:
293bb7acecfSBarry Smith + v  - The `Vec`
294bb7acecfSBarry Smith - dm - The `DM`
2955f1ad066SMatthew G Knepley 
29620f4b53cSBarry Smith   Level: developer
29720f4b53cSBarry Smith 
298bb7acecfSBarry Smith   Note:
299bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
300d9805387SMatthew G. Knepley 
301bb7acecfSBarry 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.
302bb7acecfSBarry Smith 
3031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3045f1ad066SMatthew G Knepley @*/
305d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
306d71ae5a4SJacob Faibussowitsch {
3075f1ad066SMatthew G Knepley   PetscFunctionBegin;
3085f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
309d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3109566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3125f1ad066SMatthew G Knepley }
3135f1ad066SMatthew G Knepley 
314521d9a4cSLisandro Dalcin /*@C
315bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3168f1509bcSBarry Smith 
31720f4b53cSBarry Smith   Logically Collective
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith   Input Parameters:
320bb7acecfSBarry Smith + dm    - the `DM` context
3218f1509bcSBarry Smith - ctype - the matrix type
3228f1509bcSBarry Smith 
32320f4b53cSBarry Smith   Options Database Key:
3248f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3258f1509bcSBarry Smith 
3268f1509bcSBarry Smith   Level: intermediate
3278f1509bcSBarry Smith 
3281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
329bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3308f1509bcSBarry Smith @*/
331d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
332d71ae5a4SJacob Faibussowitsch {
3338f1509bcSBarry Smith   PetscFunctionBegin;
3348f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3358f1509bcSBarry Smith   dm->coloringtype = ctype;
3363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3378f1509bcSBarry Smith }
3388f1509bcSBarry Smith 
3398f1509bcSBarry Smith /*@C
340bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
341521d9a4cSLisandro Dalcin 
34220f4b53cSBarry Smith   Logically Collective
343521d9a4cSLisandro Dalcin 
344521d9a4cSLisandro Dalcin   Input Parameter:
345bb7acecfSBarry Smith . dm - the `DM` context
3468f1509bcSBarry Smith 
3478f1509bcSBarry Smith   Output Parameter:
3488f1509bcSBarry Smith . ctype - the matrix type
3498f1509bcSBarry Smith 
35020f4b53cSBarry Smith   Options Database Key:
3518f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3528f1509bcSBarry Smith 
3538f1509bcSBarry Smith   Level: intermediate
3548f1509bcSBarry Smith 
3551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
35642747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3578f1509bcSBarry Smith @*/
358d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
359d71ae5a4SJacob Faibussowitsch {
3608f1509bcSBarry Smith   PetscFunctionBegin;
3618f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3628f1509bcSBarry Smith   *ctype = dm->coloringtype;
3633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3648f1509bcSBarry Smith }
3658f1509bcSBarry Smith 
3668f1509bcSBarry Smith /*@C
367bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3688f1509bcSBarry Smith 
36920f4b53cSBarry Smith   Logically Collective
3708f1509bcSBarry Smith 
3718f1509bcSBarry Smith   Input Parameters:
372bb7acecfSBarry Smith + dm    - the `DM` context
373bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
374521d9a4cSLisandro Dalcin 
37520f4b53cSBarry Smith   Options Database Key:
376bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
377521d9a4cSLisandro Dalcin 
378521d9a4cSLisandro Dalcin   Level: intermediate
379521d9a4cSLisandro Dalcin 
38042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
381521d9a4cSLisandro Dalcin @*/
382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
383d71ae5a4SJacob Faibussowitsch {
384521d9a4cSLisandro Dalcin   PetscFunctionBegin;
385521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3869566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3879566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
3883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
389521d9a4cSLisandro Dalcin }
390521d9a4cSLisandro Dalcin 
391c0dedaeaSBarry Smith /*@C
39220f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
393c0dedaeaSBarry Smith 
39420f4b53cSBarry Smith   Logically Collective
395c0dedaeaSBarry Smith 
396c0dedaeaSBarry Smith   Input Parameter:
397bb7acecfSBarry Smith . dm - the `DM` context
398c0dedaeaSBarry Smith 
399c0dedaeaSBarry Smith   Output Parameter:
400c0dedaeaSBarry Smith . ctype - the matrix type
401c0dedaeaSBarry Smith 
402c0dedaeaSBarry Smith   Level: intermediate
403c0dedaeaSBarry Smith 
40442747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
405c0dedaeaSBarry Smith @*/
406d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
407d71ae5a4SJacob Faibussowitsch {
408c0dedaeaSBarry Smith   PetscFunctionBegin;
409c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
410c0dedaeaSBarry Smith   *ctype = dm->mattype;
4113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
412c0dedaeaSBarry Smith }
413c0dedaeaSBarry Smith 
414c688c046SMatthew G Knepley /*@
415bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
416c688c046SMatthew G Knepley 
41720f4b53cSBarry Smith   Not Collective
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   Input Parameter:
420bb7acecfSBarry Smith . A - The `Mat`
421c688c046SMatthew G Knepley 
422c688c046SMatthew G Knepley   Output Parameter:
423bb7acecfSBarry Smith . dm - The `DM`
424c688c046SMatthew G Knepley 
425c688c046SMatthew G Knepley   Level: intermediate
426c688c046SMatthew G Knepley 
427bb7acecfSBarry Smith   Note:
428bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
429bb7acecfSBarry Smith 
43060225df5SJacob Faibussowitsch   Developer Notes:
431bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4328f1509bcSBarry Smith 
4331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
434c688c046SMatthew G Knepley @*/
435d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
436d71ae5a4SJacob Faibussowitsch {
437c688c046SMatthew G Knepley   PetscFunctionBegin;
438c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4394f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4409566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
442c688c046SMatthew G Knepley }
443c688c046SMatthew G Knepley 
444c688c046SMatthew G Knepley /*@
445bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
446c688c046SMatthew G Knepley 
44720f4b53cSBarry Smith   Not Collective
448c688c046SMatthew G Knepley 
449c688c046SMatthew G Knepley   Input Parameters:
45020f4b53cSBarry Smith + A  - The `Mat`
45120f4b53cSBarry Smith - dm - The `DM`
452c688c046SMatthew G Knepley 
453bb7acecfSBarry Smith   Level: developer
454c688c046SMatthew G Knepley 
455bb7acecfSBarry Smith   Note:
456bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
457bb7acecfSBarry Smith 
45860225df5SJacob Faibussowitsch   Developer Notes:
459bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
460bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4618f1509bcSBarry Smith 
4621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
463c688c046SMatthew G Knepley @*/
464d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
465d71ae5a4SJacob Faibussowitsch {
466c688c046SMatthew G Knepley   PetscFunctionBegin;
467c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4688865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4699566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471c688c046SMatthew G Knepley }
472c688c046SMatthew G Knepley 
4739a42bb27SBarry Smith /*@C
474bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4759a42bb27SBarry Smith 
47620f4b53cSBarry Smith   Logically Collective
4779a42bb27SBarry Smith 
478d8d19677SJose E. Roman   Input Parameters:
47960225df5SJacob Faibussowitsch + dm     - the `DM` context
480bb7acecfSBarry Smith - prefix - the prefix to prepend
4819a42bb27SBarry Smith 
48220f4b53cSBarry Smith   Level: advanced
48320f4b53cSBarry Smith 
48420f4b53cSBarry Smith   Note:
4859a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
4869a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
4879a42bb27SBarry Smith 
4881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4899a42bb27SBarry Smith @*/
490d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
491d71ae5a4SJacob Faibussowitsch {
4929a42bb27SBarry Smith   PetscFunctionBegin;
4939a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4949566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4951baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4961baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
4973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4989a42bb27SBarry Smith }
4999a42bb27SBarry Smith 
50031697293SDave May /*@C
501da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
502bb7acecfSBarry Smith   `DM` options in the options database.
50331697293SDave May 
50420f4b53cSBarry Smith   Logically Collective
50531697293SDave May 
50631697293SDave May   Input Parameters:
507bb7acecfSBarry Smith + dm     - the `DM` context
508bb7acecfSBarry Smith - prefix - the string to append to the current prefix
50931697293SDave May 
51020f4b53cSBarry Smith   Level: advanced
51120f4b53cSBarry Smith 
51220f4b53cSBarry Smith   Note:
513bb7acecfSBarry 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.
51431697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
51531697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
51631697293SDave May 
5171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51831697293SDave May @*/
519d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
520d71ae5a4SJacob Faibussowitsch {
52131697293SDave May   PetscFunctionBegin;
52231697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5239566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
52531697293SDave May }
52631697293SDave May 
52731697293SDave May /*@C
52831697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
529bb7acecfSBarry Smith   DM options in the options database.
53031697293SDave May 
53131697293SDave May   Not Collective
53231697293SDave May 
5332fe279fdSBarry Smith   Input Parameter:
534bb7acecfSBarry Smith . dm - the `DM` context
53531697293SDave May 
5362fe279fdSBarry Smith   Output Parameter:
53731697293SDave May . prefix - pointer to the prefix string used is returned
53831697293SDave May 
53931697293SDave May   Level: advanced
54031697293SDave May 
54160225df5SJacob Faibussowitsch   Fortran Notes:
54220f4b53cSBarry Smith   Pass in a string 'prefix' of
54320f4b53cSBarry Smith   sufficient length to hold the prefix.
54420f4b53cSBarry Smith 
5451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54631697293SDave May @*/
547d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
548d71ae5a4SJacob Faibussowitsch {
54931697293SDave May   PetscFunctionBegin;
55031697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55331697293SDave May }
55431697293SDave May 
55562e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
556d71ae5a4SJacob Faibussowitsch {
5576eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
55888bdff64SToby Isaac 
55988bdff64SToby Isaac   PetscFunctionBegin;
560aab5bcd8SJed Brown   *ncrefct = 0;
56188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56288bdff64SToby Isaac     refct--;
56388bdff64SToby Isaac     if (recurseCoarse) {
56488bdff64SToby Isaac       PetscInt coarseCount;
56588bdff64SToby Isaac 
56662e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
56788bdff64SToby Isaac       refct += coarseCount;
56888bdff64SToby Isaac     }
56988bdff64SToby Isaac   }
57088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
57188bdff64SToby Isaac     refct--;
57288bdff64SToby Isaac     if (recurseFine) {
57388bdff64SToby Isaac       PetscInt fineCount;
57488bdff64SToby Isaac 
57562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
57688bdff64SToby Isaac       refct += fineCount;
57788bdff64SToby Isaac     }
57888bdff64SToby Isaac   }
57988bdff64SToby Isaac   *ncrefct = refct;
5803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58188bdff64SToby Isaac }
58288bdff64SToby Isaac 
58362e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
58462e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
58562e5d2d2SJDBetteridge {
58662e5d2d2SJDBetteridge   PetscFunctionBegin;
58762e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
5883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58962e5d2d2SJDBetteridge }
59062e5d2d2SJDBetteridge 
591d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
592d71ae5a4SJacob Faibussowitsch {
5935d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
594354557abSToby Isaac 
595354557abSToby Isaac   PetscFunctionBegin;
596354557abSToby Isaac   /* destroy the labels */
597354557abSToby Isaac   while (next) {
598354557abSToby Isaac     DMLabelLink tmp = next->next;
599354557abSToby Isaac 
6005d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
601ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6029566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6039566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
604354557abSToby Isaac     next = tmp;
605354557abSToby Isaac   }
6065d80c0bfSVaclav Hapla   dm->labels = NULL;
6073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
608354557abSToby Isaac }
609354557abSToby Isaac 
610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
611d71ae5a4SJacob Faibussowitsch {
6126858538eSMatthew G. Knepley   PetscFunctionBegin;
6136858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6146858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6156858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6166858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6176858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6196858538eSMatthew G. Knepley }
6206858538eSMatthew G. Knepley 
6211fb7b255SJunchao Zhang /*@C
622bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
62347c6ae99SBarry Smith 
62420f4b53cSBarry Smith   Collective
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith   Input Parameter:
627bb7acecfSBarry Smith . dm - the `DM` object to destroy
62847c6ae99SBarry Smith 
62947c6ae99SBarry Smith   Level: developer
63047c6ae99SBarry Smith 
6311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
63247c6ae99SBarry Smith @*/
633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
634d71ae5a4SJacob Faibussowitsch {
6356eb26441SStefano Zampini   PetscInt cnt;
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith   PetscFunctionBegin;
6383ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
6396bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
64087e657c6SBarry Smith 
64188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
64262e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
64388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6449371c9d4SSatish Balay   if (--cnt > 0) {
6459371c9d4SSatish Balay     *dm = NULL;
6463ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6479371c9d4SSatish Balay   }
6483ba16761SJacob Faibussowitsch   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
6496bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6506eb26441SStefano Zampini 
6519566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6529566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
653974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
654974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6552348bcf4SPeter Brune 
656b17ce1afSJed Brown   /* Destroy the list of hooks */
657c833c3b5SJed Brown   {
658c833c3b5SJed Brown     DMCoarsenHookLink link, next;
659b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
660b17ce1afSJed Brown       next = link->next;
6619566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
662b17ce1afSJed Brown     }
6630298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
664c833c3b5SJed Brown   }
665c833c3b5SJed Brown   {
666c833c3b5SJed Brown     DMRefineHookLink link, next;
667c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
668c833c3b5SJed Brown       next = link->next;
6699566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
670c833c3b5SJed Brown     }
6710298fd71SBarry Smith     (*dm)->refinehook = NULL;
672c833c3b5SJed Brown   }
673be081cd6SPeter Brune   {
674be081cd6SPeter Brune     DMSubDomainHookLink link, next;
675be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
676be081cd6SPeter Brune       next = link->next;
6779566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
678be081cd6SPeter Brune     }
6790298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
680be081cd6SPeter Brune   }
681baf369e7SPeter Brune   {
682baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
683baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
684baf369e7SPeter Brune       next = link->next;
6859566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
686baf369e7SPeter Brune     }
6870298fd71SBarry Smith     (*dm)->gtolhook = NULL;
688baf369e7SPeter Brune   }
689d4d07f1eSToby Isaac   {
690d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
691d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
692d4d07f1eSToby Isaac       next = link->next;
6939566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
694d4d07f1eSToby Isaac     }
695d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
696d4d07f1eSToby Isaac   }
697aa1993deSMatthew G Knepley   /* Destroy the work arrays */
698aa1993deSMatthew G Knepley   {
699aa1993deSMatthew G Knepley     DMWorkLink link, next;
700936381afSPierre Jolivet     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (void *)(*dm)->workout->mem);
701aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
702aa1993deSMatthew G Knepley       next = link->next;
7039566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7049566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
705aa1993deSMatthew G Knepley     }
7060298fd71SBarry Smith     (*dm)->workin = NULL;
707aa1993deSMatthew G Knepley   }
708c58f1c22SToby Isaac   /* destroy the labels */
7099566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
710f4cdcedcSVaclav Hapla   /* destroy the fields */
7119566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
712f4cdcedcSVaclav Hapla   /* destroy the boundaries */
713e6f8dbb6SToby Isaac   {
714e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
715e6f8dbb6SToby Isaac     while (next) {
716e6f8dbb6SToby Isaac       DMBoundary b = next;
717e6f8dbb6SToby Isaac 
718e6f8dbb6SToby Isaac       next = b->next;
7199566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
720e6f8dbb6SToby Isaac     }
721e6f8dbb6SToby Isaac   }
722b17ce1afSJed Brown 
7239566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7249566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7259566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
72652536dc3SBarry Smith 
72748a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7289566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7299566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7309566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7319566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
73288ed4aceSMatthew G Knepley 
7339566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7349566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7359566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7369566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7379566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7389566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7399566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
740736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
74148a46eb9SPierre Jolivet     if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7429566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
743736995cdSBlaise Bourdin   }
7449a2a23afSMatthew G. Knepley   {
7459a2a23afSMatthew G. Knepley     Vec     *auxData;
7469a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7479a2a23afSMatthew G. Knepley 
7489566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7499566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7509566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7519566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7529566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7539566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7549a2a23afSMatthew G. Knepley   }
75548a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7566eb26441SStefano Zampini 
7579566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
75848a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7599566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7604fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7619566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7629566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7636858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7646858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7659566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7686725e60dSJed Brown   PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local));
7696725e60dSJed Brown   PetscCall(VecDestroy(&(*dm)->periodic.affine));
7706636e97aSMatthew G Knepley 
7719566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7729566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
773e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7749566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
775732e2eb9SMatthew G Knepley 
77648a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7779566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
778f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7799566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7809566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
781f918ec44SMatthew G. Knepley #endif
782435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7839566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78547c6ae99SBarry Smith }
78647c6ae99SBarry Smith 
787d7bf68aeSBarry Smith /*@
788bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
789d7bf68aeSBarry Smith 
79020f4b53cSBarry Smith   Collective
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith   Input Parameter:
793bb7acecfSBarry Smith . dm - the `DM` object to setup
794d7bf68aeSBarry Smith 
795bb7acecfSBarry Smith   Level: intermediate
796d7bf68aeSBarry Smith 
797bb7acecfSBarry Smith   Note:
798bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
799bb7acecfSBarry Smith 
8001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
801d7bf68aeSBarry Smith @*/
802d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
803d71ae5a4SJacob Faibussowitsch {
804d7bf68aeSBarry Smith   PetscFunctionBegin;
805171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8063ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
807dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8088387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
810d7bf68aeSBarry Smith }
811d7bf68aeSBarry Smith 
812d7bf68aeSBarry Smith /*@
813bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
814d7bf68aeSBarry Smith 
81520f4b53cSBarry Smith   Collective
816d7bf68aeSBarry Smith 
817d7bf68aeSBarry Smith   Input Parameter:
818bb7acecfSBarry Smith . dm - the `DM` object to set options for
819d7bf68aeSBarry Smith 
82020f4b53cSBarry Smith   Options Database Keys:
821bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
822bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
823bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
824a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
82520f4b53cSBarry 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`
82620f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8279318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
828cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8295dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8309318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8319318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8329318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
833bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
834bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8359318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
8369318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8379318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8389318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
839bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8409318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8419318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8429318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8439318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
844bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8459318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8469318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8479318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8489318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
849bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
850d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
851d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
852d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
853d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
854d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
855909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
856909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8579318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8589318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8599318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
86020f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
86120f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
862bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
863bb7acecfSBarry 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()`
864bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
865bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
866bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
867384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
868d7bf68aeSBarry Smith 
86995eb5ee5SVaclav Hapla   Level: intermediate
87095eb5ee5SVaclav Hapla 
8711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
872bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
87360225df5SJacob Faibussowitsch          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`
874d7bf68aeSBarry Smith @*/
875d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
876d71ae5a4SJacob Faibussowitsch {
8777781c08eSBarry Smith   char      typeName[256];
878ca266f36SBarry Smith   PetscBool flg;
879d7bf68aeSBarry Smith 
880d7bf68aeSBarry Smith   PetscFunctionBegin;
881171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88249be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8839566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8849566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
885dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
886d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
8889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
8891baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
8909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
8911baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
892863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
8939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
8949566063dSJacob 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));
895dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
896f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
897dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
898d0609cedSBarry Smith   PetscOptionsEnd();
8993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
900d7bf68aeSBarry Smith }
901d7bf68aeSBarry Smith 
902fc9bc008SSatish Balay /*@C
903bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
904fe2efc57SMark 
90520f4b53cSBarry Smith   Collective
906fe2efc57SMark 
907fe2efc57SMark   Input Parameters:
908bb7acecfSBarry Smith + dm   - the `DM` object
90920f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
91060225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
911fe2efc57SMark 
912fe2efc57SMark   Level: intermediate
913bb7acecfSBarry Smith 
914bb7acecfSBarry Smith   Note:
915bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
916bb7acecfSBarry Smith 
91760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
918fe2efc57SMark @*/
919d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
920d71ae5a4SJacob Faibussowitsch {
921fe2efc57SMark   PetscFunctionBegin;
922fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9239566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
925fe2efc57SMark }
926fe2efc57SMark 
927fe2efc57SMark /*@C
928bb7acecfSBarry 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
929bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
93047c6ae99SBarry Smith 
93120f4b53cSBarry Smith   Collective
93247c6ae99SBarry Smith 
933d8d19677SJose E. Roman   Input Parameters:
934bb7acecfSBarry Smith + dm - the `DM` object to view
93547c6ae99SBarry Smith - v  - the viewer
93647c6ae99SBarry Smith 
93720f4b53cSBarry Smith   Level: beginner
93820f4b53cSBarry Smith 
939cd7e8a5eSksagiyam   Notes:
940bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
941bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
942bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
943cd7e8a5eSksagiyam 
9441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
94547c6ae99SBarry Smith @*/
946d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
947d71ae5a4SJacob Faibussowitsch {
94832c0f0efSBarry Smith   PetscBool         isbinary;
94976a8abe0SBarry Smith   PetscMPIInt       size;
95076a8abe0SBarry Smith   PetscViewerFormat format;
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith   PetscFunctionBegin;
953171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
95448a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
955b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
95674903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
95774903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
95874903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
95974903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
96074903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
96174903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
96274903a4fSStefano Zampini      in an error here */
96374903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9649566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
965b1b135c8SBarry Smith 
9669566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9679566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9683ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9699566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9709566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
97132c0f0efSBarry Smith   if (isbinary) {
97255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
97332c0f0efSBarry Smith     char     type[256];
97432c0f0efSBarry Smith 
9759566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
976c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9779566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
97832c0f0efSBarry Smith   }
979dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
98147c6ae99SBarry Smith }
98247c6ae99SBarry Smith 
98347c6ae99SBarry Smith /*@
984bb7acecfSBarry 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,
985bb7acecfSBarry Smith   that is it has no ghost locations.
98647c6ae99SBarry Smith 
98720f4b53cSBarry Smith   Collective
98847c6ae99SBarry Smith 
98947c6ae99SBarry Smith   Input Parameter:
990bb7acecfSBarry Smith . dm - the `DM` object
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith   Output Parameter:
99347c6ae99SBarry Smith . vec - the global vector
99447c6ae99SBarry Smith 
995073dac72SJed Brown   Level: beginner
99647c6ae99SBarry Smith 
9971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
998bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
99947c6ae99SBarry Smith @*/
1000d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1001d71ae5a4SJacob Faibussowitsch {
100247c6ae99SBarry Smith   PetscFunctionBegin;
1003171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10044f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1005dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
100676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1007c6b011d8SStefano Zampini     DM vdm;
1008c6b011d8SStefano Zampini 
10099566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10107a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1011c6b011d8SStefano Zampini   }
10123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
101347c6ae99SBarry Smith }
101447c6ae99SBarry Smith 
101547c6ae99SBarry Smith /*@
1016bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
101747c6ae99SBarry Smith 
101847c6ae99SBarry Smith   Not Collective
101947c6ae99SBarry Smith 
102047c6ae99SBarry Smith   Input Parameter:
1021bb7acecfSBarry Smith . dm - the `DM` object
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith   Output Parameter:
102447c6ae99SBarry Smith . vec - the local vector
102547c6ae99SBarry Smith 
1026073dac72SJed Brown   Level: beginner
102747c6ae99SBarry Smith 
102820f4b53cSBarry Smith   Note:
1029bb7acecfSBarry 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.
1030bb7acecfSBarry Smith 
10311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1032bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
103347c6ae99SBarry Smith @*/
1034d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1035d71ae5a4SJacob Faibussowitsch {
103647c6ae99SBarry Smith   PetscFunctionBegin;
1037171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10384f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1039dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
104076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1041c6b011d8SStefano Zampini     DM vdm;
1042c6b011d8SStefano Zampini 
10439566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10447a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1045c6b011d8SStefano Zampini   }
10463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104747c6ae99SBarry Smith }
104847c6ae99SBarry Smith 
10491411c6eeSJed Brown /*@
1050bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10511411c6eeSJed Brown 
105220f4b53cSBarry Smith   Collective
10531411c6eeSJed Brown 
10541411c6eeSJed Brown   Input Parameter:
1055bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10561411c6eeSJed Brown 
10571411c6eeSJed Brown   Output Parameter:
10581411c6eeSJed Brown . ltog - the mapping
10591411c6eeSJed Brown 
1060bb7acecfSBarry Smith   Level: advanced
10611411c6eeSJed Brown 
10621411c6eeSJed Brown   Notes:
1063bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10641411c6eeSJed Brown 
1065bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1066bb7acecfSBarry Smith   need to use this function with those objects.
1067bb7acecfSBarry Smith 
1068bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1069bb7acecfSBarry Smith 
107060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1071bb7acecfSBarry Smith           `DMCreateMatrix()`
10721411c6eeSJed Brown @*/
1073d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1074d71ae5a4SJacob Faibussowitsch {
10750be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10761411c6eeSJed Brown 
10771411c6eeSJed Brown   PetscFunctionBegin;
10781411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10794f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10801411c6eeSJed Brown   if (!dm->ltogmap) {
108137d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
108237d0c07bSMatthew G Knepley 
10839566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
108437d0c07bSMatthew G Knepley     if (section) {
1085a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
108637d0c07bSMatthew G Knepley       PetscInt       *ltog;
1087ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
108837d0c07bSMatthew G Knepley 
10899566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
10909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
10919566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
10929566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
109337d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1094e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
109537d0c07bSMatthew G Knepley 
109637d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
10979566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
10989566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
10999566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11009566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11011a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11021a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1103ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11045227eafbSStefano Zampini 
1105e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11065227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1107e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1108e6befd46SJed Brown             cind++;
1109e6befd46SJed Brown           } else {
11105227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1111e6befd46SJed Brown           }
111237d0c07bSMatthew G Knepley         }
111337d0c07bSMatthew G Knepley       }
1114bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11159371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11169371c9d4SSatish Balay       bsLocal[1] = bs;
11179566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11189371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11199371c9d4SSatish Balay         bs = 1;
11209371c9d4SSatish Balay       } else {
11219371c9d4SSatish Balay         bs = bsMinMax[0];
11229371c9d4SSatish Balay       }
11237591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11247591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1125ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1126ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1127ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1128ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1129ca469d19SJed Brown         }
1130ccf3bd66SMatthew G. Knepley         n /= bs;
1131ccf3bd66SMatthew G. Knepley       }
11329566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1133dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
113437d0c07bSMatthew G Knepley   }
11351411c6eeSJed Brown   *ltog = dm->ltogmap;
11363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11371411c6eeSJed Brown }
11381411c6eeSJed Brown 
11391411c6eeSJed Brown /*@
1140bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11411411c6eeSJed Brown 
11421411c6eeSJed Brown   Not Collective
11431411c6eeSJed Brown 
11441411c6eeSJed Brown   Input Parameter:
1145bb7acecfSBarry Smith . dm - the `DM` with block structure
11461411c6eeSJed Brown 
11471411c6eeSJed Brown   Output Parameter:
11481411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11491411c6eeSJed Brown 
11501411c6eeSJed Brown   Level: intermediate
11511411c6eeSJed Brown 
1152bb7acecfSBarry Smith   Note:
1153bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1154bb7acecfSBarry Smith 
1155bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1156bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1157bb7acecfSBarry Smith 
11581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11591411c6eeSJed Brown @*/
1160d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1161d71ae5a4SJacob Faibussowitsch {
11621411c6eeSJed Brown   PetscFunctionBegin;
11631411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11644f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11657a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11661411c6eeSJed Brown   *bs = dm->bs;
11673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11681411c6eeSJed Brown }
11691411c6eeSJed Brown 
117048eeb7c8SBarry Smith /*@C
1171bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1172bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
117347c6ae99SBarry Smith 
117420f4b53cSBarry Smith   Collective
117547c6ae99SBarry Smith 
1176d8d19677SJose E. Roman   Input Parameters:
1177bb7acecfSBarry Smith + dmc - the `DM` object
1178bb7acecfSBarry Smith - dmf - the second, finer `DM` object
117947c6ae99SBarry Smith 
1180d8d19677SJose E. Roman   Output Parameters:
118147c6ae99SBarry Smith + mat - the interpolation
1182bb7acecfSBarry Smith - vec - the scaling (optional), see `DMCreateInterpolationScale()`
118347c6ae99SBarry Smith 
118447c6ae99SBarry Smith   Level: developer
118547c6ae99SBarry Smith 
118695452b02SPatrick Sanan   Notes:
1187bb7acecfSBarry 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
1188bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1189d52bd9f3SBarry Smith 
1190bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1191bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
119285afcc9aSBarry Smith 
11931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
119447c6ae99SBarry Smith @*/
1195d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1196d71ae5a4SJacob Faibussowitsch {
119747c6ae99SBarry Smith   PetscFunctionBegin;
1198a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1199a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12004f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1202dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12039566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
120547c6ae99SBarry Smith }
120647c6ae99SBarry Smith 
12073ad4599aSBarry Smith /*@
1208*a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1209*a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12102ed6491fSPatrick Sanan 
12112ed6491fSPatrick Sanan   Input Parameters:
1212bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1213bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12142ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12152ed6491fSPatrick Sanan 
12162ed6491fSPatrick Sanan   Output Parameter:
12172ed6491fSPatrick Sanan . scale - the scaled vector
12182ed6491fSPatrick Sanan 
1219bb7acecfSBarry Smith   Level: advanced
12202ed6491fSPatrick Sanan 
1221*a4e35b19SJacob Faibussowitsch   Notes:
1222*a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1223*a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1224*a4e35b19SJacob Faibussowitsch 
122560225df5SJacob Faibussowitsch   Developer Notes:
1226bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1227e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1228e9c74fd6SRichard Tran Mills 
122960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12302ed6491fSPatrick Sanan @*/
1231d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1232d71ae5a4SJacob Faibussowitsch {
12332ed6491fSPatrick Sanan   Vec         fine;
12342ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12359704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1236e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12379704db99SRichard Tran Mills #endif
12382ed6491fSPatrick Sanan 
12392ed6491fSPatrick Sanan   PetscFunctionBegin;
12409566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12419566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12429566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12439704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12449704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12459704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12469704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12479566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1248e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12499566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12509566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12519566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
125283aa49f4SRichard Tran Mills   }
12539704db99SRichard Tran Mills #endif
12549566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12559566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12569566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12582ed6491fSPatrick Sanan }
12592ed6491fSPatrick Sanan 
12602ed6491fSPatrick Sanan /*@
1261bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1262bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12633ad4599aSBarry Smith 
126420f4b53cSBarry Smith   Collective
12653ad4599aSBarry Smith 
1266d8d19677SJose E. Roman   Input Parameters:
1267bb7acecfSBarry Smith + dmc - the `DM` object
1268bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12693ad4599aSBarry Smith 
12703ad4599aSBarry Smith   Output Parameter:
12713ad4599aSBarry Smith . mat - the restriction
12723ad4599aSBarry Smith 
12733ad4599aSBarry Smith   Level: developer
12743ad4599aSBarry Smith 
1275bb7acecfSBarry Smith   Note:
1276bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1277bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12783ad4599aSBarry Smith 
12791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12803ad4599aSBarry Smith @*/
1281d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1282d71ae5a4SJacob Faibussowitsch {
12833ad4599aSBarry Smith   PetscFunctionBegin;
1284a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1285a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12864f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12879566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1288dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
12899566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
12903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12913ad4599aSBarry Smith }
12923ad4599aSBarry Smith 
129347c6ae99SBarry Smith /*@
1294*a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
129547c6ae99SBarry Smith 
129620f4b53cSBarry Smith   Collective
129747c6ae99SBarry Smith 
1298d8d19677SJose E. Roman   Input Parameters:
1299bb7acecfSBarry Smith + dac - the `DM` object
1300bb7acecfSBarry Smith - daf - the second, finer `DM` object
130147c6ae99SBarry Smith 
130247c6ae99SBarry Smith   Output Parameter:
13036dbf9973SLawrence Mitchell . mat - the injection
130447c6ae99SBarry Smith 
130547c6ae99SBarry Smith   Level: developer
130647c6ae99SBarry Smith 
1307*a4e35b19SJacob Faibussowitsch   Notes:
1308*a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1309*a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1310*a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1311*a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1312*a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1313*a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1314*a4e35b19SJacob Faibussowitsch 
1315bb7acecfSBarry 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
1316bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
131785afcc9aSBarry Smith 
13181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1319bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
132047c6ae99SBarry Smith @*/
1321d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1322d71ae5a4SJacob Faibussowitsch {
132347c6ae99SBarry Smith   PetscFunctionBegin;
1324a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1325a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13264f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13279566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1328dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13299566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
133147c6ae99SBarry Smith }
133247c6ae99SBarry Smith 
1333b412c318SBarry Smith /*@
1334bb7acecfSBarry 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
1335bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1336bd041c0cSMatthew G. Knepley 
133720f4b53cSBarry Smith   Collective
1338bd041c0cSMatthew G. Knepley 
1339d8d19677SJose E. Roman   Input Parameters:
1340bb7acecfSBarry Smith + dmc - the target `DM` object
1341bb7acecfSBarry Smith - dmf - the source `DM` object
1342bd041c0cSMatthew G. Knepley 
1343bd041c0cSMatthew G. Knepley   Output Parameter:
1344b4937a87SMatthew G. Knepley . mat - the mass matrix
1345bd041c0cSMatthew G. Knepley 
1346bd041c0cSMatthew G. Knepley   Level: developer
1347bd041c0cSMatthew G. Knepley 
1348bb7acecfSBarry Smith   Notes:
1349bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1350bb7acecfSBarry Smith 
135120f4b53cSBarry 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()`
1352bb7acecfSBarry Smith 
135342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1354bd041c0cSMatthew G. Knepley @*/
1355d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1356d71ae5a4SJacob Faibussowitsch {
1357bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1358b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1359b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13604f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13615b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1362dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13635b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1365b4937a87SMatthew G. Knepley }
1366b4937a87SMatthew G. Knepley 
1367b4937a87SMatthew G. Knepley /*@
1368bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1369b4937a87SMatthew G. Knepley 
137020f4b53cSBarry Smith   Collective
1371b4937a87SMatthew G. Knepley 
1372b4937a87SMatthew G. Knepley   Input Parameter:
1373bb7acecfSBarry Smith . dm - the `DM` object
1374b4937a87SMatthew G. Knepley 
1375b4937a87SMatthew G. Knepley   Output Parameter:
1376bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1377b4937a87SMatthew G. Knepley 
1378b4937a87SMatthew G. Knepley   Level: developer
1379b4937a87SMatthew G. Knepley 
1380bb7acecfSBarry Smith   Note:
1381bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1382bb7acecfSBarry Smith 
138360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1384b4937a87SMatthew G. Knepley @*/
1385d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1386d71ae5a4SJacob Faibussowitsch {
1387b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1388b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13894f572ea9SToby Isaac   PetscAssertPointer(lm, 2);
1390dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
13913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1392bd041c0cSMatthew G. Knepley }
1393bd041c0cSMatthew G. Knepley 
1394bd041c0cSMatthew G. Knepley /*@
1395bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1396bb7acecfSBarry Smith   of a PDE on the `DM`.
139747c6ae99SBarry Smith 
139820f4b53cSBarry Smith   Collective
139947c6ae99SBarry Smith 
1400d8d19677SJose E. Roman   Input Parameters:
1401bb7acecfSBarry Smith + dm    - the `DM` object
1402bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
140347c6ae99SBarry Smith 
140447c6ae99SBarry Smith   Output Parameter:
140547c6ae99SBarry Smith . coloring - the coloring
140647c6ae99SBarry Smith 
14071bf8429eSBarry Smith   Level: developer
14081bf8429eSBarry Smith 
1409ec5066bdSBarry Smith   Notes:
1410bb7acecfSBarry 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
1411bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1412ec5066bdSBarry Smith 
1413bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14141bf8429eSBarry Smith   For `DMDA` in three dimensions with periodic boundary conditions the number of grid points in each dimension must be divisible by 2*stencil_width + 1,
14151bf8429eSBarry Smith   otherwise an error will be generated.
1416ec5066bdSBarry Smith 
14171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1418aab9d709SJed Brown @*/
1419d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1420d71ae5a4SJacob Faibussowitsch {
142147c6ae99SBarry Smith   PetscFunctionBegin;
1422171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14234f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1424dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
142647c6ae99SBarry Smith }
142747c6ae99SBarry Smith 
1428b412c318SBarry Smith /*@
1429bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
143047c6ae99SBarry Smith 
143120f4b53cSBarry Smith   Collective
143247c6ae99SBarry Smith 
143347c6ae99SBarry Smith   Input Parameter:
1434bb7acecfSBarry Smith . dm - the `DM` object
143547c6ae99SBarry Smith 
143647c6ae99SBarry Smith   Output Parameter:
143747c6ae99SBarry Smith . mat - the empty Jacobian
143847c6ae99SBarry Smith 
143920f4b53cSBarry Smith   Options Database Key:
1440bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1441f27dd7c6SMatthew G. Knepley 
144220f4b53cSBarry Smith   Level: beginner
144320f4b53cSBarry Smith 
144495452b02SPatrick Sanan   Notes:
144595452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
144694013140SBarry Smith   do not need to do it yourself.
144794013140SBarry Smith 
144894013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1449bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
145094013140SBarry Smith 
1451bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
145294013140SBarry Smith   internally by PETSc.
145394013140SBarry Smith 
1454bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1455bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
145694013140SBarry Smith 
14571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1458aab9d709SJed Brown @*/
1459d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1460d71ae5a4SJacob Faibussowitsch {
146147c6ae99SBarry Smith   PetscFunctionBegin;
1462171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14634f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14649566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14659566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1466dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
146776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1468c6b011d8SStefano Zampini     DM mdm;
1469c6b011d8SStefano Zampini 
14709566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14717a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1472c6b011d8SStefano Zampini   }
1473e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1474e5e52638SMatthew G. Knepley   if (dm->Nf) {
1475e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1476649ef022SMatthew Knepley     PetscInt     Nf, f;
1477e571a35bSMatthew G. Knepley 
14789566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1479649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1480649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14819566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14829566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14839566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1484649ef022SMatthew Knepley         break;
1485e571a35bSMatthew G. Knepley       }
1486649ef022SMatthew Knepley     }
1487649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1488649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14899566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14909566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
14919566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1492e571a35bSMatthew G. Knepley       }
1493e571a35bSMatthew G. Knepley     }
1494e571a35bSMatthew G. Knepley   }
14959566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
14963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
149747c6ae99SBarry Smith }
149847c6ae99SBarry Smith 
1499732e2eb9SMatthew G Knepley /*@
1500*a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1501*a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1502*a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1503aa0f6e3cSJed Brown 
150420f4b53cSBarry Smith   Logically Collective
1505aa0f6e3cSJed Brown 
1506aa0f6e3cSJed Brown   Input Parameters:
1507bb7acecfSBarry Smith + dm   - the `DM`
1508bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1509aa0f6e3cSJed Brown 
1510aa0f6e3cSJed Brown   Level: developer
1511aa0f6e3cSJed Brown 
1512*a4e35b19SJacob Faibussowitsch   Notes:
1513*a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1514*a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1515*a4e35b19SJacob Faibussowitsch 
15161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1517aa0f6e3cSJed Brown @*/
1518d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1519d71ae5a4SJacob Faibussowitsch {
1520aa0f6e3cSJed Brown   PetscFunctionBegin;
1521aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1522aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1524aa0f6e3cSJed Brown }
1525aa0f6e3cSJed Brown 
1526aa0f6e3cSJed Brown /*@
1527bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1528732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1529732e2eb9SMatthew G Knepley 
153020f4b53cSBarry Smith   Logically Collective
1531732e2eb9SMatthew G Knepley 
1532d8d19677SJose E. Roman   Input Parameters:
1533bb7acecfSBarry Smith + dm   - the `DM`
1534bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1535732e2eb9SMatthew G Knepley 
153620f4b53cSBarry Smith   Options Database Key:
1537bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1538f27dd7c6SMatthew G. Knepley 
153920f4b53cSBarry Smith   Level: developer
154020f4b53cSBarry Smith 
15411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1542732e2eb9SMatthew G Knepley @*/
1543d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1544d71ae5a4SJacob Faibussowitsch {
1545732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1546732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1547732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1549732e2eb9SMatthew G Knepley }
1550732e2eb9SMatthew G Knepley 
1551b06ff27eSHong Zhang /*@
1552bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1553bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1554b06ff27eSHong Zhang 
155520f4b53cSBarry Smith   Logically Collective
1556b06ff27eSHong Zhang 
1557d8d19677SJose E. Roman   Input Parameters:
1558bb7acecfSBarry Smith + dm   - the `DM`
1559da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1560b06ff27eSHong Zhang 
1561b06ff27eSHong Zhang   Level: developer
1562bb7acecfSBarry Smith 
15631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1564b06ff27eSHong Zhang @*/
1565d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1566d71ae5a4SJacob Faibussowitsch {
1567b06ff27eSHong Zhang   PetscFunctionBegin;
1568b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1569b06ff27eSHong Zhang   dm->structure_only = only;
15703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1571b06ff27eSHong Zhang }
1572b06ff27eSHong Zhang 
1573863027abSJed Brown /*@
1574863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1575863027abSJed Brown 
157620f4b53cSBarry Smith   Logically Collective
1577863027abSJed Brown 
1578863027abSJed Brown   Input Parameters:
1579863027abSJed Brown + dm    - the `DM`
1580863027abSJed Brown - btype - block by topological point or field node
1581863027abSJed Brown 
158220f4b53cSBarry Smith   Options Database Key:
15830e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1584863027abSJed Brown 
158520f4b53cSBarry Smith   Level: advanced
158620f4b53cSBarry Smith 
15871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1588863027abSJed Brown @*/
1589863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1590863027abSJed Brown {
1591863027abSJed Brown   PetscFunctionBegin;
1592863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1593863027abSJed Brown   dm->blocking_type = btype;
15943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1595863027abSJed Brown }
1596863027abSJed Brown 
1597863027abSJed Brown /*@
1598863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1599863027abSJed Brown 
1600863027abSJed Brown   Not Collective
1601863027abSJed Brown 
16022fe279fdSBarry Smith   Input Parameter:
1603863027abSJed Brown . dm - the `DM`
1604863027abSJed Brown 
16052fe279fdSBarry Smith   Output Parameter:
1606863027abSJed Brown . btype - block by topological point or field node
1607863027abSJed Brown 
1608863027abSJed Brown   Level: advanced
1609863027abSJed Brown 
16101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1611863027abSJed Brown @*/
1612863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1613863027abSJed Brown {
1614863027abSJed Brown   PetscFunctionBegin;
1615863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16164f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1617863027abSJed Brown   *btype = dm->blocking_type;
16183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1619863027abSJed Brown }
1620863027abSJed Brown 
1621a89ea682SMatthew G Knepley /*@C
1622bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1623a89ea682SMatthew G Knepley 
1624a89ea682SMatthew G Knepley   Not Collective
1625a89ea682SMatthew G Knepley 
1626a89ea682SMatthew G Knepley   Input Parameters:
1627bb7acecfSBarry Smith + dm    - the `DM` object
1628a5b23f4aSJose E. Roman . count - The minimum size
162920f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1630a89ea682SMatthew G Knepley 
1631a89ea682SMatthew G Knepley   Output Parameter:
163260225df5SJacob Faibussowitsch . mem - the work array
1633a89ea682SMatthew G Knepley 
1634a89ea682SMatthew G Knepley   Level: developer
1635a89ea682SMatthew G Knepley 
1636bb7acecfSBarry Smith   Note:
1637da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1638bb7acecfSBarry Smith 
1639bb7acecfSBarry Smith   The array may contain nonzero values
1640bb7acecfSBarry Smith 
16411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1642a89ea682SMatthew G Knepley @*/
1643d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1644d71ae5a4SJacob Faibussowitsch {
1645aa1993deSMatthew G Knepley   DMWorkLink  link;
164669291d52SBarry Smith   PetscMPIInt dsize;
1647a89ea682SMatthew G Knepley 
1648a89ea682SMatthew G Knepley   PetscFunctionBegin;
1649a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16504f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1651442f3b32SStefano Zampini   if (!count) {
1652442f3b32SStefano Zampini     *(void **)mem = NULL;
1653442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1654442f3b32SStefano Zampini   }
1655aa1993deSMatthew G Knepley   if (dm->workin) {
1656aa1993deSMatthew G Knepley     link       = dm->workin;
1657aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1658aa1993deSMatthew G Knepley   } else {
16594dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1660a89ea682SMatthew G Knepley   }
1661b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1662b77fa653SStefano Zampini      Get size directly */
1663b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1664b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1665b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1666b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1667b77fa653SStefano Zampini #endif
1668b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1669b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1670b77fa653SStefano Zampini #endif
1671b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1672b77fa653SStefano Zampini 
16735056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16749566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16759566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1676854ce69bSBarry Smith     link->bytes = dsize * count;
1677aa1993deSMatthew G Knepley   }
1678aa1993deSMatthew G Knepley   link->next  = dm->workout;
1679aa1993deSMatthew G Knepley   dm->workout = link;
1680cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
168100d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
168200d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
168300d952a4SJed Brown #endif
1684aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1686a89ea682SMatthew G Knepley }
1687a89ea682SMatthew G Knepley 
1688aa1993deSMatthew G Knepley /*@C
1689bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1690aa1993deSMatthew G Knepley 
1691aa1993deSMatthew G Knepley   Not Collective
1692aa1993deSMatthew G Knepley 
1693aa1993deSMatthew G Knepley   Input Parameters:
1694bb7acecfSBarry Smith + dm    - the `DM` object
1695a5b23f4aSJose E. Roman . count - The minimum size
169620f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1697aa1993deSMatthew G Knepley 
1698aa1993deSMatthew G Knepley   Output Parameter:
169960225df5SJacob Faibussowitsch . mem - the work array
1700aa1993deSMatthew G Knepley 
1701aa1993deSMatthew G Knepley   Level: developer
1702aa1993deSMatthew G Knepley 
170360225df5SJacob Faibussowitsch   Developer Notes:
1704bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1705147403d9SBarry Smith 
17061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1707aa1993deSMatthew G Knepley @*/
1708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1709d71ae5a4SJacob Faibussowitsch {
1710aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1711aa1993deSMatthew G Knepley 
1712aa1993deSMatthew G Knepley   PetscFunctionBegin;
1713aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17144f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1715*a4e35b19SJacob Faibussowitsch   (void)count;
1716*a4e35b19SJacob Faibussowitsch   (void)dtype;
1717442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1718aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1719aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1720aa1993deSMatthew G Knepley       *p            = link->next;
1721aa1993deSMatthew G Knepley       link->next    = dm->workin;
1722aa1993deSMatthew G Knepley       dm->workin    = link;
17230298fd71SBarry Smith       *(void **)mem = NULL;
17243ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1725aa1993deSMatthew G Knepley     }
1726aa1993deSMatthew G Knepley   }
1727aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1728aa1993deSMatthew G Knepley }
1729e7c4fc90SDmitry Karpeev 
17308cda7954SMatthew G. Knepley /*@C
1731bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1732bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17338cda7954SMatthew G. Knepley 
173420f4b53cSBarry Smith   Logically Collective; No Fortran Support
17358cda7954SMatthew G. Knepley 
17368cda7954SMatthew G. Knepley   Input Parameters:
1737bb7acecfSBarry Smith + dm     - The `DM`
17388cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17398cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17408cda7954SMatthew G. Knepley 
174120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1742bb7acecfSBarry Smith + dm        - The present `DM`
1743bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1744147403d9SBarry Smith . field     - The field number in dm
1745147403d9SBarry Smith - nullSpace - The nullspace for the given field
17468cda7954SMatthew G. Knepley 
174749762cbcSSatish Balay   Level: intermediate
174849762cbcSSatish Balay 
17491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1750147403d9SBarry Smith @*/
1751*a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1752d71ae5a4SJacob Faibussowitsch {
1753435a35e8SMatthew G Knepley   PetscFunctionBegin;
1754435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17557a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1756435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1758435a35e8SMatthew G Knepley }
1759435a35e8SMatthew G Knepley 
17608cda7954SMatthew G. Knepley /*@C
1761bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17628cda7954SMatthew G. Knepley 
176320f4b53cSBarry Smith   Not Collective; No Fortran Support
17648cda7954SMatthew G. Knepley 
17658cda7954SMatthew G. Knepley   Input Parameters:
1766bb7acecfSBarry Smith + dm    - The `DM`
17678cda7954SMatthew G. Knepley - field - The field number for the nullspace
17688cda7954SMatthew G. Knepley 
17698cda7954SMatthew G. Knepley   Output Parameter:
17708cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17718cda7954SMatthew G. Knepley 
177220f4b53cSBarry Smith   Calling sequence of `nullsp`:
1773147403d9SBarry Smith + dm        - The present DM
1774147403d9SBarry Smith . origField - The field number given above, in the original DM
1775147403d9SBarry Smith . field     - The field number in dm
1776147403d9SBarry Smith - nullSpace - The nullspace for the given field
17778cda7954SMatthew G. Knepley 
177849762cbcSSatish Balay   Level: intermediate
177949762cbcSSatish Balay 
17801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1781147403d9SBarry Smith @*/
1782*a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1783d71ae5a4SJacob Faibussowitsch {
17840a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17850a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17864f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
17877a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17880a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17900a50eb56SMatthew G. Knepley }
17910a50eb56SMatthew G. Knepley 
17928cda7954SMatthew G. Knepley /*@C
1793bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17948cda7954SMatthew G. Knepley 
179520f4b53cSBarry Smith   Logically Collective; No Fortran Support
17968cda7954SMatthew G. Knepley 
17978cda7954SMatthew G. Knepley   Input Parameters:
1798bb7acecfSBarry Smith + dm     - The `DM`
17998cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18008cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18018cda7954SMatthew G. Knepley 
180220f4b53cSBarry Smith   Calling sequence of `nullsp`:
1803bb7acecfSBarry Smith + dm        - The present `DM`
1804bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1805147403d9SBarry Smith . field     - The field number in dm
1806147403d9SBarry Smith - nullSpace - The nullspace for the given field
18078cda7954SMatthew G. Knepley 
180849762cbcSSatish Balay   Level: intermediate
180949762cbcSSatish Balay 
18101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1811bb7acecfSBarry Smith           `MatNullSpace`
1812147403d9SBarry Smith @*/
1813*a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1814d71ae5a4SJacob Faibussowitsch {
1815f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1816f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18177a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1818f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1820f9d4088aSMatthew G. Knepley }
1821f9d4088aSMatthew G. Knepley 
18228cda7954SMatthew G. Knepley /*@C
1823bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18248cda7954SMatthew G. Knepley 
182520f4b53cSBarry Smith   Not Collective; No Fortran Support
18268cda7954SMatthew G. Knepley 
18278cda7954SMatthew G. Knepley   Input Parameters:
1828bb7acecfSBarry Smith + dm    - The `DM`
18298cda7954SMatthew G. Knepley - field - The field number for the nullspace
18308cda7954SMatthew G. Knepley 
18318cda7954SMatthew G. Knepley   Output Parameter:
18328cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18338cda7954SMatthew G. Knepley 
183420f4b53cSBarry Smith   Calling sequence of `nullsp`:
1835bb7acecfSBarry Smith + dm        - The present `DM`
1836bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1837147403d9SBarry Smith . field     - The field number in dm
1838147403d9SBarry Smith - nullSpace - The nullspace for the given field
18398cda7954SMatthew G. Knepley 
184049762cbcSSatish Balay   Level: intermediate
184149762cbcSSatish Balay 
18421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1843bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1844147403d9SBarry Smith @*/
1845*a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1846d71ae5a4SJacob Faibussowitsch {
1847f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1848f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18494f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18507a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1851f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1853f9d4088aSMatthew G. Knepley }
1854f9d4088aSMatthew G. Knepley 
18554f3b5142SJed Brown /*@C
1856bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18574d343eeaSMatthew G Knepley 
185820f4b53cSBarry Smith   Not Collective; No Fortran Support
18594d343eeaSMatthew G Knepley 
18604d343eeaSMatthew G Knepley   Input Parameter:
1861bb7acecfSBarry Smith . dm - the `DM` object
18624d343eeaSMatthew G Knepley 
18634d343eeaSMatthew G Knepley   Output Parameters:
186420f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
186520f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
186620f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18674d343eeaSMatthew G Knepley 
18684d343eeaSMatthew G Knepley   Level: intermediate
18694d343eeaSMatthew G Knepley 
1870bb7acecfSBarry Smith   Note:
187121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1872bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1873bb7acecfSBarry Smith   `PetscFree()`.
187421c9b008SJed Brown 
187560225df5SJacob Faibussowitsch   Developer Notes:
1876bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1877bb7acecfSBarry Smith   likely be removed.
1878bb7acecfSBarry Smith 
18791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1880bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18814d343eeaSMatthew G Knepley @*/
1882d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1883d71ae5a4SJacob Faibussowitsch {
188437d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18854d343eeaSMatthew G Knepley 
18864d343eeaSMatthew G Knepley   PetscFunctionBegin;
18874d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
188869ca1f37SDmitry Karpeev   if (numFields) {
18894f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
189069ca1f37SDmitry Karpeev     *numFields = 0;
189169ca1f37SDmitry Karpeev   }
189237d0c07bSMatthew G Knepley   if (fieldNames) {
18934f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
18940298fd71SBarry Smith     *fieldNames = NULL;
189569ca1f37SDmitry Karpeev   }
189669ca1f37SDmitry Karpeev   if (fields) {
18974f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
18980298fd71SBarry Smith     *fields = NULL;
189969ca1f37SDmitry Karpeev   }
19009566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
190137d0c07bSMatthew G Knepley   if (section) {
19023a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
190337d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
190437d0c07bSMatthew G Knepley 
19059566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19079566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19089566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
190937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
191037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19119566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
191237d0c07bSMatthew G Knepley     }
191337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
191437d0c07bSMatthew G Knepley       PetscInt gdof;
191537d0c07bSMatthew G Knepley 
19169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
191737d0c07bSMatthew G Knepley       if (gdof > 0) {
191837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19193a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
192037d0c07bSMatthew G Knepley 
19219566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19229566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19233a544194SStefano Zampini           fpdof = fdof - fcdof;
19243a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19253a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19263a544194SStefano Zampini             fieldNc[f] = 1;
19273a544194SStefano Zampini           }
19283a544194SStefano Zampini           fieldSizes[f] += fpdof;
192937d0c07bSMatthew G Knepley         }
193037d0c07bSMatthew G Knepley       }
193137d0c07bSMatthew G Knepley     }
193237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19339566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
193437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
193537d0c07bSMatthew G Knepley     }
193637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
193837d0c07bSMatthew G Knepley 
19399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
194037d0c07bSMatthew G Knepley       if (gdof > 0) {
19419566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
194237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
194337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
194437d0c07bSMatthew G Knepley 
19459566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19469566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1947ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
194837d0c07bSMatthew G Knepley         }
194937d0c07bSMatthew G Knepley       }
195037d0c07bSMatthew G Knepley     }
19518865f1eaSKarl Rupp     if (numFields) *numFields = nF;
195237d0c07bSMatthew G Knepley     if (fieldNames) {
19539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
195437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
195537d0c07bSMatthew G Knepley         const char *fieldName;
195637d0c07bSMatthew G Knepley 
19579566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19589566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
195937d0c07bSMatthew G Knepley       }
196037d0c07bSMatthew G Knepley     }
196137d0c07bSMatthew G Knepley     if (fields) {
19629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
196337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19643a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19653a544194SStefano Zampini 
19669566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19673a544194SStefano Zampini         in[0] = -fieldNc[f];
19683a544194SStefano Zampini         in[1] = fieldNc[f];
19691c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19703a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19719566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
197237d0c07bSMatthew G Knepley       }
197337d0c07bSMatthew G Knepley     }
19749566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1975dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19774d343eeaSMatthew G Knepley }
19784d343eeaSMatthew G Knepley 
197916621825SDmitry Karpeev /*@C
1980bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1981*a4e35b19SJacob Faibussowitsch   corresponding to different fields.
1982e7c4fc90SDmitry Karpeev 
198320f4b53cSBarry Smith   Not Collective; No Fortran Support
1984e7c4fc90SDmitry Karpeev 
1985e7c4fc90SDmitry Karpeev   Input Parameter:
1986bb7acecfSBarry Smith . dm - the `DM` object
1987e7c4fc90SDmitry Karpeev 
1988e7c4fc90SDmitry Karpeev   Output Parameters:
198920f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
199020f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
199120f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
199220f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
1993e7c4fc90SDmitry Karpeev 
1994e7c4fc90SDmitry Karpeev   Level: intermediate
1995e7c4fc90SDmitry Karpeev 
1996*a4e35b19SJacob Faibussowitsch   Notes:
1997*a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
1998*a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1999*a4e35b19SJacob Faibussowitsch 
2000*a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2001*a4e35b19SJacob Faibussowitsch 
2002e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2003bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2004bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2005e7c4fc90SDmitry Karpeev 
200660225df5SJacob Faibussowitsch   Developer Notes:
2007bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2008bb7acecfSBarry Smith 
200960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2010e7c4fc90SDmitry Karpeev @*/
2011d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2012d71ae5a4SJacob Faibussowitsch {
2013e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2014e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20158865f1eaSKarl Rupp   if (len) {
20164f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20178865f1eaSKarl Rupp     *len = 0;
20188865f1eaSKarl Rupp   }
20198865f1eaSKarl Rupp   if (namelist) {
20204f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2021ea78f98cSLisandro Dalcin     *namelist = NULL;
20228865f1eaSKarl Rupp   }
20238865f1eaSKarl Rupp   if (islist) {
20244f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2025ea78f98cSLisandro Dalcin     *islist = NULL;
20268865f1eaSKarl Rupp   }
20278865f1eaSKarl Rupp   if (dmlist) {
20284f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2029ea78f98cSLisandro Dalcin     *dmlist = NULL;
20308865f1eaSKarl Rupp   }
2031f3f0edfdSDmitry Karpeev   /*
2032f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2033f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2034f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2035f3f0edfdSDmitry Karpeev    */
20367a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
203716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2038435a35e8SMatthew G Knepley     PetscSection section;
2039435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2040435a35e8SMatthew G Knepley 
20419566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20429566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2043435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2044f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20459566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20469566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20479566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2048435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2049435a35e8SMatthew G Knepley         const char *fieldName;
2050435a35e8SMatthew G Knepley 
20519566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
205203dc3394SMatthew G. Knepley         if (namelist) {
20539566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20549566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2055435a35e8SMatthew G Knepley         }
205603dc3394SMatthew G. Knepley       }
2057435a35e8SMatthew G Knepley     } else {
20589566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2059e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20600298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2061e7c4fc90SDmitry Karpeev     }
2062dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
206416621825SDmitry Karpeev }
206516621825SDmitry Karpeev 
2066412a4547SAlexis Marboeuf /*@C
206720f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
206820f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2069435a35e8SMatthew G Knepley 
2070435a35e8SMatthew G Knepley   Not collective
2071435a35e8SMatthew G Knepley 
2072435a35e8SMatthew G Knepley   Input Parameters:
2073bb7acecfSBarry Smith + dm        - The `DM` object
2074bb7acecfSBarry Smith . numFields - The number of fields to select
20752adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2076435a35e8SMatthew G Knepley 
2077435a35e8SMatthew G Knepley   Output Parameters:
2078bb7acecfSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`
2079bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2080435a35e8SMatthew G Knepley 
208120f4b53cSBarry Smith   Level: intermediate
208220f4b53cSBarry Smith 
2083bb7acecfSBarry Smith   Note:
2084bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20855d3b26e6SMatthew G. Knepley 
208660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2087435a35e8SMatthew G Knepley @*/
2088d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2089d71ae5a4SJacob Faibussowitsch {
2090435a35e8SMatthew G Knepley   PetscFunctionBegin;
2091435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20924f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
20934f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
20944f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2095dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
20963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2097435a35e8SMatthew G Knepley }
2098435a35e8SMatthew G Knepley 
20992adcc780SMatthew G. Knepley /*@C
2100bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21012adcc780SMatthew G. Knepley 
21022adcc780SMatthew G. Knepley   Not collective
21032adcc780SMatthew G. Knepley 
2104d8d19677SJose E. Roman   Input Parameters:
2105bb7acecfSBarry Smith + dms - The `DM` objects
2106bb7acecfSBarry Smith - n   - The number of `DM`s
21072adcc780SMatthew G. Knepley 
21082adcc780SMatthew G. Knepley   Output Parameters:
2109bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2110bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21112adcc780SMatthew G. Knepley 
211220f4b53cSBarry Smith   Level: intermediate
211320f4b53cSBarry Smith 
2114bb7acecfSBarry Smith   Note:
2115bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21165d3b26e6SMatthew G. Knepley 
21171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21182adcc780SMatthew G. Knepley @*/
2119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2120d71ae5a4SJacob Faibussowitsch {
21212adcc780SMatthew G. Knepley   PetscInt i;
21222adcc780SMatthew G. Knepley 
21232adcc780SMatthew G. Knepley   PetscFunctionBegin;
21244f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2125ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21264f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21274f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2128bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2129bb7acecfSBarry Smith   if (n) {
2130b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2131dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21322adcc780SMatthew G. Knepley   }
21333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21342adcc780SMatthew G. Knepley }
21352adcc780SMatthew G. Knepley 
213616621825SDmitry Karpeev /*@C
2137*a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2138*a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
213916621825SDmitry Karpeev 
214020f4b53cSBarry Smith   Not Collective
214116621825SDmitry Karpeev 
214216621825SDmitry Karpeev   Input Parameter:
2143bb7acecfSBarry Smith . dm - the `DM` object
214416621825SDmitry Karpeev 
214516621825SDmitry Karpeev   Output Parameters:
214620f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
214720f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21480298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21490298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
215020f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
215116621825SDmitry Karpeev 
215216621825SDmitry Karpeev   Level: intermediate
215316621825SDmitry Karpeev 
2154bb7acecfSBarry Smith   Note:
2155*a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2156*a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2157*a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2158*a4e35b19SJacob Faibussowitsch 
2159*a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2160*a4e35b19SJacob Faibussowitsch 
216116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2162bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2163bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
216416621825SDmitry Karpeev 
2165*a4e35b19SJacob Faibussowitsch   Developer Notes:
216620f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2167bb7acecfSBarry Smith 
216860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
216916621825SDmitry Karpeev @*/
2170d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2171d71ae5a4SJacob Faibussowitsch {
2172be081cd6SPeter Brune   DMSubDomainHookLink link;
2173be081cd6SPeter Brune   PetscInt            i, l;
217416621825SDmitry Karpeev 
217516621825SDmitry Karpeev   PetscFunctionBegin;
217616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21779371c9d4SSatish Balay   if (n) {
21784f572ea9SToby Isaac     PetscAssertPointer(n, 2);
21799371c9d4SSatish Balay     *n = 0;
21809371c9d4SSatish Balay   }
21819371c9d4SSatish Balay   if (namelist) {
21824f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
21839371c9d4SSatish Balay     *namelist = NULL;
21849371c9d4SSatish Balay   }
21859371c9d4SSatish Balay   if (innerislist) {
21864f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
21879371c9d4SSatish Balay     *innerislist = NULL;
21889371c9d4SSatish Balay   }
21899371c9d4SSatish Balay   if (outerislist) {
21904f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
21919371c9d4SSatish Balay     *outerislist = NULL;
21929371c9d4SSatish Balay   }
21939371c9d4SSatish Balay   if (dmlist) {
21944f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
21959371c9d4SSatish Balay     *dmlist = NULL;
21969371c9d4SSatish Balay   }
2197f3f0edfdSDmitry Karpeev   /*
2198f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2199f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2200f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2201f3f0edfdSDmitry Karpeev    */
22027a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
220316621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2204dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
220514a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2206f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2207be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2208be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22099566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2210be081cd6SPeter Brune         }
2211648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2212e7c4fc90SDmitry Karpeev       }
221314a18fd3SPeter Brune     }
2214bb7acecfSBarry Smith     if (n) *n = l;
221514a18fd3SPeter Brune   }
22163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2217e30e807fSPeter Brune }
2218e30e807fSPeter Brune 
2219e30e807fSPeter Brune /*@C
2220e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2221e30e807fSPeter Brune 
222220f4b53cSBarry Smith   Not Collective
2223e30e807fSPeter Brune 
2224e30e807fSPeter Brune   Input Parameters:
2225bb7acecfSBarry Smith + dm     - the `DM` object
2226e30e807fSPeter Brune . n      - the number of subdomain scatters
2227e30e807fSPeter Brune - subdms - the local subdomains
2228e30e807fSPeter Brune 
2229e30e807fSPeter Brune   Output Parameters:
22306b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2231e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2232e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2233e30e807fSPeter Brune 
223420f4b53cSBarry Smith   Level: developer
223520f4b53cSBarry Smith 
2236bb7acecfSBarry Smith   Note:
2237bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2238e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2239e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2240e30e807fSPeter Brune   solution and residual data.
2241e30e807fSPeter Brune 
2242*a4e35b19SJacob Faibussowitsch   Developer Notes:
2243*a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2244*a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2245bb7acecfSBarry Smith 
22461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2247e30e807fSPeter Brune @*/
2248d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2249d71ae5a4SJacob Faibussowitsch {
2250e30e807fSPeter Brune   PetscFunctionBegin;
2251e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22524f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2253dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2255e7c4fc90SDmitry Karpeev }
2256e7c4fc90SDmitry Karpeev 
225747c6ae99SBarry Smith /*@
2258bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
225947c6ae99SBarry Smith 
226020f4b53cSBarry Smith   Collective
226147c6ae99SBarry Smith 
2262d8d19677SJose E. Roman   Input Parameters:
2263bb7acecfSBarry Smith + dm   - the `DM` object
2264bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
226547c6ae99SBarry Smith 
226647c6ae99SBarry Smith   Output Parameter:
226720f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2268ae0a1c52SMatthew G Knepley 
226920f4b53cSBarry Smith   Options Database Key:
2270412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2271412e9a14SMatthew G. Knepley 
227247c6ae99SBarry Smith   Level: developer
227347c6ae99SBarry Smith 
227420f4b53cSBarry Smith   Note:
227520f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
227620f4b53cSBarry Smith 
22771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
227847c6ae99SBarry Smith @*/
2279d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2280d71ae5a4SJacob Faibussowitsch {
2281c833c3b5SJed Brown   DMRefineHookLink link;
228247c6ae99SBarry Smith 
228347c6ae99SBarry Smith   PetscFunctionBegin;
2284732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22859566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2286dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22874057135bSMatthew G Knepley   if (*dmf) {
228843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22898865f1eaSKarl Rupp 
22909566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
22918865f1eaSKarl Rupp 
2292644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22930598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2294656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22958865f1eaSKarl Rupp 
22969566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2297c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
22981baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2299c833c3b5SJed Brown     }
2300c833c3b5SJed Brown   }
23019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2303c833c3b5SJed Brown }
2304c833c3b5SJed Brown 
2305bb9467b5SJed Brown /*@C
2306c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2307c833c3b5SJed Brown 
230820f4b53cSBarry Smith   Logically Collective; No Fortran Support
2309c833c3b5SJed Brown 
23104165533cSJose E. Roman   Input Parameters:
2311bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2312bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2313f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
231420f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2315c833c3b5SJed Brown 
231620f4b53cSBarry Smith   Calling sequence of `refinehook`:
2317bb7acecfSBarry Smith + coarse - coarse level `DM`
2318bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2319c833c3b5SJed Brown - ctx    - optional user-defined function context
2320c833c3b5SJed Brown 
232120f4b53cSBarry Smith   Calling sequence of `interphook`:
2322bb7acecfSBarry Smith + coarse - coarse level `DM`
2323c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2324bb7acecfSBarry Smith . fine   - fine level `DM` to update
2325c833c3b5SJed Brown - ctx    - optional user-defined function context
2326c833c3b5SJed Brown 
2327c833c3b5SJed Brown   Level: advanced
2328c833c3b5SJed Brown 
2329c833c3b5SJed Brown   Notes:
2330bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2331bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2332bb7acecfSBarry Smith 
2333bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2334c833c3b5SJed Brown 
2335c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2336c833c3b5SJed Brown 
23371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2338c833c3b5SJed Brown @*/
2339*a4e35b19SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM coarse, DM fine, void *ctx), PetscErrorCode (*interphook)(DM coarse, Mat interp, DM fine, void *ctx), void *ctx)
2340d71ae5a4SJacob Faibussowitsch {
2341c833c3b5SJed Brown   DMRefineHookLink link, *p;
2342c833c3b5SJed Brown 
2343c833c3b5SJed Brown   PetscFunctionBegin;
2344c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23453d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23463ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23473d8e3701SJed Brown   }
23489566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2349c833c3b5SJed Brown   link->refinehook = refinehook;
2350c833c3b5SJed Brown   link->interphook = interphook;
2351c833c3b5SJed Brown   link->ctx        = ctx;
23520298fd71SBarry Smith   link->next       = NULL;
2353c833c3b5SJed Brown   *p               = link;
23543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2355c833c3b5SJed Brown }
2356c833c3b5SJed Brown 
23573d8e3701SJed Brown /*@C
2358bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2359bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23603d8e3701SJed Brown 
236120f4b53cSBarry Smith   Logically Collective; No Fortran Support
23623d8e3701SJed Brown 
23634165533cSJose E. Roman   Input Parameters:
2364bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2365bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2366bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
236720f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23683d8e3701SJed Brown 
23693d8e3701SJed Brown   Level: advanced
23703d8e3701SJed Brown 
2371bb7acecfSBarry Smith   Note:
23723d8e3701SJed Brown   This function does nothing if the hook is not in the list.
23733d8e3701SJed Brown 
23741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23753d8e3701SJed Brown @*/
2376d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2377d71ae5a4SJacob Faibussowitsch {
23783d8e3701SJed Brown   DMRefineHookLink link, *p;
23793d8e3701SJed Brown 
23803d8e3701SJed Brown   PetscFunctionBegin;
23813d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23823d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23833d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23843d8e3701SJed Brown       link = *p;
23853d8e3701SJed Brown       *p   = link->next;
23869566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23873d8e3701SJed Brown       break;
23883d8e3701SJed Brown     }
23893d8e3701SJed Brown   }
23903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23913d8e3701SJed Brown }
23923d8e3701SJed Brown 
2393c833c3b5SJed Brown /*@
2394bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2395c833c3b5SJed Brown 
2396c833c3b5SJed Brown   Collective if any hooks are
2397c833c3b5SJed Brown 
23984165533cSJose E. Roman   Input Parameters:
2399bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2400bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2401bb7acecfSBarry Smith - fine   - finer `DM` to update
2402c833c3b5SJed Brown 
2403c833c3b5SJed Brown   Level: developer
2404c833c3b5SJed Brown 
240560225df5SJacob Faibussowitsch   Developer Notes:
2406bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2407bb7acecfSBarry Smith   an API with consistent terminology.
2408bb7acecfSBarry Smith 
24091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2410c833c3b5SJed Brown @*/
2411d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2412d71ae5a4SJacob Faibussowitsch {
2413c833c3b5SJed Brown   DMRefineHookLink link;
2414c833c3b5SJed Brown 
2415c833c3b5SJed Brown   PetscFunctionBegin;
2416c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24171baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24184057135bSMatthew G Knepley   }
24193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
242047c6ae99SBarry Smith }
242147c6ae99SBarry Smith 
2422eb3f98d2SBarry Smith /*@
24231f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24241f3379b2SToby Isaac 
242520f4b53cSBarry Smith   Collective
24261f3379b2SToby Isaac 
24274165533cSJose E. Roman   Input Parameters:
2428bb7acecfSBarry Smith + coarse    - coarse `DM`
2429bb7acecfSBarry Smith . fine      - fine `DM`
2430bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2431bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2432bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24331f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24341f3379b2SToby Isaac 
24354165533cSJose E. Roman   Output Parameter:
24361f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24371f3379b2SToby Isaac 
24381f3379b2SToby Isaac   Level: developer
24391f3379b2SToby Isaac 
2440bb7acecfSBarry Smith   Note:
2441bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24421f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24431f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24441f3379b2SToby Isaac   slope-limiting reconstruction.
24451f3379b2SToby Isaac 
244660225df5SJacob Faibussowitsch   Developer Notes:
2447bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2448bb7acecfSBarry Smith 
24491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24501f3379b2SToby Isaac @*/
2451d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2452d71ae5a4SJacob Faibussowitsch {
24531f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24541f3379b2SToby Isaac 
24551f3379b2SToby Isaac   PetscFunctionBegin;
24561f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24571f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24581f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24591f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24601f3379b2SToby Isaac 
24619566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24621f3379b2SToby Isaac   if (interpsol) {
24639566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24641f3379b2SToby Isaac   } else if (interp) {
24659566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
246698921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24681f3379b2SToby Isaac }
24691f3379b2SToby Isaac 
24701f3379b2SToby Isaac /*@
2471bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2472eb3f98d2SBarry Smith 
2473eb3f98d2SBarry Smith   Not Collective
2474eb3f98d2SBarry Smith 
2475eb3f98d2SBarry Smith   Input Parameter:
2476bb7acecfSBarry Smith . dm - the `DM` object
2477eb3f98d2SBarry Smith 
2478eb3f98d2SBarry Smith   Output Parameter:
2479eb3f98d2SBarry Smith . level - number of refinements
2480eb3f98d2SBarry Smith 
2481eb3f98d2SBarry Smith   Level: developer
2482eb3f98d2SBarry Smith 
2483bb7acecfSBarry Smith   Note:
2484bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2485bb7acecfSBarry Smith 
24861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2487eb3f98d2SBarry Smith @*/
2488d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2489d71ae5a4SJacob Faibussowitsch {
2490eb3f98d2SBarry Smith   PetscFunctionBegin;
2491eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2492eb3f98d2SBarry Smith   *level = dm->levelup;
24933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2494eb3f98d2SBarry Smith }
2495eb3f98d2SBarry Smith 
2496fef3a512SBarry Smith /*@
2497bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2498fef3a512SBarry Smith 
2499fef3a512SBarry Smith   Not Collective
2500fef3a512SBarry Smith 
2501d8d19677SJose E. Roman   Input Parameters:
2502bb7acecfSBarry Smith + dm    - the `DM` object
2503fef3a512SBarry Smith - level - number of refinements
2504fef3a512SBarry Smith 
2505fef3a512SBarry Smith   Level: advanced
2506fef3a512SBarry Smith 
250795452b02SPatrick Sanan   Notes:
2508bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2509fef3a512SBarry Smith 
2510bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2511bb7acecfSBarry Smith 
25121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2513fef3a512SBarry Smith @*/
2514d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2515d71ae5a4SJacob Faibussowitsch {
2516fef3a512SBarry Smith   PetscFunctionBegin;
2517fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2518fef3a512SBarry Smith   dm->levelup = level;
25193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2520fef3a512SBarry Smith }
2521fef3a512SBarry Smith 
2522d410b0cfSMatthew G. Knepley /*@
2523bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2524d410b0cfSMatthew G. Knepley 
252520f4b53cSBarry Smith   Collective
2526d410b0cfSMatthew G. Knepley 
2527f1a722f8SMatthew G. Knepley   Input Parameters:
2528bb7acecfSBarry Smith + dm     - the `DM` object
2529d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2530d410b0cfSMatthew G. Knepley 
2531d410b0cfSMatthew G. Knepley   Output Parameter:
253220f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2533d410b0cfSMatthew G. Knepley 
2534d410b0cfSMatthew G. Knepley   Level: developer
2535d410b0cfSMatthew G. Knepley 
253620f4b53cSBarry Smith   Note:
253720f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
253820f4b53cSBarry Smith 
25391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2540d410b0cfSMatthew G. Knepley @*/
2541d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2542d71ae5a4SJacob Faibussowitsch {
2543d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2544d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2545dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2546d410b0cfSMatthew G. Knepley   if (*dme) {
2547d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25489566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2549d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25509566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2551d410b0cfSMatthew G. Knepley   }
25523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2553d410b0cfSMatthew G. Knepley }
2554d410b0cfSMatthew G. Knepley 
2555d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2556d71ae5a4SJacob Faibussowitsch {
2557ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2558ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25594f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2560ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2562ca3d3a14SMatthew G. Knepley }
2563ca3d3a14SMatthew G. Knepley 
2564d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2565d71ae5a4SJacob Faibussowitsch {
2566ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2567ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25684f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2569ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2571ca3d3a14SMatthew G. Knepley }
2572ca3d3a14SMatthew G. Knepley 
2573ca3d3a14SMatthew G. Knepley /*@
2574bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2575ca3d3a14SMatthew G. Knepley 
2576ca3d3a14SMatthew G. Knepley   Input Parameter:
257720f4b53cSBarry Smith . dm - The `DM`
2578ca3d3a14SMatthew G. Knepley 
2579ca3d3a14SMatthew G. Knepley   Output Parameter:
258020f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2581ca3d3a14SMatthew G. Knepley 
2582ca3d3a14SMatthew G. Knepley   Level: developer
2583ca3d3a14SMatthew G. Knepley 
25841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2585ca3d3a14SMatthew G. Knepley @*/
2586d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2587d71ae5a4SJacob Faibussowitsch {
2588ca3d3a14SMatthew G. Knepley   Vec tv;
2589ca3d3a14SMatthew G. Knepley 
2590ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2591ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25924f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
25939566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2594ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
25953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2596ca3d3a14SMatthew G. Knepley }
2597ca3d3a14SMatthew G. Knepley 
2598d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2599d71ae5a4SJacob Faibussowitsch {
2600ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2601ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2602ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2603ca3d3a14SMatthew G. Knepley 
2604ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26059566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26069566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26079566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26089566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26099566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26109566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26119566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2613ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2615ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2616ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2617ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26189566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2619ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26209566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26219566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2622ca3d3a14SMatthew G. Knepley     }
2623ca3d3a14SMatthew G. Knepley   }
26249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26259566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26269566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2627ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2628ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2630ca3d3a14SMatthew G. Knepley       if (dof) {
2631ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2632ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2633ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2634ca3d3a14SMatthew G. Knepley 
2635ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26369566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26379566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26389566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2639ca3d3a14SMatthew G. Knepley       }
2640ca3d3a14SMatthew G. Knepley     }
2641ca3d3a14SMatthew G. Knepley   }
26429566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2644ca3d3a14SMatthew G. Knepley }
2645ca3d3a14SMatthew G. Knepley 
2646d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2647d71ae5a4SJacob Faibussowitsch {
2648ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2649ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2650ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2651ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2652ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2653ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2654ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26559566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2657ca3d3a14SMatthew G. Knepley }
2658ca3d3a14SMatthew G. Knepley 
2659bb9467b5SJed Brown /*@C
2660bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2661baf369e7SPeter Brune 
266220f4b53cSBarry Smith   Logically Collective
2663baf369e7SPeter Brune 
26644165533cSJose E. Roman   Input Parameters:
2665bb7acecfSBarry Smith + dm        - the `DM`
2666bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2667bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
266820f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2669baf369e7SPeter Brune 
267020f4b53cSBarry Smith   Calling sequence of `beginhook`:
2671*a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2672baf369e7SPeter Brune . g    - global vector
2673baf369e7SPeter Brune . mode - mode
2674baf369e7SPeter Brune . l    - local vector
2675baf369e7SPeter Brune - ctx  - optional user-defined function context
2676baf369e7SPeter Brune 
267720f4b53cSBarry Smith   Calling sequence of `endhook`:
2678*a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2679*a4e35b19SJacob Faibussowitsch . g    - global vector
2680*a4e35b19SJacob Faibussowitsch . mode - mode
2681*a4e35b19SJacob Faibussowitsch . l    - local vector
2682baf369e7SPeter Brune - ctx  - optional user-defined function context
2683baf369e7SPeter Brune 
2684baf369e7SPeter Brune   Level: advanced
2685baf369e7SPeter Brune 
2686bb7acecfSBarry Smith   Note:
2687bb7acecfSBarry 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.
2688bb7acecfSBarry Smith 
26891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2690baf369e7SPeter Brune @*/
2691*a4e35b19SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), PetscErrorCode (*endhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), void *ctx)
2692d71ae5a4SJacob Faibussowitsch {
2693baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2694baf369e7SPeter Brune 
2695baf369e7SPeter Brune   PetscFunctionBegin;
2696baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2697baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
26989566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2699baf369e7SPeter Brune   link->beginhook = beginhook;
2700baf369e7SPeter Brune   link->endhook   = endhook;
2701baf369e7SPeter Brune   link->ctx       = ctx;
27020298fd71SBarry Smith   link->next      = NULL;
2703baf369e7SPeter Brune   *p              = link;
27043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2705baf369e7SPeter Brune }
2706baf369e7SPeter Brune 
2707d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2708d71ae5a4SJacob Faibussowitsch {
27094c274da1SToby Isaac   Mat          cMat;
271079769bd5SJed Brown   Vec          cVec, cBias;
27114c274da1SToby Isaac   PetscSection section, cSec;
27124c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27134c274da1SToby Isaac 
27144c274da1SToby Isaac   PetscFunctionBegin;
2715*a4e35b19SJacob Faibussowitsch   (void)g;
2716*a4e35b19SJacob Faibussowitsch   (void)ctx;
27174c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27189566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27194c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27205db9a05bSToby Isaac     PetscInt nRows;
27215db9a05bSToby Isaac 
27229566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27233ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27249566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27259566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27269566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27279566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27294c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27314c274da1SToby Isaac       if (dof) {
27324c274da1SToby Isaac         PetscScalar *vals;
27339566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27349566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27354c274da1SToby Isaac       }
27364c274da1SToby Isaac     }
27379566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27384c274da1SToby Isaac   }
27393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27404c274da1SToby Isaac }
27414c274da1SToby Isaac 
274247c6ae99SBarry Smith /*@
274301729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
274401729b5cSPatrick Sanan 
274520f4b53cSBarry Smith   Neighbor-wise Collective
274601729b5cSPatrick Sanan 
274701729b5cSPatrick Sanan   Input Parameters:
2748bb7acecfSBarry Smith + dm   - the `DM` object
274901729b5cSPatrick Sanan . g    - the global vector
2750bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
275101729b5cSPatrick Sanan - l    - the local vector
275201729b5cSPatrick Sanan 
275320f4b53cSBarry Smith   Level: beginner
275420f4b53cSBarry Smith 
275501729b5cSPatrick Sanan   Notes:
2756bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2757bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2758bb7acecfSBarry Smith 
2759bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
276001729b5cSPatrick Sanan 
27611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
276260225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2763bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
276401729b5cSPatrick Sanan @*/
2765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2766d71ae5a4SJacob Faibussowitsch {
276701729b5cSPatrick Sanan   PetscFunctionBegin;
27689566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27699566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
277101729b5cSPatrick Sanan }
277201729b5cSPatrick Sanan 
277301729b5cSPatrick Sanan /*@
277447c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
277547c6ae99SBarry Smith 
277620f4b53cSBarry Smith   Neighbor-wise Collective
277747c6ae99SBarry Smith 
277847c6ae99SBarry Smith   Input Parameters:
2779bb7acecfSBarry Smith + dm   - the `DM` object
278047c6ae99SBarry Smith . g    - the global vector
2781bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
278247c6ae99SBarry Smith - l    - the local vector
278347c6ae99SBarry Smith 
278401729b5cSPatrick Sanan   Level: intermediate
278547c6ae99SBarry Smith 
2786bb7acecfSBarry Smith   Notes:
2787bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2788bb7acecfSBarry Smith 
2789bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2790bb7acecfSBarry Smith 
2791bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2792bb7acecfSBarry Smith 
2793bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2794bb7acecfSBarry Smith 
279560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
279647c6ae99SBarry Smith @*/
2797d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2798d71ae5a4SJacob Faibussowitsch {
27997128ae9fSMatthew G Knepley   PetscSF                 sf;
2800baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
280147c6ae99SBarry Smith 
280247c6ae99SBarry Smith   PetscFunctionBegin;
2803171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2804baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28051baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2806baf369e7SPeter Brune   }
28079566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28087128ae9fSMatthew G Knepley   if (sf) {
2809ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2810ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2811d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28127128ae9fSMatthew G Knepley 
28137a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28149566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28159566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28169566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28179566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28189566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28197128ae9fSMatthew G Knepley   } else {
28209566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28217128ae9fSMatthew G Knepley   }
28223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
282347c6ae99SBarry Smith }
282447c6ae99SBarry Smith 
282547c6ae99SBarry Smith /*@
282647c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
282747c6ae99SBarry Smith 
282820f4b53cSBarry Smith   Neighbor-wise Collective
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith   Input Parameters:
2831bb7acecfSBarry Smith + dm   - the `DM` object
283247c6ae99SBarry Smith . g    - the global vector
2833bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
283447c6ae99SBarry Smith - l    - the local vector
283547c6ae99SBarry Smith 
283601729b5cSPatrick Sanan   Level: intermediate
283747c6ae99SBarry Smith 
2838bb7acecfSBarry Smith   Note:
2839bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2840bb7acecfSBarry Smith 
284160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
284247c6ae99SBarry Smith @*/
2843d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2844d71ae5a4SJacob Faibussowitsch {
28457128ae9fSMatthew G Knepley   PetscSF                 sf;
2846ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2847ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2848ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2849baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2850d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
285147c6ae99SBarry Smith 
285247c6ae99SBarry Smith   PetscFunctionBegin;
2853171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28549566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28559566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28567128ae9fSMatthew G Knepley   if (sf) {
28577a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28587128ae9fSMatthew G Knepley 
28599566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28609566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28619566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28629566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28639566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28649566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28657128ae9fSMatthew G Knepley   } else {
28669566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28677128ae9fSMatthew G Knepley   }
28689566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2869baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28709566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2871baf369e7SPeter Brune   }
28723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
287347c6ae99SBarry Smith }
287447c6ae99SBarry Smith 
2875d4d07f1eSToby Isaac /*@C
2876d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2877d4d07f1eSToby Isaac 
287820f4b53cSBarry Smith   Logically Collective
2879d4d07f1eSToby Isaac 
28804165533cSJose E. Roman   Input Parameters:
2881bb7acecfSBarry Smith + dm        - the `DM`
2882bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2883bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
288420f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2885d4d07f1eSToby Isaac 
288620f4b53cSBarry Smith   Calling sequence of `beginhook`:
2887*a4e35b19SJacob Faibussowitsch + global - global `DM`
2888d4d07f1eSToby Isaac . l      - local vector
2889d4d07f1eSToby Isaac . mode   - mode
2890d4d07f1eSToby Isaac . g      - global vector
2891d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2892d4d07f1eSToby Isaac 
289320f4b53cSBarry Smith   Calling sequence of `endhook`:
2894bb7acecfSBarry Smith + global - global `DM`
2895d4d07f1eSToby Isaac . l      - local vector
2896d4d07f1eSToby Isaac . mode   - mode
2897d4d07f1eSToby Isaac . g      - global vector
2898d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2899d4d07f1eSToby Isaac 
2900d4d07f1eSToby Isaac   Level: advanced
2901d4d07f1eSToby Isaac 
29021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2903d4d07f1eSToby Isaac @*/
2904*a4e35b19SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), PetscErrorCode (*endhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), void *ctx)
2905d71ae5a4SJacob Faibussowitsch {
2906d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2907d4d07f1eSToby Isaac 
2908d4d07f1eSToby Isaac   PetscFunctionBegin;
2909d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2910d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29119566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2912d4d07f1eSToby Isaac   link->beginhook = beginhook;
2913d4d07f1eSToby Isaac   link->endhook   = endhook;
2914d4d07f1eSToby Isaac   link->ctx       = ctx;
2915d4d07f1eSToby Isaac   link->next      = NULL;
2916d4d07f1eSToby Isaac   *p              = link;
29173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2918d4d07f1eSToby Isaac }
2919d4d07f1eSToby Isaac 
2920d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2921d71ae5a4SJacob Faibussowitsch {
29224c274da1SToby Isaac   Mat          cMat;
29234c274da1SToby Isaac   Vec          cVec;
29244c274da1SToby Isaac   PetscSection section, cSec;
29254c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29264c274da1SToby Isaac 
29274c274da1SToby Isaac   PetscFunctionBegin;
2928*a4e35b19SJacob Faibussowitsch   (void)g;
2929*a4e35b19SJacob Faibussowitsch   (void)ctx;
29304c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29319566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29324c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29335db9a05bSToby Isaac     PetscInt nRows;
29345db9a05bSToby Isaac 
29359566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29363ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29379566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29389566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29404c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29419566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29424c274da1SToby Isaac       if (dof) {
29434c274da1SToby Isaac         PetscInt     d;
29444c274da1SToby Isaac         PetscScalar *vals;
29459566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29469566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29474c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29484c274da1SToby Isaac          * we just extracted */
2949ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29504c274da1SToby Isaac       }
29514c274da1SToby Isaac     }
29529566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29539566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29544c274da1SToby Isaac   }
29553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29564c274da1SToby Isaac }
295701729b5cSPatrick Sanan /*@
295801729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
295901729b5cSPatrick Sanan 
296020f4b53cSBarry Smith   Neighbor-wise Collective
296101729b5cSPatrick Sanan 
296201729b5cSPatrick Sanan   Input Parameters:
2963bb7acecfSBarry Smith + dm   - the `DM` object
296401729b5cSPatrick Sanan . l    - the local vector
2965bb7acecfSBarry 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.
296601729b5cSPatrick Sanan - g    - the global vector
296701729b5cSPatrick Sanan 
296820f4b53cSBarry Smith   Level: beginner
296920f4b53cSBarry Smith 
297001729b5cSPatrick Sanan   Notes:
297101729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
2972bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
297301729b5cSPatrick Sanan 
2974bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2975bb7acecfSBarry Smith 
2976bb7acecfSBarry 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.
2977bb7acecfSBarry Smith 
2978bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
297901729b5cSPatrick Sanan 
29801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
298101729b5cSPatrick Sanan @*/
2982d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2983d71ae5a4SJacob Faibussowitsch {
298401729b5cSPatrick Sanan   PetscFunctionBegin;
29859566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29869566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
29873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
298801729b5cSPatrick Sanan }
29894c274da1SToby Isaac 
299047c6ae99SBarry Smith /*@
299101729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
29929a42bb27SBarry Smith 
299320f4b53cSBarry Smith   Neighbor-wise Collective
29949a42bb27SBarry Smith 
29959a42bb27SBarry Smith   Input Parameters:
2996bb7acecfSBarry Smith + dm   - the `DM` object
2997f6813fd5SJed Brown . l    - the local vector
2998aa624791SPierre Jolivet . mode - if `INSERT_VALUES` then no parallel communication is used, if `ADD_VALUES` then all ghost points from the same base point accumulate into that base point.
29991eb28f2eSBarry Smith - g    - the global vector
30009a42bb27SBarry Smith 
300120f4b53cSBarry Smith   Level: intermediate
300220f4b53cSBarry Smith 
300395452b02SPatrick Sanan   Notes:
3004bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3005bb7acecfSBarry Smith 
3006bb7acecfSBarry 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.
3007bb7acecfSBarry Smith 
3008bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3009bb7acecfSBarry Smith 
3010bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3011bb7acecfSBarry Smith 
3012bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30139a42bb27SBarry Smith 
30141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30159a42bb27SBarry Smith @*/
3016d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3017d71ae5a4SJacob Faibussowitsch {
30187128ae9fSMatthew G Knepley   PetscSF                 sf;
301984330215SMatthew G. Knepley   PetscSection            s, gs;
3020d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3021ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3022ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3023ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3024fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3025d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30269a42bb27SBarry Smith 
30279a42bb27SBarry Smith   PetscFunctionBegin;
3028171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3029d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30301baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3031d4d07f1eSToby Isaac   }
30329566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30339566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30349566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30357128ae9fSMatthew G Knepley   switch (mode) {
30367128ae9fSMatthew G Knepley   case INSERT_VALUES:
30377128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3038d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3039d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3040d71ae5a4SJacob Faibussowitsch     break;
30417128ae9fSMatthew G Knepley   case ADD_VALUES:
30427128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3043d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3044d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3045d71ae5a4SJacob Faibussowitsch     break;
3046d71ae5a4SJacob Faibussowitsch   default:
3047d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30487128ae9fSMatthew G Knepley   }
3049ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30509566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3051ca3d3a14SMatthew G. Knepley     if (transform) {
30529566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30539566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30549566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30559566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3056fa88e482SJed Brown     } else if (isInsert) {
30579566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3058fa88e482SJed Brown     } else {
30599566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3060fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3061ca3d3a14SMatthew G. Knepley     }
3062fa88e482SJed Brown     if (s && isInsert) {
30639566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3064fa88e482SJed Brown     } else {
30659566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3066fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3067fa88e482SJed Brown     }
3068ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30699566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
307084330215SMatthew G. Knepley     } else if (s && isInsert) {
307184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
307284330215SMatthew G. Knepley 
30739566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30759566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
307684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3077b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
307884330215SMatthew G. Knepley 
30799566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30809566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30819566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30829566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30839566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3085b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
308603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30877a8be351SBarry 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);
3088b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3089b3b16f48SMatthew G. Knepley         if (!gcdof) {
309084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3091b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3092b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
309384330215SMatthew G. Knepley           const PetscInt *cdofs;
309484330215SMatthew G. Knepley           PetscInt        cind = 0;
309584330215SMatthew G. Knepley 
30969566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3097b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
30989371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
30999371c9d4SSatish Balay               ++cind;
31009371c9d4SSatish Balay               continue;
31019371c9d4SSatish Balay             }
3102b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
310384330215SMatthew G. Knepley           }
31047a8be351SBarry 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);
310584330215SMatthew G. Knepley       }
3106ca3d3a14SMatthew G. Knepley     }
3107fa88e482SJed Brown     if (g_inplace) {
31089566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3109fa88e482SJed Brown     } else {
31109566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3111fa88e482SJed Brown     }
3112ca3d3a14SMatthew G. Knepley     if (transform) {
31139566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31149566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3115fa88e482SJed Brown     } else if (l_inplace) {
31169566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3117ca3d3a14SMatthew G. Knepley     } else {
31189566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3119ca3d3a14SMatthew G. Knepley     }
31207128ae9fSMatthew G Knepley   } else {
31219566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31227128ae9fSMatthew G Knepley   }
31233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31249a42bb27SBarry Smith }
31259a42bb27SBarry Smith 
31269a42bb27SBarry Smith /*@
31279a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
312847c6ae99SBarry Smith 
312920f4b53cSBarry Smith   Neighbor-wise Collective
313047c6ae99SBarry Smith 
313147c6ae99SBarry Smith   Input Parameters:
3132bb7acecfSBarry Smith + dm   - the `DM` object
3133f6813fd5SJed Brown . l    - the local vector
3134bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3135f6813fd5SJed Brown - g    - the global vector
313647c6ae99SBarry Smith 
313701729b5cSPatrick Sanan   Level: intermediate
313847c6ae99SBarry Smith 
3139bb7acecfSBarry Smith   Note:
3140bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3141bb7acecfSBarry Smith 
314260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
314347c6ae99SBarry Smith @*/
3144d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3145d71ae5a4SJacob Faibussowitsch {
31467128ae9fSMatthew G Knepley   PetscSF                 sf;
314784330215SMatthew G. Knepley   PetscSection            s;
3148d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3149ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
315047c6ae99SBarry Smith 
315147c6ae99SBarry Smith   PetscFunctionBegin;
3152171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31539566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31549566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31557128ae9fSMatthew G Knepley   switch (mode) {
31567128ae9fSMatthew G Knepley   case INSERT_VALUES:
3157d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3158d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3159d71ae5a4SJacob Faibussowitsch     break;
31607128ae9fSMatthew G Knepley   case ADD_VALUES:
3161d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3162d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3163d71ae5a4SJacob Faibussowitsch     break;
3164d71ae5a4SJacob Faibussowitsch   default:
3165d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31667128ae9fSMatthew G Knepley   }
316784330215SMatthew G. Knepley   if (sf && !isInsert) {
3168ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3169ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3170ca3d3a14SMatthew G. Knepley     Vec                tmpl;
317184330215SMatthew G. Knepley 
31729566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3173ca3d3a14SMatthew G. Knepley     if (transform) {
31749566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31759566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3176ca3d3a14SMatthew G. Knepley     } else {
31779566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3178ca3d3a14SMatthew G. Knepley     }
31799566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31809566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3181ca3d3a14SMatthew G. Knepley     if (transform) {
31829566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31839566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3184ca3d3a14SMatthew G. Knepley     } else {
31859566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3186ca3d3a14SMatthew G. Knepley     }
31879566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
318884330215SMatthew G. Knepley   } else if (s && isInsert) {
31897128ae9fSMatthew G Knepley   } else {
31909566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31917128ae9fSMatthew G Knepley   }
3192d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
31939566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3194d4d07f1eSToby Isaac   }
31953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
319647c6ae99SBarry Smith }
319747c6ae99SBarry Smith 
3198f089877aSRichard Tran Mills /*@
3199*a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3200*a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3201*a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3202f089877aSRichard Tran Mills 
320320f4b53cSBarry Smith   Neighbor-wise Collective
3204f089877aSRichard Tran Mills 
3205f089877aSRichard Tran Mills   Input Parameters:
3206bb7acecfSBarry Smith + dm   - the `DM` object
3207bc0a1609SRichard Tran Mills . g    - the original local vector
3208bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3209f089877aSRichard Tran Mills 
3210bc0a1609SRichard Tran Mills   Output Parameter:
3211bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3212f089877aSRichard Tran Mills 
3213f089877aSRichard Tran Mills   Level: intermediate
3214f089877aSRichard Tran Mills 
3215*a4e35b19SJacob Faibussowitsch   Notes:
3216*a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3217*a4e35b19SJacob Faibussowitsch 
321860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3219f089877aSRichard Tran Mills @*/
3220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3221d71ae5a4SJacob Faibussowitsch {
3222f089877aSRichard Tran Mills   PetscFunctionBegin;
3223f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32249f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32259f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32269f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3228f089877aSRichard Tran Mills }
3229f089877aSRichard Tran Mills 
3230f089877aSRichard Tran Mills /*@
3231bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3232bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3233f089877aSRichard Tran Mills 
323420f4b53cSBarry Smith   Neighbor-wise Collective
3235f089877aSRichard Tran Mills 
3236f089877aSRichard Tran Mills   Input Parameters:
323760225df5SJacob Faibussowitsch + dm   - the `DM` object
3238bc0a1609SRichard Tran Mills . g    - the original local vector
3239bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3240f089877aSRichard Tran Mills 
3241bc0a1609SRichard Tran Mills   Output Parameter:
3242bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3243f089877aSRichard Tran Mills 
3244f089877aSRichard Tran Mills   Level: intermediate
3245f089877aSRichard Tran Mills 
324660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3247f089877aSRichard Tran Mills @*/
3248d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3249d71ae5a4SJacob Faibussowitsch {
3250f089877aSRichard Tran Mills   PetscFunctionBegin;
3251f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32529f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32539f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32549f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3256f089877aSRichard Tran Mills }
3257f089877aSRichard Tran Mills 
325847c6ae99SBarry Smith /*@
3259bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
326047c6ae99SBarry Smith 
326120f4b53cSBarry Smith   Collective
326247c6ae99SBarry Smith 
3263d8d19677SJose E. Roman   Input Parameters:
3264bb7acecfSBarry Smith + dm   - the `DM` object
326520f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
326647c6ae99SBarry Smith 
326747c6ae99SBarry Smith   Output Parameter:
3268bb7acecfSBarry Smith . dmc - the coarsened `DM`
326947c6ae99SBarry Smith 
327047c6ae99SBarry Smith   Level: developer
327147c6ae99SBarry Smith 
32721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
327347c6ae99SBarry Smith @*/
3274d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3275d71ae5a4SJacob Faibussowitsch {
3276b17ce1afSJed Brown   DMCoarsenHookLink link;
327747c6ae99SBarry Smith 
327847c6ae99SBarry Smith   PetscFunctionBegin;
3279171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32809566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3281dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3282b9d85ea2SLisandro Dalcin   if (*dmc) {
3283a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32849566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
328543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32869566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3287644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
32880598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3289656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
32909566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3291b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
32929566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3293b17ce1afSJed Brown     }
3294b9d85ea2SLisandro Dalcin   }
32959566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
32967a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
32973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3298b17ce1afSJed Brown }
3299b17ce1afSJed Brown 
3300bb9467b5SJed Brown /*@C
3301b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3302b17ce1afSJed Brown 
330320f4b53cSBarry Smith   Logically Collective; No Fortran Support
3304b17ce1afSJed Brown 
33054165533cSJose E. Roman   Input Parameters:
3306bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3307b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3308bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
330920f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3310b17ce1afSJed Brown 
331120f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3312bb7acecfSBarry Smith + fine   - fine level `DM`
3313bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3314b17ce1afSJed Brown - ctx    - optional user-defined function context
3315b17ce1afSJed Brown 
331620f4b53cSBarry Smith   Calling sequence of `restricthook`:
3317bb7acecfSBarry Smith + fine      - fine level `DM`
3318bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3319c833c3b5SJed Brown . rscale    - scaling vector for restriction
3320c833c3b5SJed Brown . inject    - matrix restricting by injection
3321b17ce1afSJed Brown . coarse    - coarse level DM to update
3322b17ce1afSJed Brown - ctx       - optional user-defined function context
3323b17ce1afSJed Brown 
3324b17ce1afSJed Brown   Level: advanced
3325b17ce1afSJed Brown 
3326b17ce1afSJed Brown   Notes:
3327bb7acecfSBarry 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`.
3328b17ce1afSJed Brown 
3329b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3330b17ce1afSJed Brown 
3331b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3332bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3333b17ce1afSJed Brown 
3334bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3335bb7acecfSBarry Smith 
33361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3337b17ce1afSJed Brown @*/
3338*a4e35b19SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM fine, DM coarse, void *ctx), PetscErrorCode (*restricthook)(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx), void *ctx)
3339d71ae5a4SJacob Faibussowitsch {
3340b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3341b17ce1afSJed Brown 
3342b17ce1afSJed Brown   PetscFunctionBegin;
3343b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33441e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33453ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33461e3d8eccSJed Brown   }
33479566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3348b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3349b17ce1afSJed Brown   link->restricthook = restricthook;
3350b17ce1afSJed Brown   link->ctx          = ctx;
33510298fd71SBarry Smith   link->next         = NULL;
3352b17ce1afSJed Brown   *p                 = link;
33533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3354b17ce1afSJed Brown }
3355b17ce1afSJed Brown 
3356dc822a44SJed Brown /*@C
3357bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3358dc822a44SJed Brown 
335920f4b53cSBarry Smith   Logically Collective; No Fortran Support
3360dc822a44SJed Brown 
33614165533cSJose E. Roman   Input Parameters:
3362bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3363dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3364bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
336520f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3366dc822a44SJed Brown 
3367dc822a44SJed Brown   Level: advanced
3368dc822a44SJed Brown 
3369bb7acecfSBarry Smith   Note:
3370dc822a44SJed Brown   This function does nothing if the hook is not in the list.
3371dc822a44SJed Brown 
33721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3373dc822a44SJed Brown @*/
3374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3375d71ae5a4SJacob Faibussowitsch {
3376dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3377dc822a44SJed Brown 
3378dc822a44SJed Brown   PetscFunctionBegin;
3379dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3380dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3381dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3382dc822a44SJed Brown       link = *p;
3383dc822a44SJed Brown       *p   = link->next;
33849566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3385dc822a44SJed Brown       break;
3386dc822a44SJed Brown     }
3387dc822a44SJed Brown   }
33883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3389dc822a44SJed Brown }
3390dc822a44SJed Brown 
3391b17ce1afSJed Brown /*@
3392bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3393b17ce1afSJed Brown 
3394b17ce1afSJed Brown   Collective if any hooks are
3395b17ce1afSJed Brown 
33964165533cSJose E. Roman   Input Parameters:
3397bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3398bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3399e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3400bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
340120f4b53cSBarry Smith - coarse  - coarser `DM` to update
3402b17ce1afSJed Brown 
3403b17ce1afSJed Brown   Level: developer
3404b17ce1afSJed Brown 
340560225df5SJacob Faibussowitsch   Developer Notes:
3406bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3407bb7acecfSBarry Smith 
34081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3409b17ce1afSJed Brown @*/
3410d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3411d71ae5a4SJacob Faibussowitsch {
3412b17ce1afSJed Brown   DMCoarsenHookLink link;
3413b17ce1afSJed Brown 
3414b17ce1afSJed Brown   PetscFunctionBegin;
3415b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34161baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3417b17ce1afSJed Brown   }
34183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
341947c6ae99SBarry Smith }
342047c6ae99SBarry Smith 
3421bb9467b5SJed Brown /*@C
3422be081cd6SPeter Brune   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34235dbd56e3SPeter Brune 
342420f4b53cSBarry Smith   Logically Collective; No Fortran Support
34255dbd56e3SPeter Brune 
34264165533cSJose E. Roman   Input Parameters:
3427bb7acecfSBarry Smith + global       - global `DM`
3428bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34295dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
343020f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34315dbd56e3SPeter Brune 
343220f4b53cSBarry Smith   Calling sequence of `ddhook`:
3433bb7acecfSBarry Smith + global - global `DM`
3434bb7acecfSBarry Smith . block  - block `DM`
3435ec4806b8SPeter Brune - ctx    - optional user-defined function context
3436ec4806b8SPeter Brune 
343720f4b53cSBarry Smith   Calling sequence of `restricthook`:
3438bb7acecfSBarry Smith + global - global `DM`
34395dbd56e3SPeter Brune . out    - scatter to the outer (with ghost and overlap points) block vector
34405dbd56e3SPeter Brune . in     - scatter to block vector values only owned locally
3441bb7acecfSBarry Smith . block  - block `DM`
34425dbd56e3SPeter Brune - ctx    - optional user-defined function context
34435dbd56e3SPeter Brune 
34445dbd56e3SPeter Brune   Level: advanced
34455dbd56e3SPeter Brune 
34465dbd56e3SPeter Brune   Notes:
3447bb7acecfSBarry Smith   This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34485dbd56e3SPeter Brune 
34495dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34505dbd56e3SPeter Brune 
34515dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3452bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34535dbd56e3SPeter Brune 
34541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34555dbd56e3SPeter Brune @*/
3456*a4e35b19SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM global, DM block, void *ctx), PetscErrorCode (*restricthook)(DM global, VecScatter out, VecScatter in, DM block, void *ctx), void *ctx)
3457d71ae5a4SJacob Faibussowitsch {
3458be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34595dbd56e3SPeter Brune 
34605dbd56e3SPeter Brune   PetscFunctionBegin;
34615dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3462b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34633ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3464b3a6b972SJed Brown   }
34659566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34665dbd56e3SPeter Brune   link->restricthook = restricthook;
3467be081cd6SPeter Brune   link->ddhook       = ddhook;
34685dbd56e3SPeter Brune   link->ctx          = ctx;
34690298fd71SBarry Smith   link->next         = NULL;
34705dbd56e3SPeter Brune   *p                 = link;
34713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34725dbd56e3SPeter Brune }
34735dbd56e3SPeter Brune 
3474b3a6b972SJed Brown /*@C
3475b3a6b972SJed Brown   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3476b3a6b972SJed Brown 
347720f4b53cSBarry Smith   Logically Collective; No Fortran Support
3478b3a6b972SJed Brown 
34794165533cSJose E. Roman   Input Parameters:
3480bb7acecfSBarry Smith + global       - global `DM`
3481bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3482b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
348320f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3484b3a6b972SJed Brown 
3485b3a6b972SJed Brown   Level: advanced
3486b3a6b972SJed Brown 
34871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3488b3a6b972SJed Brown @*/
3489d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3490d71ae5a4SJacob Faibussowitsch {
3491b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3492b3a6b972SJed Brown 
3493b3a6b972SJed Brown   PetscFunctionBegin;
3494b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3495b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3496b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3497b3a6b972SJed Brown       link = *p;
3498b3a6b972SJed Brown       *p   = link->next;
34999566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3500b3a6b972SJed Brown       break;
3501b3a6b972SJed Brown     }
3502b3a6b972SJed Brown   }
35033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3504b3a6b972SJed Brown }
3505b3a6b972SJed Brown 
35065dbd56e3SPeter Brune /*@
3507bb7acecfSBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35085dbd56e3SPeter Brune 
35095dbd56e3SPeter Brune   Collective if any hooks are
35105dbd56e3SPeter Brune 
35114165533cSJose E. Roman   Input Parameters:
3512*a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3513*a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3514*a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3515*a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35165dbd56e3SPeter Brune 
35175dbd56e3SPeter Brune   Level: developer
35185dbd56e3SPeter Brune 
35191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`
35205dbd56e3SPeter Brune @*/
3521d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3522d71ae5a4SJacob Faibussowitsch {
3523be081cd6SPeter Brune   DMSubDomainHookLink link;
35245dbd56e3SPeter Brune 
35255dbd56e3SPeter Brune   PetscFunctionBegin;
3526be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35271baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35285dbd56e3SPeter Brune   }
35293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35305dbd56e3SPeter Brune }
35315dbd56e3SPeter Brune 
35325fe1f584SPeter Brune /*@
3533bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35345fe1f584SPeter Brune 
35355fe1f584SPeter Brune   Not Collective
35365fe1f584SPeter Brune 
35375fe1f584SPeter Brune   Input Parameter:
3538bb7acecfSBarry Smith . dm - the `DM` object
35395fe1f584SPeter Brune 
35405fe1f584SPeter Brune   Output Parameter:
35416a7d9d85SPeter Brune . level - number of coarsenings
35425fe1f584SPeter Brune 
35435fe1f584SPeter Brune   Level: developer
35445fe1f584SPeter Brune 
35451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35465fe1f584SPeter Brune @*/
3547d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3548d71ae5a4SJacob Faibussowitsch {
35495fe1f584SPeter Brune   PetscFunctionBegin;
35505fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35514f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35525fe1f584SPeter Brune   *level = dm->leveldown;
35533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35545fe1f584SPeter Brune }
35555fe1f584SPeter Brune 
35569a64c4a8SMatthew G. Knepley /*@
3557bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35589a64c4a8SMatthew G. Knepley 
355920f4b53cSBarry Smith   Collective
35609a64c4a8SMatthew G. Knepley 
35619a64c4a8SMatthew G. Knepley   Input Parameters:
3562bb7acecfSBarry Smith + dm    - the `DM` object
35639a64c4a8SMatthew G. Knepley - level - number of coarsenings
35649a64c4a8SMatthew G. Knepley 
35659a64c4a8SMatthew G. Knepley   Level: developer
35669a64c4a8SMatthew G. Knepley 
3567bb7acecfSBarry Smith   Note:
3568bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3569bb7acecfSBarry Smith 
357042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35719a64c4a8SMatthew G. Knepley @*/
3572d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3573d71ae5a4SJacob Faibussowitsch {
35749a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35759a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35769a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35789a64c4a8SMatthew G. Knepley }
35799a64c4a8SMatthew G. Knepley 
358047c6ae99SBarry Smith /*@C
3581bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
358247c6ae99SBarry Smith 
358320f4b53cSBarry Smith   Collective
358447c6ae99SBarry Smith 
3585d8d19677SJose E. Roman   Input Parameters:
3586bb7acecfSBarry Smith + dm      - the `DM` object
358747c6ae99SBarry Smith - nlevels - the number of levels of refinement
358847c6ae99SBarry Smith 
358947c6ae99SBarry Smith   Output Parameter:
3590bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
359147c6ae99SBarry Smith 
359247c6ae99SBarry Smith   Level: developer
359347c6ae99SBarry Smith 
35941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
359547c6ae99SBarry Smith @*/
3596d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3597d71ae5a4SJacob Faibussowitsch {
359847c6ae99SBarry Smith   PetscFunctionBegin;
3599171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36007a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36013ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36024f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
360347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3604dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
360547c6ae99SBarry Smith   } else if (dm->ops->refine) {
360647c6ae99SBarry Smith     PetscInt i;
360747c6ae99SBarry Smith 
36089566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
360948a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3610ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
36113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
361247c6ae99SBarry Smith }
361347c6ae99SBarry Smith 
361447c6ae99SBarry Smith /*@C
3615bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
361647c6ae99SBarry Smith 
361720f4b53cSBarry Smith   Collective
361847c6ae99SBarry Smith 
3619d8d19677SJose E. Roman   Input Parameters:
3620bb7acecfSBarry Smith + dm      - the `DM` object
362147c6ae99SBarry Smith - nlevels - the number of levels of coarsening
362247c6ae99SBarry Smith 
362347c6ae99SBarry Smith   Output Parameter:
3624bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
362547c6ae99SBarry Smith 
362647c6ae99SBarry Smith   Level: developer
362747c6ae99SBarry Smith 
36281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
362947c6ae99SBarry Smith @*/
3630d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3631d71ae5a4SJacob Faibussowitsch {
363247c6ae99SBarry Smith   PetscFunctionBegin;
3633171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36347a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36353ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36364f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
363747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3638dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
363947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
364047c6ae99SBarry Smith     PetscInt i;
364147c6ae99SBarry Smith 
36429566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
364348a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3644ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
36453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
364647c6ae99SBarry Smith }
364747c6ae99SBarry Smith 
36481a266240SBarry Smith /*@C
3649bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36501a266240SBarry Smith 
3651bb7acecfSBarry Smith   Logically Collective if the function is collective
36521a266240SBarry Smith 
36531a266240SBarry Smith   Input Parameters:
3654bb7acecfSBarry Smith + dm      - the `DM` object
36551a266240SBarry Smith - destroy - the destroy function
36561a266240SBarry Smith 
36571a266240SBarry Smith   Level: intermediate
36581a266240SBarry Smith 
36591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3660f07f9ceaSJed Brown @*/
3661d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3662d71ae5a4SJacob Faibussowitsch {
36631a266240SBarry Smith   PetscFunctionBegin;
3664171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36651a266240SBarry Smith   dm->ctxdestroy = destroy;
36663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36671a266240SBarry Smith }
36681a266240SBarry Smith 
3669b07ff414SBarry Smith /*@
3670bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
367147c6ae99SBarry Smith 
367247c6ae99SBarry Smith   Not Collective
367347c6ae99SBarry Smith 
367447c6ae99SBarry Smith   Input Parameters:
3675bb7acecfSBarry Smith + dm  - the `DM` object
367647c6ae99SBarry Smith - ctx - the user context
367747c6ae99SBarry Smith 
367847c6ae99SBarry Smith   Level: intermediate
367947c6ae99SBarry Smith 
3680bb7acecfSBarry Smith   Note:
368135cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3682bb7acecfSBarry Smith 
368360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
368447c6ae99SBarry Smith @*/
3685d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3686d71ae5a4SJacob Faibussowitsch {
368747c6ae99SBarry Smith   PetscFunctionBegin;
3688171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
368947c6ae99SBarry Smith   dm->ctx = ctx;
36903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
369147c6ae99SBarry Smith }
369247c6ae99SBarry Smith 
369347c6ae99SBarry Smith /*@
3694bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
369547c6ae99SBarry Smith 
369647c6ae99SBarry Smith   Not Collective
369747c6ae99SBarry Smith 
369847c6ae99SBarry Smith   Input Parameter:
3699bb7acecfSBarry Smith . dm - the `DM` object
370047c6ae99SBarry Smith 
370147c6ae99SBarry Smith   Output Parameter:
370247c6ae99SBarry Smith . ctx - the user context
370347c6ae99SBarry Smith 
370447c6ae99SBarry Smith   Level: intermediate
370547c6ae99SBarry Smith 
3706bb7acecfSBarry Smith   Note:
370735cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3708bb7acecfSBarry Smith 
370942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
371047c6ae99SBarry Smith @*/
3711d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3712d71ae5a4SJacob Faibussowitsch {
371347c6ae99SBarry Smith   PetscFunctionBegin;
3714171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37151b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
371747c6ae99SBarry Smith }
371847c6ae99SBarry Smith 
371908da532bSDmitry Karpeev /*@C
3720bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
372108da532bSDmitry Karpeev 
372220f4b53cSBarry Smith   Logically Collective
372308da532bSDmitry Karpeev 
3724d8d19677SJose E. Roman   Input Parameters:
372508da532bSDmitry Karpeev + dm - the DM object
372620f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
372708da532bSDmitry Karpeev 
372808da532bSDmitry Karpeev   Level: intermediate
372908da532bSDmitry Karpeev 
37301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3731db781477SPatrick Sanan          `DMSetJacobian()`
373208da532bSDmitry Karpeev @*/
3733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3734d71ae5a4SJacob Faibussowitsch {
373508da532bSDmitry Karpeev   PetscFunctionBegin;
37365a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
373908da532bSDmitry Karpeev }
374008da532bSDmitry Karpeev 
374108da532bSDmitry Karpeev /*@
3742bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
374308da532bSDmitry Karpeev 
374408da532bSDmitry Karpeev   Not Collective
374508da532bSDmitry Karpeev 
374608da532bSDmitry Karpeev   Input Parameter:
3747bb7acecfSBarry Smith . dm - the `DM` object to destroy
374808da532bSDmitry Karpeev 
374908da532bSDmitry Karpeev   Output Parameter:
3750bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
375108da532bSDmitry Karpeev 
375208da532bSDmitry Karpeev   Level: developer
375308da532bSDmitry Karpeev 
37541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
375508da532bSDmitry Karpeev @*/
3756d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3757d71ae5a4SJacob Faibussowitsch {
375808da532bSDmitry Karpeev   PetscFunctionBegin;
37595a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37604f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
376108da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
376308da532bSDmitry Karpeev }
376408da532bSDmitry Karpeev 
376508da532bSDmitry Karpeev /*@C
3766bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
376708da532bSDmitry Karpeev 
376820f4b53cSBarry Smith   Logically Collective
376908da532bSDmitry Karpeev 
3770f899ff85SJose E. Roman   Input Parameter:
3771bb7acecfSBarry Smith . dm - the `DM` object
377208da532bSDmitry Karpeev 
377360225df5SJacob Faibussowitsch   Output Parameters:
377408da532bSDmitry Karpeev + xl - lower bound
377508da532bSDmitry Karpeev - xu - upper bound
377608da532bSDmitry Karpeev 
3777907376e6SBarry Smith   Level: advanced
3778907376e6SBarry Smith 
377920f4b53cSBarry Smith   Note:
378095452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
378108da532bSDmitry Karpeev 
37821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
378308da532bSDmitry Karpeev @*/
3784d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3785d71ae5a4SJacob Faibussowitsch {
378608da532bSDmitry Karpeev   PetscFunctionBegin;
37875a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3790dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
37913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379208da532bSDmitry Karpeev }
379308da532bSDmitry Karpeev 
3794b0ae01b7SPeter Brune /*@
3795bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3796b0ae01b7SPeter Brune 
3797b0ae01b7SPeter Brune   Not Collective
3798b0ae01b7SPeter Brune 
3799b0ae01b7SPeter Brune   Input Parameter:
3800b0ae01b7SPeter Brune . dm - the DM object
3801b0ae01b7SPeter Brune 
3802b0ae01b7SPeter Brune   Output Parameter:
3803bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3804b0ae01b7SPeter Brune 
3805b0ae01b7SPeter Brune   Level: developer
3806b0ae01b7SPeter Brune 
38071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3808b0ae01b7SPeter Brune @*/
3809d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3810d71ae5a4SJacob Faibussowitsch {
3811b0ae01b7SPeter Brune   PetscFunctionBegin;
38125a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38134f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3814b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3816b0ae01b7SPeter Brune }
3817b0ae01b7SPeter Brune 
38183ad4599aSBarry Smith /*@
3819bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38203ad4599aSBarry Smith 
38213ad4599aSBarry Smith   Not Collective
38223ad4599aSBarry Smith 
38233ad4599aSBarry Smith   Input Parameter:
3824bb7acecfSBarry Smith . dm - the `DM` object
38253ad4599aSBarry Smith 
38263ad4599aSBarry Smith   Output Parameter:
3827bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38283ad4599aSBarry Smith 
38293ad4599aSBarry Smith   Level: developer
38303ad4599aSBarry Smith 
38311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38323ad4599aSBarry Smith @*/
3833d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3834d71ae5a4SJacob Faibussowitsch {
38353ad4599aSBarry Smith   PetscFunctionBegin;
38365a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38374f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38383ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38403ad4599aSBarry Smith }
38413ad4599aSBarry Smith 
3842a7058e45SLawrence Mitchell /*@
3843bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3844a7058e45SLawrence Mitchell 
3845a7058e45SLawrence Mitchell   Not Collective
3846a7058e45SLawrence Mitchell 
3847a7058e45SLawrence Mitchell   Input Parameter:
3848bb7acecfSBarry Smith . dm - the `DM` object
3849a7058e45SLawrence Mitchell 
3850a7058e45SLawrence Mitchell   Output Parameter:
3851bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3852a7058e45SLawrence Mitchell 
3853a7058e45SLawrence Mitchell   Level: developer
3854a7058e45SLawrence Mitchell 
38551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3856a7058e45SLawrence Mitchell @*/
3857d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3858d71ae5a4SJacob Faibussowitsch {
3859a7058e45SLawrence Mitchell   PetscFunctionBegin;
38605a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38614f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3862dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3863ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3865a7058e45SLawrence Mitchell }
3866a7058e45SLawrence Mitchell 
38670298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3868264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3869264ace61SBarry Smith 
3870264ace61SBarry Smith /*@C
3871bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3872264ace61SBarry Smith 
387320f4b53cSBarry Smith   Collective
3874264ace61SBarry Smith 
3875264ace61SBarry Smith   Input Parameters:
3876bb7acecfSBarry Smith + dm     - The `DM` object
3877bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3878264ace61SBarry Smith 
3879264ace61SBarry Smith   Options Database Key:
3880bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3881264ace61SBarry Smith 
3882264ace61SBarry Smith   Level: intermediate
3883264ace61SBarry Smith 
3884bb7acecfSBarry Smith   Note:
38850462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3886bb7acecfSBarry Smith 
38871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3888264ace61SBarry Smith @*/
3889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3890d71ae5a4SJacob Faibussowitsch {
3891264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3892264ace61SBarry Smith   PetscBool match;
3893264ace61SBarry Smith 
3894264ace61SBarry Smith   PetscFunctionBegin;
3895264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38969566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
38973ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3898264ace61SBarry Smith 
38999566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39009566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39017a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3902264ace61SBarry Smith 
3903dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39049566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39059566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39069566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3908264ace61SBarry Smith }
3909264ace61SBarry Smith 
3910264ace61SBarry Smith /*@C
3911bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3912264ace61SBarry Smith 
3913264ace61SBarry Smith   Not Collective
3914264ace61SBarry Smith 
3915264ace61SBarry Smith   Input Parameter:
3916bb7acecfSBarry Smith . dm - The `DM`
3917264ace61SBarry Smith 
3918264ace61SBarry Smith   Output Parameter:
3919bb7acecfSBarry Smith . type - The `DMType` name
3920264ace61SBarry Smith 
3921264ace61SBarry Smith   Level: intermediate
3922264ace61SBarry Smith 
39231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3924264ace61SBarry Smith @*/
3925d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3926d71ae5a4SJacob Faibussowitsch {
3927264ace61SBarry Smith   PetscFunctionBegin;
3928264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39294f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39309566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3931264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3933264ace61SBarry Smith }
3934264ace61SBarry Smith 
393567a56275SMatthew G Knepley /*@C
3936bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
393767a56275SMatthew G Knepley 
393820f4b53cSBarry Smith   Collective
393967a56275SMatthew G Knepley 
394067a56275SMatthew G Knepley   Input Parameters:
3941bb7acecfSBarry Smith + dm      - the `DM`
3942bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
394367a56275SMatthew G Knepley 
394467a56275SMatthew G Knepley   Output Parameter:
3945bb7acecfSBarry Smith . M - pointer to new `DM`
394667a56275SMatthew G Knepley 
394720f4b53cSBarry Smith   Level: intermediate
394820f4b53cSBarry Smith 
394967a56275SMatthew G Knepley   Notes:
3950bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3951bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3952bb7acecfSBarry Smith   of the input `DM`.
395367a56275SMatthew G Knepley 
39541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
395567a56275SMatthew G Knepley @*/
3956d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3957d71ae5a4SJacob Faibussowitsch {
395867a56275SMatthew G Knepley   DM        B;
395967a56275SMatthew G Knepley   char      convname[256];
3960c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
396167a56275SMatthew G Knepley 
396267a56275SMatthew G Knepley   PetscFunctionBegin;
396367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
396467a56275SMatthew G Knepley   PetscValidType(dm, 1);
39654f572ea9SToby Isaac   PetscAssertPointer(M, 3);
39669566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39679566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3968c067b6caSMatthew G. Knepley   if (sametype) {
3969c067b6caSMatthew G. Knepley     *M = dm;
39709566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39713ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3972c067b6caSMatthew G. Knepley   } else {
39730298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
397467a56275SMatthew G Knepley 
397567a56275SMatthew G Knepley     /*
397667a56275SMatthew G Knepley        Order of precedence:
397767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
397867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
397967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
398067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
398167a56275SMatthew G Knepley        5) Use a really basic converter.
398267a56275SMatthew G Knepley     */
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39859566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39869566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39879566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39889566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39899566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39909566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
399167a56275SMatthew G Knepley     if (conv) goto foundconv;
399267a56275SMatthew G Knepley 
399367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
39949566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
39959566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
39969566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39979566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40009566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40019566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
400267a56275SMatthew G Knepley     if (conv) {
40039566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
400467a56275SMatthew G Knepley       goto foundconv;
400567a56275SMatthew G Knepley     }
400667a56275SMatthew G Knepley 
400767a56275SMatthew G Knepley #if 0
400867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
400967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40109566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
401167a56275SMatthew G Knepley     if (conv) goto foundconv;
401267a56275SMatthew G Knepley 
401367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
401467a56275SMatthew G Knepley     if (dm->ops->convert) {
401567a56275SMatthew G Knepley       conv = dm->ops->convert;
401667a56275SMatthew G Knepley     }
401767a56275SMatthew G Knepley     if (conv) goto foundconv;
401867a56275SMatthew G Knepley #endif
401967a56275SMatthew G Knepley 
402067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
402198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
402267a56275SMatthew G Knepley 
402367a56275SMatthew G Knepley   foundconv:
40249566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40259566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
402612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
402790b157c4SStefano Zampini     {
40284fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40296858538eSMatthew G. Knepley 
40304fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40314fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4032c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40339566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40349566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40359566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40369566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
403712fa691eSMatthew G. Knepley     }
40389566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
403967a56275SMatthew G Knepley   }
40409566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
404267a56275SMatthew G Knepley }
4043264ace61SBarry Smith 
4044264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4045264ace61SBarry Smith 
4046264ace61SBarry Smith /*@C
4047bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40481c84c290SBarry Smith 
40491c84c290SBarry Smith   Not Collective
40501c84c290SBarry Smith 
40511c84c290SBarry Smith   Input Parameters:
405220f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
405320f4b53cSBarry Smith - function - The creation routine itself
405420f4b53cSBarry Smith 
405520f4b53cSBarry Smith   Level: advanced
40561c84c290SBarry Smith 
40571c84c290SBarry Smith   Notes:
405820f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40591c84c290SBarry Smith 
406060225df5SJacob Faibussowitsch   Example Usage:
40611c84c290SBarry Smith .vb
4062bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40631c84c290SBarry Smith .ve
40641c84c290SBarry Smith 
406520f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40661c84c290SBarry Smith .vb
40671c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40681c84c290SBarry Smith     DMSetType(DM,"my_da");
40691c84c290SBarry Smith .ve
40701c84c290SBarry Smith   or at runtime via the option
40711c84c290SBarry Smith .vb
40721c84c290SBarry Smith     -da_type my_da
40731c84c290SBarry Smith .ve
4074264ace61SBarry Smith 
40751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4076264ace61SBarry Smith @*/
4077d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4078d71ae5a4SJacob Faibussowitsch {
4079264ace61SBarry Smith   PetscFunctionBegin;
40809566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40819566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4083264ace61SBarry Smith }
4084264ace61SBarry Smith 
4085b859378eSBarry Smith /*@C
4086bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4087b859378eSBarry Smith 
408820f4b53cSBarry Smith   Collective
4089b859378eSBarry Smith 
4090b859378eSBarry Smith   Input Parameters:
4091bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4092bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4093bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4094bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4095b859378eSBarry Smith 
4096b859378eSBarry Smith   Level: intermediate
4097b859378eSBarry Smith 
4098b859378eSBarry Smith   Notes:
409955849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4100b859378eSBarry Smith 
4101bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4102bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4103bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4104cd7e8a5eSksagiyam 
41051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4106b859378eSBarry Smith @*/
4107d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4108d71ae5a4SJacob Faibussowitsch {
41099331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4110b859378eSBarry Smith 
4111b859378eSBarry Smith   PetscFunctionBegin;
4112b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4113b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41149566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41159566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41169566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41189331c7a4SMatthew G. Knepley   if (isbinary) {
41199331c7a4SMatthew G. Knepley     PetscInt classid;
41209331c7a4SMatthew G. Knepley     char     type[256];
4121b859378eSBarry Smith 
41229566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41237a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41249566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41259566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4126dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41279331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4128dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41299331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41309566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4132b859378eSBarry Smith }
4133b859378eSBarry Smith 
41347da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41357da65231SMatthew G Knepley 
4136d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4137d71ae5a4SJacob Faibussowitsch {
41381d47ebbbSSatish Balay   PetscInt f;
41391b30c384SMatthew G Knepley 
41407da65231SMatthew G Knepley   PetscFunctionBegin;
414163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
414248a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41447da65231SMatthew G Knepley }
41457da65231SMatthew G Knepley 
4146d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4147d71ae5a4SJacob Faibussowitsch {
41481b30c384SMatthew G Knepley   PetscInt f, g;
41497da65231SMatthew G Knepley 
41507da65231SMatthew G Knepley   PetscFunctionBegin;
415163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41521d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41539566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
415448a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41559566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41567da65231SMatthew G Knepley   }
41573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41587da65231SMatthew G Knepley }
4159e7c4fc90SDmitry Karpeev 
4160d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4161d71ae5a4SJacob Faibussowitsch {
41620c5b8624SToby Isaac   PetscInt           localSize, bs;
41630c5b8624SToby Isaac   PetscMPIInt        size;
41640c5b8624SToby Isaac   Vec                x, xglob;
41650c5b8624SToby Isaac   const PetscScalar *xarray;
4166e759306cSMatthew G. Knepley 
4167e759306cSMatthew G. Knepley   PetscFunctionBegin;
41689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41699566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41709566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
417193ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
41729566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
41730c5b8624SToby Isaac   if (size > 1) {
41749566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
41759566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
41769566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
41779566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
41780c5b8624SToby Isaac   } else {
41790c5b8624SToby Isaac     xglob = x;
41800c5b8624SToby Isaac   }
41819566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
41820c5b8624SToby Isaac   if (size > 1) {
41839566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
41849566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
41850c5b8624SToby Isaac   }
41869566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
41873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4188e759306cSMatthew G. Knepley }
4189e759306cSMatthew G. Knepley 
419088ed4aceSMatthew G Knepley /*@
4191bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4192061576a5SJed Brown 
4193061576a5SJed Brown   Input Parameter:
4194bb7acecfSBarry Smith . dm - The `DM`
4195061576a5SJed Brown 
4196061576a5SJed Brown   Output Parameter:
4197bb7acecfSBarry Smith . section - The `PetscSection`
4198061576a5SJed Brown 
419920f4b53cSBarry Smith   Options Database Key:
4200bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4201061576a5SJed Brown 
4202061576a5SJed Brown   Level: advanced
4203061576a5SJed Brown 
4204061576a5SJed Brown   Notes:
4205bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4206061576a5SJed Brown 
4207bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4208061576a5SJed Brown 
42091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4210061576a5SJed Brown @*/
4211d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4212d71ae5a4SJacob Faibussowitsch {
4213061576a5SJed Brown   PetscFunctionBegin;
42149566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4216061576a5SJed Brown }
4217061576a5SJed Brown 
4218061576a5SJed Brown /*@
4219bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
422088ed4aceSMatthew G Knepley 
422188ed4aceSMatthew G Knepley   Input Parameter:
4222bb7acecfSBarry Smith . dm - The `DM`
422388ed4aceSMatthew G Knepley 
422488ed4aceSMatthew G Knepley   Output Parameter:
4225bb7acecfSBarry Smith . section - The `PetscSection`
422688ed4aceSMatthew G Knepley 
422720f4b53cSBarry Smith   Options Database Key:
4228bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4229e5893cccSMatthew G. Knepley 
423088ed4aceSMatthew G Knepley   Level: intermediate
423188ed4aceSMatthew G Knepley 
4232bb7acecfSBarry Smith   Note:
4233bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
423488ed4aceSMatthew G Knepley 
42351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
423688ed4aceSMatthew G Knepley @*/
4237d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4238d71ae5a4SJacob Faibussowitsch {
423988ed4aceSMatthew G Knepley   PetscFunctionBegin;
424088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42414f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42421bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4243e5e52638SMatthew G. Knepley     PetscInt d;
4244e5e52638SMatthew G. Knepley 
424545480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
424645480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
424745480ffeSMatthew G. Knepley       PetscViewer       viewer;
424845480ffeSMatthew G. Knepley       PetscViewerFormat format;
424945480ffeSMatthew G. Knepley       PetscBool         flg;
425045480ffeSMatthew G. Knepley 
42519566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42529566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
425345480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42549566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42559566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
425645480ffeSMatthew G. Knepley       }
425745480ffeSMatthew G. Knepley       if (flg) {
42589566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42599566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42609566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
426145480ffeSMatthew G. Knepley       }
426245480ffeSMatthew G. Knepley     }
4263dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42649566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42652f0f8703SMatthew G. Knepley   }
42661bb6d2a8SBarry Smith   *section = dm->localSection;
42673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
426888ed4aceSMatthew G Knepley }
426988ed4aceSMatthew G Knepley 
427088ed4aceSMatthew G Knepley /*@
4271bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4272061576a5SJed Brown 
4273061576a5SJed Brown   Input Parameters:
4274bb7acecfSBarry Smith + dm      - The `DM`
4275bb7acecfSBarry Smith - section - The `PetscSection`
4276061576a5SJed Brown 
4277061576a5SJed Brown   Level: advanced
4278061576a5SJed Brown 
4279061576a5SJed Brown   Notes:
4280bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4281061576a5SJed Brown 
4282bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4283061576a5SJed Brown 
42841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4285061576a5SJed Brown @*/
4286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4287d71ae5a4SJacob Faibussowitsch {
4288061576a5SJed Brown   PetscFunctionBegin;
42899566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
42903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4291061576a5SJed Brown }
4292061576a5SJed Brown 
4293061576a5SJed Brown /*@
4294bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
429588ed4aceSMatthew G Knepley 
429688ed4aceSMatthew G Knepley   Input Parameters:
4297bb7acecfSBarry Smith + dm      - The `DM`
4298bb7acecfSBarry Smith - section - The `PetscSection`
429988ed4aceSMatthew G Knepley 
430088ed4aceSMatthew G Knepley   Level: intermediate
430188ed4aceSMatthew G Knepley 
4302bb7acecfSBarry Smith   Note:
4303bb7acecfSBarry Smith   Any existing Section will be destroyed
430488ed4aceSMatthew G Knepley 
43051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
430688ed4aceSMatthew G Knepley @*/
4307d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4308d71ae5a4SJacob Faibussowitsch {
4309c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4310af122d2aSMatthew G Knepley   PetscInt f;
431188ed4aceSMatthew G Knepley 
431288ed4aceSMatthew G Knepley   PetscFunctionBegin;
431388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4314b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43169566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43171bb6d2a8SBarry Smith   dm->localSection = section;
43189566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4319af122d2aSMatthew G Knepley   if (numFields) {
43209566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4321af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43220f21e855SMatthew G. Knepley       PetscObject disc;
4323af122d2aSMatthew G Knepley       const char *name;
4324af122d2aSMatthew G Knepley 
43259566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43269566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43279566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4328af122d2aSMatthew G Knepley     }
4329af122d2aSMatthew G Knepley   }
4330e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43319566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
43323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
433388ed4aceSMatthew G Knepley }
433488ed4aceSMatthew G Knepley 
43359435951eSToby Isaac /*@
4336bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43379435951eSToby Isaac 
433820f4b53cSBarry Smith   not Collective
4339e228b242SToby Isaac 
43409435951eSToby Isaac   Input Parameter:
4341bb7acecfSBarry Smith . dm - The `DM`
43429435951eSToby Isaac 
4343d8d19677SJose E. Roman   Output Parameters:
434420f4b53cSBarry 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.
434520f4b53cSBarry 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.
434679769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
43479435951eSToby Isaac 
43489435951eSToby Isaac   Level: advanced
43499435951eSToby Isaac 
4350bb7acecfSBarry Smith   Note:
4351bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43529435951eSToby Isaac 
43531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
43549435951eSToby Isaac @*/
4355d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4356d71ae5a4SJacob Faibussowitsch {
43579435951eSToby Isaac   PetscFunctionBegin;
43589435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4359dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
43603b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43613b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
436279769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43649435951eSToby Isaac }
43659435951eSToby Isaac 
43669435951eSToby Isaac /*@
4367bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
43689435951eSToby Isaac 
436920f4b53cSBarry Smith   Collective
4370e228b242SToby Isaac 
43719435951eSToby Isaac   Input Parameters:
4372bb7acecfSBarry Smith + dm      - The `DM`
4373bb7acecfSBarry 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).
437420f4b53cSBarry 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).
437520f4b53cSBarry 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).
43769435951eSToby Isaac 
43779435951eSToby Isaac   Level: advanced
43789435951eSToby Isaac 
437920f4b53cSBarry Smith   Notes:
438020f4b53cSBarry 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()`.
438120f4b53cSBarry Smith 
438220f4b53cSBarry 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.
438320f4b53cSBarry Smith 
4384bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
43859435951eSToby Isaac 
43861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
43879435951eSToby Isaac @*/
4388d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4389d71ae5a4SJacob Faibussowitsch {
4390e228b242SToby Isaac   PetscMPIInt result;
43919435951eSToby Isaac 
43929435951eSToby Isaac   PetscFunctionBegin;
43939435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4394e228b242SToby Isaac   if (section) {
4395e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43969566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
43977a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4398e228b242SToby Isaac   }
4399e228b242SToby Isaac   if (mat) {
4400e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44019566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44027a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4403e228b242SToby Isaac   }
440479769bd5SJed Brown   if (bias) {
440579769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44069566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
440779769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
440879769bd5SJed Brown   }
44099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44109566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44113b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44129566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44139566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44143b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44169566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
441779769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44199435951eSToby Isaac }
44209435951eSToby Isaac 
4421497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4422507e4973SMatthew G. Knepley /*
4423bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4424507e4973SMatthew G. Knepley 
4425507e4973SMatthew G. Knepley   Input Parameters:
4426bb7acecfSBarry Smith + dm - The `DM`
4427bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4428bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4429507e4973SMatthew G. Knepley 
4430507e4973SMatthew G. Knepley   Level: intermediate
4431507e4973SMatthew G. Knepley 
44321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4433507e4973SMatthew G. Knepley */
4434d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4435d71ae5a4SJacob Faibussowitsch {
4436507e4973SMatthew G. Knepley   MPI_Comm        comm;
4437507e4973SMatthew G. Knepley   PetscLayout     layout;
4438507e4973SMatthew G. Knepley   const PetscInt *ranges;
4439507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4440507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4441507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4442507e4973SMatthew G. Knepley 
4443507e4973SMatthew G. Knepley   PetscFunctionBegin;
44449566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4445507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44479566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44509566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44549566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4455507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4456f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4457507e4973SMatthew G. Knepley 
44589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4464507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
44659371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
44669371c9d4SSatish 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));
44679371c9d4SSatish Balay       valid = PETSC_FALSE;
44689371c9d4SSatish Balay     }
44699371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
44709371c9d4SSatish 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));
44719371c9d4SSatish Balay       valid = PETSC_FALSE;
44729371c9d4SSatish Balay     }
4473507e4973SMatthew G. Knepley     if (gdof < 0) {
4474507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4475507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4476507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4477507e4973SMatthew G. Knepley 
44789566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4479507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
44809371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
44819371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
44829371c9d4SSatish Balay           valid = PETSC_FALSE;
44839371c9d4SSatish Balay           break;
44849371c9d4SSatish Balay         }
4485507e4973SMatthew G. Knepley       }
4486507e4973SMatthew G. Knepley     }
4487507e4973SMatthew G. Knepley   }
44889566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
44899566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
44901c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4491507e4973SMatthew G. Knepley   if (!gvalid) {
44929566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4493507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4494507e4973SMatthew G. Knepley   }
44953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4496507e4973SMatthew G. Knepley }
4497f741bcd2SMatthew G. Knepley #endif
4498507e4973SMatthew G. Knepley 
44995f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45006725e60dSJed Brown {
45016725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45026725e60dSJed Brown   PetscFunctionBegin;
45036725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45044f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45055f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45066725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45076725e60dSJed Brown   else *sf = dm->sf;
45083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45096725e60dSJed Brown }
45106725e60dSJed Brown 
451188ed4aceSMatthew G Knepley /*@
4512bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
451388ed4aceSMatthew G Knepley 
451420f4b53cSBarry Smith   Collective
45158b1ab98fSJed Brown 
451688ed4aceSMatthew G Knepley   Input Parameter:
4517bb7acecfSBarry Smith . dm - The `DM`
451888ed4aceSMatthew G Knepley 
451988ed4aceSMatthew G Knepley   Output Parameter:
4520bb7acecfSBarry Smith . section - The `PetscSection`
452188ed4aceSMatthew G Knepley 
452288ed4aceSMatthew G Knepley   Level: intermediate
452388ed4aceSMatthew G Knepley 
4524bb7acecfSBarry Smith   Note:
4525bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
452688ed4aceSMatthew G Knepley 
45271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
452888ed4aceSMatthew G Knepley @*/
4529d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4530d71ae5a4SJacob Faibussowitsch {
453188ed4aceSMatthew G Knepley   PetscFunctionBegin;
453288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45334f572ea9SToby Isaac   PetscAssertPointer(section, 2);
45341bb6d2a8SBarry Smith   if (!dm->globalSection) {
4535fd59a867SMatthew G. Knepley     PetscSection s;
45366725e60dSJed Brown     PetscSF      sf;
4537fd59a867SMatthew G. Knepley 
45389566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45397a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45407a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45415f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45426725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45439566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45459566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
454688ed4aceSMatthew G Knepley   }
45471bb6d2a8SBarry Smith   *section = dm->globalSection;
45483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
454988ed4aceSMatthew G Knepley }
455088ed4aceSMatthew G Knepley 
4551b21d0597SMatthew G Knepley /*@
4552bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4553b21d0597SMatthew G Knepley 
4554b21d0597SMatthew G Knepley   Input Parameters:
4555bb7acecfSBarry Smith + dm      - The `DM`
45562fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4557b21d0597SMatthew G Knepley 
4558b21d0597SMatthew G Knepley   Level: intermediate
4559b21d0597SMatthew G Knepley 
4560bb7acecfSBarry Smith   Note:
4561bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4562b21d0597SMatthew G Knepley 
45631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4564b21d0597SMatthew G Knepley @*/
4565d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4566d71ae5a4SJacob Faibussowitsch {
4567b21d0597SMatthew G Knepley   PetscFunctionBegin;
4568b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45695080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45709566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45719566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45721bb6d2a8SBarry Smith   dm->globalSection = section;
4573497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45749566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4575507e4973SMatthew G. Knepley #endif
45763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4577b21d0597SMatthew G Knepley }
4578b21d0597SMatthew G Knepley 
457988ed4aceSMatthew G Knepley /*@
4580bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4581bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
458288ed4aceSMatthew G Knepley 
458388ed4aceSMatthew G Knepley   Input Parameter:
4584bb7acecfSBarry Smith . dm - The `DM`
458588ed4aceSMatthew G Knepley 
458688ed4aceSMatthew G Knepley   Output Parameter:
4587bb7acecfSBarry Smith . sf - The `PetscSF`
458888ed4aceSMatthew G Knepley 
458988ed4aceSMatthew G Knepley   Level: intermediate
459088ed4aceSMatthew G Knepley 
4591bb7acecfSBarry Smith   Note:
4592bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
459388ed4aceSMatthew G Knepley 
45941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
459588ed4aceSMatthew G Knepley @*/
4596d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4597d71ae5a4SJacob Faibussowitsch {
459888ed4aceSMatthew G Knepley   PetscInt nroots;
459988ed4aceSMatthew G Knepley 
460088ed4aceSMatthew G Knepley   PetscFunctionBegin;
460188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46024f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
460348a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46049566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
460588ed4aceSMatthew G Knepley   if (nroots < 0) {
460688ed4aceSMatthew G Knepley     PetscSection section, gSection;
460788ed4aceSMatthew G Knepley 
46089566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
460931ea6d37SMatthew G Knepley     if (section) {
46109566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46119566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
461231ea6d37SMatthew G Knepley     } else {
46130298fd71SBarry Smith       *sf = NULL;
46143ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
461531ea6d37SMatthew G Knepley     }
461688ed4aceSMatthew G Knepley   }
46171bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
461988ed4aceSMatthew G Knepley }
462088ed4aceSMatthew G Knepley 
462188ed4aceSMatthew G Knepley /*@
4622bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
462388ed4aceSMatthew G Knepley 
462488ed4aceSMatthew G Knepley   Input Parameters:
4625bb7acecfSBarry Smith + dm - The `DM`
4626bb7acecfSBarry Smith - sf - The `PetscSF`
462788ed4aceSMatthew G Knepley 
462888ed4aceSMatthew G Knepley   Level: intermediate
462988ed4aceSMatthew G Knepley 
4630bb7acecfSBarry Smith   Note:
4631bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
463288ed4aceSMatthew G Knepley 
46331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
463488ed4aceSMatthew G Knepley @*/
4635d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4636d71ae5a4SJacob Faibussowitsch {
463788ed4aceSMatthew G Knepley   PetscFunctionBegin;
463888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4639b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46409566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46419566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46421bb6d2a8SBarry Smith   dm->sectionSF = sf;
46433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
464488ed4aceSMatthew G Knepley }
464588ed4aceSMatthew G Knepley 
464688ed4aceSMatthew G Knepley /*@C
4647bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
464888ed4aceSMatthew G Knepley   describing the data layout.
464988ed4aceSMatthew G Knepley 
465088ed4aceSMatthew G Knepley   Input Parameters:
4651bb7acecfSBarry Smith + dm            - The `DM`
4652bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4653bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
465488ed4aceSMatthew G Knepley 
46551bb6d2a8SBarry Smith   Level: developer
46561bb6d2a8SBarry Smith 
4657bb7acecfSBarry Smith   Note:
4658bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4659bb7acecfSBarry Smith 
466060225df5SJacob Faibussowitsch   Developer Notes:
4661bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4662bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4663bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46641bb6d2a8SBarry Smith 
46651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
466688ed4aceSMatthew G Knepley @*/
4667d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4668d71ae5a4SJacob Faibussowitsch {
466988ed4aceSMatthew G Knepley   PetscFunctionBegin;
467088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46719566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
46723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
467388ed4aceSMatthew G Knepley }
4674af122d2aSMatthew G Knepley 
4675b21d0597SMatthew G Knepley /*@
4676bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4677bb7acecfSBarry Smith 
4678bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4679b21d0597SMatthew G Knepley 
4680b21d0597SMatthew G Knepley   Input Parameter:
4681bb7acecfSBarry Smith . dm - The `DM`
4682b21d0597SMatthew G Knepley 
4683b21d0597SMatthew G Knepley   Output Parameter:
4684bb7acecfSBarry Smith . sf - The `PetscSF`
4685b21d0597SMatthew G Knepley 
4686b21d0597SMatthew G Knepley   Level: intermediate
4687b21d0597SMatthew G Knepley 
4688bb7acecfSBarry Smith   Note:
4689bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4690b21d0597SMatthew G Knepley 
46911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4692b21d0597SMatthew G Knepley @*/
4693d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4694d71ae5a4SJacob Faibussowitsch {
4695b21d0597SMatthew G Knepley   PetscFunctionBegin;
4696b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46974f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4698b21d0597SMatthew G Knepley   *sf = dm->sf;
46993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4700b21d0597SMatthew G Knepley }
4701b21d0597SMatthew G Knepley 
4702057b4bcdSMatthew G Knepley /*@
4703bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4704bb7acecfSBarry Smith 
470520f4b53cSBarry Smith   Collective
4706057b4bcdSMatthew G Knepley 
4707057b4bcdSMatthew G Knepley   Input Parameters:
4708bb7acecfSBarry Smith + dm - The `DM`
4709bb7acecfSBarry Smith - sf - The `PetscSF`
4710057b4bcdSMatthew G Knepley 
4711057b4bcdSMatthew G Knepley   Level: intermediate
4712057b4bcdSMatthew G Knepley 
47131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4714057b4bcdSMatthew G Knepley @*/
4715d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4716d71ae5a4SJacob Faibussowitsch {
4717057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4718057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4719b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47209566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47219566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4722057b4bcdSMatthew G Knepley   dm->sf = sf;
47233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4724057b4bcdSMatthew G Knepley }
4725057b4bcdSMatthew G Knepley 
47264f37162bSMatthew G. Knepley /*@
4727bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47284f37162bSMatthew G. Knepley 
47294f37162bSMatthew G. Knepley   Input Parameter:
4730bb7acecfSBarry Smith . dm - The `DM`
47314f37162bSMatthew G. Knepley 
47324f37162bSMatthew G. Knepley   Output Parameter:
4733bb7acecfSBarry Smith . sf - The `PetscSF`
47344f37162bSMatthew G. Knepley 
47354f37162bSMatthew G. Knepley   Level: intermediate
47364f37162bSMatthew G. Knepley 
4737bb7acecfSBarry Smith   Note:
4738bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47394f37162bSMatthew G. Knepley 
47401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47414f37162bSMatthew G. Knepley @*/
4742d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4743d71ae5a4SJacob Faibussowitsch {
47444f37162bSMatthew G. Knepley   PetscFunctionBegin;
47454f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47464f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
47474f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47494f37162bSMatthew G. Knepley }
47504f37162bSMatthew G. Knepley 
47514f37162bSMatthew G. Knepley /*@
47524f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47534f37162bSMatthew G. Knepley 
47544f37162bSMatthew G. Knepley   Input Parameters:
47554f37162bSMatthew G. Knepley + dm - The DM
47564f37162bSMatthew G. Knepley - sf - The PetscSF
47574f37162bSMatthew G. Knepley 
47584f37162bSMatthew G. Knepley   Level: intermediate
47594f37162bSMatthew G. Knepley 
47601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47614f37162bSMatthew G. Knepley @*/
4762d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4763d71ae5a4SJacob Faibussowitsch {
47644f37162bSMatthew G. Knepley   PetscFunctionBegin;
47654f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47664f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47679566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47689566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47694f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47714f37162bSMatthew G. Knepley }
47724f37162bSMatthew G. Knepley 
4773d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4774d71ae5a4SJacob Faibussowitsch {
477534aa8a36SMatthew G. Knepley   PetscClassId id;
477634aa8a36SMatthew G. Knepley 
477734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47789566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
477934aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47809566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
478134aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47829566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
478317c1d62eSMatthew G. Knepley   } else {
47849566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
478534aa8a36SMatthew G. Knepley   }
47863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
478734aa8a36SMatthew G. Knepley }
478834aa8a36SMatthew G. Knepley 
4789d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4790d71ae5a4SJacob Faibussowitsch {
479144a7f3ddSMatthew G. Knepley   RegionField *tmpr;
479244a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
479344a7f3ddSMatthew G. Knepley 
479444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
47953ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
47969566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
479744a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
47989371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
47999371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48009371c9d4SSatish Balay     tmpr[f].label       = NULL;
48019371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48029371c9d4SSatish Balay   }
48039566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
480444a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
480544a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
480744a7f3ddSMatthew G. Knepley }
480844a7f3ddSMatthew G. Knepley 
480944a7f3ddSMatthew G. Knepley /*@
481020f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
481144a7f3ddSMatthew G. Knepley 
481220f4b53cSBarry Smith   Logically Collective
481344a7f3ddSMatthew G. Knepley 
481444a7f3ddSMatthew G. Knepley   Input Parameter:
481520f4b53cSBarry Smith . dm - The `DM`
481644a7f3ddSMatthew G. Knepley 
481744a7f3ddSMatthew G. Knepley   Level: intermediate
481844a7f3ddSMatthew G. Knepley 
48191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
482044a7f3ddSMatthew G. Knepley @*/
4821d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4822d71ae5a4SJacob Faibussowitsch {
482344a7f3ddSMatthew G. Knepley   PetscInt f;
482444a7f3ddSMatthew G. Knepley 
482544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
482644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
482744a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48289566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48299566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
483044a7f3ddSMatthew G. Knepley   }
48319566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
483244a7f3ddSMatthew G. Knepley   dm->fields = NULL;
483344a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
483544a7f3ddSMatthew G. Knepley }
483644a7f3ddSMatthew G. Knepley 
4837689b5837SMatthew G. Knepley /*@
483820f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4839689b5837SMatthew G. Knepley 
484020f4b53cSBarry Smith   Not Collective
4841689b5837SMatthew G. Knepley 
4842689b5837SMatthew G. Knepley   Input Parameter:
484320f4b53cSBarry Smith . dm - The `DM`
4844689b5837SMatthew G. Knepley 
4845689b5837SMatthew G. Knepley   Output Parameter:
484660225df5SJacob Faibussowitsch . numFields - The number of fields
4847689b5837SMatthew G. Knepley 
4848689b5837SMatthew G. Knepley   Level: intermediate
4849689b5837SMatthew G. Knepley 
48501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4851689b5837SMatthew G. Knepley @*/
4852d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4853d71ae5a4SJacob Faibussowitsch {
48540f21e855SMatthew G. Knepley   PetscFunctionBegin;
48550f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48564f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
485744a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
48583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4859af122d2aSMatthew G Knepley }
4860af122d2aSMatthew G Knepley 
4861689b5837SMatthew G. Knepley /*@
486220f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4863689b5837SMatthew G. Knepley 
486420f4b53cSBarry Smith   Logically Collective
4865689b5837SMatthew G. Knepley 
4866689b5837SMatthew G. Knepley   Input Parameters:
486720f4b53cSBarry Smith + dm        - The `DM`
486860225df5SJacob Faibussowitsch - numFields - The number of fields
4869689b5837SMatthew G. Knepley 
4870689b5837SMatthew G. Knepley   Level: intermediate
4871689b5837SMatthew G. Knepley 
48721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4873689b5837SMatthew G. Knepley @*/
4874d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4875d71ae5a4SJacob Faibussowitsch {
48760f21e855SMatthew G. Knepley   PetscInt Nf, f;
4877af122d2aSMatthew G Knepley 
4878af122d2aSMatthew G Knepley   PetscFunctionBegin;
4879af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48809566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48810f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48820f21e855SMatthew G. Knepley     PetscContainer obj;
48830f21e855SMatthew G. Knepley 
48849566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
48859566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
48869566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4887af122d2aSMatthew G Knepley   }
48883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4889af122d2aSMatthew G Knepley }
4890af122d2aSMatthew G Knepley 
4891c1929be8SMatthew G. Knepley /*@
4892bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4893c1929be8SMatthew G. Knepley 
489420f4b53cSBarry Smith   Not Collective
4895c1929be8SMatthew G. Knepley 
4896c1929be8SMatthew G. Knepley   Input Parameters:
4897bb7acecfSBarry Smith + dm - The `DM`
4898c1929be8SMatthew G. Knepley - f  - The field number
4899c1929be8SMatthew G. Knepley 
490044a7f3ddSMatthew G. Knepley   Output Parameters:
490120f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
490220f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4903c1929be8SMatthew G. Knepley 
490444a7f3ddSMatthew G. Knepley   Level: intermediate
4905c1929be8SMatthew G. Knepley 
49061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4907c1929be8SMatthew G. Knepley @*/
4908d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4909d71ae5a4SJacob Faibussowitsch {
4910af122d2aSMatthew G Knepley   PetscFunctionBegin;
4911af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49124f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
49137a8be351SBarry 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);
491444a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4915bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4917decb47aaSMatthew G. Knepley }
4918decb47aaSMatthew G. Knepley 
4919083401c6SMatthew G. Knepley /* Does not clear the DS */
4920d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4921d71ae5a4SJacob Faibussowitsch {
4922083401c6SMatthew G. Knepley   PetscFunctionBegin;
49239566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49249566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49259566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4926083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4927bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49289566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4929bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4931083401c6SMatthew G. Knepley }
4932083401c6SMatthew G. Knepley 
493346560f82SMatthew G. Knepley /*@C
4934bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4935bb7acecfSBarry Smith   the field numbering.
4936c1929be8SMatthew G. Knepley 
493720f4b53cSBarry Smith   Logically Collective
4938c1929be8SMatthew G. Knepley 
4939c1929be8SMatthew G. Knepley   Input Parameters:
4940bb7acecfSBarry Smith + dm    - The `DM`
4941c1929be8SMatthew G. Knepley . f     - The field number
494220f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4943bb7acecfSBarry Smith - disc  - The discretization object
4944c1929be8SMatthew G. Knepley 
494544a7f3ddSMatthew G. Knepley   Level: intermediate
4946c1929be8SMatthew G. Knepley 
49471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
4948c1929be8SMatthew G. Knepley @*/
4949d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4950d71ae5a4SJacob Faibussowitsch {
4951decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4952decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4953e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4954bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49557a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4956bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4957bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49589566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
49593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
496044a7f3ddSMatthew G. Knepley }
496144a7f3ddSMatthew G. Knepley 
496246560f82SMatthew G. Knepley /*@C
4963bb7acecfSBarry 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)
4964bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
496544a7f3ddSMatthew G. Knepley 
496620f4b53cSBarry Smith   Logically Collective
496744a7f3ddSMatthew G. Knepley 
496844a7f3ddSMatthew G. Knepley   Input Parameters:
4969bb7acecfSBarry Smith + dm    - The `DM`
497020f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4971bb7acecfSBarry Smith - disc  - The discretization object
497244a7f3ddSMatthew G. Knepley 
497344a7f3ddSMatthew G. Knepley   Level: intermediate
497444a7f3ddSMatthew G. Knepley 
4975bb7acecfSBarry Smith   Notes:
4976bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4977bb7acecfSBarry Smith 
4978da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4979bb7acecfSBarry 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
4980bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4981bb7acecfSBarry Smith 
49821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
498344a7f3ddSMatthew G. Knepley @*/
4984d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
4985d71ae5a4SJacob Faibussowitsch {
498644a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
498744a7f3ddSMatthew G. Knepley 
498844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
498944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4990064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4991bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
49929566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
499344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
4994bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
49959566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4996bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4997bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
49989566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
49993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5000af122d2aSMatthew G Knepley }
50016636e97aSMatthew G Knepley 
5002e5e52638SMatthew G. Knepley /*@
5003e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5004e0b68406SMatthew Knepley 
500520f4b53cSBarry Smith   Logically Collective
5006e0b68406SMatthew Knepley 
5007e0b68406SMatthew Knepley   Input Parameters:
5008bb7acecfSBarry Smith + dm          - The `DM`
5009e0b68406SMatthew Knepley . f           - The field index
5010bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5011e0b68406SMatthew Knepley 
5012e0b68406SMatthew Knepley   Level: intermediate
5013e0b68406SMatthew Knepley 
50141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5015e0b68406SMatthew Knepley @*/
5016d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5017d71ae5a4SJacob Faibussowitsch {
5018e0b68406SMatthew Knepley   PetscFunctionBegin;
501963a3b9bcSJacob 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);
5020e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5022e0b68406SMatthew Knepley }
5023e0b68406SMatthew Knepley 
5024e0b68406SMatthew Knepley /*@
5025e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5026e0b68406SMatthew Knepley 
502720f4b53cSBarry Smith   Not Collective
5028e0b68406SMatthew Knepley 
5029e0b68406SMatthew Knepley   Input Parameters:
5030bb7acecfSBarry Smith + dm - The `DM`
5031e0b68406SMatthew Knepley - f  - The field index
5032e0b68406SMatthew Knepley 
5033e0b68406SMatthew Knepley   Output Parameter:
5034e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5035e0b68406SMatthew Knepley 
5036e0b68406SMatthew Knepley   Level: intermediate
5037e0b68406SMatthew Knepley 
503860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5039e0b68406SMatthew Knepley @*/
5040d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5041d71ae5a4SJacob Faibussowitsch {
5042e0b68406SMatthew Knepley   PetscFunctionBegin;
504363a3b9bcSJacob 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);
5044e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
50453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5046e0b68406SMatthew Knepley }
5047e0b68406SMatthew Knepley 
5048e0b68406SMatthew Knepley /*@
5049bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5050e5e52638SMatthew G. Knepley 
505120f4b53cSBarry Smith   Collective
5052e5e52638SMatthew G. Knepley 
5053e5e52638SMatthew G. Knepley   Input Parameter:
5054bb7acecfSBarry Smith . dm - The `DM`
5055e5e52638SMatthew G. Knepley 
5056e5e52638SMatthew G. Knepley   Output Parameter:
5057bb7acecfSBarry Smith . newdm - The `DM`
5058e5e52638SMatthew G. Knepley 
5059e5e52638SMatthew G. Knepley   Level: advanced
5060e5e52638SMatthew G. Knepley 
50611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5062e5e52638SMatthew G. Knepley @*/
5063d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5064d71ae5a4SJacob Faibussowitsch {
5065e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5066e5e52638SMatthew G. Knepley 
5067e5e52638SMatthew G. Knepley   PetscFunctionBegin;
50683ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
50699566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50709566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5071e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5072e5e52638SMatthew G. Knepley     DMLabel     label;
5073e5e52638SMatthew G. Knepley     PetscObject field;
507434aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5075e5e52638SMatthew G. Knepley 
50769566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50779566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50789566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50799566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
508034aa8a36SMatthew G. Knepley   }
50813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
508234aa8a36SMatthew G. Knepley }
508334aa8a36SMatthew G. Knepley 
508434aa8a36SMatthew G. Knepley /*@
508534aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
508634aa8a36SMatthew G. Knepley 
508720f4b53cSBarry Smith   Not Collective
508834aa8a36SMatthew G. Knepley 
508934aa8a36SMatthew G. Knepley   Input Parameters:
509020f4b53cSBarry Smith + dm - The `DM` object
509120f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
509234aa8a36SMatthew G. Knepley 
5093d8d19677SJose E. Roman   Output Parameters:
509434aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
509534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
509634aa8a36SMatthew G. Knepley 
509734aa8a36SMatthew G. Knepley   Level: developer
509834aa8a36SMatthew G. Knepley 
509920f4b53cSBarry Smith   Notes:
510020f4b53cSBarry Smith .vb
510120f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
510220f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
510320f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
510420f4b53cSBarry Smith .ve
510520f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
510620f4b53cSBarry Smith 
51071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
510834aa8a36SMatthew G. Knepley @*/
5109d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5110d71ae5a4SJacob Faibussowitsch {
511134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
511234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51134f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
51144f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
511534aa8a36SMatthew G. Knepley   if (f < 0) {
511634aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
511734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
511834aa8a36SMatthew G. Knepley   } else {
511934aa8a36SMatthew G. Knepley     PetscInt Nf;
512034aa8a36SMatthew G. Knepley 
51219566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51227a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
512334aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
512434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
512534aa8a36SMatthew G. Knepley   }
51263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
512734aa8a36SMatthew G. Knepley }
512834aa8a36SMatthew G. Knepley 
512934aa8a36SMatthew G. Knepley /*@
513034aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
513134aa8a36SMatthew G. Knepley 
513220f4b53cSBarry Smith   Not Collective
513334aa8a36SMatthew G. Knepley 
513434aa8a36SMatthew G. Knepley   Input Parameters:
513520f4b53cSBarry Smith + dm         - The `DM` object
513634aa8a36SMatthew G. Knepley . f          - The field number
513734aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
513834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
513934aa8a36SMatthew G. Knepley 
514034aa8a36SMatthew G. Knepley   Level: developer
514134aa8a36SMatthew G. Knepley 
514220f4b53cSBarry Smith   Notes:
514320f4b53cSBarry Smith .vb
514420f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
514520f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
514620f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
514720f4b53cSBarry Smith .ve
514820f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
514920f4b53cSBarry Smith 
51501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
515134aa8a36SMatthew G. Knepley @*/
5152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5153d71ae5a4SJacob Faibussowitsch {
515434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
515534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
515634aa8a36SMatthew G. Knepley   if (f < 0) {
515734aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
515834aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
515934aa8a36SMatthew G. Knepley   } else {
516034aa8a36SMatthew G. Knepley     PetscInt Nf;
516134aa8a36SMatthew G. Knepley 
51629566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51637a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
516434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
516534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5166e5e52638SMatthew G. Knepley   }
51673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5168e5e52638SMatthew G. Knepley }
5169e5e52638SMatthew G. Knepley 
5170b0441da4SMatthew G. Knepley /*@
5171b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5172b0441da4SMatthew G. Knepley 
5173b0441da4SMatthew G. Knepley   Not collective
5174b0441da4SMatthew G. Knepley 
5175f899ff85SJose E. Roman   Input Parameter:
517620f4b53cSBarry Smith . dm - The `DM` object
5177b0441da4SMatthew G. Knepley 
5178d8d19677SJose E. Roman   Output Parameters:
5179b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5180b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5181b0441da4SMatthew G. Knepley 
5182b0441da4SMatthew G. Knepley   Level: developer
5183b0441da4SMatthew G. Knepley 
518420f4b53cSBarry Smith   Notes:
518520f4b53cSBarry Smith .vb
518620f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
518720f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
518820f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
518920f4b53cSBarry Smith .ve
519020f4b53cSBarry Smith 
51911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5192b0441da4SMatthew G. Knepley @*/
5193d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5194d71ae5a4SJacob Faibussowitsch {
5195b0441da4SMatthew G. Knepley   PetscInt Nf;
5196b0441da4SMatthew G. Knepley 
5197b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5198b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51994f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52004f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52019566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5202b0441da4SMatthew G. Knepley   if (!Nf) {
52039566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5204b0441da4SMatthew G. Knepley   } else {
52059566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5206b0441da4SMatthew G. Knepley   }
52073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5208b0441da4SMatthew G. Knepley }
5209b0441da4SMatthew G. Knepley 
5210b0441da4SMatthew G. Knepley /*@
5211b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5212b0441da4SMatthew G. Knepley 
521320f4b53cSBarry Smith   Not Collective
5214b0441da4SMatthew G. Knepley 
5215b0441da4SMatthew G. Knepley   Input Parameters:
521620f4b53cSBarry Smith + dm         - The `DM` object
5217b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5218b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5219b0441da4SMatthew G. Knepley 
5220b0441da4SMatthew G. Knepley   Level: developer
5221b0441da4SMatthew G. Knepley 
522220f4b53cSBarry Smith   Notes:
522320f4b53cSBarry Smith .vb
522420f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
522520f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
522620f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
522720f4b53cSBarry Smith .ve
522820f4b53cSBarry Smith 
52291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5230b0441da4SMatthew G. Knepley @*/
5231d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5232d71ae5a4SJacob Faibussowitsch {
5233b0441da4SMatthew G. Knepley   PetscInt Nf;
5234b0441da4SMatthew G. Knepley 
5235b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5236b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52379566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5238b0441da4SMatthew G. Knepley   if (!Nf) {
52399566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5240b0441da4SMatthew G. Knepley   } else {
52419566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5242e5e52638SMatthew G. Knepley   }
52433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5244e5e52638SMatthew G. Knepley }
5245e5e52638SMatthew G. Knepley 
5246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5247d71ae5a4SJacob Faibussowitsch {
5248799db056SMatthew G. Knepley   DM           plex;
5249799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5250799db056SMatthew G. Knepley   const char **names;
5251799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5252799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5253799db056SMatthew G. Knepley   size_t       len;
5254799db056SMatthew G. Knepley   MPI_Comm     comm;
5255799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5256783e2ec8SMatthew G. Knepley 
5257783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5258799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5259799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5260799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5261799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5262799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5263799db056SMatthew G. Knepley     PetscDS  dsBC;
5264799db056SMatthew G. Knepley     PetscInt numBd;
5265799db056SMatthew G. Knepley 
526607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5267799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5268799db056SMatthew G. Knepley     maxLabels += numBd;
5269799db056SMatthew G. Knepley   }
5270799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5271799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5272799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5273799db056SMatthew G. Knepley     PetscDS  dsBC;
5274799db056SMatthew G. Knepley     PetscInt numBd, bd;
5275799db056SMatthew G. Knepley 
527607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5277799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5278799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5279799db056SMatthew G. Knepley       DMLabel      label;
5280799db056SMatthew G. Knepley       PetscInt     field;
5281799db056SMatthew G. Knepley       PetscObject  obj;
5282799db056SMatthew G. Knepley       PetscClassId id;
5283799db056SMatthew G. Knepley 
5284799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52859566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52869566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5287799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
52889371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
52899371c9d4SSatish Balay         if (labels[l] == label) break;
5290799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5291783e2ec8SMatthew G. Knepley     }
5292799db056SMatthew G. Knepley   }
5293799db056SMatthew G. Knepley   /* Get label names */
5294799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5295799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
52969371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
52979371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
52989371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
52999371c9d4SSatish Balay   }
5300799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5301712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5302799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5303c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5304799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5305799db056SMatthew G. Knepley   /* Put all names on all processes */
5306799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5307799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5308799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5309799db056SMatthew G. Knepley   gNl = displs[size];
53109371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53119371c9d4SSatish Balay     counts[p] *= gmaxLen;
53129371c9d4SSatish Balay     displs[p] *= gmaxLen;
53139371c9d4SSatish Balay   }
5314799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5315799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5316799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5317799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5318799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5319799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5320799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53219371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53229371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53239566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5324799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53259566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5326799db056SMatthew G. Knepley     ++gl;
5327783e2ec8SMatthew G. Knepley   }
5328799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5330783e2ec8SMatthew G. Knepley }
5331783e2ec8SMatthew G. Knepley 
5332d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5333d71ae5a4SJacob Faibussowitsch {
5334e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5335e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5336e5e52638SMatthew G. Knepley 
5337e5e52638SMatthew G. Knepley   PetscFunctionBegin;
53383ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
53399566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5340e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53419371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53429371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53439371c9d4SSatish Balay     tmpd[s].label  = NULL;
53449371c9d4SSatish Balay     tmpd[s].fields = NULL;
53459371c9d4SSatish Balay   }
53469566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5347e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5348e5e52638SMatthew G. Knepley   dm->probs = tmpd;
53493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5350e5e52638SMatthew G. Knepley }
5351e5e52638SMatthew G. Knepley 
5352e5e52638SMatthew G. Knepley /*@
535320f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5354e5e52638SMatthew G. Knepley 
535520f4b53cSBarry Smith   Not Collective
5356e5e52638SMatthew G. Knepley 
5357e5e52638SMatthew G. Knepley   Input Parameter:
535820f4b53cSBarry Smith . dm - The `DM`
5359e5e52638SMatthew G. Knepley 
5360e5e52638SMatthew G. Knepley   Output Parameter:
536120f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5362e5e52638SMatthew G. Knepley 
5363e5e52638SMatthew G. Knepley   Level: intermediate
5364e5e52638SMatthew G. Knepley 
53651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5366e5e52638SMatthew G. Knepley @*/
5367d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5368d71ae5a4SJacob Faibussowitsch {
5369e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5370e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53714f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5372e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
53733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5374e5e52638SMatthew G. Knepley }
5375e5e52638SMatthew G. Knepley 
5376e5e52638SMatthew G. Knepley /*@
537720f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5378e5e52638SMatthew G. Knepley 
537920f4b53cSBarry Smith   Logically Collective
5380e5e52638SMatthew G. Knepley 
5381e5e52638SMatthew G. Knepley   Input Parameter:
538220f4b53cSBarry Smith . dm - The `DM`
5383e5e52638SMatthew G. Knepley 
5384e5e52638SMatthew G. Knepley   Level: intermediate
5385e5e52638SMatthew G. Knepley 
53861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5387e5e52638SMatthew G. Knepley @*/
5388d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5389d71ae5a4SJacob Faibussowitsch {
5390e5e52638SMatthew G. Knepley   PetscInt s;
5391e5e52638SMatthew G. Knepley 
5392e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5393e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5394e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
53959566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
539607218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
53979566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
53989566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5399e5e52638SMatthew G. Knepley   }
54009566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5401e5e52638SMatthew G. Knepley   dm->probs = NULL;
5402e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5404e5e52638SMatthew G. Knepley }
5405e5e52638SMatthew G. Knepley 
5406e5e52638SMatthew G. Knepley /*@
540720f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5408e5e52638SMatthew G. Knepley 
540920f4b53cSBarry Smith   Not Collective
5410e5e52638SMatthew G. Knepley 
5411e5e52638SMatthew G. Knepley   Input Parameter:
541220f4b53cSBarry Smith . dm - The `DM`
5413e5e52638SMatthew G. Knepley 
5414e5e52638SMatthew G. Knepley   Output Parameter:
541507218a29SMatthew G. Knepley . ds - The default `PetscDS`
5416e5e52638SMatthew G. Knepley 
5417e5e52638SMatthew G. Knepley   Level: intermediate
5418e5e52638SMatthew G. Knepley 
54191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5420e5e52638SMatthew G. Knepley @*/
542107218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5422d71ae5a4SJacob Faibussowitsch {
5423e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5424e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54254f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
542607218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
542707218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
54283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5429e5e52638SMatthew G. Knepley }
5430e5e52638SMatthew G. Knepley 
5431e5e52638SMatthew G. Knepley /*@
543220f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5433e5e52638SMatthew G. Knepley 
543420f4b53cSBarry Smith   Not Collective
5435e5e52638SMatthew G. Knepley 
5436e5e52638SMatthew G. Knepley   Input Parameters:
543720f4b53cSBarry Smith + dm    - The `DM`
543820f4b53cSBarry Smith - point - Cell for the `PetscDS`
5439e5e52638SMatthew G. Knepley 
544007218a29SMatthew G. Knepley   Output Parameters:
544107218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
544207218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5443e5e52638SMatthew G. Knepley 
5444e5e52638SMatthew G. Knepley   Level: developer
5445e5e52638SMatthew G. Knepley 
54461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5447e5e52638SMatthew G. Knepley @*/
544807218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5449d71ae5a4SJacob Faibussowitsch {
545007218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5451e5e52638SMatthew G. Knepley   PetscInt s;
5452e5e52638SMatthew G. Knepley 
5453e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5454e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54554f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
54564f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
545763a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
545807218a29SMatthew G. Knepley   if (ds) *ds = NULL;
545907218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5460e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5461e5e52638SMatthew G. Knepley     PetscInt val;
5462e5e52638SMatthew G. Knepley 
54639371c9d4SSatish Balay     if (!dm->probs[s].label) {
546407218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
54659371c9d4SSatish Balay     } else {
54669566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
54679371c9d4SSatish Balay       if (val >= 0) {
546807218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
546907218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
54709371c9d4SSatish Balay         break;
54719371c9d4SSatish Balay       }
5472e5e52638SMatthew G. Knepley     }
5473e5e52638SMatthew G. Knepley   }
547407218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
54753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5476e5e52638SMatthew G. Knepley }
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley /*@
547920f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5480e5e52638SMatthew G. Knepley 
548120f4b53cSBarry Smith   Not Collective
5482e5e52638SMatthew G. Knepley 
5483e5e52638SMatthew G. Knepley   Input Parameters:
548420f4b53cSBarry Smith + dm    - The `DM`
548520f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5486e5e52638SMatthew G. Knepley 
5487b3cf3223SMatthew G. Knepley   Output Parameters:
548820f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
548907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
549007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5491e5e52638SMatthew G. Knepley 
5492e5e52638SMatthew G. Knepley   Level: advanced
5493e5e52638SMatthew G. Knepley 
549420f4b53cSBarry Smith   Note:
549520f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
549620f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
549707218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
549820f4b53cSBarry Smith 
54991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5500e5e52638SMatthew G. Knepley @*/
550107218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5502d71ae5a4SJacob Faibussowitsch {
5503e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5504e5e52638SMatthew G. Knepley 
5505e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5506e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5507e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55089371c9d4SSatish Balay   if (fields) {
55094f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
55109371c9d4SSatish Balay     *fields = NULL;
55119371c9d4SSatish Balay   }
55129371c9d4SSatish Balay   if (ds) {
55134f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
55149371c9d4SSatish Balay     *ds = NULL;
55159371c9d4SSatish Balay   }
551607218a29SMatthew G. Knepley   if (dsIn) {
55174f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
551807218a29SMatthew G. Knepley     *dsIn = NULL;
551907218a29SMatthew G. Knepley   }
5520e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5521154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5522b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5523b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
552407218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55253ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5526b3cf3223SMatthew G. Knepley     }
5527e5e52638SMatthew G. Knepley   }
55283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5529e5e52638SMatthew G. Knepley }
5530e5e52638SMatthew G. Knepley 
5531e5e52638SMatthew G. Knepley /*@
5532bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5533083401c6SMatthew G. Knepley 
553420f4b53cSBarry Smith   Collective
5535083401c6SMatthew G. Knepley 
5536083401c6SMatthew G. Knepley   Input Parameters:
5537bb7acecfSBarry Smith + dm     - The `DM`
553820f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
553907218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
554007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
554107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5542083401c6SMatthew G. Knepley 
554320f4b53cSBarry Smith   Level: advanced
554420f4b53cSBarry Smith 
5545bb7acecfSBarry Smith   Note:
5546bb7acecfSBarry 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,
5547083401c6SMatthew G. Knepley   the fields argument is ignored.
5548083401c6SMatthew G. Knepley 
55491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5550083401c6SMatthew G. Knepley @*/
555107218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5552d71ae5a4SJacob Faibussowitsch {
5553083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5554083401c6SMatthew G. Knepley 
5555083401c6SMatthew G. Knepley   PetscFunctionBegin;
5556083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5557083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
555807218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5559064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
556007218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5561083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5562083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55639566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
556407218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5565083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
556607218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
55673ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5568083401c6SMatthew G. Knepley     }
5569083401c6SMatthew G. Knepley   }
55709566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
55719566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
55729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
55739566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
557407218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5575083401c6SMatthew G. Knepley   if (!label) {
5576083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5577083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5578083401c6SMatthew G. Knepley     Nds = 0;
5579083401c6SMatthew G. Knepley   }
5580083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5581083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5582083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
558307218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
55843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5585083401c6SMatthew G. Knepley }
5586083401c6SMatthew G. Knepley 
5587083401c6SMatthew G. Knepley /*@
558820f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5589e5e52638SMatthew G. Knepley 
559020f4b53cSBarry Smith   Not Collective
5591e5e52638SMatthew G. Knepley 
5592e5e52638SMatthew G. Knepley   Input Parameters:
559320f4b53cSBarry Smith + dm  - The `DM`
5594e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5595e5e52638SMatthew G. Knepley 
5596e5e52638SMatthew G. Knepley   Output Parameters:
559720f4b53cSBarry Smith + label  - The region label, or `NULL`
559820f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
559907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
560007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5601e5e52638SMatthew G. Knepley 
5602e5e52638SMatthew G. Knepley   Level: advanced
5603e5e52638SMatthew G. Knepley 
56041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5605e5e52638SMatthew G. Knepley @*/
560607218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5607d71ae5a4SJacob Faibussowitsch {
5608e5e52638SMatthew G. Knepley   PetscInt Nds;
5609e5e52638SMatthew G. Knepley 
5610e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5611e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56129566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
561363a3b9bcSJacob 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);
5614e5e52638SMatthew G. Knepley   if (label) {
56154f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5616e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5617e5e52638SMatthew G. Knepley   }
5618b3cf3223SMatthew G. Knepley   if (fields) {
56194f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5620b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5621b3cf3223SMatthew G. Knepley   }
5622e5e52638SMatthew G. Knepley   if (ds) {
56234f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5624e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5625e5e52638SMatthew G. Knepley   }
562607218a29SMatthew G. Knepley   if (dsIn) {
56274f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
562807218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
562907218a29SMatthew G. Knepley   }
56303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5631e5e52638SMatthew G. Knepley }
5632e5e52638SMatthew G. Knepley 
5633e5e52638SMatthew G. Knepley /*@
563420f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5635e5e52638SMatthew G. Knepley 
563620f4b53cSBarry Smith   Not Collective
5637e5e52638SMatthew G. Knepley 
5638e5e52638SMatthew G. Knepley   Input Parameters:
563920f4b53cSBarry Smith + dm     - The `DM`
5640083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
564120f4b53cSBarry Smith . label  - The region label, or `NULL`
564207218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
564307218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
564407218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5645e5e52638SMatthew G. Knepley 
5646e5e52638SMatthew G. Knepley   Level: advanced
5647e5e52638SMatthew G. Knepley 
56481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5649e5e52638SMatthew G. Knepley @*/
565007218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5651d71ae5a4SJacob Faibussowitsch {
5652083401c6SMatthew G. Knepley   PetscInt Nds;
5653e5e52638SMatthew G. Knepley 
5654e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5655e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5656ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56579566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
565863a3b9bcSJacob 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);
56599566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56609566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5661083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5662083401c6SMatthew G. Knepley   if (fields) {
5663083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56649566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56659566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5666083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5667e5e52638SMatthew G. Knepley   }
5668083401c6SMatthew G. Knepley   if (ds) {
5669083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56709566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56719566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5672083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5673083401c6SMatthew G. Knepley   }
567407218a29SMatthew G. Knepley   if (dsIn) {
567507218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
567607218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
567707218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
567807218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
567907218a29SMatthew G. Knepley   }
56803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5681e5e52638SMatthew G. Knepley }
5682e5e52638SMatthew G. Knepley 
5683e5e52638SMatthew G. Knepley /*@
568420f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
56851d3af9e0SMatthew G. Knepley 
568620f4b53cSBarry Smith   Not Collective
56871d3af9e0SMatthew G. Knepley 
56881d3af9e0SMatthew G. Knepley   Input Parameters:
568920f4b53cSBarry Smith + dm - The `DM`
569020f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
56911d3af9e0SMatthew G. Knepley 
56921d3af9e0SMatthew G. Knepley   Output Parameter:
56931d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
56941d3af9e0SMatthew G. Knepley 
56951d3af9e0SMatthew G. Knepley   Level: advanced
56961d3af9e0SMatthew G. Knepley 
56971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
56981d3af9e0SMatthew G. Knepley @*/
5699d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5700d71ae5a4SJacob Faibussowitsch {
57011d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57021d3af9e0SMatthew G. Knepley 
57031d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57041d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57051d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57064f572ea9SToby Isaac   PetscAssertPointer(num, 3);
57079566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57089371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57099371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57101d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57111d3af9e0SMatthew G. Knepley   else *num = n;
57123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57131d3af9e0SMatthew G. Knepley }
57141d3af9e0SMatthew G. Knepley 
57152df84da0SMatthew G. Knepley /*@C
5716bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57172df84da0SMatthew G. Knepley 
571820f4b53cSBarry Smith   Not Collective
57192df84da0SMatthew G. Knepley 
5720f1a722f8SMatthew G. Knepley   Input Parameters:
5721bb7acecfSBarry Smith + dm     - The `DM`
57222df84da0SMatthew G. Knepley . Nc     - The number of components for the field
572320f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5724bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57252df84da0SMatthew G. Knepley 
57262df84da0SMatthew G. Knepley   Output Parameter:
5727bb7acecfSBarry Smith . fem - The `PetscFE`
57282df84da0SMatthew G. Knepley 
572920f4b53cSBarry Smith   Level: intermediate
573020f4b53cSBarry Smith 
5731bb7acecfSBarry Smith   Note:
5732bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57332df84da0SMatthew G. Knepley 
57341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57352df84da0SMatthew G. Knepley @*/
5736d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5737d71ae5a4SJacob Faibussowitsch {
57382df84da0SMatthew G. Knepley   DMPolytopeType ct;
57392df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57402df84da0SMatthew G. Knepley 
57412df84da0SMatthew G. Knepley   PetscFunctionBegin;
57422df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57432df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57444f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
57452df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57464f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
57479566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57509566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57522df84da0SMatthew G. Knepley }
57532df84da0SMatthew G. Knepley 
57541d3af9e0SMatthew G. Knepley /*@
5755bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5756e5e52638SMatthew G. Knepley 
575720f4b53cSBarry Smith   Collective
5758e5e52638SMatthew G. Knepley 
5759e5e52638SMatthew G. Knepley   Input Parameter:
5760bb7acecfSBarry Smith . dm - The `DM`
5761e5e52638SMatthew G. Knepley 
576220f4b53cSBarry Smith   Options Database Key:
5763bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
576445480ffeSMatthew G. Knepley 
576520f4b53cSBarry Smith   Level: intermediate
576620f4b53cSBarry Smith 
57671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5768e5e52638SMatthew G. Knepley @*/
5769d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5770d71ae5a4SJacob Faibussowitsch {
5771e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5772083401c6SMatthew G. Knepley   PetscDS   dsDef;
5773083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5774f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5775f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5776e5e52638SMatthew G. Knepley 
5777e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5778e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57793ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
57809566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
57819566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5782083401c6SMatthew G. Knepley   /* Determine how many regions we have */
57839566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5784083401c6SMatthew G. Knepley   Nl   = 0;
5785083401c6SMatthew G. Knepley   Ndef = 0;
5786083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5787083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5788083401c6SMatthew G. Knepley     PetscInt l;
5789083401c6SMatthew G. Knepley 
5790f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5791f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5792f918ec44SMatthew G. Knepley     {
5793f918ec44SMatthew G. Knepley       PetscClassId id;
5794f918ec44SMatthew G. Knepley 
57959566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5796f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5797f918ec44SMatthew G. Knepley         Ceed ceed;
5798f918ec44SMatthew G. Knepley 
57999566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58009566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5801f918ec44SMatthew G. Knepley       }
5802f918ec44SMatthew G. Knepley     }
5803f918ec44SMatthew G. Knepley #endif
58049371c9d4SSatish Balay     if (!label) {
58059371c9d4SSatish Balay       ++Ndef;
58069371c9d4SSatish Balay       continue;
58079371c9d4SSatish Balay     }
58089371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58099371c9d4SSatish Balay       if (label == labelSet[l]) break;
5810083401c6SMatthew G. Knepley     if (l < Nl) continue;
5811083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5812083401c6SMatthew G. Knepley   }
5813083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
581407218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5815083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5816b3cf3223SMatthew G. Knepley     IS        fields;
5817b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5818b3cf3223SMatthew G. Knepley 
58199371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58209371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58217a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58229566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58239371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58249371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58259566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58269566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58279566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58289566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
582988f0c812SMatthew G. Knepley 
58309566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
583107218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
58329566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58339566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58342df9ee95SMatthew G. Knepley   }
583507218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
58369566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5837083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5838083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58390122748bSMatthew G. Knepley     DM              plex;
5840083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5841083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5842083401c6SMatthew G. Knepley     PetscInt       *fields;
5843083401c6SMatthew G. Knepley     const PetscInt *cells;
5844083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58450122748bSMatthew G. Knepley 
58469566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58489566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58499566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58505fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5851083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5852083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5853083401c6SMatthew G. Knepley       IS      pointIS;
5854083401c6SMatthew G. Knepley 
58559566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58569566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58579566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58589566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5859083401c6SMatthew G. Knepley     }
58609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5861083401c6SMatthew G. Knepley 
58629566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58639566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58649566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58659566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58669566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58679566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58689566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5869083401c6SMatthew G. Knepley 
58709566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58719371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58729371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58739566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58749566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58759566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58769566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5877083401c6SMatthew G. Knepley 
58789566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
587907218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
58809566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
58819566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
58829566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58839566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
58849566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5885083401c6SMatthew G. Knepley   }
5886083401c6SMatthew G. Knepley   /* Create label DSes
5887083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5888083401c6SMatthew G. Knepley   */
5889083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5890083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5891083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
589207218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5893083401c6SMatthew G. Knepley     IS        fields;
5894083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5895083401c6SMatthew G. Knepley 
58969566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
58979371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58989371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
58999566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59009371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59019371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59029566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59039566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59049566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59059566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59069566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5907083401c6SMatthew G. Knepley     {
5908083401c6SMatthew G. Knepley       DMPolytopeType ct;
5909083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59105fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59110122748bSMatthew G. Knepley 
59129566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5913665f567fSMatthew G. Knepley       if (lStart >= 0) {
59149566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5915412e9a14SMatthew G. Knepley         switch (ct) {
5916412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5917412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5918412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5919d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5920d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5921d71ae5a4SJacob Faibussowitsch           break;
5922d71ae5a4SJacob Faibussowitsch         default:
5923d71ae5a4SJacob Faibussowitsch           break;
5924412e9a14SMatthew G. Knepley         }
5925665f567fSMatthew G. Knepley       }
5926712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
592707218a29SMatthew G. Knepley       if (isCohesive) {
592807218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
592907218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
593007218a29SMatthew G. Knepley       }
59315fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59325fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59335fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59349566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59359566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
593607218a29SMatthew G. Knepley             if (dsIn) {
593707218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
593807218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
593907218a29SMatthew G. Knepley             }
59405fedec97SMatthew G. Knepley           }
59415fedec97SMatthew G. Knepley           ++nf;
59425fedec97SMatthew G. Knepley         }
59435fedec97SMatthew G. Knepley       }
5944e5e52638SMatthew G. Knepley     }
594507218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
594607218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
59479566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
594807218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
5949e5e52638SMatthew G. Knepley   }
59509566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5951e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5952083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5953083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
595407218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
5955083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5956083401c6SMatthew G. Knepley     const PetscInt *fld;
59575fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59585fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5959e5e52638SMatthew G. Knepley 
59609566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59619566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59629566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59639566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5964083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5965083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59665fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5967e5e52638SMatthew G. Knepley       PetscClassId id;
5968e5e52638SMatthew G. Knepley 
59695fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59709566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59715fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
597207218a29SMatthew G. Knepley       if (isCohesive) {
597307218a29SMatthew G. Knepley         if (!isCohesiveField) {
597407218a29SMatthew G. Knepley           PetscObject bdDisc;
597507218a29SMatthew G. Knepley 
597607218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
597707218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
597807218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
597907218a29SMatthew G. Knepley         } else {
59809566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
598107218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
598207218a29SMatthew G. Knepley         }
598307218a29SMatthew G. Knepley       } else {
598407218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
598507218a29SMatthew G. Knepley       }
5986083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59879566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5988e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5989e5e52638SMatthew G. Knepley     }
59909566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5991e5e52638SMatthew G. Knepley   }
5992f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
59939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5994f9244615SMatthew G. Knepley   if (flg) {
59953b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59963b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
599707218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
59983b4aee56SMatthew G. Knepley       PetscInt Nf, f;
59993b4aee56SMatthew G. Knepley 
60009566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
600107218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
600207218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
600307218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
600407218a29SMatthew G. Knepley       }
60053b4aee56SMatthew G. Knepley     }
6006f9244615SMatthew G. Knepley   }
6007e5e52638SMatthew G. Knepley   /* Setup DSes */
6008e5e52638SMatthew G. Knepley   if (doSetup) {
600907218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
601007218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
601107218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
601207218a29SMatthew G. Knepley     }
6013e5e52638SMatthew G. Knepley   }
60143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6015e5e52638SMatthew G. Knepley }
6016e5e52638SMatthew G. Knepley 
6017e5e52638SMatthew G. Knepley /*@
6018bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
60197f96f943SMatthew G. Knepley 
602020f4b53cSBarry Smith   Collective
6021f2cacb80SMatthew G. Knepley 
60227f96f943SMatthew G. Knepley   Input Parameters:
6023bb7acecfSBarry Smith + dm   - The `DM`
60247f96f943SMatthew G. Knepley - time - The time
60257f96f943SMatthew G. Knepley 
60267f96f943SMatthew G. Knepley   Output Parameters:
602720f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
602820f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
602920f4b53cSBarry Smith 
603020f4b53cSBarry Smith   Level: developer
60317f96f943SMatthew G. Knepley 
6032bb7acecfSBarry Smith   Note:
6033bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
60347f96f943SMatthew G. Knepley 
60351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
60367f96f943SMatthew G. Knepley @*/
6037d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6038d71ae5a4SJacob Faibussowitsch {
60397f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
60407f96f943SMatthew G. Knepley   void   **ectxs;
6041f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
60427f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
60437f96f943SMatthew G. Knepley 
60447f96f943SMatthew G. Knepley   PetscFunctionBegin;
6045f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6046f60fa741SMatthew G. Knepley   if (u) {
6047f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6048f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6049f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6050f60fa741SMatthew G. Knepley   }
6051f60fa741SMatthew G. Knepley   if (u_t) {
6052f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6053f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6054f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6055f60fa741SMatthew G. Knepley   }
60569566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
60579566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
60589566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60597f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
60607f96f943SMatthew G. Knepley     PetscDS         ds;
60617f96f943SMatthew G. Knepley     DMLabel         label;
60627f96f943SMatthew G. Knepley     IS              fieldIS;
60637f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60647f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60657f96f943SMatthew G. Knepley 
606607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
60679566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
60689566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
60699566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
60709566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6071f2cacb80SMatthew G. Knepley     if (u) {
6072f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6073f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6074f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
60757f96f943SMatthew G. Knepley     }
6076f2cacb80SMatthew G. Knepley     if (u_t) {
60779566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60789566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6079f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6080f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6081f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6082f2cacb80SMatthew G. Knepley     }
60839566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6084f2cacb80SMatthew G. Knepley   }
6085f2cacb80SMatthew G. Knepley   if (u) {
60869566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60879566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6088f2cacb80SMatthew G. Knepley   }
6089f2cacb80SMatthew G. Knepley   if (u_t) {
60909566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
60919566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6092f2cacb80SMatthew G. Knepley   }
60939566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6094f60fa741SMatthew G. Knepley   if (u) {
6095f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6096f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6097f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6098f60fa741SMatthew G. Knepley   }
6099f60fa741SMatthew G. Knepley   if (u_t) {
6100f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6101f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6102f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6103f60fa741SMatthew G. Knepley   }
61043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61057f96f943SMatthew G. Knepley }
61067f96f943SMatthew G. Knepley 
610707218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6108d71ae5a4SJacob Faibussowitsch {
610907218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
611045480ffeSMatthew G. Knepley 
611145480ffeSMatthew G. Knepley   PetscFunctionBegin;
61129566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
611307218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
611407218a29SMatthew G. Knepley   if (dsIn) {
611507218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
611607218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
611745480ffeSMatthew G. Knepley   }
611807218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
61199566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
612007218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
61213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
612245480ffeSMatthew G. Knepley }
612345480ffeSMatthew G. Knepley 
61247f96f943SMatthew G. Knepley /*@
6125bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6126e5e52638SMatthew G. Knepley 
612720f4b53cSBarry Smith   Collective
6128e5e52638SMatthew G. Knepley 
6129e5e52638SMatthew G. Knepley   Input Parameter:
6130bb7acecfSBarry Smith . dm - The `DM`
6131e5e52638SMatthew G. Knepley 
6132e5e52638SMatthew G. Knepley   Output Parameter:
6133bb7acecfSBarry Smith . newdm - The `DM`
6134e5e52638SMatthew G. Knepley 
6135e5e52638SMatthew G. Knepley   Level: advanced
6136e5e52638SMatthew G. Knepley 
61371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6138e5e52638SMatthew G. Knepley @*/
6139d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6140d71ae5a4SJacob Faibussowitsch {
6141e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6142e5e52638SMatthew G. Knepley 
6143e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61443ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
61459566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61469566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6147e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6148e5e52638SMatthew G. Knepley     DMLabel  label;
6149b3cf3223SMatthew G. Knepley     IS       fields;
615007218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6151783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6152e5e52638SMatthew G. Knepley 
615307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6154b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
615507218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6156d5b43468SJose E. Roman     /* Complete new labels in the new DS */
615707218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
61589566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6159783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6160b8025e53SMatthew G. Knepley       PetscWeakForm wf;
616145480ffeSMatthew G. Knepley       DMLabel       label;
6162783e2ec8SMatthew G. Knepley       PetscInt      field;
6163783e2ec8SMatthew G. Knepley 
61649566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61659566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6166783e2ec8SMatthew G. Knepley     }
6167e5e52638SMatthew G. Knepley   }
6168799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
61693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6170e5e52638SMatthew G. Knepley }
6171e5e52638SMatthew G. Knepley 
6172e5e52638SMatthew G. Knepley /*@
6173bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6174e5e52638SMatthew G. Knepley 
617520f4b53cSBarry Smith   Collective
6176e5e52638SMatthew G. Knepley 
6177e5e52638SMatthew G. Knepley   Input Parameter:
6178bb7acecfSBarry Smith . dm - The `DM`
6179e5e52638SMatthew G. Knepley 
6180e5e52638SMatthew G. Knepley   Output Parameter:
6181bb7acecfSBarry Smith . newdm - The `DM`
6182e5e52638SMatthew G. Knepley 
6183e5e52638SMatthew G. Knepley   Level: advanced
6184e5e52638SMatthew G. Knepley 
618560225df5SJacob Faibussowitsch   Developer Notes:
6186bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6187bb7acecfSBarry Smith 
61881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6189e5e52638SMatthew G. Knepley @*/
6190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6191d71ae5a4SJacob Faibussowitsch {
6192e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61939566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
61949566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
61953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6196e5e52638SMatthew G. Knepley }
6197e5e52638SMatthew G. Knepley 
6198c73cfb54SMatthew G. Knepley /*@
6199bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6200c73cfb54SMatthew G. Knepley 
620120f4b53cSBarry Smith   Not Collective
6202c73cfb54SMatthew G. Knepley 
6203c73cfb54SMatthew G. Knepley   Input Parameter:
6204bb7acecfSBarry Smith . dm - The `DM`
6205c73cfb54SMatthew G. Knepley 
6206c73cfb54SMatthew G. Knepley   Output Parameter:
6207c73cfb54SMatthew G. Knepley . dim - The topological dimension
6208c73cfb54SMatthew G. Knepley 
6209c73cfb54SMatthew G. Knepley   Level: beginner
6210c73cfb54SMatthew G. Knepley 
62111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6212c73cfb54SMatthew G. Knepley @*/
6213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6214d71ae5a4SJacob Faibussowitsch {
6215c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6216c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62174f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6218c73cfb54SMatthew G. Knepley   *dim = dm->dim;
62193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6220c73cfb54SMatthew G. Knepley }
6221c73cfb54SMatthew G. Knepley 
6222c73cfb54SMatthew G. Knepley /*@
6223bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6224c73cfb54SMatthew G. Knepley 
622520f4b53cSBarry Smith   Collective
6226c73cfb54SMatthew G. Knepley 
6227c73cfb54SMatthew G. Knepley   Input Parameters:
6228bb7acecfSBarry Smith + dm  - The `DM`
6229c73cfb54SMatthew G. Knepley - dim - The topological dimension
6230c73cfb54SMatthew G. Knepley 
6231c73cfb54SMatthew G. Knepley   Level: beginner
6232c73cfb54SMatthew G. Knepley 
62331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6234c73cfb54SMatthew G. Knepley @*/
6235d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6236d71ae5a4SJacob Faibussowitsch {
6237e5e52638SMatthew G. Knepley   PetscDS  ds;
623845480ffeSMatthew G. Knepley   PetscInt Nds, n;
6239f17e8794SMatthew G. Knepley 
6240c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6241c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6242c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6243c73cfb54SMatthew G. Knepley   dm->dim = dim;
6244d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
62459566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
624645480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
624707218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
62489566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
624945480ffeSMatthew G. Knepley     }
6250d17bd122SMatthew G. Knepley   }
62513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6252c73cfb54SMatthew G. Knepley }
6253c73cfb54SMatthew G. Knepley 
6254793f3fe5SMatthew G. Knepley /*@
6255793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6256793f3fe5SMatthew G. Knepley 
625720f4b53cSBarry Smith   Collective
6258793f3fe5SMatthew G. Knepley 
6259793f3fe5SMatthew G. Knepley   Input Parameters:
6260bb7acecfSBarry Smith + dm  - the `DM`
6261793f3fe5SMatthew G. Knepley - dim - the dimension
6262793f3fe5SMatthew G. Knepley 
6263793f3fe5SMatthew G. Knepley   Output Parameters:
6264793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6265aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6266793f3fe5SMatthew G. Knepley 
626720f4b53cSBarry Smith   Level: intermediate
626820f4b53cSBarry Smith 
6269793f3fe5SMatthew G. Knepley   Note:
6270793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6271a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6272793f3fe5SMatthew G. Knepley   then the interval is empty.
6273793f3fe5SMatthew G. Knepley 
62741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6275793f3fe5SMatthew G. Knepley @*/
6276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6277d71ae5a4SJacob Faibussowitsch {
6278793f3fe5SMatthew G. Knepley   PetscInt d;
6279793f3fe5SMatthew G. Knepley 
6280793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6281793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62829566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62837a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6284dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
62853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6286793f3fe5SMatthew G. Knepley }
6287793f3fe5SMatthew G. Knepley 
62886636e97aSMatthew G Knepley /*@
6289bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6290f4d763aaSMatthew G. Knepley 
629120f4b53cSBarry Smith   Collective
62928f700142SStefano Zampini 
6293f4d763aaSMatthew G. Knepley   Input Parameter:
6294bb7acecfSBarry Smith . dm - The original `DM`
6295f4d763aaSMatthew G. Knepley 
6296f4d763aaSMatthew G. Knepley   Output Parameter:
6297bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6298f4d763aaSMatthew G. Knepley 
6299f4d763aaSMatthew G. Knepley   Level: intermediate
6300f4d763aaSMatthew G. Knepley 
6301bb7acecfSBarry Smith   Note:
6302bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6303bb7acecfSBarry 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
6304bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6305bb7acecfSBarry Smith 
63061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6307f4d763aaSMatthew G. Knepley @*/
6308d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6309d71ae5a4SJacob Faibussowitsch {
6310c26acbdeSMatthew G. Knepley   PetscSection section;
63112d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
631214f150ffSMatthew G. Knepley 
631314f150ffSMatthew G. Knepley   PetscFunctionBegin;
631414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63154f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
63169566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
63179566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6318712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
63192d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6320c26acbdeSMatthew G. Knepley     *odm = dm;
63213ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6322c26acbdeSMatthew G. Knepley   }
632314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6324c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
632514f150ffSMatthew G. Knepley     PetscSF      sf;
632614f150ffSMatthew G. Knepley 
63279566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
63289566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
63299566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
63309566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
63319566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
63329566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
63339566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
63349566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
63359566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
633614f150ffSMatthew G. Knepley   }
633714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
63383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
633914f150ffSMatthew G. Knepley }
6340f4d763aaSMatthew G. Knepley 
6341f4d763aaSMatthew G. Knepley /*@
6342cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6343f4d763aaSMatthew G. Knepley 
6344f4d763aaSMatthew G. Knepley   Input Parameter:
6345bb7acecfSBarry Smith . dm - The original `DM`
6346f4d763aaSMatthew G. Knepley 
6347cdb7a50dSMatthew G. Knepley   Output Parameters:
6348cdb7a50dSMatthew G. Knepley + num - The output sequence number
6349cdb7a50dSMatthew G. Knepley - val - The output sequence value
6350f4d763aaSMatthew G. Knepley 
6351f4d763aaSMatthew G. Knepley   Level: intermediate
6352f4d763aaSMatthew G. Knepley 
6353bb7acecfSBarry Smith   Note:
6354bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6355bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6356bb7acecfSBarry Smith 
635760225df5SJacob Faibussowitsch   Developer Notes:
6358bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6359bb7acecfSBarry Smith   not directly related to the `DM`.
6360f4d763aaSMatthew G. Knepley 
63611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6362f4d763aaSMatthew G. Knepley @*/
6363d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6364d71ae5a4SJacob Faibussowitsch {
6365f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6366f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63679371c9d4SSatish Balay   if (num) {
63684f572ea9SToby Isaac     PetscAssertPointer(num, 2);
63699371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63709371c9d4SSatish Balay   }
63719371c9d4SSatish Balay   if (val) {
63724f572ea9SToby Isaac     PetscAssertPointer(val, 3);
63739371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63749371c9d4SSatish Balay   }
63753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6376f4d763aaSMatthew G. Knepley }
6377f4d763aaSMatthew G. Knepley 
6378f4d763aaSMatthew G. Knepley /*@
6379cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6380f4d763aaSMatthew G. Knepley 
6381f4d763aaSMatthew G. Knepley   Input Parameters:
6382bb7acecfSBarry Smith + dm  - The original `DM`
6383cdb7a50dSMatthew G. Knepley . num - The output sequence number
6384cdb7a50dSMatthew G. Knepley - val - The output sequence value
6385f4d763aaSMatthew G. Knepley 
6386f4d763aaSMatthew G. Knepley   Level: intermediate
6387f4d763aaSMatthew G. Knepley 
6388bb7acecfSBarry Smith   Note:
6389bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6390bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6391f4d763aaSMatthew G. Knepley 
63921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6393f4d763aaSMatthew G. Knepley @*/
6394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6395d71ae5a4SJacob Faibussowitsch {
6396f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6397f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6398f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6399cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
64003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6401cdb7a50dSMatthew G. Knepley }
6402cdb7a50dSMatthew G. Knepley 
6403cdb7a50dSMatthew G. Knepley /*@C
6404bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6405cdb7a50dSMatthew G. Knepley 
6406cdb7a50dSMatthew G. Knepley   Input Parameters:
6407bb7acecfSBarry Smith + dm     - The original `DM`
6408*a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from
6409cdb7a50dSMatthew G. Knepley . name   - The sequence name
6410cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6411cdb7a50dSMatthew G. Knepley 
6412cdb7a50dSMatthew G. Knepley   Output Parameter:
6413cdb7a50dSMatthew G. Knepley . val - The output sequence value
6414cdb7a50dSMatthew G. Knepley 
6415cdb7a50dSMatthew G. Knepley   Level: intermediate
6416cdb7a50dSMatthew G. Knepley 
6417bb7acecfSBarry Smith   Note:
6418bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6419bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6420bb7acecfSBarry Smith 
642160225df5SJacob Faibussowitsch   Developer Notes:
6422bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6423cdb7a50dSMatthew G. Knepley 
64241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6425cdb7a50dSMatthew G. Knepley @*/
6426d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6427d71ae5a4SJacob Faibussowitsch {
6428cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6429cdb7a50dSMatthew G. Knepley 
6430cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6431cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6432cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
64334f572ea9SToby Isaac   PetscAssertPointer(val, 5);
64349566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6435cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6436cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6437cdb7a50dSMatthew G. Knepley     PetscScalar value;
6438cdb7a50dSMatthew G. Knepley 
64399566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
64404aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6441cdb7a50dSMatthew G. Knepley #endif
6442cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
64433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6444f4d763aaSMatthew G. Knepley }
64458e4ac7eaSMatthew G. Knepley 
64468e4ac7eaSMatthew G. Knepley /*@
6447bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64488e4ac7eaSMatthew G. Knepley 
644920f4b53cSBarry Smith   Not Collective
64508e4ac7eaSMatthew G. Knepley 
64518e4ac7eaSMatthew G. Knepley   Input Parameter:
6452bb7acecfSBarry Smith . dm - The `DM`
64538e4ac7eaSMatthew G. Knepley 
64548e4ac7eaSMatthew G. Knepley   Output Parameter:
6455bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64568e4ac7eaSMatthew G. Knepley 
64578e4ac7eaSMatthew G. Knepley   Level: beginner
64588e4ac7eaSMatthew G. Knepley 
64591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
64608e4ac7eaSMatthew G. Knepley @*/
6461d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6462d71ae5a4SJacob Faibussowitsch {
64638e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64648e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64654f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
64668e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64688e4ac7eaSMatthew G. Knepley }
64698e4ac7eaSMatthew G. Knepley 
64708e4ac7eaSMatthew G. Knepley /*@
6471bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64728e4ac7eaSMatthew G. Knepley 
647320f4b53cSBarry Smith   Collective
64748e4ac7eaSMatthew G. Knepley 
64758e4ac7eaSMatthew G. Knepley   Input Parameters:
6476bb7acecfSBarry Smith + dm         - The `DM`
6477bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64788e4ac7eaSMatthew G. Knepley 
6479bb7acecfSBarry Smith   Note:
6480bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64815d3b26e6SMatthew G. Knepley 
64828e4ac7eaSMatthew G. Knepley   Level: beginner
64838e4ac7eaSMatthew G. Knepley 
64841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64858e4ac7eaSMatthew G. Knepley @*/
6486d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6487d71ae5a4SJacob Faibussowitsch {
64888e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64898e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64908833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64918e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
64923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64938e4ac7eaSMatthew G. Knepley }
6494c58f1c22SToby Isaac 
6495c58f1c22SToby Isaac /*@C
6496bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6497c58f1c22SToby Isaac 
6498c58f1c22SToby Isaac   Not Collective
6499c58f1c22SToby Isaac 
6500c58f1c22SToby Isaac   Input Parameters:
6501bb7acecfSBarry Smith + dm   - The `DM` object
6502c58f1c22SToby Isaac - name - The label name
6503c58f1c22SToby Isaac 
6504c58f1c22SToby Isaac   Level: intermediate
6505c58f1c22SToby Isaac 
65061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6507c58f1c22SToby Isaac @*/
6508d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6509d71ae5a4SJacob Faibussowitsch {
65105d80c0bfSVaclav Hapla   PetscBool flg;
65115d80c0bfSVaclav Hapla   DMLabel   label;
6512c58f1c22SToby Isaac 
6513c58f1c22SToby Isaac   PetscFunctionBegin;
6514c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65154f572ea9SToby Isaac   PetscAssertPointer(name, 2);
65169566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6517c58f1c22SToby Isaac   if (!flg) {
65189566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65199566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65209566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6521c58f1c22SToby Isaac   }
65223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6523c58f1c22SToby Isaac }
6524c58f1c22SToby Isaac 
6525c58f1c22SToby Isaac /*@C
6526bb7acecfSBarry 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.
65270fdc7489SMatthew Knepley 
65280fdc7489SMatthew Knepley   Not Collective
65290fdc7489SMatthew Knepley 
65300fdc7489SMatthew Knepley   Input Parameters:
6531bb7acecfSBarry Smith + dm   - The `DM` object
65320fdc7489SMatthew Knepley . l    - The index for the label
65330fdc7489SMatthew Knepley - name - The label name
65340fdc7489SMatthew Knepley 
65350fdc7489SMatthew Knepley   Level: intermediate
65360fdc7489SMatthew Knepley 
65371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
65380fdc7489SMatthew Knepley @*/
6539d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6540d71ae5a4SJacob Faibussowitsch {
65410fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
65420fdc7489SMatthew Knepley   DMLabel     label;
65430fdc7489SMatthew Knepley   PetscInt    Nl, m;
65440fdc7489SMatthew Knepley   PetscBool   flg, match;
65450fdc7489SMatthew Knepley   const char *lname;
65460fdc7489SMatthew Knepley 
65470fdc7489SMatthew Knepley   PetscFunctionBegin;
65480fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65494f572ea9SToby Isaac   PetscAssertPointer(name, 3);
65509566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
65510fdc7489SMatthew Knepley   if (!flg) {
65529566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65539566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65549566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65550fdc7489SMatthew Knepley   }
65569566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
655763a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65580fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65599566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65609566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65610fdc7489SMatthew Knepley     if (match) break;
65620fdc7489SMatthew Knepley   }
65633ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
65640fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65650fdc7489SMatthew Knepley   else prev->next = orig->next;
65660fdc7489SMatthew Knepley   if (!l) {
65670fdc7489SMatthew Knepley     orig->next = dm->labels;
65680fdc7489SMatthew Knepley     dm->labels = orig;
65690fdc7489SMatthew Knepley   } else {
65709371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65719371c9d4SSatish Balay       ;
65720fdc7489SMatthew Knepley     orig->next = prev->next;
65730fdc7489SMatthew Knepley     prev->next = orig;
65740fdc7489SMatthew Knepley   }
65753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65760fdc7489SMatthew Knepley }
65770fdc7489SMatthew Knepley 
65780fdc7489SMatthew Knepley /*@C
6579bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6580c58f1c22SToby Isaac 
6581c58f1c22SToby Isaac   Not Collective
6582c58f1c22SToby Isaac 
6583c58f1c22SToby Isaac   Input Parameters:
6584bb7acecfSBarry Smith + dm    - The `DM` object
6585c58f1c22SToby Isaac . name  - The label name
6586c58f1c22SToby Isaac - point - The mesh point
6587c58f1c22SToby Isaac 
6588c58f1c22SToby Isaac   Output Parameter:
6589c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6590c58f1c22SToby Isaac 
6591c58f1c22SToby Isaac   Level: beginner
6592c58f1c22SToby Isaac 
65931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6594c58f1c22SToby Isaac @*/
6595d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6596d71ae5a4SJacob Faibussowitsch {
6597c58f1c22SToby Isaac   DMLabel label;
6598c58f1c22SToby Isaac 
6599c58f1c22SToby Isaac   PetscFunctionBegin;
6600c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66014f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66029566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66037a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
66049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
66053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6606c58f1c22SToby Isaac }
6607c58f1c22SToby Isaac 
6608c58f1c22SToby Isaac /*@C
6609bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6610c58f1c22SToby Isaac 
6611c58f1c22SToby Isaac   Not Collective
6612c58f1c22SToby Isaac 
6613c58f1c22SToby Isaac   Input Parameters:
6614bb7acecfSBarry Smith + dm    - The `DM` object
6615c58f1c22SToby Isaac . name  - The label name
6616c58f1c22SToby Isaac . point - The mesh point
6617c58f1c22SToby Isaac - value - The label value for this point
6618c58f1c22SToby Isaac 
6619c58f1c22SToby Isaac   Output Parameter:
6620c58f1c22SToby Isaac 
6621c58f1c22SToby Isaac   Level: beginner
6622c58f1c22SToby Isaac 
66231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6624c58f1c22SToby Isaac @*/
6625d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6626d71ae5a4SJacob Faibussowitsch {
6627c58f1c22SToby Isaac   DMLabel label;
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   PetscFunctionBegin;
6630c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66314f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66329566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6633c58f1c22SToby Isaac   if (!label) {
66349566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
66359566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6636c58f1c22SToby Isaac   }
66379566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
66383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6639c58f1c22SToby Isaac }
6640c58f1c22SToby Isaac 
6641c58f1c22SToby Isaac /*@C
6642bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6643c58f1c22SToby Isaac 
6644c58f1c22SToby Isaac   Not Collective
6645c58f1c22SToby Isaac 
6646c58f1c22SToby Isaac   Input Parameters:
6647bb7acecfSBarry Smith + dm    - The `DM` object
6648c58f1c22SToby Isaac . name  - The label name
6649c58f1c22SToby Isaac . point - The mesh point
6650c58f1c22SToby Isaac - value - The label value for this point
6651c58f1c22SToby Isaac 
6652c58f1c22SToby Isaac   Level: beginner
6653c58f1c22SToby Isaac 
66541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6655c58f1c22SToby Isaac @*/
6656d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6657d71ae5a4SJacob Faibussowitsch {
6658c58f1c22SToby Isaac   DMLabel label;
6659c58f1c22SToby Isaac 
6660c58f1c22SToby Isaac   PetscFunctionBegin;
6661c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66624f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66639566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66643ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
66659566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
66663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6667c58f1c22SToby Isaac }
6668c58f1c22SToby Isaac 
6669c58f1c22SToby Isaac /*@C
6670bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6671c58f1c22SToby Isaac 
6672c58f1c22SToby Isaac   Not Collective
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac   Input Parameters:
6675bb7acecfSBarry Smith + dm   - The `DM` object
6676c58f1c22SToby Isaac - name - The label name
6677c58f1c22SToby Isaac 
6678c58f1c22SToby Isaac   Output Parameter:
6679c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac   Level: beginner
6682c58f1c22SToby Isaac 
668360225df5SJacob Faibussowitsch   Developer Notes:
6684bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6685bb7acecfSBarry Smith 
66861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6687c58f1c22SToby Isaac @*/
6688d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6689d71ae5a4SJacob Faibussowitsch {
6690c58f1c22SToby Isaac   DMLabel label;
6691c58f1c22SToby Isaac 
6692c58f1c22SToby Isaac   PetscFunctionBegin;
6693c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66944f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66954f572ea9SToby Isaac   PetscAssertPointer(size, 3);
66969566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6697c58f1c22SToby Isaac   *size = 0;
66983ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
66999566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
67003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6701c58f1c22SToby Isaac }
6702c58f1c22SToby Isaac 
6703c58f1c22SToby Isaac /*@C
6704bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   Not Collective
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac   Input Parameters:
670960225df5SJacob Faibussowitsch + dm   - The `DM` object
6710c58f1c22SToby Isaac - name - The label name
6711c58f1c22SToby Isaac 
6712c58f1c22SToby Isaac   Output Parameter:
671320f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac   Level: beginner
6716c58f1c22SToby Isaac 
67171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6718c58f1c22SToby Isaac @*/
6719d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6720d71ae5a4SJacob Faibussowitsch {
6721c58f1c22SToby Isaac   DMLabel label;
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   PetscFunctionBegin;
6724c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67254f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67264f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
67279566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6728c58f1c22SToby Isaac   *ids = NULL;
6729dab2e251SBlaise Bourdin   if (label) {
67309566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6731dab2e251SBlaise Bourdin   } else {
6732dab2e251SBlaise Bourdin     /* returning an empty IS */
67339566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6734dab2e251SBlaise Bourdin   }
67353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6736c58f1c22SToby Isaac }
6737c58f1c22SToby Isaac 
6738c58f1c22SToby Isaac /*@C
6739c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6740c58f1c22SToby Isaac 
6741c58f1c22SToby Isaac   Not Collective
6742c58f1c22SToby Isaac 
6743c58f1c22SToby Isaac   Input Parameters:
6744bb7acecfSBarry Smith + dm    - The `DM` object
6745c58f1c22SToby Isaac . name  - The label name
6746c58f1c22SToby Isaac - value - The stratum value
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   Output Parameter:
6749bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6750c58f1c22SToby Isaac 
6751c58f1c22SToby Isaac   Level: beginner
6752c58f1c22SToby Isaac 
67531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6754c58f1c22SToby Isaac @*/
6755d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6756d71ae5a4SJacob Faibussowitsch {
6757c58f1c22SToby Isaac   DMLabel label;
6758c58f1c22SToby Isaac 
6759c58f1c22SToby Isaac   PetscFunctionBegin;
6760c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67614f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67624f572ea9SToby Isaac   PetscAssertPointer(size, 4);
67639566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6764c58f1c22SToby Isaac   *size = 0;
67653ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67669566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
67673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6768c58f1c22SToby Isaac }
6769c58f1c22SToby Isaac 
6770c58f1c22SToby Isaac /*@C
6771c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6772c58f1c22SToby Isaac 
6773c58f1c22SToby Isaac   Not Collective
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   Input Parameters:
6776bb7acecfSBarry Smith + dm    - The `DM` object
6777c58f1c22SToby Isaac . name  - The label name
6778c58f1c22SToby Isaac - value - The stratum value
6779c58f1c22SToby Isaac 
6780c58f1c22SToby Isaac   Output Parameter:
678120f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac   Level: beginner
6784c58f1c22SToby Isaac 
67851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6786c58f1c22SToby Isaac @*/
6787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6788d71ae5a4SJacob Faibussowitsch {
6789c58f1c22SToby Isaac   DMLabel label;
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac   PetscFunctionBegin;
6792c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67934f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67944f572ea9SToby Isaac   PetscAssertPointer(points, 4);
67959566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6796c58f1c22SToby Isaac   *points = NULL;
67973ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67989566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
67993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6800c58f1c22SToby Isaac }
6801c58f1c22SToby Isaac 
68024de306b1SToby Isaac /*@C
68039044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68044de306b1SToby Isaac 
68054de306b1SToby Isaac   Not Collective
68064de306b1SToby Isaac 
68074de306b1SToby Isaac   Input Parameters:
6808bb7acecfSBarry Smith + dm     - The `DM` object
68094de306b1SToby Isaac . name   - The label name
68104de306b1SToby Isaac . value  - The stratum value
68114de306b1SToby Isaac - points - The stratum points
68124de306b1SToby Isaac 
68134de306b1SToby Isaac   Level: beginner
68144de306b1SToby Isaac 
68151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
68164de306b1SToby Isaac @*/
6817d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6818d71ae5a4SJacob Faibussowitsch {
68194de306b1SToby Isaac   DMLabel label;
68204de306b1SToby Isaac 
68214de306b1SToby Isaac   PetscFunctionBegin;
68224de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68234f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6824292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
68259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68263ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68279566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
68283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
68294de306b1SToby Isaac }
68304de306b1SToby Isaac 
6831c58f1c22SToby Isaac /*@C
6832bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6833c58f1c22SToby Isaac 
6834c58f1c22SToby Isaac   Not Collective
6835c58f1c22SToby Isaac 
6836c58f1c22SToby Isaac   Input Parameters:
6837bb7acecfSBarry Smith + dm    - The `DM` object
6838c58f1c22SToby Isaac . name  - The label name
6839c58f1c22SToby Isaac - value - The label value for this point
6840c58f1c22SToby Isaac 
6841c58f1c22SToby Isaac   Output Parameter:
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   Level: beginner
6844c58f1c22SToby Isaac 
68451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6846c58f1c22SToby Isaac @*/
6847d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6848d71ae5a4SJacob Faibussowitsch {
6849c58f1c22SToby Isaac   DMLabel label;
6850c58f1c22SToby Isaac 
6851c58f1c22SToby Isaac   PetscFunctionBegin;
6852c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68534f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68549566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68553ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68569566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
68573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6858c58f1c22SToby Isaac }
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac /*@
6861bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   Not Collective
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac   Input Parameter:
6866bb7acecfSBarry Smith . dm - The `DM` object
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Output Parameter:
6869c58f1c22SToby Isaac . numLabels - the number of Labels
6870c58f1c22SToby Isaac 
6871c58f1c22SToby Isaac   Level: intermediate
6872c58f1c22SToby Isaac 
68731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6874c58f1c22SToby Isaac @*/
6875d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6876d71ae5a4SJacob Faibussowitsch {
68775d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6878c58f1c22SToby Isaac   PetscInt    n    = 0;
6879c58f1c22SToby Isaac 
6880c58f1c22SToby Isaac   PetscFunctionBegin;
6881c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68824f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
68839371c9d4SSatish Balay   while (next) {
68849371c9d4SSatish Balay     ++n;
68859371c9d4SSatish Balay     next = next->next;
68869371c9d4SSatish Balay   }
6887c58f1c22SToby Isaac   *numLabels = n;
68883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6889c58f1c22SToby Isaac }
6890c58f1c22SToby Isaac 
6891c58f1c22SToby Isaac /*@C
6892c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Not Collective
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac   Input Parameters:
6897bb7acecfSBarry Smith + dm - The `DM` object
6898c58f1c22SToby Isaac - n  - the label number
6899c58f1c22SToby Isaac 
6900c58f1c22SToby Isaac   Output Parameter:
6901c58f1c22SToby Isaac . name - the label name
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Level: intermediate
6904c58f1c22SToby Isaac 
690560225df5SJacob Faibussowitsch   Developer Notes:
6906bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6907bb7acecfSBarry Smith 
69081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6909c58f1c22SToby Isaac @*/
6910d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6911d71ae5a4SJacob Faibussowitsch {
69125d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6913c58f1c22SToby Isaac   PetscInt    l    = 0;
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   PetscFunctionBegin;
6916c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69174f572ea9SToby Isaac   PetscAssertPointer(name, 3);
6918c58f1c22SToby Isaac   while (next) {
6919c58f1c22SToby Isaac     if (l == n) {
69209566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
69213ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
6922c58f1c22SToby Isaac     }
6923c58f1c22SToby Isaac     ++l;
6924c58f1c22SToby Isaac     next = next->next;
6925c58f1c22SToby Isaac   }
692663a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6927c58f1c22SToby Isaac }
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac /*@C
6930bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6931c58f1c22SToby Isaac 
6932c58f1c22SToby Isaac   Not Collective
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac   Input Parameters:
6935bb7acecfSBarry Smith + dm   - The `DM` object
6936c58f1c22SToby Isaac - name - The label name
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac   Output Parameter:
6939bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6940c58f1c22SToby Isaac 
6941c58f1c22SToby Isaac   Level: intermediate
6942c58f1c22SToby Isaac 
69431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6944c58f1c22SToby Isaac @*/
6945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6946d71ae5a4SJacob Faibussowitsch {
69475d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6948d67d17b1SMatthew G. Knepley   const char *lname;
6949c58f1c22SToby Isaac 
6950c58f1c22SToby Isaac   PetscFunctionBegin;
6951c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69524f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69534f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
6954c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6955c58f1c22SToby Isaac   while (next) {
69569566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69579566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6958c58f1c22SToby Isaac     if (*hasLabel) break;
6959c58f1c22SToby Isaac     next = next->next;
6960c58f1c22SToby Isaac   }
69613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6962c58f1c22SToby Isaac }
6963c58f1c22SToby Isaac 
6964*a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
6965c58f1c22SToby Isaac /*@C
696620f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Not Collective
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   Input Parameters:
6971bb7acecfSBarry Smith + dm   - The `DM` object
6972c58f1c22SToby Isaac - name - The label name
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Output Parameter:
697520f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
6976c58f1c22SToby Isaac 
6977bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6978bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
6979bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
6980bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6981bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6982bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6983bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69846d7c9049SMatthew G. Knepley 
6985c58f1c22SToby Isaac   Level: intermediate
6986c58f1c22SToby Isaac 
698760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6988c58f1c22SToby Isaac @*/
6989d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6990d71ae5a4SJacob Faibussowitsch {
69915d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6992c58f1c22SToby Isaac   PetscBool   hasLabel;
6993d67d17b1SMatthew G. Knepley   const char *lname;
6994c58f1c22SToby Isaac 
6995c58f1c22SToby Isaac   PetscFunctionBegin;
6996c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69974f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69984f572ea9SToby Isaac   PetscAssertPointer(label, 3);
6999c58f1c22SToby Isaac   *label = NULL;
7000c58f1c22SToby Isaac   while (next) {
70019566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70029566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7003c58f1c22SToby Isaac     if (hasLabel) {
7004c58f1c22SToby Isaac       *label = next->label;
7005c58f1c22SToby Isaac       break;
7006c58f1c22SToby Isaac     }
7007c58f1c22SToby Isaac     next = next->next;
7008c58f1c22SToby Isaac   }
70093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7010c58f1c22SToby Isaac }
7011c58f1c22SToby Isaac 
7012c58f1c22SToby Isaac /*@C
7013bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac   Not Collective
7016c58f1c22SToby Isaac 
7017c58f1c22SToby Isaac   Input Parameters:
7018bb7acecfSBarry Smith + dm - The `DM` object
7019c58f1c22SToby Isaac - n  - the label number
7020c58f1c22SToby Isaac 
7021c58f1c22SToby Isaac   Output Parameter:
7022c58f1c22SToby Isaac . label - the label
7023c58f1c22SToby Isaac 
7024c58f1c22SToby Isaac   Level: intermediate
7025c58f1c22SToby Isaac 
70261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7027c58f1c22SToby Isaac @*/
7028d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7029d71ae5a4SJacob Faibussowitsch {
70305d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7031c58f1c22SToby Isaac   PetscInt    l    = 0;
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac   PetscFunctionBegin;
7034c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70354f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7036c58f1c22SToby Isaac   while (next) {
7037c58f1c22SToby Isaac     if (l == n) {
7038c58f1c22SToby Isaac       *label = next->label;
70393ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7040c58f1c22SToby Isaac     }
7041c58f1c22SToby Isaac     ++l;
7042c58f1c22SToby Isaac     next = next->next;
7043c58f1c22SToby Isaac   }
704463a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7045c58f1c22SToby Isaac }
7046c58f1c22SToby Isaac 
7047c58f1c22SToby Isaac /*@C
7048bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7049c58f1c22SToby Isaac 
7050c58f1c22SToby Isaac   Not Collective
7051c58f1c22SToby Isaac 
7052c58f1c22SToby Isaac   Input Parameters:
7053bb7acecfSBarry Smith + dm    - The `DM` object
7054bb7acecfSBarry Smith - label - The `DMLabel`
7055c58f1c22SToby Isaac 
7056c58f1c22SToby Isaac   Level: developer
7057c58f1c22SToby Isaac 
705860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7059c58f1c22SToby Isaac @*/
7060d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7061d71ae5a4SJacob Faibussowitsch {
70625d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7063c58f1c22SToby Isaac   PetscBool   hasLabel;
7064d67d17b1SMatthew G. Knepley   const char *lname;
70655d80c0bfSVaclav Hapla   PetscBool   flg;
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac   PetscFunctionBegin;
7068c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70699566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70709566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70717a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70729566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7073c58f1c22SToby Isaac   tmpLabel->label  = label;
7074c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70755d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70765d80c0bfSVaclav Hapla   *p = tmpLabel;
70779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70789566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70795d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70809566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7081ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
70823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7083c58f1c22SToby Isaac }
7084c58f1c22SToby Isaac 
7085*a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7086c58f1c22SToby Isaac /*@C
70874a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
70884a7ee7d0SMatthew G. Knepley 
70894a7ee7d0SMatthew G. Knepley   Not Collective
70904a7ee7d0SMatthew G. Knepley 
70914a7ee7d0SMatthew G. Knepley   Input Parameters:
7092bb7acecfSBarry Smith + dm    - The `DM` object
7093bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
70944a7ee7d0SMatthew G. Knepley 
7095bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7096bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7097bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7098bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7099bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7100bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7101bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71024a7ee7d0SMatthew G. Knepley 
71034a7ee7d0SMatthew G. Knepley   Level: intermediate
71044a7ee7d0SMatthew G. Knepley 
71051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
71064a7ee7d0SMatthew G. Knepley @*/
7107d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7108d71ae5a4SJacob Faibussowitsch {
71094a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
71104a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
71114a7ee7d0SMatthew G. Knepley   const char *name, *lname;
71124a7ee7d0SMatthew G. Knepley 
71134a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
71144a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71154a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
71169566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
71174a7ee7d0SMatthew G. Knepley   while (next) {
71189566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71199566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
71204a7ee7d0SMatthew G. Knepley     if (hasLabel) {
71219566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
71229566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
71234a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
71249566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
71254a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
71269566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
71274a7ee7d0SMatthew G. Knepley       next->label = label;
71284a7ee7d0SMatthew G. Knepley       break;
71294a7ee7d0SMatthew G. Knepley     }
71304a7ee7d0SMatthew G. Knepley     next = next->next;
71314a7ee7d0SMatthew G. Knepley   }
71323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
71334a7ee7d0SMatthew G. Knepley }
71344a7ee7d0SMatthew G. Knepley 
71354a7ee7d0SMatthew G. Knepley /*@C
7136bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7137c58f1c22SToby Isaac 
7138c58f1c22SToby Isaac   Not Collective
7139c58f1c22SToby Isaac 
7140c58f1c22SToby Isaac   Input Parameters:
7141bb7acecfSBarry Smith + dm   - The `DM` object
7142c58f1c22SToby Isaac - name - The label name
7143c58f1c22SToby Isaac 
7144c58f1c22SToby Isaac   Output Parameter:
714520f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7146bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7147c58f1c22SToby Isaac 
7148c58f1c22SToby Isaac   Level: developer
7149c58f1c22SToby Isaac 
71501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7151c58f1c22SToby Isaac @*/
7152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7153d71ae5a4SJacob Faibussowitsch {
715495d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7155c58f1c22SToby Isaac   PetscBool   hasLabel;
7156d67d17b1SMatthew G. Knepley   const char *lname;
7157c58f1c22SToby Isaac 
7158c58f1c22SToby Isaac   PetscFunctionBegin;
7159c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71604f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7161e5472504SVaclav Hapla   if (label) {
71624f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7163c58f1c22SToby Isaac     *label = NULL;
7164e5472504SVaclav Hapla   }
71655d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71669566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71679566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7168c58f1c22SToby Isaac     if (hasLabel) {
716995d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71709566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
717195d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71729566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7173ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
717495d578d6SVaclav Hapla       if (label) *label = link->label;
71759566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71769566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7177c58f1c22SToby Isaac       break;
7178c58f1c22SToby Isaac     }
7179c58f1c22SToby Isaac   }
71803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7181c58f1c22SToby Isaac }
7182c58f1c22SToby Isaac 
7183306894acSVaclav Hapla /*@
7184bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7185306894acSVaclav Hapla 
7186306894acSVaclav Hapla   Not Collective
7187306894acSVaclav Hapla 
7188306894acSVaclav Hapla   Input Parameters:
7189bb7acecfSBarry Smith + dm           - The `DM` object
7190bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
719120f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7192306894acSVaclav Hapla 
7193306894acSVaclav Hapla   Level: developer
7194306894acSVaclav Hapla 
7195bb7acecfSBarry Smith   Note:
7196306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7197bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7198306894acSVaclav Hapla   *label nullified.
7199306894acSVaclav Hapla 
72001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7201306894acSVaclav Hapla @*/
7202d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7203d71ae5a4SJacob Faibussowitsch {
720443e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7205306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7206306894acSVaclav Hapla 
7207306894acSVaclav Hapla   PetscFunctionBegin;
7208306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72094f572ea9SToby Isaac   PetscAssertPointer(label, 2);
72103ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7211306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7212306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
72135d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
721443e45a93SVaclav Hapla     if (*label == link->label) {
7215306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
721643e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7217306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7218ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
721943e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
72209566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
72219566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7222306894acSVaclav Hapla       break;
7223306894acSVaclav Hapla     }
7224306894acSVaclav Hapla   }
72257a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
72263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7227306894acSVaclav Hapla }
7228306894acSVaclav Hapla 
7229c58f1c22SToby Isaac /*@C
7230c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7231c58f1c22SToby Isaac 
7232c58f1c22SToby Isaac   Not Collective
7233c58f1c22SToby Isaac 
7234c58f1c22SToby Isaac   Input Parameters:
7235bb7acecfSBarry Smith + dm   - The `DM` object
7236c58f1c22SToby Isaac - name - The label name
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   Output Parameter:
7239c58f1c22SToby Isaac . output - The flag for output
7240c58f1c22SToby Isaac 
7241c58f1c22SToby Isaac   Level: developer
7242c58f1c22SToby Isaac 
72431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7244c58f1c22SToby Isaac @*/
7245d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7246d71ae5a4SJacob Faibussowitsch {
72475d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7248d67d17b1SMatthew G. Knepley   const char *lname;
7249c58f1c22SToby Isaac 
7250c58f1c22SToby Isaac   PetscFunctionBegin;
7251c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72524f572ea9SToby Isaac   PetscAssertPointer(name, 2);
72534f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7254c58f1c22SToby Isaac   while (next) {
7255c58f1c22SToby Isaac     PetscBool flg;
7256c58f1c22SToby Isaac 
72579566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72589566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72599371c9d4SSatish Balay     if (flg) {
72609371c9d4SSatish Balay       *output = next->output;
72613ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
72629371c9d4SSatish Balay     }
7263c58f1c22SToby Isaac     next = next->next;
7264c58f1c22SToby Isaac   }
726598921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7266c58f1c22SToby Isaac }
7267c58f1c22SToby Isaac 
7268c58f1c22SToby Isaac /*@C
7269bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7270c58f1c22SToby Isaac 
7271c58f1c22SToby Isaac   Not Collective
7272c58f1c22SToby Isaac 
7273c58f1c22SToby Isaac   Input Parameters:
7274bb7acecfSBarry Smith + dm     - The `DM` object
7275c58f1c22SToby Isaac . name   - The label name
7276bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7277c58f1c22SToby Isaac 
7278c58f1c22SToby Isaac   Level: developer
7279c58f1c22SToby Isaac 
72801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7281c58f1c22SToby Isaac @*/
7282d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7283d71ae5a4SJacob Faibussowitsch {
72845d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7285d67d17b1SMatthew G. Knepley   const char *lname;
7286c58f1c22SToby Isaac 
7287c58f1c22SToby Isaac   PetscFunctionBegin;
7288c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72894f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7290c58f1c22SToby Isaac   while (next) {
7291c58f1c22SToby Isaac     PetscBool flg;
7292c58f1c22SToby Isaac 
72939566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72949566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72959371c9d4SSatish Balay     if (flg) {
72969371c9d4SSatish Balay       next->output = output;
72973ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
72989371c9d4SSatish Balay     }
7299c58f1c22SToby Isaac     next = next->next;
7300c58f1c22SToby Isaac   }
730198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7302c58f1c22SToby Isaac }
7303c58f1c22SToby Isaac 
7304c58f1c22SToby Isaac /*@
7305bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7306c58f1c22SToby Isaac 
730720f4b53cSBarry Smith   Collective
7308c58f1c22SToby Isaac 
7309d8d19677SJose E. Roman   Input Parameters:
7310bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7311bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7312bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7313bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7314bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac   Level: intermediate
7317c58f1c22SToby Isaac 
7318bb7acecfSBarry Smith   Note:
73192cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7320c58f1c22SToby Isaac 
73211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7322c58f1c22SToby Isaac @*/
7323d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7324d71ae5a4SJacob Faibussowitsch {
73252cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7326c58f1c22SToby Isaac   const char *name;
7327c58f1c22SToby Isaac   PetscBool   flg;
73285d80c0bfSVaclav Hapla   DMLabelLink link;
7329c58f1c22SToby Isaac 
73305d80c0bfSVaclav Hapla   PetscFunctionBegin;
73315d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73325d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73335d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
73345d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73357a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73363ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
73375d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
73385d80c0bfSVaclav Hapla     label = link->label;
73399566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
73405d80c0bfSVaclav Hapla     if (!all) {
73419566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7342c58f1c22SToby Isaac       if (flg) continue;
73439566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
73447d5acc75SStefano Zampini       if (flg) continue;
73459566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7346ba2698f1SMatthew G. Knepley       if (flg) continue;
73475d80c0bfSVaclav Hapla     }
73489566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
73492cbb9b06SVaclav Hapla     if (labelOld) {
73502cbb9b06SVaclav Hapla       switch (emode) {
7351d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7352d71ae5a4SJacob Faibussowitsch         continue;
7353d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7354d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7355d71ae5a4SJacob Faibussowitsch         break;
7356d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7357d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7358d71ae5a4SJacob Faibussowitsch       default:
7359d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73602cbb9b06SVaclav Hapla       }
73612cbb9b06SVaclav Hapla     }
73625d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73639566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73645d80c0bfSVaclav Hapla     } else {
73655d80c0bfSVaclav Hapla       labelNew = label;
73665d80c0bfSVaclav Hapla     }
73679566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73689566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7369c58f1c22SToby Isaac   }
73703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7371c58f1c22SToby Isaac }
7372461a15a0SLisandro Dalcin 
7373609dae6eSVaclav Hapla /*@C
7374b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7375609dae6eSVaclav Hapla 
737620f4b53cSBarry Smith   Collective; No Fortran Support
7377609dae6eSVaclav Hapla 
7378609dae6eSVaclav Hapla   Input Parameters:
7379bb7acecfSBarry Smith + dm0 - First `DM` object
7380bb7acecfSBarry Smith - dm1 - Second `DM` object
7381609dae6eSVaclav Hapla 
7382*a4e35b19SJacob Faibussowitsch   Output Parameters:
73835efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
738420f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7385609dae6eSVaclav Hapla 
7386609dae6eSVaclav Hapla   Level: intermediate
7387609dae6eSVaclav Hapla 
7388609dae6eSVaclav Hapla   Notes:
7389bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7390bb7acecfSBarry Smith 
739120f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7392bb7acecfSBarry Smith 
739320f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7394609dae6eSVaclav Hapla 
73955efe38ccSVaclav Hapla   The output message is set independently on each rank.
7396bb7acecfSBarry Smith 
7397bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7398bb7acecfSBarry Smith 
739920f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7400bb7acecfSBarry Smith 
740120f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7402609dae6eSVaclav Hapla 
7403609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7404bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7405609dae6eSVaclav Hapla 
74061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7407609dae6eSVaclav Hapla @*/
7408d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7409d71ae5a4SJacob Faibussowitsch {
74105efe38ccSVaclav Hapla   PetscInt    n, i;
7411609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
74125efe38ccSVaclav Hapla   PetscBool   eq;
7413609dae6eSVaclav Hapla   MPI_Comm    comm;
74145efe38ccSVaclav Hapla   PetscMPIInt rank;
7415609dae6eSVaclav Hapla 
7416609dae6eSVaclav Hapla   PetscFunctionBegin;
7417609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7418609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7419609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
74204f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
74214f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
74229566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
74239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
74245efe38ccSVaclav Hapla   {
74255efe38ccSVaclav Hapla     PetscInt n1;
74265efe38ccSVaclav Hapla 
74279566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
74289566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
74295efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
743048a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7431712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
74325efe38ccSVaclav Hapla     if (!eq) goto finish;
74335efe38ccSVaclav Hapla   }
74345efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7435609dae6eSVaclav Hapla     DMLabel     l0, l1;
7436609dae6eSVaclav Hapla     const char *name;
7437609dae6eSVaclav Hapla     char       *msgInner;
7438609dae6eSVaclav Hapla 
7439609dae6eSVaclav Hapla     /* Ignore label order */
74409566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
74419566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
74429566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7443609dae6eSVaclav Hapla     if (!l1) {
744463a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
74455efe38ccSVaclav Hapla       eq = PETSC_FALSE;
74465efe38ccSVaclav Hapla       break;
7447609dae6eSVaclav Hapla     }
74489566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
74499566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
74509566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74515efe38ccSVaclav Hapla     if (!eq) break;
7452609dae6eSVaclav Hapla   }
7453712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7454609dae6eSVaclav Hapla finish:
74555efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7456609dae6eSVaclav Hapla   if (message) {
7457609dae6eSVaclav Hapla     *message = NULL;
745848a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74595efe38ccSVaclav Hapla   } else {
746048a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74619566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74625efe38ccSVaclav Hapla   }
74635efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74645efe38ccSVaclav Hapla   if (equal) *equal = eq;
74657a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
74663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7467609dae6eSVaclav Hapla }
7468609dae6eSVaclav Hapla 
7469d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7470d71ae5a4SJacob Faibussowitsch {
7471461a15a0SLisandro Dalcin   PetscFunctionBegin;
74724f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7473461a15a0SLisandro Dalcin   if (!*label) {
74749566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74759566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7476461a15a0SLisandro Dalcin   }
74779566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
74783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7479461a15a0SLisandro Dalcin }
7480461a15a0SLisandro Dalcin 
74810fdc7489SMatthew Knepley /*
74820fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74830fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74840fdc7489SMatthew Knepley   (label, id) pair in the DM.
74850fdc7489SMatthew Knepley 
74860fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
74870fdc7489SMatthew Knepley   each label.
74880fdc7489SMatthew Knepley */
7489d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7490d71ae5a4SJacob Faibussowitsch {
74910fdc7489SMatthew Knepley   DMUniversalLabel ul;
74920fdc7489SMatthew Knepley   PetscBool       *active;
74930fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
74940fdc7489SMatthew Knepley 
74950fdc7489SMatthew Knepley   PetscFunctionBegin;
74969566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
74979566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
74989566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
74999566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
75000fdc7489SMatthew Knepley   ul->Nl = 0;
75010fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75020fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
75030fdc7489SMatthew Knepley     const char *name;
75040fdc7489SMatthew Knepley 
75059566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75069566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
75079566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
75080fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
75090fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
75100fdc7489SMatthew Knepley   }
75119566063dSJacob 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));
75120fdc7489SMatthew Knepley   ul->Nv = 0;
75130fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75140fdc7489SMatthew Knepley     DMLabel     label;
75150fdc7489SMatthew Knepley     PetscInt    nv;
75160fdc7489SMatthew Knepley     const char *name;
75170fdc7489SMatthew Knepley 
75180fdc7489SMatthew Knepley     if (!active[l]) continue;
75199566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75209566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75219566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75229566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
75230fdc7489SMatthew Knepley     ul->indices[m] = l;
75240fdc7489SMatthew Knepley     ul->Nv += nv;
75250fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
75260fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
75270fdc7489SMatthew Knepley     ++m;
75280fdc7489SMatthew Knepley   }
75290fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
75300fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
75310fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
75320fdc7489SMatthew Knepley   }
75330fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
75340fdc7489SMatthew Knepley     PetscInt b;
75350fdc7489SMatthew Knepley 
75360fdc7489SMatthew Knepley     ul->masks[l] = 0;
75370fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
75380fdc7489SMatthew Knepley   }
75399566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
75400fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75410fdc7489SMatthew Knepley     DMLabel         label;
75420fdc7489SMatthew Knepley     IS              valueIS;
75430fdc7489SMatthew Knepley     const PetscInt *varr;
75440fdc7489SMatthew Knepley     PetscInt        nv, v;
75450fdc7489SMatthew Knepley 
75460fdc7489SMatthew Knepley     if (!active[l]) continue;
75479566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75489566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75499566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
75509566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7551ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75529566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75549566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75550fdc7489SMatthew Knepley     ++m;
75560fdc7489SMatthew Knepley   }
75579566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75580fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75590fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75600fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75610fdc7489SMatthew Knepley 
75620fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75630fdc7489SMatthew Knepley       DMLabel  label;
75640649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75650fdc7489SMatthew Knepley 
75660fdc7489SMatthew Knepley       if (!active[l]) continue;
75679566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75689566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75699566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75709371c9d4SSatish Balay       if (val == defval) {
75719371c9d4SSatish Balay         ++m;
75729371c9d4SSatish Balay         continue;
75739371c9d4SSatish Balay       }
75740649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75750fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75769566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
757763a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75780fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75790fdc7489SMatthew Knepley       ++m;
75800fdc7489SMatthew Knepley     }
75819566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75820fdc7489SMatthew Knepley   }
75839566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75840fdc7489SMatthew Knepley   *universal = ul;
75853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75860fdc7489SMatthew Knepley }
75870fdc7489SMatthew Knepley 
7588d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7589d71ae5a4SJacob Faibussowitsch {
75900fdc7489SMatthew Knepley   PetscInt l;
75910fdc7489SMatthew Knepley 
75920fdc7489SMatthew Knepley   PetscFunctionBegin;
75939566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
75949566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
75959566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
75969566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
75979566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
75980fdc7489SMatthew Knepley   *universal = NULL;
75993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76000fdc7489SMatthew Knepley }
76010fdc7489SMatthew Knepley 
7602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7603d71ae5a4SJacob Faibussowitsch {
76040fdc7489SMatthew Knepley   PetscFunctionBegin;
76054f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
76060fdc7489SMatthew Knepley   *ulabel = ul->label;
76073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76080fdc7489SMatthew Knepley }
76090fdc7489SMatthew Knepley 
7610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7611d71ae5a4SJacob Faibussowitsch {
76120fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
76130fdc7489SMatthew Knepley 
76140fdc7489SMatthew Knepley   PetscFunctionBegin;
7615064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
76160fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76179566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
76189566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
76190fdc7489SMatthew Knepley   }
76200fdc7489SMatthew Knepley   if (preserveOrder) {
76210fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
76220fdc7489SMatthew Knepley       const char *name;
76230fdc7489SMatthew Knepley       PetscBool   match;
76240fdc7489SMatthew Knepley 
76259566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
76269566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
762763a3b9bcSJacob 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]);
76280fdc7489SMatthew Knepley     }
76290fdc7489SMatthew Knepley   }
76303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76310fdc7489SMatthew Knepley }
76320fdc7489SMatthew Knepley 
7633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7634d71ae5a4SJacob Faibussowitsch {
76350fdc7489SMatthew Knepley   PetscInt l;
76360fdc7489SMatthew Knepley 
76370fdc7489SMatthew Knepley   PetscFunctionBegin;
76380fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76390fdc7489SMatthew Knepley     DMLabel  label;
76400fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
76410fdc7489SMatthew Knepley 
76420fdc7489SMatthew Knepley     if (lval) {
76439566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
76449566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
76459566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
76460fdc7489SMatthew Knepley     }
76470fdc7489SMatthew Knepley   }
76483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76490fdc7489SMatthew Knepley }
7650a8fb8f29SToby Isaac 
7651a8fb8f29SToby Isaac /*@
7652bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7653bb7acecfSBarry Smith 
765420f4b53cSBarry Smith   Not Collective
7655a8fb8f29SToby Isaac 
7656a8fb8f29SToby Isaac   Input Parameter:
7657bb7acecfSBarry Smith . dm - The `DM` object
7658a8fb8f29SToby Isaac 
7659a8fb8f29SToby Isaac   Output Parameter:
7660bb7acecfSBarry Smith . cdm - The coarse `DM`
7661a8fb8f29SToby Isaac 
7662a8fb8f29SToby Isaac   Level: intermediate
7663a8fb8f29SToby Isaac 
76641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7665a8fb8f29SToby Isaac @*/
7666d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7667d71ae5a4SJacob Faibussowitsch {
7668a8fb8f29SToby Isaac   PetscFunctionBegin;
7669a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76704f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7671a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
76723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7673a8fb8f29SToby Isaac }
7674a8fb8f29SToby Isaac 
7675a8fb8f29SToby Isaac /*@
7676bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7677a8fb8f29SToby Isaac 
7678a8fb8f29SToby Isaac   Input Parameters:
7679bb7acecfSBarry Smith + dm  - The `DM` object
7680bb7acecfSBarry Smith - cdm - The coarse `DM`
7681a8fb8f29SToby Isaac 
7682a8fb8f29SToby Isaac   Level: intermediate
7683a8fb8f29SToby Isaac 
7684bb7acecfSBarry Smith   Note:
7685bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7686bb7acecfSBarry Smith 
76871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7688a8fb8f29SToby Isaac @*/
7689d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7690d71ae5a4SJacob Faibussowitsch {
7691a8fb8f29SToby Isaac   PetscFunctionBegin;
7692a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7693a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
769489d734beSBarry Smith   if (dm == cdm) cdm = NULL;
76959566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
76969566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7697a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
76983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7699a8fb8f29SToby Isaac }
7700a8fb8f29SToby Isaac 
770188bdff64SToby Isaac /*@
7702bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
770388bdff64SToby Isaac 
770488bdff64SToby Isaac   Input Parameter:
7705bb7acecfSBarry Smith . dm - The `DM` object
770688bdff64SToby Isaac 
770788bdff64SToby Isaac   Output Parameter:
7708bb7acecfSBarry Smith . fdm - The fine `DM`
770988bdff64SToby Isaac 
771088bdff64SToby Isaac   Level: intermediate
771188bdff64SToby Isaac 
77121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
771388bdff64SToby Isaac @*/
7714d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7715d71ae5a4SJacob Faibussowitsch {
771688bdff64SToby Isaac   PetscFunctionBegin;
771788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77184f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
771988bdff64SToby Isaac   *fdm = dm->fineMesh;
77203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
772188bdff64SToby Isaac }
772288bdff64SToby Isaac 
772388bdff64SToby Isaac /*@
7724bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
772588bdff64SToby Isaac 
772688bdff64SToby Isaac   Input Parameters:
7727bb7acecfSBarry Smith + dm  - The `DM` object
7728bb7acecfSBarry Smith - fdm - The fine `DM`
772988bdff64SToby Isaac 
7730bb7acecfSBarry Smith   Level: developer
773188bdff64SToby Isaac 
7732bb7acecfSBarry Smith   Note:
7733bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7734bb7acecfSBarry Smith 
77351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
773688bdff64SToby Isaac @*/
7737d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7738d71ae5a4SJacob Faibussowitsch {
773988bdff64SToby Isaac   PetscFunctionBegin;
774088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
774188bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
774289d734beSBarry Smith   if (dm == fdm) fdm = NULL;
77439566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
77449566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
774588bdff64SToby Isaac   dm->fineMesh = fdm;
77463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
774788bdff64SToby Isaac }
774888bdff64SToby Isaac 
7749a6ba4734SToby Isaac /*@C
7750bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7751a6ba4734SToby Isaac 
775220f4b53cSBarry Smith   Collective
7753783e2ec8SMatthew G. Knepley 
7754a6ba4734SToby Isaac   Input Parameters:
7755bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7756bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7757a6ba4734SToby Isaac . name     - The BC name
775845480ffeSMatthew G. Knepley . label    - The label defining constrained points
7759bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
776045480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7761a6ba4734SToby Isaac . field    - The field to constrain
776245480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7763a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7764a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
776556cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7766a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7767a6ba4734SToby Isaac 
776845480ffeSMatthew G. Knepley   Output Parameter:
776945480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
777045480ffeSMatthew G. Knepley 
7771a6ba4734SToby Isaac   Options Database Keys:
7772a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7773a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7774a6ba4734SToby Isaac 
777520f4b53cSBarry Smith   Level: intermediate
777620f4b53cSBarry Smith 
7777bb7acecfSBarry Smith   Notes:
777837fdd005SBarry 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\:
777956cf3b9cSMatthew G. Knepley 
778020f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
778156cf3b9cSMatthew G. Knepley 
7782*a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
778356cf3b9cSMatthew G. Knepley 
778420f4b53cSBarry Smith .vb
778520f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
778620f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
778720f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
778820f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
778920f4b53cSBarry Smith .ve
779056cf3b9cSMatthew G. Knepley + dim - the spatial dimension
779156cf3b9cSMatthew G. Knepley . Nf - the number of fields
779256cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
779356cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
779456cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
779556cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
779656cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
779756cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
779856cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
779956cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
780056cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
780156cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
780256cf3b9cSMatthew G. Knepley . t - current time
780356cf3b9cSMatthew G. Knepley . x - coordinates of the current point
780456cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
780556cf3b9cSMatthew G. Knepley . constants - constant parameters
780656cf3b9cSMatthew G. Knepley - bcval - output values at the current point
780756cf3b9cSMatthew G. Knepley 
78081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7809a6ba4734SToby Isaac @*/
7810d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], DMLabel label, PetscInt Nv, const PetscInt values[], PetscInt field, PetscInt Nc, const PetscInt comps[], void (*bcFunc)(void), void (*bcFunc_t)(void), void *ctx, PetscInt *bd)
7811d71ae5a4SJacob Faibussowitsch {
7812e5e52638SMatthew G. Knepley   PetscDS ds;
7813a6ba4734SToby Isaac 
7814a6ba4734SToby Isaac   PetscFunctionBegin;
7815a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7816783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
781745480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
781845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
781945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
782045480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
782101a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
78229566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7823799db056SMatthew G. Knepley   /* Complete label */
7824799db056SMatthew G. Knepley   if (label) {
7825799db056SMatthew G. Knepley     PetscObject  obj;
7826799db056SMatthew G. Knepley     PetscClassId id;
7827799db056SMatthew G. Knepley 
7828799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7829799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7830799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7831799db056SMatthew G. Knepley       DM plex;
7832799db056SMatthew G. Knepley 
7833799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7834799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7835799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7836799db056SMatthew G. Knepley     }
7837799db056SMatthew G. Knepley   }
78389566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
78393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7840a6ba4734SToby Isaac }
7841a6ba4734SToby Isaac 
784245480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7843d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7844d71ae5a4SJacob Faibussowitsch {
7845e5e52638SMatthew G. Knepley   PetscDS     ds;
7846dff059c6SToby Isaac   DMBoundary *lastnext;
7847e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7848e6f8dbb6SToby Isaac 
7849e6f8dbb6SToby Isaac   PetscFunctionBegin;
78509566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7851e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
785247a1f5adSToby Isaac   if (dm->boundary) {
785347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
785447a1f5adSToby Isaac 
785547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
78563ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
785747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
785847a1f5adSToby Isaac     while (next) {
785947a1f5adSToby Isaac       DMBoundary b = next;
786047a1f5adSToby Isaac 
786147a1f5adSToby Isaac       next = b->next;
78629566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7863a6ba4734SToby Isaac     }
786447a1f5adSToby Isaac     dm->boundary = NULL;
7865a6ba4734SToby Isaac   }
786647a1f5adSToby Isaac 
7867dff059c6SToby Isaac   lastnext = &(dm->boundary);
7868e6f8dbb6SToby Isaac   while (dsbound) {
7869e6f8dbb6SToby Isaac     DMBoundary dmbound;
7870e6f8dbb6SToby Isaac 
78719566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7872e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
787345480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
787447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7875dff059c6SToby Isaac     *lastnext = dmbound;
7876dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7877dff059c6SToby Isaac     dsbound   = dsbound->next;
7878a6ba4734SToby Isaac   }
78793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7880a6ba4734SToby Isaac }
7881a6ba4734SToby Isaac 
7882bb7acecfSBarry Smith /* TODO: missing manual page */
7883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7884d71ae5a4SJacob Faibussowitsch {
7885b95f2879SToby Isaac   DMBoundary b;
7886a6ba4734SToby Isaac 
7887a6ba4734SToby Isaac   PetscFunctionBegin;
7888a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78894f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
7890a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
78919566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7892b95f2879SToby Isaac   b = dm->boundary;
7893a6ba4734SToby Isaac   while (b && !(*isBd)) {
7894e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7895e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7896a6ba4734SToby Isaac     PetscInt   i;
7897a6ba4734SToby Isaac 
789845480ffeSMatthew G. Knepley     if (label) {
78999566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7900a6ba4734SToby Isaac     }
7901a6ba4734SToby Isaac     b = b->next;
7902a6ba4734SToby Isaac   }
79033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7904a6ba4734SToby Isaac }
79054d6f44ffSToby Isaac 
79064d6f44ffSToby Isaac /*@C
7907bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7908a6e0b375SMatthew G. Knepley 
790920f4b53cSBarry Smith   Collective
79104d6f44ffSToby Isaac 
79114d6f44ffSToby Isaac   Input Parameters:
7912bb7acecfSBarry Smith + dm    - The `DM`
79130709b2feSToby Isaac . time  - The time
79144d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
79154d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79164d6f44ffSToby Isaac - mode  - The insertion mode for values
79174d6f44ffSToby Isaac 
79184d6f44ffSToby Isaac   Output Parameter:
79194d6f44ffSToby Isaac . X - vector
79204d6f44ffSToby Isaac 
792120f4b53cSBarry Smith   Calling sequence of `funcs`:
79224d6f44ffSToby Isaac + dim  - The spatial dimension
79238ec8862eSJed Brown . time - The time at which to sample
79244d6f44ffSToby Isaac . x    - The coordinates
792577b739a6SMatthew Knepley . Nc   - The number of components
79264d6f44ffSToby Isaac . u    - The output field values
79274d6f44ffSToby Isaac - ctx  - optional user-defined function context
79284d6f44ffSToby Isaac 
79294d6f44ffSToby Isaac   Level: developer
79304d6f44ffSToby Isaac 
7931bb7acecfSBarry Smith   Developer Notes:
7932bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7933bb7acecfSBarry Smith 
7934bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7935bb7acecfSBarry Smith 
79361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
79374d6f44ffSToby Isaac @*/
7938*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
7939d71ae5a4SJacob Faibussowitsch {
79404d6f44ffSToby Isaac   Vec localX;
79414d6f44ffSToby Isaac 
79424d6f44ffSToby Isaac   PetscFunctionBegin;
79434d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7944708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
79459566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7946f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79479566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
79489566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79499566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79509566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
7951708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
79523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79534d6f44ffSToby Isaac }
79544d6f44ffSToby Isaac 
7955a6e0b375SMatthew G. Knepley /*@C
7956bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7957a6e0b375SMatthew G. Knepley 
795820f4b53cSBarry Smith   Not Collective
7959a6e0b375SMatthew G. Knepley 
7960a6e0b375SMatthew G. Knepley   Input Parameters:
7961bb7acecfSBarry Smith + dm    - The `DM`
7962a6e0b375SMatthew G. Knepley . time  - The time
7963a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
7964a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7965a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
7966a6e0b375SMatthew G. Knepley 
7967a6e0b375SMatthew G. Knepley   Output Parameter:
7968a6e0b375SMatthew G. Knepley . localX - vector
7969a6e0b375SMatthew G. Knepley 
797020f4b53cSBarry Smith   Calling sequence of `funcs`:
7971a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
7972*a4e35b19SJacob Faibussowitsch . time - The current timestep
7973a6e0b375SMatthew G. Knepley . x    - The coordinates
797477b739a6SMatthew Knepley . Nc   - The number of components
7975a6e0b375SMatthew G. Knepley . u    - The output field values
7976a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
7977a6e0b375SMatthew G. Knepley 
7978a6e0b375SMatthew G. Knepley   Level: developer
7979a6e0b375SMatthew G. Knepley 
7980bb7acecfSBarry Smith   Developer Notes:
7981bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7982bb7acecfSBarry Smith 
7983bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7984bb7acecfSBarry Smith 
79851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7986a6e0b375SMatthew G. Knepley @*/
7987*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
7988d71ae5a4SJacob Faibussowitsch {
79894d6f44ffSToby Isaac   PetscFunctionBegin;
79904d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7991064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
79929566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
79933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79944d6f44ffSToby Isaac }
79954d6f44ffSToby Isaac 
7996a6e0b375SMatthew G. Knepley /*@C
7997bb7acecfSBarry 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.
7998a6e0b375SMatthew G. Knepley 
799920f4b53cSBarry Smith   Collective
8000a6e0b375SMatthew G. Knepley 
8001a6e0b375SMatthew G. Knepley   Input Parameters:
8002bb7acecfSBarry Smith + dm     - The `DM`
8003a6e0b375SMatthew G. Knepley . time   - The time
8004*a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8005*a4e35b19SJacob Faibussowitsch . ids    - The ids
8006*a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8007*a4e35b19SJacob Faibussowitsch . comps  - The components
8008bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8009a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8010bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8011a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8012a6e0b375SMatthew G. Knepley 
8013a6e0b375SMatthew G. Knepley   Output Parameter:
8014a6e0b375SMatthew G. Knepley . X - vector
8015a6e0b375SMatthew G. Knepley 
801620f4b53cSBarry Smith   Calling sequence of `funcs`:
8017a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8018*a4e35b19SJacob Faibussowitsch . time - The current timestep
8019a6e0b375SMatthew G. Knepley . x    - The coordinates
802077b739a6SMatthew Knepley . Nc   - The number of components
8021a6e0b375SMatthew G. Knepley . u    - The output field values
8022a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8023a6e0b375SMatthew G. Knepley 
8024a6e0b375SMatthew G. Knepley   Level: developer
8025a6e0b375SMatthew G. Knepley 
8026bb7acecfSBarry Smith   Developer Notes:
8027bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8028bb7acecfSBarry Smith 
8029bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8030bb7acecfSBarry Smith 
80311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8032a6e0b375SMatthew G. Knepley @*/
8033*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
8034d71ae5a4SJacob Faibussowitsch {
80352c53366bSMatthew G. Knepley   Vec localX;
80362c53366bSMatthew G. Knepley 
80372c53366bSMatthew G. Knepley   PetscFunctionBegin;
80382c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80399566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8040f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80419566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80429566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80439566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80449566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
80453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80462c53366bSMatthew G. Knepley }
80472c53366bSMatthew G. Knepley 
8048a6e0b375SMatthew G. Knepley /*@C
8049bb7acecfSBarry 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.
8050a6e0b375SMatthew G. Knepley 
805120f4b53cSBarry Smith   Not Collective
8052a6e0b375SMatthew G. Knepley 
8053a6e0b375SMatthew G. Knepley   Input Parameters:
8054bb7acecfSBarry Smith + dm     - The `DM`
8055a6e0b375SMatthew G. Knepley . time   - The time
8056bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8057*a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8058*a4e35b19SJacob Faibussowitsch . ids    - The ids
8059*a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8060*a4e35b19SJacob Faibussowitsch . comps  - The components
8061a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8062a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8063a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8064a6e0b375SMatthew G. Knepley 
8065a6e0b375SMatthew G. Knepley   Output Parameter:
8066a6e0b375SMatthew G. Knepley . localX - vector
8067a6e0b375SMatthew G. Knepley 
806820f4b53cSBarry Smith   Calling sequence of `funcs`:
8069a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8070*a4e35b19SJacob Faibussowitsch . time - The current time
8071a6e0b375SMatthew G. Knepley . x    - The coordinates
807277b739a6SMatthew Knepley . Nc   - The number of components
8073a6e0b375SMatthew G. Knepley . u    - The output field values
8074a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8075a6e0b375SMatthew G. Knepley 
8076a6e0b375SMatthew G. Knepley   Level: developer
8077a6e0b375SMatthew G. Knepley 
8078bb7acecfSBarry Smith   Developer Notes:
8079bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8080bb7acecfSBarry Smith 
8081bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8082bb7acecfSBarry Smith 
80831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8084a6e0b375SMatthew G. Knepley @*/
8085*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
8086d71ae5a4SJacob Faibussowitsch {
80874d6f44ffSToby Isaac   PetscFunctionBegin;
80884d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8089064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
80909566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80924d6f44ffSToby Isaac }
80932716604bSToby Isaac 
8094a6e0b375SMatthew G. Knepley /*@C
8095bb7acecfSBarry 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.
8096a6e0b375SMatthew G. Knepley 
809720f4b53cSBarry Smith   Not Collective
8098a6e0b375SMatthew G. Knepley 
8099a6e0b375SMatthew G. Knepley   Input Parameters:
8100bb7acecfSBarry Smith + dm     - The `DM`
8101a6e0b375SMatthew G. Knepley . time   - The time
810220f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8103a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8104a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8105a6e0b375SMatthew G. Knepley 
8106a6e0b375SMatthew G. Knepley   Output Parameter:
8107a6e0b375SMatthew G. Knepley . localX - The output vector
8108a6e0b375SMatthew G. Knepley 
810920f4b53cSBarry Smith   Calling sequence of `funcs`:
8110a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8111a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8112a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8113a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8114a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8115a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8116a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8117a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8118a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8119a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8120a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8121a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8122a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8123a6e0b375SMatthew G. Knepley . t            - The current time
8124a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8125a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8126a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8127a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8128a6e0b375SMatthew G. Knepley 
8129bb7acecfSBarry Smith   Note:
8130bb7acecfSBarry 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.
8131bb7acecfSBarry 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
8132bb7acecfSBarry 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
8133a6e0b375SMatthew 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.
8134a6e0b375SMatthew G. Knepley 
8135a6e0b375SMatthew G. Knepley   Level: intermediate
8136a6e0b375SMatthew G. Knepley 
8137bb7acecfSBarry Smith   Developer Notes:
8138bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8139bb7acecfSBarry Smith 
8140bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8141bb7acecfSBarry Smith 
8142*a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8143*a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8144a6e0b375SMatthew G. Knepley @*/
8145*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8146d71ae5a4SJacob Faibussowitsch {
81478c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81488c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8149eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
81508c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
81519566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
81523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81538c6c5593SMatthew G. Knepley }
81548c6c5593SMatthew G. Knepley 
8155a6e0b375SMatthew G. Knepley /*@C
8156a6e0b375SMatthew 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.
8157a6e0b375SMatthew G. Knepley 
815820f4b53cSBarry Smith   Not Collective
8159a6e0b375SMatthew G. Knepley 
8160a6e0b375SMatthew G. Knepley   Input Parameters:
8161bb7acecfSBarry Smith + dm     - The `DM`
8162a6e0b375SMatthew G. Knepley . time   - The time
8163bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8164a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8165a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8166bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
816720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8168a6e0b375SMatthew G. Knepley . localU - The input field vector
8169a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8170a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8171a6e0b375SMatthew G. Knepley 
8172a6e0b375SMatthew G. Knepley   Output Parameter:
8173a6e0b375SMatthew G. Knepley . localX - The output vector
8174a6e0b375SMatthew G. Knepley 
817520f4b53cSBarry Smith   Calling sequence of `funcs`:
8176a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8177a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8178a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8179a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8180a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8181a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8182a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8183a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8184a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8185a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8186a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8187a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8188a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8189a6e0b375SMatthew G. Knepley . t            - The current time
8190a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8191a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8192a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8193a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8194a6e0b375SMatthew G. Knepley 
8195bb7acecfSBarry Smith   Note:
8196bb7acecfSBarry 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.
8197bb7acecfSBarry 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
8198bb7acecfSBarry 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
8199a6e0b375SMatthew 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.
8200a6e0b375SMatthew G. Knepley 
8201a6e0b375SMatthew G. Knepley   Level: intermediate
8202a6e0b375SMatthew G. Knepley 
8203bb7acecfSBarry Smith   Developer Notes:
8204bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8205bb7acecfSBarry Smith 
8206bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8207bb7acecfSBarry Smith 
82081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8209a6e0b375SMatthew G. Knepley @*/
8210*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8211d71ae5a4SJacob Faibussowitsch {
82128c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82138c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8214064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8215064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
82169566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
82173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82188c6c5593SMatthew G. Knepley }
82198c6c5593SMatthew G. Knepley 
82202716604bSToby Isaac /*@C
8221d29d7c6eSMatthew 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.
8222d29d7c6eSMatthew G. Knepley 
822320f4b53cSBarry Smith   Not Collective
8224d29d7c6eSMatthew G. Knepley 
8225d29d7c6eSMatthew G. Knepley   Input Parameters:
8226bb7acecfSBarry Smith + dm     - The `DM`
8227d29d7c6eSMatthew G. Knepley . time   - The time
8228bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8229d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8230d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8231bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
823220f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8233d29d7c6eSMatthew G. Knepley . U      - The input field vector
8234d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8235d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8236d29d7c6eSMatthew G. Knepley 
8237d29d7c6eSMatthew G. Knepley   Output Parameter:
8238d29d7c6eSMatthew G. Knepley . X - The output vector
8239d29d7c6eSMatthew G. Knepley 
824020f4b53cSBarry Smith   Calling sequence of `funcs`:
8241d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8242d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8243d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8244d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8245d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8246d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8247d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8248d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8249d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8250d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8251d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8252d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8253d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8254d29d7c6eSMatthew G. Knepley . t            - The current time
8255d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8256d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8257d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8258d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8259d29d7c6eSMatthew G. Knepley 
8260bb7acecfSBarry Smith   Note:
8261bb7acecfSBarry 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.
8262bb7acecfSBarry 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
8263bb7acecfSBarry 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
8264d29d7c6eSMatthew 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.
8265d29d7c6eSMatthew G. Knepley 
8266d29d7c6eSMatthew G. Knepley   Level: intermediate
8267d29d7c6eSMatthew G. Knepley 
8268bb7acecfSBarry Smith   Developer Notes:
8269bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8270bb7acecfSBarry Smith 
8271bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8272bb7acecfSBarry Smith 
82731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8274d29d7c6eSMatthew G. Knepley @*/
8275*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec X)
8276d71ae5a4SJacob Faibussowitsch {
8277d29d7c6eSMatthew G. Knepley   DM  dmIn;
8278d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8279d29d7c6eSMatthew G. Knepley 
8280d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8281d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8282d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8283d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8284d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8285f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
828672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
828772fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8288d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8289d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8290d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8291d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8292d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
82933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8294d29d7c6eSMatthew G. Knepley }
8295d29d7c6eSMatthew G. Knepley 
8296d29d7c6eSMatthew G. Knepley /*@C
8297ece3a9fcSMatthew 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.
8298ece3a9fcSMatthew G. Knepley 
829920f4b53cSBarry Smith   Not Collective
8300ece3a9fcSMatthew G. Knepley 
8301ece3a9fcSMatthew G. Knepley   Input Parameters:
8302bb7acecfSBarry Smith + dm     - The `DM`
8303ece3a9fcSMatthew G. Knepley . time   - The time
8304bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8305ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8306ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8307bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
830820f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8309ece3a9fcSMatthew G. Knepley . localU - The input field vector
8310ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8311ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8312ece3a9fcSMatthew G. Knepley 
8313ece3a9fcSMatthew G. Knepley   Output Parameter:
8314ece3a9fcSMatthew G. Knepley . localX - The output vector
8315ece3a9fcSMatthew G. Knepley 
831620f4b53cSBarry Smith   Calling sequence of `funcs`:
8317ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8318ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8319ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8320ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8321ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8322ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8323ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8324ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8325ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8326ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8327ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8328ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8329ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8330ece3a9fcSMatthew G. Knepley . t            - The current time
8331ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8332ece3a9fcSMatthew G. Knepley . n            - The face normal
8333ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8334ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8335ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8336ece3a9fcSMatthew G. Knepley 
8337ece3a9fcSMatthew G. Knepley   Note:
8338bb7acecfSBarry 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.
8339bb7acecfSBarry 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
8340bb7acecfSBarry 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
8341ece3a9fcSMatthew 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.
8342ece3a9fcSMatthew G. Knepley 
8343ece3a9fcSMatthew G. Knepley   Level: intermediate
8344ece3a9fcSMatthew G. Knepley 
8345bb7acecfSBarry Smith   Developer Notes:
8346bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8347bb7acecfSBarry Smith 
8348bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8349bb7acecfSBarry Smith 
83501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8351ece3a9fcSMatthew G. Knepley @*/
8352*a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8353d71ae5a4SJacob Faibussowitsch {
8354ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8355ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8356064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8357064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83589566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
83593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8360ece3a9fcSMatthew G. Knepley }
8361ece3a9fcSMatthew G. Knepley 
8362ece3a9fcSMatthew G. Knepley /*@C
83632716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83642716604bSToby Isaac 
836520f4b53cSBarry Smith   Collective
8366bb7acecfSBarry Smith 
83672716604bSToby Isaac   Input Parameters:
8368bb7acecfSBarry Smith + dm    - The `DM`
83690709b2feSToby Isaac . time  - The time
83702716604bSToby Isaac . funcs - The functions to evaluate for each field component
83712716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8372574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83732716604bSToby Isaac 
83742716604bSToby Isaac   Output Parameter:
83752716604bSToby Isaac . diff - The diff ||u - u_h||_2
83762716604bSToby Isaac 
83772716604bSToby Isaac   Level: developer
83782716604bSToby Isaac 
8379bb7acecfSBarry Smith   Developer Notes:
8380bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8381bb7acecfSBarry Smith 
8382bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8383bb7acecfSBarry Smith 
83841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
83852716604bSToby Isaac @*/
8386d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8387d71ae5a4SJacob Faibussowitsch {
83882716604bSToby Isaac   PetscFunctionBegin;
83892716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8390b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
83919566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
83923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83932716604bSToby Isaac }
8394b698f381SToby Isaac 
8395b698f381SToby Isaac /*@C
8396b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8397b698f381SToby Isaac 
839820f4b53cSBarry Smith   Collective
8399d083f849SBarry Smith 
8400b698f381SToby Isaac   Input Parameters:
8401bb7acecfSBarry Smith + dm    - The `DM`
8402*a4e35b19SJacob Faibussowitsch . time  - The time
8403b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8404b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8405574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8406b698f381SToby Isaac - n     - The vector to project along
8407b698f381SToby Isaac 
8408b698f381SToby Isaac   Output Parameter:
8409b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8410b698f381SToby Isaac 
8411b698f381SToby Isaac   Level: developer
8412b698f381SToby Isaac 
8413bb7acecfSBarry Smith   Developer Notes:
8414bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8415bb7acecfSBarry Smith 
8416bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8417bb7acecfSBarry Smith 
84181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8419b698f381SToby Isaac @*/
8420d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2GradientDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
8421d71ae5a4SJacob Faibussowitsch {
8422b698f381SToby Isaac   PetscFunctionBegin;
8423b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8424b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84259566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
84263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8427b698f381SToby Isaac }
8428b698f381SToby Isaac 
84292a16baeaSToby Isaac /*@C
84302a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84312a16baeaSToby Isaac 
843220f4b53cSBarry Smith   Collective
8433d083f849SBarry Smith 
84342a16baeaSToby Isaac   Input Parameters:
8435bb7acecfSBarry Smith + dm    - The `DM`
84362a16baeaSToby Isaac . time  - The time
84372a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84382a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8439574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84402a16baeaSToby Isaac 
84412a16baeaSToby Isaac   Output Parameter:
84422a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84432a16baeaSToby Isaac 
84442a16baeaSToby Isaac   Level: developer
84452a16baeaSToby Isaac 
8446bb7acecfSBarry Smith   Developer Notes:
8447bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8448bb7acecfSBarry Smith 
8449bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8450bb7acecfSBarry Smith 
845142747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
84522a16baeaSToby Isaac @*/
8453d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8454d71ae5a4SJacob Faibussowitsch {
84552a16baeaSToby Isaac   PetscFunctionBegin;
84562a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84572a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84589566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84602a16baeaSToby Isaac }
84612a16baeaSToby Isaac 
8462df0b854cSToby Isaac /*@C
8463bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8464502a2867SDave May 
8465502a2867SDave May   Not Collective
8466502a2867SDave May 
8467502a2867SDave May   Input Parameter:
8468bb7acecfSBarry Smith . dm - The `DM`
8469502a2867SDave May 
84700a19bb7dSprj-   Output Parameters:
84710a19bb7dSprj- + nranks - the number of neighbours
84720a19bb7dSprj- - ranks  - the neighbors ranks
8473502a2867SDave May 
84749bdbcad8SBarry Smith   Level: beginner
84759bdbcad8SBarry Smith 
8476bb7acecfSBarry Smith   Note:
8477bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8478502a2867SDave May 
84791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8480502a2867SDave May @*/
8481d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8482d71ae5a4SJacob Faibussowitsch {
8483502a2867SDave May   PetscFunctionBegin;
8484502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84859566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
84863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8487502a2867SDave May }
8488502a2867SDave May 
8489531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8490531c7667SBarry Smith 
8491531c7667SBarry Smith /*
8492531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
84932b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8494531c7667SBarry Smith */
8495d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8496d71ae5a4SJacob Faibussowitsch {
8497531c7667SBarry Smith   PetscFunctionBegin;
8498531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8499531c7667SBarry Smith     Vec x1local;
8500531c7667SBarry Smith     DM  dm;
85019566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85027a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
85039566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
85049566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
85059566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8506531c7667SBarry Smith     x1 = x1local;
8507531c7667SBarry Smith   }
85089566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8509531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8510531c7667SBarry Smith     DM dm;
85119566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85129566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8513531c7667SBarry Smith   }
85143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8515531c7667SBarry Smith }
8516531c7667SBarry Smith 
8517531c7667SBarry Smith /*@
8518bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8519531c7667SBarry Smith 
8520*a4e35b19SJacob Faibussowitsch   Input Parameters:
8521*a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8522*a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8523531c7667SBarry Smith 
85249bdbcad8SBarry Smith   Level: advanced
85259bdbcad8SBarry Smith 
852660225df5SJacob Faibussowitsch   Developer Notes:
8527bb7acecfSBarry Smith   this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8528531c7667SBarry Smith 
85291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8530531c7667SBarry Smith @*/
8531d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8532d71ae5a4SJacob Faibussowitsch {
8533531c7667SBarry Smith   PetscFunctionBegin;
8534531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
85353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8536531c7667SBarry Smith }
85378320bc6fSPatrick Sanan 
85388320bc6fSPatrick Sanan /*@
8539bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
85408320bc6fSPatrick Sanan 
85418320bc6fSPatrick Sanan   Collective
85428320bc6fSPatrick Sanan 
85438320bc6fSPatrick Sanan   Input Parameters:
8544bb7acecfSBarry Smith + dm1 - the first `DM`
8545bb7acecfSBarry Smith - dm2 - the second `DM`
85468320bc6fSPatrick Sanan 
85478320bc6fSPatrick Sanan   Output Parameters:
8548bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8549bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
85508320bc6fSPatrick Sanan 
855120f4b53cSBarry Smith   Level: advanced
855220f4b53cSBarry Smith 
85538320bc6fSPatrick Sanan   Notes:
8554bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
85553d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
85568320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8557bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
85583d862458SPatrick Sanan   decomposition, but hold different data.
85598320bc6fSPatrick Sanan 
85608320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8561bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
85628320bc6fSPatrick Sanan 
8563bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
85648320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
85658320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8566bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
85678320bc6fSPatrick Sanan 
8568bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8569bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85708320bc6fSPatrick Sanan .vb
85718320bc6fSPatrick Sanan   ...
85729566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85738320bc6fSPatrick Sanan   if (set && compatible)  {
85749566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
85759566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
85769566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
85778320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85788320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
85798320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
85808320bc6fSPatrick Sanan       }
85818320bc6fSPatrick Sanan     }
85829566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
85839566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
85848320bc6fSPatrick Sanan   } else {
85858320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
85868320bc6fSPatrick Sanan   }
85878320bc6fSPatrick Sanan   ...
85888320bc6fSPatrick Sanan .ve
85898320bc6fSPatrick Sanan 
8590bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
85918320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
85928320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
85938320bc6fSPatrick Sanan   always check the "set" output parameter.
85948320bc6fSPatrick Sanan 
8595bb7acecfSBarry Smith   A `DM` is always compatible with itself.
85968320bc6fSPatrick Sanan 
8597bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
85988320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
85998320bc6fSPatrick Sanan   incompatible.
86008320bc6fSPatrick Sanan 
86018320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8602bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
86038320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
86048320bc6fSPatrick Sanan 
860560225df5SJacob Faibussowitsch   Developer Notes:
8606bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
86073d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8608a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8609bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
86108320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
86113d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8612bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
86138320bc6fSPatrick Sanan 
86141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
86158320bc6fSPatrick Sanan @*/
8616d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8617d71ae5a4SJacob Faibussowitsch {
86188320bc6fSPatrick Sanan   PetscMPIInt compareResult;
86198320bc6fSPatrick Sanan   DMType      type, type2;
86208320bc6fSPatrick Sanan   PetscBool   sameType;
86218320bc6fSPatrick Sanan 
86228320bc6fSPatrick Sanan   PetscFunctionBegin;
8623a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
86248320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
86258320bc6fSPatrick Sanan 
86268320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8627a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86288320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86298320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86318320bc6fSPatrick Sanan   }
86328320bc6fSPatrick Sanan 
86338320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86348320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86358320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86368320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86379566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
86388320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86398320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86408320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86413ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86428320bc6fSPatrick Sanan   }
86438320bc6fSPatrick Sanan 
86448320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8645a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8646dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
86473ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
86488320bc6fSPatrick Sanan   }
86498320bc6fSPatrick Sanan 
8650a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86518320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86529566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86539566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86549566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86558320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8656dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86578320bc6fSPatrick Sanan   } else {
86588320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86598320bc6fSPatrick Sanan   }
86603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86618320bc6fSPatrick Sanan }
8662c0f0dcc3SMatthew G. Knepley 
8663c0f0dcc3SMatthew G. Knepley /*@C
8664bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8665c0f0dcc3SMatthew G. Knepley 
866620f4b53cSBarry Smith   Logically Collective
8667c0f0dcc3SMatthew G. Knepley 
8668c0f0dcc3SMatthew G. Knepley   Input Parameters:
866960225df5SJacob Faibussowitsch + dm             - the `DM`
8670c0f0dcc3SMatthew G. Knepley . f              - the monitor function
867120f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
867220f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8673c0f0dcc3SMatthew G. Knepley 
867420f4b53cSBarry Smith   Options Database Key:
867560225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8676c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8677c0f0dcc3SMatthew G. Knepley 
86789bdbcad8SBarry Smith   Level: intermediate
86799bdbcad8SBarry Smith 
8680bb7acecfSBarry Smith   Note:
8681c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8682bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8683c0f0dcc3SMatthew G. Knepley   order in which they were set.
8684c0f0dcc3SMatthew G. Knepley 
868560225df5SJacob Faibussowitsch   Fortran Notes:
8686bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8687bb7acecfSBarry Smith 
868860225df5SJacob Faibussowitsch   Developer Notes:
8689bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8690c0f0dcc3SMatthew G. Knepley 
86911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8692c0f0dcc3SMatthew G. Knepley @*/
8693d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8694d71ae5a4SJacob Faibussowitsch {
8695c0f0dcc3SMatthew G. Knepley   PetscInt m;
8696c0f0dcc3SMatthew G. Knepley 
8697c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8698c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8699c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8700c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8701c0f0dcc3SMatthew G. Knepley 
87029566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
87033ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8704c0f0dcc3SMatthew G. Knepley   }
87057a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8706c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8707c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8708c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
87093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8710c0f0dcc3SMatthew G. Knepley }
8711c0f0dcc3SMatthew G. Knepley 
8712c0f0dcc3SMatthew G. Knepley /*@
8713bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8714c0f0dcc3SMatthew G. Knepley 
871520f4b53cSBarry Smith   Logically Collective
8716c0f0dcc3SMatthew G. Knepley 
8717c0f0dcc3SMatthew G. Knepley   Input Parameter:
8718c0f0dcc3SMatthew G. Knepley . dm - the DM
8719c0f0dcc3SMatthew G. Knepley 
8720c0f0dcc3SMatthew G. Knepley   Options Database Key:
8721c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8722bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8723c0f0dcc3SMatthew G. Knepley   set via the options database
8724c0f0dcc3SMatthew G. Knepley 
87259bdbcad8SBarry Smith   Level: intermediate
87269bdbcad8SBarry Smith 
8727bb7acecfSBarry Smith   Note:
8728bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8729c0f0dcc3SMatthew G. Knepley 
87301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8731c0f0dcc3SMatthew G. Knepley @*/
8732d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8733d71ae5a4SJacob Faibussowitsch {
8734c0f0dcc3SMatthew G. Knepley   PetscInt m;
8735c0f0dcc3SMatthew G. Knepley 
8736c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8737c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8738c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87399566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8740c0f0dcc3SMatthew G. Knepley   }
8741c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
87423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8743c0f0dcc3SMatthew G. Knepley }
8744c0f0dcc3SMatthew G. Knepley 
8745c0f0dcc3SMatthew G. Knepley /*@C
8746c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8747c0f0dcc3SMatthew G. Knepley 
874820f4b53cSBarry Smith   Collective
8749c0f0dcc3SMatthew G. Knepley 
8750c0f0dcc3SMatthew G. Knepley   Input Parameters:
8751bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8752c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8753c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8754c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8755c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8756bb7acecfSBarry 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
8757c0f0dcc3SMatthew G. Knepley 
8758c0f0dcc3SMatthew G. Knepley   Output Parameter:
8759c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8760c0f0dcc3SMatthew G. Knepley 
8761c0f0dcc3SMatthew G. Knepley   Level: developer
8762c0f0dcc3SMatthew G. Knepley 
87631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8764db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
876560225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8766db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8767c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8768db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8769bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8770c0f0dcc3SMatthew G. Knepley @*/
8771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8772d71ae5a4SJacob Faibussowitsch {
8773c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8774c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8775c0f0dcc3SMatthew G. Knepley 
8776c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8777c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8779c0f0dcc3SMatthew G. Knepley   if (*flg) {
8780c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8781c0f0dcc3SMatthew G. Knepley 
87829566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
87839566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
87849566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
87859566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8786c0f0dcc3SMatthew G. Knepley   }
87873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8788c0f0dcc3SMatthew G. Knepley }
8789c0f0dcc3SMatthew G. Knepley 
8790c0f0dcc3SMatthew G. Knepley /*@
8791c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8792c0f0dcc3SMatthew G. Knepley 
879320f4b53cSBarry Smith   Collective
8794c0f0dcc3SMatthew G. Knepley 
87952fe279fdSBarry Smith   Input Parameter:
8796bb7acecfSBarry Smith . dm - The `DM`
8797c0f0dcc3SMatthew G. Knepley 
8798c0f0dcc3SMatthew G. Knepley   Level: developer
8799c0f0dcc3SMatthew G. Knepley 
8800*a4e35b19SJacob Faibussowitsch   Developer Notes:
8801*a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8802*a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8803*a4e35b19SJacob Faibussowitsch   concept of discretization.
8804bb7acecfSBarry Smith 
88051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8806c0f0dcc3SMatthew G. Knepley @*/
8807d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8808d71ae5a4SJacob Faibussowitsch {
8809c0f0dcc3SMatthew G. Knepley   PetscInt m;
8810c0f0dcc3SMatthew G. Knepley 
8811c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
88123ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8813c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
881448a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
88153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8816c0f0dcc3SMatthew G. Knepley }
88172e4af2aeSMatthew G. Knepley 
88182e4af2aeSMatthew G. Knepley /*@
8819bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
88202e4af2aeSMatthew G. Knepley 
882120f4b53cSBarry Smith   Collective
88222e4af2aeSMatthew G. Knepley 
88232e4af2aeSMatthew G. Knepley   Input Parameters:
8824bb7acecfSBarry Smith + dm  - The `DM`
88256b867d5aSJose E. Roman - sol - The solution vector
88262e4af2aeSMatthew G. Knepley 
88276b867d5aSJose E. Roman   Input/Output Parameter:
882820f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
88296b867d5aSJose E. Roman            contains the error in each field
88306b867d5aSJose E. Roman 
88316b867d5aSJose E. Roman   Output Parameter:
883220f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
883320f4b53cSBarry Smith 
883420f4b53cSBarry Smith   Level: developer
88352e4af2aeSMatthew G. Knepley 
8836bb7acecfSBarry Smith   Note:
8837bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88382e4af2aeSMatthew G. Knepley 
88391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88402e4af2aeSMatthew G. Knepley @*/
8841d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8842d71ae5a4SJacob Faibussowitsch {
88432e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88442e4af2aeSMatthew G. Knepley   void    **ctxs;
88452e4af2aeSMatthew G. Knepley   PetscReal time;
88462e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88472e4af2aeSMatthew G. Knepley 
88482e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88499566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88509566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88519566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88522e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88532e4af2aeSMatthew G. Knepley     PetscDS         ds;
88542e4af2aeSMatthew G. Knepley     DMLabel         label;
88552e4af2aeSMatthew G. Knepley     IS              fieldIS;
88562e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88572e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88582e4af2aeSMatthew G. Knepley 
885907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
88609566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88619566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88622e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88632e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88649566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88652e4af2aeSMatthew G. Knepley     }
88669566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88672e4af2aeSMatthew G. Knepley   }
8868ad540459SPierre 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);
88699566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88709566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88712e4af2aeSMatthew G. Knepley   if (errorVec) {
88722e4af2aeSMatthew G. Knepley     DM             edm;
88732e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
88742e4af2aeSMatthew G. Knepley     PetscBool      simplex;
88752e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
88762e4af2aeSMatthew G. Knepley 
88779566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
88789566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
88799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
88809566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
88812e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
88829566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
88832e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
88842e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
88852e4af2aeSMatthew G. Knepley       PetscQuadrature q;
88862e4af2aeSMatthew G. Knepley       const char     *name;
88872e4af2aeSMatthew G. Knepley 
88889566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
88899566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
88909566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
88919566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
88929566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
88939566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
88949566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
88959566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
88962e4af2aeSMatthew G. Knepley     }
88979566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
88982e4af2aeSMatthew G. Knepley 
88999566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
89009566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
89019566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
89029566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
89032e4af2aeSMatthew G. Knepley   }
89049566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
89053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89062e4af2aeSMatthew G. Knepley }
89079a2a23afSMatthew G. Knepley 
89089a2a23afSMatthew G. Knepley /*@
8909bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
89109a2a23afSMatthew G. Knepley 
891120f4b53cSBarry Smith   Not Collective
89129a2a23afSMatthew G. Knepley 
89139a2a23afSMatthew G. Knepley   Input Parameter:
8914bb7acecfSBarry Smith . dm - The `DM`
89159a2a23afSMatthew G. Knepley 
89169a2a23afSMatthew G. Knepley   Output Parameter:
8917a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
89189a2a23afSMatthew G. Knepley 
89199a2a23afSMatthew G. Knepley   Level: advanced
89209a2a23afSMatthew G. Knepley 
892160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
89229a2a23afSMatthew G. Knepley @*/
8923d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8924d71ae5a4SJacob Faibussowitsch {
89259a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89269a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89279566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89299a2a23afSMatthew G. Knepley }
89309a2a23afSMatthew G. Knepley 
89319a2a23afSMatthew G. Knepley /*@
8932ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89339a2a23afSMatthew G. Knepley 
893420f4b53cSBarry Smith   Not Collective
89359a2a23afSMatthew G. Knepley 
89369a2a23afSMatthew G. Knepley   Input Parameters:
8937bb7acecfSBarry Smith + dm    - The `DM`
8938bb7acecfSBarry Smith . label - The `DMLabel`
8939ac17215fSMatthew G. Knepley . value - The label value indicating the region
8940ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
89419a2a23afSMatthew G. Knepley 
89429a2a23afSMatthew G. Knepley   Output Parameter:
8943bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
89449a2a23afSMatthew G. Knepley 
89459bdbcad8SBarry Smith   Level: advanced
89469bdbcad8SBarry Smith 
8947bb7acecfSBarry Smith   Note:
8948bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
894904c51a94SMatthew G. Knepley 
89501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89519a2a23afSMatthew G. Knepley @*/
8952d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8953d71ae5a4SJacob Faibussowitsch {
8954ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
895504c51a94SMatthew G. Knepley   PetscBool       has;
89569a2a23afSMatthew G. Knepley 
89579a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89589a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89599a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89609a2a23afSMatthew G. Knepley   key.label = label;
89619a2a23afSMatthew G. Knepley   key.value = value;
8962ac17215fSMatthew G. Knepley   key.part  = part;
89639566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89649566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89659566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89679a2a23afSMatthew G. Knepley }
89689a2a23afSMatthew G. Knepley 
89699a2a23afSMatthew G. Knepley /*@
8970bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89719a2a23afSMatthew G. Knepley 
897220f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
89739a2a23afSMatthew G. Knepley 
89749a2a23afSMatthew G. Knepley   Input Parameters:
8975bb7acecfSBarry Smith + dm    - The `DM`
8976bb7acecfSBarry Smith . label - The `DMLabel`
89779a2a23afSMatthew G. Knepley . value - The label value indicating the region
8978ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
8979bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
89809a2a23afSMatthew G. Knepley 
89819a2a23afSMatthew G. Knepley   Level: advanced
89829a2a23afSMatthew G. Knepley 
89831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
89849a2a23afSMatthew G. Knepley @*/
8985d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
8986d71ae5a4SJacob Faibussowitsch {
89879a2a23afSMatthew G. Knepley   Vec             old;
89889a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
89899a2a23afSMatthew G. Knepley 
89909a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89919a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89929a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89939a2a23afSMatthew G. Knepley   key.label = label;
89949a2a23afSMatthew G. Knepley   key.value = value;
8995ac17215fSMatthew G. Knepley   key.part  = part;
89969566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
89979566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
89989566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
89999566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
90009566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
90013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90029a2a23afSMatthew G. Knepley }
90039a2a23afSMatthew G. Knepley 
90049a2a23afSMatthew G. Knepley /*@C
9005bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
90069a2a23afSMatthew G. Knepley 
900720f4b53cSBarry Smith   Not Collective
90089a2a23afSMatthew G. Knepley 
90099a2a23afSMatthew G. Knepley   Input Parameter:
9010bb7acecfSBarry Smith . dm - The `DM`
90119a2a23afSMatthew G. Knepley 
90129a2a23afSMatthew G. Knepley   Output Parameters:
9013bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9014bb7acecfSBarry Smith . values - The label values for each `Vec`
9015bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
90169a2a23afSMatthew G. Knepley 
90179bdbcad8SBarry Smith   Level: advanced
90189bdbcad8SBarry Smith 
9019bb7acecfSBarry Smith   Note:
9020bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
90219a2a23afSMatthew G. Knepley 
902260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
90239a2a23afSMatthew G. Knepley @*/
9024d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9025d71ae5a4SJacob Faibussowitsch {
90269a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
90279a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90289a2a23afSMatthew G. Knepley 
90299a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90309a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90314f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
90324f572ea9SToby Isaac   PetscAssertPointer(values, 3);
90334f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
90349566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90359566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90369566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
90379371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
90389371c9d4SSatish Balay     labels[i] = keys[i].label;
90399371c9d4SSatish Balay     values[i] = keys[i].value;
90409371c9d4SSatish Balay     parts[i]  = keys[i].part;
90419371c9d4SSatish Balay   }
90429566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90449a2a23afSMatthew G. Knepley }
90459a2a23afSMatthew G. Knepley 
90469a2a23afSMatthew G. Knepley /*@
9047bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90489a2a23afSMatthew G. Knepley 
904920f4b53cSBarry Smith   Not Collective
90509a2a23afSMatthew G. Knepley 
90519a2a23afSMatthew G. Knepley   Input Parameter:
9052bb7acecfSBarry Smith . dm - The `DM`
90539a2a23afSMatthew G. Knepley 
90549a2a23afSMatthew G. Knepley   Output Parameter:
9055bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90569a2a23afSMatthew G. Knepley 
90579a2a23afSMatthew G. Knepley   Level: advanced
90589a2a23afSMatthew G. Knepley 
9059bb7acecfSBarry Smith   Note:
9060bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9061bb7acecfSBarry Smith 
90621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90639a2a23afSMatthew G. Knepley @*/
9064d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9065d71ae5a4SJacob Faibussowitsch {
90669a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90679a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90689566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90699566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90719a2a23afSMatthew G. Knepley }
9072b5a892a1SMatthew G. Knepley 
9073b5a892a1SMatthew G. Knepley /*@C
9074bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9075b5a892a1SMatthew G. Knepley 
907620f4b53cSBarry Smith   Not Collective
9077b5a892a1SMatthew G. Knepley 
9078b5a892a1SMatthew G. Knepley   Input Parameters:
9079bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9080b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9081b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9082b5a892a1SMatthew G. Knepley 
9083b5a892a1SMatthew G. Knepley   Output Parameters:
9084bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9085b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9086b5a892a1SMatthew G. Knepley 
9087b5a892a1SMatthew G. Knepley   Level: advanced
9088b5a892a1SMatthew G. Knepley 
9089bb7acecfSBarry Smith   Note:
9090bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9091bb7acecfSBarry Smith 
9092bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9093bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9094bb7acecfSBarry Smith 
9095bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9096bb7acecfSBarry Smith 
90971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9098b5a892a1SMatthew G. Knepley @*/
9099d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9100d71ae5a4SJacob Faibussowitsch {
9101b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9102b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9103b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9104b5a892a1SMatthew G. Knepley 
9105b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91069371c9d4SSatish Balay   if (!nO) {
91079371c9d4SSatish Balay     *ornt  = 0;
91089371c9d4SSatish Balay     *found = PETSC_TRUE;
91093ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
91109371c9d4SSatish Balay   }
9111b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9112b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9113b5a892a1SMatthew G. Knepley 
91149371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91159371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
91169371c9d4SSatish Balay     if (c == cS) {
91179371c9d4SSatish Balay       *ornt = o;
91189371c9d4SSatish Balay       break;
91199371c9d4SSatish Balay     }
9120b5a892a1SMatthew G. Knepley   }
9121b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
91223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9123b5a892a1SMatthew G. Knepley }
9124b5a892a1SMatthew G. Knepley 
9125b5a892a1SMatthew G. Knepley /*@C
9126bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9127b5a892a1SMatthew G. Knepley 
912820f4b53cSBarry Smith   Not Collective
9129b5a892a1SMatthew G. Knepley 
9130b5a892a1SMatthew G. Knepley   Input Parameters:
9131bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9132b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9133b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9134b5a892a1SMatthew G. Knepley 
91352fe279fdSBarry Smith   Output Parameter:
9136bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9137b5a892a1SMatthew G. Knepley 
9138b5a892a1SMatthew G. Knepley   Level: advanced
9139b5a892a1SMatthew G. Knepley 
9140bb7acecfSBarry Smith   Note:
9141bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9142bb7acecfSBarry Smith 
914360225df5SJacob Faibussowitsch   Developer Notes:
9144bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9145bb7acecfSBarry Smith 
91461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9147b5a892a1SMatthew G. Knepley @*/
9148d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9149d71ae5a4SJacob Faibussowitsch {
9150b5a892a1SMatthew G. Knepley   PetscBool found;
9151b5a892a1SMatthew G. Knepley 
9152b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91539566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91547a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
91553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9156b5a892a1SMatthew G. Knepley }
9157b5a892a1SMatthew G. Knepley 
9158b5a892a1SMatthew G. Knepley /*@C
9159bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9160b5a892a1SMatthew G. Knepley 
916120f4b53cSBarry Smith   Not Collective
9162b5a892a1SMatthew G. Knepley 
9163b5a892a1SMatthew G. Knepley   Input Parameters:
9164bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9165b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9166b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9167b5a892a1SMatthew G. Knepley 
9168b5a892a1SMatthew G. Knepley   Output Parameters:
9169bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9170b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9171b5a892a1SMatthew G. Knepley 
9172b5a892a1SMatthew G. Knepley   Level: advanced
9173b5a892a1SMatthew G. Knepley 
9174bb7acecfSBarry Smith   Note:
9175bb7acecfSBarry Smith   An arrangement is a vertex order
9176bb7acecfSBarry Smith 
9177bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9178bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9179bb7acecfSBarry Smith 
9180bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9181bb7acecfSBarry Smith 
91821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9183b5a892a1SMatthew G. Knepley @*/
9184d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9185d71ae5a4SJacob Faibussowitsch {
9186b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9187b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9188b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9189b5a892a1SMatthew G. Knepley 
9190b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91919371c9d4SSatish Balay   if (!nO) {
91929371c9d4SSatish Balay     *ornt  = 0;
91939371c9d4SSatish Balay     *found = PETSC_TRUE;
91943ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
91959371c9d4SSatish Balay   }
9196b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9197b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9198b5a892a1SMatthew G. Knepley 
91999371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92009371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
92019371c9d4SSatish Balay     if (c == cS) {
92029371c9d4SSatish Balay       *ornt = o;
92039371c9d4SSatish Balay       break;
92049371c9d4SSatish Balay     }
9205b5a892a1SMatthew G. Knepley   }
9206b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9208b5a892a1SMatthew G. Knepley }
9209b5a892a1SMatthew G. Knepley 
9210b5a892a1SMatthew G. Knepley /*@C
9211bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9212b5a892a1SMatthew G. Knepley 
921320f4b53cSBarry Smith   Not Collective
9214b5a892a1SMatthew G. Knepley 
9215b5a892a1SMatthew G. Knepley   Input Parameters:
9216bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9217b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9218b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9219b5a892a1SMatthew G. Knepley 
92202fe279fdSBarry Smith   Output Parameter:
9221bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9222b5a892a1SMatthew G. Knepley 
9223b5a892a1SMatthew G. Knepley   Level: advanced
9224b5a892a1SMatthew G. Knepley 
9225bb7acecfSBarry Smith   Note:
9226bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9227bb7acecfSBarry Smith 
922860225df5SJacob Faibussowitsch   Developer Notes:
9229bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9230bb7acecfSBarry Smith 
92311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9232b5a892a1SMatthew G. Knepley @*/
9233d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9234d71ae5a4SJacob Faibussowitsch {
9235b5a892a1SMatthew G. Knepley   PetscBool found;
9236b5a892a1SMatthew G. Knepley 
9237b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92389566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
92397a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
92403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9241b5a892a1SMatthew G. Knepley }
9242012bc364SMatthew G. Knepley 
9243012bc364SMatthew G. Knepley /*@C
9244012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9245012bc364SMatthew G. Knepley 
924620f4b53cSBarry Smith   Not Collective
9247012bc364SMatthew G. Knepley 
9248012bc364SMatthew G. Knepley   Input Parameters:
9249bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9250012bc364SMatthew G. Knepley - point - Coordinates of the point
9251012bc364SMatthew G. Knepley 
92522fe279fdSBarry Smith   Output Parameter:
9253012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9254012bc364SMatthew G. Knepley 
9255012bc364SMatthew G. Knepley   Level: advanced
9256012bc364SMatthew G. Knepley 
92571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9258012bc364SMatthew G. Knepley @*/
9259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9260d71ae5a4SJacob Faibussowitsch {
9261012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9262012bc364SMatthew G. Knepley   PetscInt  d;
9263012bc364SMatthew G. Knepley 
9264012bc364SMatthew G. Knepley   PetscFunctionBegin;
9265012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9266012bc364SMatthew G. Knepley   switch (ct) {
9267012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9268012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9269012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92709371c9d4SSatish Balay       if (point[d] < -1.0) {
92719371c9d4SSatish Balay         *inside = PETSC_FALSE;
92729371c9d4SSatish Balay         break;
92739371c9d4SSatish Balay       }
9274012bc364SMatthew G. Knepley       sum += point[d];
9275012bc364SMatthew G. Knepley     }
92769371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
92779371c9d4SSatish Balay       *inside = PETSC_FALSE;
92789371c9d4SSatish Balay       break;
92799371c9d4SSatish Balay     }
9280012bc364SMatthew G. Knepley     break;
9281012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9282012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9283012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
92849371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
92859371c9d4SSatish Balay         *inside = PETSC_FALSE;
9286012bc364SMatthew G. Knepley         break;
92879371c9d4SSatish Balay       }
92889371c9d4SSatish Balay     break;
9289d71ae5a4SJacob Faibussowitsch   default:
9290d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9291012bc364SMatthew G. Knepley   }
92923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9293012bc364SMatthew G. Knepley }
9294