xref: /petsc/src/dm/interface/dm.c (revision b77fa653b0e8b1598bff61858d7407b6bd679cfb)
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;
571411c6eeSJed Brown   PetscValidPointer(dm, 2);
580298fd71SBarry Smith   *dm = NULL;
599566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
60a4121054SBarry Smith 
619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
62e7c4fc90SDmitry Karpeev 
6362e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6462e5d2d2SJDBetteridge 
6549be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6649be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
670298fd71SBarry Smith   v->ltogmap              = NULL;
68a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
691411c6eeSJed Brown   v->bs                   = 1;
70171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
719566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
729566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
73c58f1c22SToby Isaac   v->labels                    = NULL;
7434aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7534aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
76c58f1c22SToby Isaac   v->depthLabel                = NULL;
77ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
781bb6d2a8SBarry Smith   v->localSection              = NULL;
791bb6d2a8SBarry Smith   v->globalSection             = NULL;
803b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
813b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8279769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
836858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
846858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
856858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8696173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
87435a35e8SMatthew G Knepley   {
88435a35e8SMatthew G Knepley     PetscInt i;
89435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
900298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
91f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
92435a35e8SMatthew G Knepley     }
93435a35e8SMatthew G Knepley   }
949566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
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 
1311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMSetType()`, `DMConvert()`
13238221697SMatthew G. Knepley @*/
133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
134d71ae5a4SJacob Faibussowitsch {
13538221697SMatthew G. Knepley   PetscSF  sf;
13638221697SMatthew G. Knepley   Vec      coords;
13738221697SMatthew G. Knepley   void    *ctx;
1386858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
13938221697SMatthew G. Knepley 
14038221697SMatthew G. Knepley   PetscFunctionBegin;
14138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14238221697SMatthew G. Knepley   PetscValidPointer(newdm, 2);
1439566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1449566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
145ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
146ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
147c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1499566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1519566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1529566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
154dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1553f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1569566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1589566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1599566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1606858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1616858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
162be4c1c3eSMatthew G. Knepley       DM           ncdm;
163be4c1c3eSMatthew G. Knepley       PetscSection cs;
1645a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
165be4c1c3eSMatthew G. Knepley 
1666858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1679566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
168712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1695a0206caSToby Isaac       if (pEndMax >= 0) {
1706858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1716858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1729566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1736858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1746858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1759566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
176be4c1c3eSMatthew G. Knepley       }
177be4c1c3eSMatthew G. Knepley     }
1786858538eSMatthew G. Knepley   }
1799566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1809566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1819566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18238221697SMatthew G. Knepley   if (coords) {
1839566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18438221697SMatthew G. Knepley   } else {
1859566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1869566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18738221697SMatthew G. Knepley   }
1886858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1896858538eSMatthew G. Knepley   if (coords) {
1906858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1916858538eSMatthew G. Knepley   } else {
1926858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1936858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1946858538eSMatthew G. Knepley   }
19590b157c4SStefano Zampini   {
1964fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1976858538eSMatthew G. Knepley 
1984fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
1994fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
200c6b900c6SMatthew G. Knepley   }
20134aa8a36SMatthew G. Knepley   {
20234aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20334aa8a36SMatthew G. Knepley 
2049566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2059566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20634aa8a36SMatthew G. Knepley   }
2073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20838221697SMatthew G. Knepley }
20938221697SMatthew G. Knepley 
2109a42bb27SBarry Smith /*@C
211bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2129a42bb27SBarry Smith 
21320f4b53cSBarry Smith    Logically Collective
2149a42bb27SBarry Smith 
215147403d9SBarry Smith    Input Parameters:
2169a42bb27SBarry Smith +  da - initial distributed array
217bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2189a42bb27SBarry Smith 
21920f4b53cSBarry Smith    Options Database Key:
220147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2219a42bb27SBarry Smith 
2229a42bb27SBarry Smith    Level: intermediate
2239a42bb27SBarry Smith 
2241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
225bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2269a42bb27SBarry Smith @*/
227d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype)
228d71ae5a4SJacob Faibussowitsch {
2299a42bb27SBarry Smith   PetscFunctionBegin;
2309a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2319566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2329566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
2333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2349a42bb27SBarry Smith }
2359a42bb27SBarry Smith 
236c0dedaeaSBarry Smith /*@C
237bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
238c0dedaeaSBarry Smith 
23920f4b53cSBarry Smith    Logically Collective
240c0dedaeaSBarry Smith 
241c0dedaeaSBarry Smith    Input Parameter:
242c0dedaeaSBarry Smith .  da - initial distributed array
243c0dedaeaSBarry Smith 
244c0dedaeaSBarry Smith    Output Parameter:
245c0dedaeaSBarry Smith .  ctype - the vector type
246c0dedaeaSBarry Smith 
247c0dedaeaSBarry Smith    Level: intermediate
248c0dedaeaSBarry Smith 
2491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
250c0dedaeaSBarry Smith @*/
251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
252d71ae5a4SJacob Faibussowitsch {
253c0dedaeaSBarry Smith   PetscFunctionBegin;
254c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
255c0dedaeaSBarry Smith   *ctype = da->vectype;
2563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
257c0dedaeaSBarry Smith }
258c0dedaeaSBarry Smith 
2595f1ad066SMatthew G Knepley /*@
260bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2615f1ad066SMatthew G Knepley 
26220f4b53cSBarry Smith   Not Collective
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   Input Parameter:
265bb7acecfSBarry Smith . v - The `Vec`
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley   Output Parameter:
268bb7acecfSBarry Smith . dm - The `DM`
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Level: intermediate
2715f1ad066SMatthew G Knepley 
272bb7acecfSBarry Smith   Note:
273bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
274bb7acecfSBarry Smith 
2751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2765f1ad066SMatthew G Knepley @*/
277d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
278d71ae5a4SJacob Faibussowitsch {
2795f1ad066SMatthew G Knepley   PetscFunctionBegin;
2805f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2815f1ad066SMatthew G Knepley   PetscValidPointer(dm, 2);
2829566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2845f1ad066SMatthew G Knepley }
2855f1ad066SMatthew G Knepley 
2865f1ad066SMatthew G Knepley /*@
287bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2885f1ad066SMatthew G Knepley 
28920f4b53cSBarry Smith   Not Collective
2905f1ad066SMatthew G Knepley 
2915f1ad066SMatthew G Knepley   Input Parameters:
292bb7acecfSBarry Smith + v - The `Vec`
293bb7acecfSBarry Smith - dm - The `DM`
2945f1ad066SMatthew G Knepley 
29520f4b53cSBarry Smith   Level: developer
29620f4b53cSBarry Smith 
297bb7acecfSBarry Smith   Note:
298bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
299d9805387SMatthew G. Knepley 
300bb7acecfSBarry Smith   This is NOT the same as `DMCreateGlobalVector()` since it does not change the view methods or perform other customization, but merely sets the `DM` member.
301bb7acecfSBarry Smith 
3021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3035f1ad066SMatthew G Knepley @*/
304d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
305d71ae5a4SJacob Faibussowitsch {
3065f1ad066SMatthew G Knepley   PetscFunctionBegin;
3075f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
308d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3099566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3115f1ad066SMatthew G Knepley }
3125f1ad066SMatthew G Knepley 
313521d9a4cSLisandro Dalcin /*@C
314bb7acecfSBarry Smith        DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3158f1509bcSBarry Smith 
31620f4b53cSBarry Smith    Logically Collective
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith    Input Parameters:
319bb7acecfSBarry Smith +  dm - the `DM` context
3208f1509bcSBarry Smith -  ctype - the matrix type
3218f1509bcSBarry Smith 
32220f4b53cSBarry Smith    Options Database Key:
3238f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Level: intermediate
3268f1509bcSBarry Smith 
3271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
328bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3298f1509bcSBarry Smith @*/
330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
331d71ae5a4SJacob Faibussowitsch {
3328f1509bcSBarry Smith   PetscFunctionBegin;
3338f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3348f1509bcSBarry Smith   dm->coloringtype = ctype;
3353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3368f1509bcSBarry Smith }
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith /*@C
339bb7acecfSBarry Smith        DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
340521d9a4cSLisandro Dalcin 
34120f4b53cSBarry Smith    Logically Collective
342521d9a4cSLisandro Dalcin 
343521d9a4cSLisandro Dalcin    Input Parameter:
344bb7acecfSBarry Smith .  dm - the `DM` context
3458f1509bcSBarry Smith 
3468f1509bcSBarry Smith    Output Parameter:
3478f1509bcSBarry Smith .  ctype - the matrix type
3488f1509bcSBarry Smith 
34920f4b53cSBarry Smith    Options Database Key:
3508f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3518f1509bcSBarry Smith 
3528f1509bcSBarry Smith    Level: intermediate
3538f1509bcSBarry Smith 
3541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
355bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3568f1509bcSBarry Smith @*/
357d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
358d71ae5a4SJacob Faibussowitsch {
3598f1509bcSBarry Smith   PetscFunctionBegin;
3608f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3618f1509bcSBarry Smith   *ctype = dm->coloringtype;
3623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3638f1509bcSBarry Smith }
3648f1509bcSBarry Smith 
3658f1509bcSBarry Smith /*@C
366bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3678f1509bcSBarry Smith 
36820f4b53cSBarry Smith    Logically Collective
3698f1509bcSBarry Smith 
3708f1509bcSBarry Smith    Input Parameters:
371bb7acecfSBarry Smith +  dm - the `DM` context
372bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
373521d9a4cSLisandro Dalcin 
37420f4b53cSBarry Smith    Options Database Key:
375bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
376521d9a4cSLisandro Dalcin 
377521d9a4cSLisandro Dalcin    Level: intermediate
378521d9a4cSLisandro Dalcin 
3791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
380521d9a4cSLisandro Dalcin @*/
381d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
382d71ae5a4SJacob Faibussowitsch {
383521d9a4cSLisandro Dalcin   PetscFunctionBegin;
384521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3859566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3869566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
3873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
388521d9a4cSLisandro Dalcin }
389521d9a4cSLisandro Dalcin 
390c0dedaeaSBarry Smith /*@C
39120f4b53cSBarry Smith        DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
392c0dedaeaSBarry Smith 
39320f4b53cSBarry Smith    Logically Collective
394c0dedaeaSBarry Smith 
395c0dedaeaSBarry Smith    Input Parameter:
396bb7acecfSBarry Smith .  dm - the `DM` context
397c0dedaeaSBarry Smith 
398c0dedaeaSBarry Smith    Output Parameter:
399c0dedaeaSBarry Smith .  ctype - the matrix type
400c0dedaeaSBarry Smith 
401c0dedaeaSBarry Smith    Level: intermediate
402c0dedaeaSBarry Smith 
4031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
404c0dedaeaSBarry Smith @*/
405d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
406d71ae5a4SJacob Faibussowitsch {
407c0dedaeaSBarry Smith   PetscFunctionBegin;
408c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409c0dedaeaSBarry Smith   *ctype = dm->mattype;
4103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
411c0dedaeaSBarry Smith }
412c0dedaeaSBarry Smith 
413c688c046SMatthew G Knepley /*@
414bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
415c688c046SMatthew G Knepley 
41620f4b53cSBarry Smith   Not Collective
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley   Input Parameter:
419bb7acecfSBarry Smith . A - The `Mat`
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley   Output Parameter:
422bb7acecfSBarry Smith . dm - The `DM`
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Level: intermediate
425c688c046SMatthew G Knepley 
426bb7acecfSBarry Smith   Note:
427bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
428bb7acecfSBarry Smith 
429bb7acecfSBarry Smith   Developer Note:
430bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4318f1509bcSBarry Smith 
4321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
433c688c046SMatthew G Knepley @*/
434d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
435d71ae5a4SJacob Faibussowitsch {
436c688c046SMatthew G Knepley   PetscFunctionBegin;
437c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
438c688c046SMatthew G Knepley   PetscValidPointer(dm, 2);
4399566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
441c688c046SMatthew G Knepley }
442c688c046SMatthew G Knepley 
443c688c046SMatthew G Knepley /*@
444bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
445c688c046SMatthew G Knepley 
44620f4b53cSBarry Smith   Not Collective
447c688c046SMatthew G Knepley 
448c688c046SMatthew G Knepley   Input Parameters:
44920f4b53cSBarry Smith + A - The `Mat`
45020f4b53cSBarry Smith - dm - The `DM`
451c688c046SMatthew G Knepley 
452bb7acecfSBarry Smith   Level: developer
453c688c046SMatthew G Knepley 
454bb7acecfSBarry Smith   Note:
455bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
456bb7acecfSBarry Smith 
457bb7acecfSBarry Smith   Developer Note:
458bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
459bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4608f1509bcSBarry Smith 
4611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
462c688c046SMatthew G Knepley @*/
463d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
464d71ae5a4SJacob Faibussowitsch {
465c688c046SMatthew G Knepley   PetscFunctionBegin;
466c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4678865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4689566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
470c688c046SMatthew G Knepley }
471c688c046SMatthew G Knepley 
4729a42bb27SBarry Smith /*@C
473bb7acecfSBarry Smith    DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4749a42bb27SBarry Smith 
47520f4b53cSBarry Smith    Logically Collective
4769a42bb27SBarry Smith 
477d8d19677SJose E. Roman    Input Parameters:
478bb7acecfSBarry Smith +  da - the `DM` context
479bb7acecfSBarry Smith -  prefix - the prefix to prepend
4809a42bb27SBarry Smith 
48120f4b53cSBarry Smith    Level: advanced
48220f4b53cSBarry Smith 
48320f4b53cSBarry Smith    Note:
4849a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4859a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4869a42bb27SBarry Smith 
4871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4889a42bb27SBarry Smith @*/
489d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
490d71ae5a4SJacob Faibussowitsch {
4919a42bb27SBarry Smith   PetscFunctionBegin;
4929a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4939566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4941baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4951baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
4963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4979a42bb27SBarry Smith }
4989a42bb27SBarry Smith 
49931697293SDave May /*@C
500da81f932SPierre Jolivet    DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
501bb7acecfSBarry Smith    `DM` options in the options database.
50231697293SDave May 
50320f4b53cSBarry Smith    Logically Collective
50431697293SDave May 
50531697293SDave May    Input Parameters:
506bb7acecfSBarry Smith +  dm - the `DM` context
507bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
50831697293SDave May 
50920f4b53cSBarry Smith    Level: advanced
51020f4b53cSBarry Smith 
51120f4b53cSBarry Smith    Note:
512bb7acecfSBarry Smith    If the `DM` does not currently have an options prefix then this value is used alone as the prefix as if `DMSetOptionsPrefix()` had been called.
51331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
51431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51531697293SDave May 
5161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51731697293SDave May @*/
518d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
519d71ae5a4SJacob Faibussowitsch {
52031697293SDave May   PetscFunctionBegin;
52131697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5229566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
52431697293SDave May }
52531697293SDave May 
52631697293SDave May /*@C
52731697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
528bb7acecfSBarry Smith    DM options in the options database.
52931697293SDave May 
53031697293SDave May    Not Collective
53131697293SDave May 
5322fe279fdSBarry Smith    Input Parameter:
533bb7acecfSBarry Smith .  dm - the `DM` context
53431697293SDave May 
5352fe279fdSBarry Smith    Output Parameter:
53631697293SDave May .  prefix - pointer to the prefix string used is returned
53731697293SDave May 
53831697293SDave May    Level: advanced
53931697293SDave May 
54020f4b53cSBarry Smith    Fortran Note:
54120f4b53cSBarry Smith    Pass in a string 'prefix' of
54220f4b53cSBarry Smith    sufficient length to hold the prefix.
54320f4b53cSBarry Smith 
5441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54531697293SDave May @*/
546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
547d71ae5a4SJacob Faibussowitsch {
54831697293SDave May   PetscFunctionBegin;
54931697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5509566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55231697293SDave May }
55331697293SDave May 
55462e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
555d71ae5a4SJacob Faibussowitsch {
5566eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
55788bdff64SToby Isaac 
55888bdff64SToby Isaac   PetscFunctionBegin;
559aab5bcd8SJed Brown   *ncrefct = 0;
56088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56188bdff64SToby Isaac     refct--;
56288bdff64SToby Isaac     if (recurseCoarse) {
56388bdff64SToby Isaac       PetscInt coarseCount;
56488bdff64SToby Isaac 
56562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
56688bdff64SToby Isaac       refct += coarseCount;
56788bdff64SToby Isaac     }
56888bdff64SToby Isaac   }
56988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
57088bdff64SToby Isaac     refct--;
57188bdff64SToby Isaac     if (recurseFine) {
57288bdff64SToby Isaac       PetscInt fineCount;
57388bdff64SToby Isaac 
57462e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
57588bdff64SToby Isaac       refct += fineCount;
57688bdff64SToby Isaac     }
57788bdff64SToby Isaac   }
57888bdff64SToby Isaac   *ncrefct = refct;
5793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58088bdff64SToby Isaac }
58188bdff64SToby Isaac 
58262e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
58362e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
58462e5d2d2SJDBetteridge {
58562e5d2d2SJDBetteridge   PetscFunctionBegin;
58662e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
5873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58862e5d2d2SJDBetteridge }
58962e5d2d2SJDBetteridge 
590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
591d71ae5a4SJacob Faibussowitsch {
5925d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
593354557abSToby Isaac 
594354557abSToby Isaac   PetscFunctionBegin;
595354557abSToby Isaac   /* destroy the labels */
596354557abSToby Isaac   while (next) {
597354557abSToby Isaac     DMLabelLink tmp = next->next;
598354557abSToby Isaac 
5995d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
600ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6019566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6029566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
603354557abSToby Isaac     next = tmp;
604354557abSToby Isaac   }
6055d80c0bfSVaclav Hapla   dm->labels = NULL;
6063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
607354557abSToby Isaac }
608354557abSToby Isaac 
609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
610d71ae5a4SJacob Faibussowitsch {
6116858538eSMatthew G. Knepley   PetscFunctionBegin;
6126858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6136858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6146858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6156858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6166858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6186858538eSMatthew G. Knepley }
6196858538eSMatthew G. Knepley 
6201fb7b255SJunchao Zhang /*@C
621bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
62247c6ae99SBarry Smith 
62320f4b53cSBarry Smith     Collective
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Input Parameter:
626bb7acecfSBarry Smith .   dm - the `DM` object to destroy
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Level: developer
62947c6ae99SBarry Smith 
6301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
63147c6ae99SBarry Smith @*/
632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
633d71ae5a4SJacob Faibussowitsch {
6346eb26441SStefano Zampini   PetscInt       cnt;
635dfe15315SJed Brown   DMNamedVecLink nlink, nnext;
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith   PetscFunctionBegin;
6383ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
6396bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
64087e657c6SBarry Smith 
64188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
64262e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
64388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6449371c9d4SSatish Balay   if (--cnt > 0) {
6459371c9d4SSatish Balay     *dm = NULL;
6463ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6479371c9d4SSatish Balay   }
6483ba16761SJacob Faibussowitsch   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
6496bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6506eb26441SStefano Zampini 
6519566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6529566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6536eb26441SStefano Zampini 
654f490541aSPeter Brune   nnext              = (*dm)->namedglobal;
6550298fd71SBarry Smith   (*dm)->namedglobal = NULL;
656f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named vectors */
6572348bcf4SPeter Brune     nnext = nlink->next;
6587a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6599566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6609566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6619566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6622348bcf4SPeter Brune   }
663f490541aSPeter Brune   nnext             = (*dm)->namedlocal;
6640298fd71SBarry Smith   (*dm)->namedlocal = NULL;
665f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named local vectors */
666f490541aSPeter Brune     nnext = nlink->next;
6677a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6689566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6699566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6709566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
671f490541aSPeter Brune   }
6722348bcf4SPeter Brune 
673b17ce1afSJed Brown   /* Destroy the list of hooks */
674c833c3b5SJed Brown   {
675c833c3b5SJed Brown     DMCoarsenHookLink link, next;
676b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
677b17ce1afSJed Brown       next = link->next;
6789566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
679b17ce1afSJed Brown     }
6800298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
681c833c3b5SJed Brown   }
682c833c3b5SJed Brown   {
683c833c3b5SJed Brown     DMRefineHookLink link, next;
684c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
685c833c3b5SJed Brown       next = link->next;
6869566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
687c833c3b5SJed Brown     }
6880298fd71SBarry Smith     (*dm)->refinehook = NULL;
689c833c3b5SJed Brown   }
690be081cd6SPeter Brune   {
691be081cd6SPeter Brune     DMSubDomainHookLink link, next;
692be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
693be081cd6SPeter Brune       next = link->next;
6949566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
695be081cd6SPeter Brune     }
6960298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
697be081cd6SPeter Brune   }
698baf369e7SPeter Brune   {
699baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
700baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
701baf369e7SPeter Brune       next = link->next;
7029566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
703baf369e7SPeter Brune     }
7040298fd71SBarry Smith     (*dm)->gtolhook = NULL;
705baf369e7SPeter Brune   }
706d4d07f1eSToby Isaac   {
707d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
708d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
709d4d07f1eSToby Isaac       next = link->next;
7109566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
711d4d07f1eSToby Isaac     }
712d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
713d4d07f1eSToby Isaac   }
714aa1993deSMatthew G Knepley   /* Destroy the work arrays */
715aa1993deSMatthew G Knepley   {
716aa1993deSMatthew G Knepley     DMWorkLink link, next;
717442f3b32SStefano Zampini     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (*dm)->workout, (*dm)->workout->mem);
718aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
719aa1993deSMatthew G Knepley       next = link->next;
7209566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7219566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
722aa1993deSMatthew G Knepley     }
7230298fd71SBarry Smith     (*dm)->workin = NULL;
724aa1993deSMatthew G Knepley   }
725c58f1c22SToby Isaac   /* destroy the labels */
7269566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
727f4cdcedcSVaclav Hapla   /* destroy the fields */
7289566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
729f4cdcedcSVaclav Hapla   /* destroy the boundaries */
730e6f8dbb6SToby Isaac   {
731e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
732e6f8dbb6SToby Isaac     while (next) {
733e6f8dbb6SToby Isaac       DMBoundary b = next;
734e6f8dbb6SToby Isaac 
735e6f8dbb6SToby Isaac       next = b->next;
7369566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
737e6f8dbb6SToby Isaac     }
738e6f8dbb6SToby Isaac   }
739b17ce1afSJed Brown 
7409566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7419566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7429566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
74352536dc3SBarry Smith 
74448a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7459566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7469566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7479566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74988ed4aceSMatthew G Knepley 
7509566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7519566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7539566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7549566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7559566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7569566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
757736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
75848a46eb9SPierre Jolivet     if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7599566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
760736995cdSBlaise Bourdin   }
7619a2a23afSMatthew G. Knepley   {
7629a2a23afSMatthew G. Knepley     Vec     *auxData;
7639a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7649a2a23afSMatthew G. Knepley 
7659566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7669566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7679566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7689566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7699566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7709566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7719a2a23afSMatthew G. Knepley   }
77248a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7736eb26441SStefano Zampini 
7749566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
77548a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7774fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7789566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7799566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7806858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7816858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7829566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7839566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7849566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7856725e60dSJed Brown   PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local));
7866725e60dSJed Brown   PetscCall(VecDestroy(&(*dm)->periodic.affine));
7876636e97aSMatthew G Knepley 
7889566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7899566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
790e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7919566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
792732e2eb9SMatthew G Knepley 
79348a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7949566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
795f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7969566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7979566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
798f918ec44SMatthew G. Knepley #endif
799435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
8009566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
8013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80247c6ae99SBarry Smith }
80347c6ae99SBarry Smith 
804d7bf68aeSBarry Smith /*@
805bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
806d7bf68aeSBarry Smith 
80720f4b53cSBarry Smith     Collective
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith     Input Parameter:
810bb7acecfSBarry Smith .   dm - the `DM` object to setup
811d7bf68aeSBarry Smith 
812bb7acecfSBarry Smith     Level: intermediate
813d7bf68aeSBarry Smith 
814bb7acecfSBarry Smith     Note:
815bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
816bb7acecfSBarry Smith 
8171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
818d7bf68aeSBarry Smith @*/
819d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
820d71ae5a4SJacob Faibussowitsch {
821d7bf68aeSBarry Smith   PetscFunctionBegin;
822171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8233ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
824dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8258387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
827d7bf68aeSBarry Smith }
828d7bf68aeSBarry Smith 
829d7bf68aeSBarry Smith /*@
830bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
831d7bf68aeSBarry Smith 
83220f4b53cSBarry Smith     Collective
833d7bf68aeSBarry Smith 
834d7bf68aeSBarry Smith     Input Parameter:
835bb7acecfSBarry Smith .   dm - the `DM` object to set options for
836d7bf68aeSBarry Smith 
83720f4b53cSBarry Smith     Options Database Keys:
838bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
839bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
840bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
841a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
84220f4b53cSBarry 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`
84320f4b53cSBarry Smith . -dm_plex_filename <str>           - File containing a mesh
8449318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
845cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8465dca41c3SJed Brown . -dm_plex_shape <shape>            - The domain shape, such as `BOX`, `SPHERE`, etc.
8479318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8489318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8499318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
850bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
851bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8529318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8539318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8549318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8559318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
856bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType` for each direction
8579318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8589318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8599318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8609318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
861bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8629318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8639318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8649318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8659318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
866bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
871d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
872909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
873909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8749318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8759318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8769318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
87720f4b53cSBarry Smith .  -dm_plex_adj_closure <bool>       - Set adjacency size
87820f4b53cSBarry Smith .   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
879bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
880bb7acecfSBarry Smith .   -dm_plex_check_faces           - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - `DMPlexCheckFaces()`
881bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
882bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
883bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
884384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
885d7bf68aeSBarry Smith 
88695eb5ee5SVaclav Hapla     Level: intermediate
88795eb5ee5SVaclav Hapla 
8881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
889bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
890bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
891d7bf68aeSBarry Smith @*/
892d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
893d71ae5a4SJacob Faibussowitsch {
8947781c08eSBarry Smith   char      typeName[256];
895ca266f36SBarry Smith   PetscBool flg;
896d7bf68aeSBarry Smith 
897d7bf68aeSBarry Smith   PetscFunctionBegin;
898171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9009566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9019566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
902dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
903d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9061baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9081baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
909863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-dm_bind_below", "Set the size threshold (in entries) below which the Vec is bound to the CPU", "VecBindToCPU", dm->bind_below, &dm->bind_below, &flg));
912dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
913f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
914dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
915d0609cedSBarry Smith   PetscOptionsEnd();
9163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
917d7bf68aeSBarry Smith }
918d7bf68aeSBarry Smith 
919fc9bc008SSatish Balay /*@C
920bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
921fe2efc57SMark 
92220f4b53cSBarry Smith    Collective
923fe2efc57SMark 
924fe2efc57SMark    Input Parameters:
925bb7acecfSBarry Smith +  dm - the `DM` object
92620f4b53cSBarry Smith .  obj - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
927bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
928fe2efc57SMark 
929fe2efc57SMark    Level: intermediate
930bb7acecfSBarry Smith 
931bb7acecfSBarry Smith    Note:
932bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
933bb7acecfSBarry Smith 
9341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
935fe2efc57SMark @*/
936d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
937d71ae5a4SJacob Faibussowitsch {
938fe2efc57SMark   PetscFunctionBegin;
939fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9409566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
942fe2efc57SMark }
943fe2efc57SMark 
944fe2efc57SMark /*@C
945bb7acecfSBarry Smith     DMView - Views a `DM`. Depending on the `PetscViewer` and its `PetscViewerFormat` it may print some ASCII information about the `DM` to the screen or a file or
946bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94747c6ae99SBarry Smith 
94820f4b53cSBarry Smith     Collective
94947c6ae99SBarry Smith 
950d8d19677SJose E. Roman     Input Parameters:
951bb7acecfSBarry Smith +   dm - the `DM` object to view
95247c6ae99SBarry Smith -   v - the viewer
95347c6ae99SBarry Smith 
95420f4b53cSBarry Smith     Level: beginner
95520f4b53cSBarry Smith 
956cd7e8a5eSksagiyam     Notes:
957bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
958bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
959bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
960cd7e8a5eSksagiyam 
9611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96247c6ae99SBarry Smith @*/
963d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
964d71ae5a4SJacob Faibussowitsch {
96532c0f0efSBarry Smith   PetscBool         isbinary;
96676a8abe0SBarry Smith   PetscMPIInt       size;
96776a8abe0SBarry Smith   PetscViewerFormat format;
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith   PetscFunctionBegin;
970171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97148a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
972b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97374903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97474903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97574903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97674903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97774903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97874903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97974903a4fSStefano Zampini      in an error here */
98074903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9819566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
982b1b135c8SBarry Smith 
9839566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9853ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9869566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9879566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98832c0f0efSBarry Smith   if (isbinary) {
98955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99032c0f0efSBarry Smith     char     type[256];
99132c0f0efSBarry Smith 
9929566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
993c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9949566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99532c0f0efSBarry Smith   }
996dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
99847c6ae99SBarry Smith }
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith /*@
1001bb7acecfSBarry 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,
1002bb7acecfSBarry Smith     that is it has no ghost locations.
100347c6ae99SBarry Smith 
100420f4b53cSBarry Smith     Collective
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith     Input Parameter:
1007bb7acecfSBarry Smith .   dm - the `DM` object
100847c6ae99SBarry Smith 
100947c6ae99SBarry Smith     Output Parameter:
101047c6ae99SBarry Smith .   vec - the global vector
101147c6ae99SBarry Smith 
1012073dac72SJed Brown     Level: beginner
101347c6ae99SBarry Smith 
10141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1015bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101647c6ae99SBarry Smith @*/
1017d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1018d71ae5a4SJacob Faibussowitsch {
101947c6ae99SBarry Smith   PetscFunctionBegin;
1020171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1021b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1022dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1024c6b011d8SStefano Zampini     DM vdm;
1025c6b011d8SStefano Zampini 
10269566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10277a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1028c6b011d8SStefano Zampini   }
10293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
103047c6ae99SBarry Smith }
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith /*@
1033bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Not Collective
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith     Input Parameter:
1038bb7acecfSBarry Smith .   dm - the `DM` object
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith     Output Parameter:
104147c6ae99SBarry Smith .   vec - the local vector
104247c6ae99SBarry Smith 
1043073dac72SJed Brown     Level: beginner
104447c6ae99SBarry Smith 
104520f4b53cSBarry Smith     Note:
1046bb7acecfSBarry 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.
1047bb7acecfSBarry Smith 
10481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1049bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105047c6ae99SBarry Smith @*/
1051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1052d71ae5a4SJacob Faibussowitsch {
105347c6ae99SBarry Smith   PetscFunctionBegin;
1054171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1055b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1056dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1058c6b011d8SStefano Zampini     DM vdm;
1059c6b011d8SStefano Zampini 
10609566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10617a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1062c6b011d8SStefano Zampini   }
10633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106447c6ae99SBarry Smith }
106547c6ae99SBarry Smith 
10661411c6eeSJed Brown /*@
1067bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10681411c6eeSJed Brown 
106920f4b53cSBarry Smith    Collective
10701411c6eeSJed Brown 
10711411c6eeSJed Brown    Input Parameter:
1072bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10731411c6eeSJed Brown 
10741411c6eeSJed Brown    Output Parameter:
10751411c6eeSJed Brown .  ltog - the mapping
10761411c6eeSJed Brown 
1077bb7acecfSBarry Smith    Level: advanced
10781411c6eeSJed Brown 
10791411c6eeSJed Brown    Notes:
1080bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10811411c6eeSJed Brown 
1082bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1083bb7acecfSBarry Smith    need to use this function with those objects.
1084bb7acecfSBarry Smith 
1085bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1086bb7acecfSBarry Smith 
10871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1088bb7acecfSBarry Smith           `DMCreateMatrix()`
10891411c6eeSJed Brown @*/
1090d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1091d71ae5a4SJacob Faibussowitsch {
10920be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10931411c6eeSJed Brown 
10941411c6eeSJed Brown   PetscFunctionBegin;
10951411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10961411c6eeSJed Brown   PetscValidPointer(ltog, 2);
10971411c6eeSJed Brown   if (!dm->ltogmap) {
109837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109937d0c07bSMatthew G Knepley 
11009566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110137d0c07bSMatthew G Knepley     if (section) {
1102a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110337d0c07bSMatthew G Knepley       PetscInt       *ltog;
1104ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110537d0c07bSMatthew G Knepley 
11069566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11099566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1111e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111237d0c07bSMatthew G Knepley 
111337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11149566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11181a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11191a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1120ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11215227eafbSStefano Zampini 
1122e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11235227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1124e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1125e6befd46SJed Brown             cind++;
1126e6befd46SJed Brown           } else {
11275227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1128e6befd46SJed Brown           }
112937d0c07bSMatthew G Knepley         }
113037d0c07bSMatthew G Knepley       }
1131bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11329371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11339371c9d4SSatish Balay       bsLocal[1] = bs;
11349566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11359371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11369371c9d4SSatish Balay         bs = 1;
11379371c9d4SSatish Balay       } else {
11389371c9d4SSatish Balay         bs = bsMinMax[0];
11399371c9d4SSatish Balay       }
11407591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11417591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1142ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1143ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1144ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1145ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1146ca469d19SJed Brown         }
1147ccf3bd66SMatthew G. Knepley         n /= bs;
1148ccf3bd66SMatthew G. Knepley       }
11499566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1150dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115137d0c07bSMatthew G Knepley   }
11521411c6eeSJed Brown   *ltog = dm->ltogmap;
11533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11541411c6eeSJed Brown }
11551411c6eeSJed Brown 
11561411c6eeSJed Brown /*@
1157bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11581411c6eeSJed Brown 
11591411c6eeSJed Brown    Not Collective
11601411c6eeSJed Brown 
11611411c6eeSJed Brown    Input Parameter:
1162bb7acecfSBarry Smith .  dm - the `DM` with block structure
11631411c6eeSJed Brown 
11641411c6eeSJed Brown    Output Parameter:
11651411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11661411c6eeSJed Brown 
11671411c6eeSJed Brown    Level: intermediate
11681411c6eeSJed Brown 
1169bb7acecfSBarry Smith    Note:
1170bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1171bb7acecfSBarry Smith 
1172bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1173bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1174bb7acecfSBarry Smith 
11751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11761411c6eeSJed Brown @*/
1177d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1178d71ae5a4SJacob Faibussowitsch {
11791411c6eeSJed Brown   PetscFunctionBegin;
11801411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1181534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11827a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11831411c6eeSJed Brown   *bs = dm->bs;
11843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11851411c6eeSJed Brown }
11861411c6eeSJed Brown 
118748eeb7c8SBarry Smith /*@C
1188bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1189bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119047c6ae99SBarry Smith 
119120f4b53cSBarry Smith     Collective
119247c6ae99SBarry Smith 
1193d8d19677SJose E. Roman     Input Parameters:
1194bb7acecfSBarry Smith +   dmc - the `DM` object
1195bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
119647c6ae99SBarry Smith 
1197d8d19677SJose E. Roman     Output Parameters:
119847c6ae99SBarry Smith +  mat - the interpolation
1199bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
120047c6ae99SBarry Smith 
120147c6ae99SBarry Smith     Level: developer
120247c6ae99SBarry Smith 
120395452b02SPatrick Sanan     Notes:
1204bb7acecfSBarry 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
1205bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1206d52bd9f3SBarry Smith 
1207bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1208bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120985afcc9aSBarry Smith 
12101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121147c6ae99SBarry Smith @*/
1212d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1213d71ae5a4SJacob Faibussowitsch {
121447c6ae99SBarry Smith   PetscFunctionBegin;
1215a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1216a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1217c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
12189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1219dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122247c6ae99SBarry Smith }
122347c6ae99SBarry Smith 
12243ad4599aSBarry Smith /*@
1225bb7acecfSBarry Smith     DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is the transpose of the interpolation between the `DM`.
1226bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1227bb7acecfSBarry Smith     representation of xfine.
12282ed6491fSPatrick Sanan 
12292ed6491fSPatrick Sanan   Input Parameters:
1230bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1231bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12322ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12332ed6491fSPatrick Sanan 
12342ed6491fSPatrick Sanan   Output Parameter:
12352ed6491fSPatrick Sanan .    scale - the scaled vector
12362ed6491fSPatrick Sanan 
1237bb7acecfSBarry Smith   Level: advanced
12382ed6491fSPatrick Sanan 
123920f4b53cSBarry Smith   Developer Note:
1240bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1241e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1242e9c74fd6SRichard Tran Mills 
12431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12442ed6491fSPatrick Sanan @*/
1245d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1246d71ae5a4SJacob Faibussowitsch {
12472ed6491fSPatrick Sanan   Vec         fine;
12482ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12499704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1250e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12519704db99SRichard Tran Mills #endif
12522ed6491fSPatrick Sanan 
12532ed6491fSPatrick Sanan   PetscFunctionBegin;
12549566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12559566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12569566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12579704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12589704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12599704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12609704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12619566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1262e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12639566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12649566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12659566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126683aa49f4SRichard Tran Mills   }
12679704db99SRichard Tran Mills #endif
12689566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12699566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12709566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12722ed6491fSPatrick Sanan }
12732ed6491fSPatrick Sanan 
12742ed6491fSPatrick Sanan /*@
1275bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1276bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12773ad4599aSBarry Smith 
127820f4b53cSBarry Smith     Collective
12793ad4599aSBarry Smith 
1280d8d19677SJose E. Roman     Input Parameters:
1281bb7acecfSBarry Smith +   dmc - the `DM` object
1282bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12833ad4599aSBarry Smith 
12843ad4599aSBarry Smith     Output Parameter:
12853ad4599aSBarry Smith .  mat - the restriction
12863ad4599aSBarry Smith 
12873ad4599aSBarry Smith     Level: developer
12883ad4599aSBarry Smith 
1289bb7acecfSBarry Smith     Note:
1290bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1291bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12923ad4599aSBarry Smith 
12931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12943ad4599aSBarry Smith @*/
1295d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1296d71ae5a4SJacob Faibussowitsch {
12973ad4599aSBarry Smith   PetscFunctionBegin;
1298a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1299a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13005a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1302dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13039566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13053ad4599aSBarry Smith }
13063ad4599aSBarry Smith 
130747c6ae99SBarry Smith /*@
1308bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1309bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1310bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1311bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
131247c6ae99SBarry Smith 
131320f4b53cSBarry Smith     Collective
131447c6ae99SBarry Smith 
1315d8d19677SJose E. Roman     Input Parameters:
1316bb7acecfSBarry Smith +   dac - the `DM` object
1317bb7acecfSBarry Smith -   daf - the second, finer `DM` object
131847c6ae99SBarry Smith 
131947c6ae99SBarry Smith     Output Parameter:
13206dbf9973SLawrence Mitchell .   mat - the injection
132147c6ae99SBarry Smith 
132247c6ae99SBarry Smith     Level: developer
132347c6ae99SBarry Smith 
1324bb7acecfSBarry Smith    Note:
1325bb7acecfSBarry 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
1326bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132785afcc9aSBarry Smith 
13281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1329bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133047c6ae99SBarry Smith @*/
1331d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1332d71ae5a4SJacob Faibussowitsch {
133347c6ae99SBarry Smith   PetscFunctionBegin;
1334a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1335a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13365a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1338dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134147c6ae99SBarry Smith }
134247c6ae99SBarry Smith 
1343b412c318SBarry Smith /*@
1344bb7acecfSBarry 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
1345bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1346bd041c0cSMatthew G. Knepley 
134720f4b53cSBarry Smith   Collective
1348bd041c0cSMatthew G. Knepley 
1349d8d19677SJose E. Roman   Input Parameters:
1350bb7acecfSBarry Smith + dmc - the target `DM` object
1351bb7acecfSBarry Smith - dmf - the source `DM` object
1352bd041c0cSMatthew G. Knepley 
1353bd041c0cSMatthew G. Knepley   Output Parameter:
1354b4937a87SMatthew G. Knepley . mat - the mass matrix
1355bd041c0cSMatthew G. Knepley 
1356bd041c0cSMatthew G. Knepley   Level: developer
1357bd041c0cSMatthew G. Knepley 
1358bb7acecfSBarry Smith   Notes:
1359bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1360bb7acecfSBarry Smith 
136120f4b53cSBarry 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()`
1362bb7acecfSBarry Smith 
13631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1364bd041c0cSMatthew G. Knepley @*/
1365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1366d71ae5a4SJacob Faibussowitsch {
1367bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1368b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1369b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13705a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13715b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1372dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13735b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1375b4937a87SMatthew G. Knepley }
1376b4937a87SMatthew G. Knepley 
1377b4937a87SMatthew G. Knepley /*@
1378bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1379b4937a87SMatthew G. Knepley 
138020f4b53cSBarry Smith   Collective
1381b4937a87SMatthew G. Knepley 
1382b4937a87SMatthew G. Knepley   Input Parameter:
1383bb7acecfSBarry Smith . dm - the `DM` object
1384b4937a87SMatthew G. Knepley 
1385b4937a87SMatthew G. Knepley   Output Parameter:
1386bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1387b4937a87SMatthew G. Knepley 
1388b4937a87SMatthew G. Knepley   Level: developer
1389b4937a87SMatthew G. Knepley 
1390bb7acecfSBarry Smith   Note:
1391bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1392bb7acecfSBarry Smith 
13931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1394b4937a87SMatthew G. Knepley @*/
1395d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1396d71ae5a4SJacob Faibussowitsch {
1397b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1398b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1399b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1400dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1402bd041c0cSMatthew G. Knepley }
1403bd041c0cSMatthew G. Knepley 
1404bd041c0cSMatthew G. Knepley /*@
1405bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1406bb7acecfSBarry Smith     of a PDE on the `DM`.
140747c6ae99SBarry Smith 
140820f4b53cSBarry Smith     Collective
140947c6ae99SBarry Smith 
1410d8d19677SJose E. Roman     Input Parameters:
1411bb7acecfSBarry Smith +   dm - the `DM` object
1412bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141347c6ae99SBarry Smith 
141447c6ae99SBarry Smith     Output Parameter:
141547c6ae99SBarry Smith .   coloring - the coloring
141647c6ae99SBarry Smith 
14171bf8429eSBarry Smith     Level: developer
14181bf8429eSBarry Smith 
1419ec5066bdSBarry Smith     Notes:
1420bb7acecfSBarry 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
1421bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1422ec5066bdSBarry Smith 
1423bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14241bf8429eSBarry 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,
14251bf8429eSBarry Smith     otherwise an error will be generated.
1426ec5066bdSBarry Smith 
14271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1428aab9d709SJed Brown @*/
1429d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1430d71ae5a4SJacob Faibussowitsch {
143147c6ae99SBarry Smith   PetscFunctionBegin;
1432171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14335a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1434dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
143647c6ae99SBarry Smith }
143747c6ae99SBarry Smith 
1438b412c318SBarry Smith /*@
1439bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144047c6ae99SBarry Smith 
144120f4b53cSBarry Smith     Collective
144247c6ae99SBarry Smith 
144347c6ae99SBarry Smith     Input Parameter:
1444bb7acecfSBarry Smith .   dm - the `DM` object
144547c6ae99SBarry Smith 
144647c6ae99SBarry Smith     Output Parameter:
144747c6ae99SBarry Smith .   mat - the empty Jacobian
144847c6ae99SBarry Smith 
144920f4b53cSBarry Smith     Options Database Key:
1450bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1451f27dd7c6SMatthew G. Knepley 
145220f4b53cSBarry Smith     Level: beginner
145320f4b53cSBarry Smith 
145495452b02SPatrick Sanan     Notes:
145595452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
145694013140SBarry Smith     do not need to do it yourself.
145794013140SBarry Smith 
145894013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1459bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146094013140SBarry Smith 
1461bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146294013140SBarry Smith     internally by PETSc.
146394013140SBarry Smith 
1464bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1465bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146694013140SBarry Smith 
14671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1468aab9d709SJed Brown @*/
1469d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1470d71ae5a4SJacob Faibussowitsch {
147147c6ae99SBarry Smith   PetscFunctionBegin;
1472171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1473064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14749566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14759566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1476dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1478c6b011d8SStefano Zampini     DM mdm;
1479c6b011d8SStefano Zampini 
14809566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14817a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1482c6b011d8SStefano Zampini   }
1483e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1484e5e52638SMatthew G. Knepley   if (dm->Nf) {
1485e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1486649ef022SMatthew Knepley     PetscInt     Nf, f;
1487e571a35bSMatthew G. Knepley 
14889566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1489649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1490649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14919566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14929566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14939566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1494649ef022SMatthew Knepley         break;
1495e571a35bSMatthew G. Knepley       }
1496649ef022SMatthew Knepley     }
1497649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1498649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14999566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15009566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15019566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1502e571a35bSMatthew G. Knepley       }
1503e571a35bSMatthew G. Knepley     }
1504e571a35bSMatthew G. Knepley   }
15059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
150747c6ae99SBarry Smith }
150847c6ae99SBarry Smith 
1509732e2eb9SMatthew G Knepley /*@
1510bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1511bb7acecfSBarry Smith   properly set, but the data structures to store values in the matrices will not be preallocated. This is most useful to reduce initialization costs when
1512bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1513aa0f6e3cSJed Brown 
151420f4b53cSBarry Smith   Logically Collective
1515aa0f6e3cSJed Brown 
1516aa0f6e3cSJed Brown   Input Parameters:
1517bb7acecfSBarry Smith + dm - the `DM`
1518bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1519aa0f6e3cSJed Brown 
1520aa0f6e3cSJed Brown   Level: developer
1521aa0f6e3cSJed Brown 
15221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1523aa0f6e3cSJed Brown @*/
1524d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1525d71ae5a4SJacob Faibussowitsch {
1526aa0f6e3cSJed Brown   PetscFunctionBegin;
1527aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1528aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1530aa0f6e3cSJed Brown }
1531aa0f6e3cSJed Brown 
1532aa0f6e3cSJed Brown /*@
1533bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1534732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1535732e2eb9SMatthew G Knepley 
153620f4b53cSBarry Smith   Logically Collective
1537732e2eb9SMatthew G Knepley 
1538d8d19677SJose E. Roman   Input Parameters:
1539bb7acecfSBarry Smith + dm - the `DM`
1540bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1541732e2eb9SMatthew G Knepley 
154220f4b53cSBarry Smith   Options Database Key:
1543bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1544f27dd7c6SMatthew G. Knepley 
154520f4b53cSBarry Smith   Level: developer
154620f4b53cSBarry Smith 
15471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1548732e2eb9SMatthew G Knepley @*/
1549d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1550d71ae5a4SJacob Faibussowitsch {
1551732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1552732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1553732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1555732e2eb9SMatthew G Knepley }
1556732e2eb9SMatthew G Knepley 
1557b06ff27eSHong Zhang /*@
1558bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1559bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1560b06ff27eSHong Zhang 
156120f4b53cSBarry Smith   Logically Collective
1562b06ff27eSHong Zhang 
1563d8d19677SJose E. Roman   Input Parameters:
1564bb7acecfSBarry Smith + dm - the `DM`
1565da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1566b06ff27eSHong Zhang 
1567b06ff27eSHong Zhang   Level: developer
1568bb7acecfSBarry Smith 
15691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1570b06ff27eSHong Zhang @*/
1571d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1572d71ae5a4SJacob Faibussowitsch {
1573b06ff27eSHong Zhang   PetscFunctionBegin;
1574b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1575b06ff27eSHong Zhang   dm->structure_only = only;
15763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1577b06ff27eSHong Zhang }
1578b06ff27eSHong Zhang 
1579863027abSJed Brown /*@
1580863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1581863027abSJed Brown 
158220f4b53cSBarry Smith   Logically Collective
1583863027abSJed Brown 
1584863027abSJed Brown   Input Parameters:
1585863027abSJed Brown + dm - the `DM`
1586863027abSJed Brown - btype - block by topological point or field node
1587863027abSJed Brown 
158820f4b53cSBarry Smith   Options Database Key:
15890e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1590863027abSJed Brown 
159120f4b53cSBarry Smith   Level: advanced
159220f4b53cSBarry Smith 
15931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1594863027abSJed Brown @*/
1595863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1596863027abSJed Brown {
1597863027abSJed Brown   PetscFunctionBegin;
1598863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1599863027abSJed Brown   dm->blocking_type = btype;
16003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1601863027abSJed Brown }
1602863027abSJed Brown 
1603863027abSJed Brown /*@
1604863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1605863027abSJed Brown 
1606863027abSJed Brown   Not Collective
1607863027abSJed Brown 
16082fe279fdSBarry Smith   Input Parameter:
1609863027abSJed Brown . dm - the `DM`
1610863027abSJed Brown 
16112fe279fdSBarry Smith   Output Parameter:
1612863027abSJed Brown . btype - block by topological point or field node
1613863027abSJed Brown 
1614863027abSJed Brown   Level: advanced
1615863027abSJed Brown 
16161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1617863027abSJed Brown @*/
1618863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1619863027abSJed Brown {
1620863027abSJed Brown   PetscFunctionBegin;
1621863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1622863027abSJed Brown   PetscValidPointer(btype, 2);
1623863027abSJed Brown   *btype = dm->blocking_type;
16243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1625863027abSJed Brown }
1626863027abSJed Brown 
1627a89ea682SMatthew G Knepley /*@C
1628bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1629a89ea682SMatthew G Knepley 
1630a89ea682SMatthew G Knepley   Not Collective
1631a89ea682SMatthew G Knepley 
1632a89ea682SMatthew G Knepley   Input Parameters:
1633bb7acecfSBarry Smith + dm - the `DM` object
1634a5b23f4aSJose E. Roman . count - The minimum size
163520f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1636a89ea682SMatthew G Knepley 
1637a89ea682SMatthew G Knepley   Output Parameter:
1638a89ea682SMatthew G Knepley . array - the work array
1639a89ea682SMatthew G Knepley 
1640a89ea682SMatthew G Knepley   Level: developer
1641a89ea682SMatthew G Knepley 
1642bb7acecfSBarry Smith   Note:
1643da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1644bb7acecfSBarry Smith 
1645bb7acecfSBarry Smith   The array may contain nonzero values
1646bb7acecfSBarry Smith 
16471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1648a89ea682SMatthew G Knepley @*/
1649d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1650d71ae5a4SJacob Faibussowitsch {
1651aa1993deSMatthew G Knepley   DMWorkLink  link;
165269291d52SBarry Smith   PetscMPIInt dsize;
1653a89ea682SMatthew G Knepley 
1654a89ea682SMatthew G Knepley   PetscFunctionBegin;
1655a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1656aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1657442f3b32SStefano Zampini   if (!count) {
1658442f3b32SStefano Zampini     *(void **)mem = NULL;
1659442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1660442f3b32SStefano Zampini   }
1661aa1993deSMatthew G Knepley   if (dm->workin) {
1662aa1993deSMatthew G Knepley     link       = dm->workin;
1663aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1664aa1993deSMatthew G Knepley   } else {
16654dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1666a89ea682SMatthew G Knepley   }
1667*b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1668*b77fa653SStefano Zampini      Get size directly */
1669*b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1670*b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1671*b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1672*b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1673*b77fa653SStefano Zampini #endif
1674*b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1675*b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1676*b77fa653SStefano Zampini #endif
1677*b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1678*b77fa653SStefano Zampini 
16795056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16809566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16819566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1682854ce69bSBarry Smith     link->bytes = dsize * count;
1683aa1993deSMatthew G Knepley   }
1684aa1993deSMatthew G Knepley   link->next  = dm->workout;
1685aa1993deSMatthew G Knepley   dm->workout = link;
1686cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
168700d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
168800d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
168900d952a4SJed Brown #endif
1690aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1692a89ea682SMatthew G Knepley }
1693a89ea682SMatthew G Knepley 
1694aa1993deSMatthew G Knepley /*@C
1695bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1696aa1993deSMatthew G Knepley 
1697aa1993deSMatthew G Knepley   Not Collective
1698aa1993deSMatthew G Knepley 
1699aa1993deSMatthew G Knepley   Input Parameters:
1700bb7acecfSBarry Smith + dm - the `DM` object
1701a5b23f4aSJose E. Roman . count - The minimum size
170220f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1703aa1993deSMatthew G Knepley 
1704aa1993deSMatthew G Knepley   Output Parameter:
1705aa1993deSMatthew G Knepley . array - the work array
1706aa1993deSMatthew G Knepley 
1707aa1993deSMatthew G Knepley   Level: developer
1708aa1993deSMatthew G Knepley 
170920f4b53cSBarry Smith   Developer Note:
1710bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1711147403d9SBarry Smith 
17121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1713aa1993deSMatthew G Knepley @*/
1714d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1715d71ae5a4SJacob Faibussowitsch {
1716aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1717aa1993deSMatthew G Knepley 
1718aa1993deSMatthew G Knepley   PetscFunctionBegin;
1719aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1720aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1721442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1722aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1723aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1724aa1993deSMatthew G Knepley       *p            = link->next;
1725aa1993deSMatthew G Knepley       link->next    = dm->workin;
1726aa1993deSMatthew G Knepley       dm->workin    = link;
17270298fd71SBarry Smith       *(void **)mem = NULL;
17283ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1729aa1993deSMatthew G Knepley     }
1730aa1993deSMatthew G Knepley   }
1731aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1732aa1993deSMatthew G Knepley }
1733e7c4fc90SDmitry Karpeev 
17348cda7954SMatthew G. Knepley /*@C
1735bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1736bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17378cda7954SMatthew G. Knepley 
173820f4b53cSBarry Smith   Logically Collective; No Fortran Support
17398cda7954SMatthew G. Knepley 
17408cda7954SMatthew G. Knepley   Input Parameters:
1741bb7acecfSBarry Smith + dm     - The `DM`
17428cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17438cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17448cda7954SMatthew G. Knepley 
174520f4b53cSBarry Smith   Calling sequence of `nullsp`:
1746147403d9SBarry Smith .vb
1747147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1748147403d9SBarry Smith .ve
1749bb7acecfSBarry Smith +  dm        - The present `DM`
1750bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1751147403d9SBarry Smith .  field     - The field number in dm
1752147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17538cda7954SMatthew G. Knepley 
175449762cbcSSatish Balay   Level: intermediate
175549762cbcSSatish Balay 
17561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1757147403d9SBarry Smith @*/
1758d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1759d71ae5a4SJacob Faibussowitsch {
1760435a35e8SMatthew G Knepley   PetscFunctionBegin;
1761435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17627a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1763435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1765435a35e8SMatthew G Knepley }
1766435a35e8SMatthew G Knepley 
17678cda7954SMatthew G. Knepley /*@C
1768bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17698cda7954SMatthew G. Knepley 
177020f4b53cSBarry Smith   Not Collective; No Fortran Support
17718cda7954SMatthew G. Knepley 
17728cda7954SMatthew G. Knepley   Input Parameters:
1773bb7acecfSBarry Smith + dm     - The `DM`
17748cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17758cda7954SMatthew G. Knepley 
17768cda7954SMatthew G. Knepley   Output Parameter:
17778cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17788cda7954SMatthew G. Knepley 
177920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1780147403d9SBarry Smith .vb
1781147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1782147403d9SBarry Smith .ve
1783147403d9SBarry Smith +  dm        - The present DM
1784147403d9SBarry Smith .  origField - The field number given above, in the original DM
1785147403d9SBarry Smith .  field     - The field number in dm
1786147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17878cda7954SMatthew G. Knepley 
178849762cbcSSatish Balay    Level: intermediate
178949762cbcSSatish Balay 
17901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1791147403d9SBarry Smith @*/
1792d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1793d71ae5a4SJacob Faibussowitsch {
17940a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17950a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1796f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17977a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17980a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18000a50eb56SMatthew G. Knepley }
18010a50eb56SMatthew G. Knepley 
18028cda7954SMatthew G. Knepley /*@C
1803bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18048cda7954SMatthew G. Knepley 
180520f4b53cSBarry Smith   Logically Collective; No Fortran Support
18068cda7954SMatthew G. Knepley 
18078cda7954SMatthew G. Knepley   Input Parameters:
1808bb7acecfSBarry Smith + dm     - The `DM`
18098cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18108cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18118cda7954SMatthew G. Knepley 
181220f4b53cSBarry Smith   Calling sequence of `nullsp`:
1813147403d9SBarry Smith .vb
1814147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1815147403d9SBarry Smith .ve
1816bb7acecfSBarry Smith +  dm        - The present `DM`
1817bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1818147403d9SBarry Smith .  field     - The field number in dm
1819147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18208cda7954SMatthew G. Knepley 
182149762cbcSSatish Balay    Level: intermediate
182249762cbcSSatish Balay 
18231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1824bb7acecfSBarry Smith           `MatNullSpace`
1825147403d9SBarry Smith @*/
1826d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1827d71ae5a4SJacob Faibussowitsch {
1828f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1829f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18307a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1831f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1833f9d4088aSMatthew G. Knepley }
1834f9d4088aSMatthew G. Knepley 
18358cda7954SMatthew G. Knepley /*@C
1836bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18378cda7954SMatthew G. Knepley 
183820f4b53cSBarry Smith   Not Collective; No Fortran Support
18398cda7954SMatthew G. Knepley 
18408cda7954SMatthew G. Knepley   Input Parameters:
1841bb7acecfSBarry Smith + dm     - The `DM`
18428cda7954SMatthew G. Knepley - field  - The field number for the nullspace
18438cda7954SMatthew G. Knepley 
18448cda7954SMatthew G. Knepley   Output Parameter:
18458cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18468cda7954SMatthew G. Knepley 
184720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1848147403d9SBarry Smith .vb
1849147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1850147403d9SBarry Smith .ve
1851bb7acecfSBarry Smith +  dm        - The present `DM`
1852bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1853147403d9SBarry Smith .  field     - The field number in dm
1854147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18558cda7954SMatthew G. Knepley 
185649762cbcSSatish Balay    Level: intermediate
185749762cbcSSatish Balay 
18581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1859bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1860147403d9SBarry Smith @*/
1861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1862d71ae5a4SJacob Faibussowitsch {
1863f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1864f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1865f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18667a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1867f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1869f9d4088aSMatthew G. Knepley }
1870f9d4088aSMatthew G. Knepley 
18714f3b5142SJed Brown /*@C
1872bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18734d343eeaSMatthew G Knepley 
187420f4b53cSBarry Smith   Not Collective; No Fortran Support
18754d343eeaSMatthew G Knepley 
18764d343eeaSMatthew G Knepley   Input Parameter:
1877bb7acecfSBarry Smith . dm - the `DM` object
18784d343eeaSMatthew G Knepley 
18794d343eeaSMatthew G Knepley   Output Parameters:
188020f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
188120f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
188220f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18834d343eeaSMatthew G Knepley 
18844d343eeaSMatthew G Knepley   Level: intermediate
18854d343eeaSMatthew G Knepley 
1886bb7acecfSBarry Smith   Note:
188721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1888bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1889bb7acecfSBarry Smith   `PetscFree()`.
189021c9b008SJed Brown 
1891bb7acecfSBarry Smith   Developer Note:
1892bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1893bb7acecfSBarry Smith   likely be removed.
1894bb7acecfSBarry Smith 
18951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1896bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18974d343eeaSMatthew G Knepley @*/
1898d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1899d71ae5a4SJacob Faibussowitsch {
190037d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19014d343eeaSMatthew G Knepley 
19024d343eeaSMatthew G Knepley   PetscFunctionBegin;
19034d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190469ca1f37SDmitry Karpeev   if (numFields) {
1905534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
190669ca1f37SDmitry Karpeev     *numFields = 0;
190769ca1f37SDmitry Karpeev   }
190837d0c07bSMatthew G Knepley   if (fieldNames) {
190937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
19100298fd71SBarry Smith     *fieldNames = NULL;
191169ca1f37SDmitry Karpeev   }
191269ca1f37SDmitry Karpeev   if (fields) {
191369ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
19140298fd71SBarry Smith     *fields = NULL;
191569ca1f37SDmitry Karpeev   }
19169566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
191737d0c07bSMatthew G Knepley   if (section) {
19183a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
191937d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
192037d0c07bSMatthew G Knepley 
19219566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19239566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
192637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
192837d0c07bSMatthew G Knepley     }
192937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193037d0c07bSMatthew G Knepley       PetscInt gdof;
193137d0c07bSMatthew G Knepley 
19329566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193337d0c07bSMatthew G Knepley       if (gdof > 0) {
193437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19353a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
193637d0c07bSMatthew G Knepley 
19379566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19389566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19393a544194SStefano Zampini           fpdof = fdof - fcdof;
19403a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19413a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19423a544194SStefano Zampini             fieldNc[f] = 1;
19433a544194SStefano Zampini           }
19443a544194SStefano Zampini           fieldSizes[f] += fpdof;
194537d0c07bSMatthew G Knepley         }
194637d0c07bSMatthew G Knepley       }
194737d0c07bSMatthew G Knepley     }
194837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19499566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
195037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195137d0c07bSMatthew G Knepley     }
195237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195437d0c07bSMatthew G Knepley 
19559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195637d0c07bSMatthew G Knepley       if (gdof > 0) {
19579566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
195837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
195937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
196037d0c07bSMatthew G Knepley 
19619566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19629566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1963ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196437d0c07bSMatthew G Knepley         }
196537d0c07bSMatthew G Knepley       }
196637d0c07bSMatthew G Knepley     }
19678865f1eaSKarl Rupp     if (numFields) *numFields = nF;
196837d0c07bSMatthew G Knepley     if (fieldNames) {
19699566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
197037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197137d0c07bSMatthew G Knepley         const char *fieldName;
197237d0c07bSMatthew G Knepley 
19739566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19749566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197537d0c07bSMatthew G Knepley       }
197637d0c07bSMatthew G Knepley     }
197737d0c07bSMatthew G Knepley     if (fields) {
19789566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
197937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19803a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19813a544194SStefano Zampini 
19829566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19833a544194SStefano Zampini         in[0] = -fieldNc[f];
19843a544194SStefano Zampini         in[1] = fieldNc[f];
19851c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19863a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19879566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
198837d0c07bSMatthew G Knepley       }
198937d0c07bSMatthew G Knepley     }
19909566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1991dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19934d343eeaSMatthew G Knepley }
19944d343eeaSMatthew G Knepley 
199516621825SDmitry Karpeev /*@C
1996bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1997bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1998bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1999bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2000e7c4fc90SDmitry Karpeev 
200120f4b53cSBarry Smith   Not Collective; No Fortran Support
2002e7c4fc90SDmitry Karpeev 
2003e7c4fc90SDmitry Karpeev   Input Parameter:
2004bb7acecfSBarry Smith . dm - the `DM` object
2005e7c4fc90SDmitry Karpeev 
2006e7c4fc90SDmitry Karpeev   Output Parameters:
200720f4b53cSBarry Smith + len       - The number of fields (or `NULL` if not requested)
200820f4b53cSBarry Smith . namelist  - The name for each field (or `NULL` if not requested)
200920f4b53cSBarry Smith . islist    - The global indices for each field (or `NULL` if not requested)
201020f4b53cSBarry Smith - dmlist    - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2011e7c4fc90SDmitry Karpeev 
2012e7c4fc90SDmitry Karpeev   Level: intermediate
2013e7c4fc90SDmitry Karpeev 
2014bb7acecfSBarry Smith   Note:
2015e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2016bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2017bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2018e7c4fc90SDmitry Karpeev 
2019bb7acecfSBarry Smith   Developer Note:
2020bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2021bb7acecfSBarry Smith 
20221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2023e7c4fc90SDmitry Karpeev @*/
2024d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2025d71ae5a4SJacob Faibussowitsch {
2026e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2027e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20288865f1eaSKarl Rupp   if (len) {
2029534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
20308865f1eaSKarl Rupp     *len = 0;
20318865f1eaSKarl Rupp   }
20328865f1eaSKarl Rupp   if (namelist) {
20338865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
2034ea78f98cSLisandro Dalcin     *namelist = NULL;
20358865f1eaSKarl Rupp   }
20368865f1eaSKarl Rupp   if (islist) {
20378865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
2038ea78f98cSLisandro Dalcin     *islist = NULL;
20398865f1eaSKarl Rupp   }
20408865f1eaSKarl Rupp   if (dmlist) {
20418865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
2042ea78f98cSLisandro Dalcin     *dmlist = NULL;
20438865f1eaSKarl Rupp   }
2044f3f0edfdSDmitry Karpeev   /*
2045f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2046f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2047f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2048f3f0edfdSDmitry Karpeev    */
20497a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2051435a35e8SMatthew G Knepley     PetscSection section;
2052435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2053435a35e8SMatthew G Knepley 
20549566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20559566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2056435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2057f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20589566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20599566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20609566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2061435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2062435a35e8SMatthew G Knepley         const char *fieldName;
2063435a35e8SMatthew G Knepley 
20649566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
206503dc3394SMatthew G. Knepley         if (namelist) {
20669566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20679566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2068435a35e8SMatthew G Knepley         }
206903dc3394SMatthew G. Knepley       }
2070435a35e8SMatthew G Knepley     } else {
20719566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2072e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20730298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2074e7c4fc90SDmitry Karpeev     }
2075dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
207716621825SDmitry Karpeev }
207816621825SDmitry Karpeev 
2079412a4547SAlexis Marboeuf /*@C
208020f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
208120f4b53cSBarry Smith                   The fields are defined by `DMCreateFieldIS()`.
2082435a35e8SMatthew G Knepley 
2083435a35e8SMatthew G Knepley   Not collective
2084435a35e8SMatthew G Knepley 
2085435a35e8SMatthew G Knepley   Input Parameters:
2086bb7acecfSBarry Smith + dm        - The `DM` object
2087bb7acecfSBarry Smith . numFields - The number of fields to select
20882adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2089435a35e8SMatthew G Knepley 
2090435a35e8SMatthew G Knepley   Output Parameters:
2091bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2092bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2093435a35e8SMatthew G Knepley 
209420f4b53cSBarry Smith   Level: intermediate
209520f4b53cSBarry Smith 
2096bb7acecfSBarry Smith   Note:
2097bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20985d3b26e6SMatthew G. Knepley 
20991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2100435a35e8SMatthew G Knepley @*/
2101d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2102d71ae5a4SJacob Faibussowitsch {
2103435a35e8SMatthew G Knepley   PetscFunctionBegin;
2104435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2105dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
21068865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
21078865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2108dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2110435a35e8SMatthew G Knepley }
2111435a35e8SMatthew G Knepley 
21122adcc780SMatthew G. Knepley /*@C
2113bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21142adcc780SMatthew G. Knepley 
21152adcc780SMatthew G. Knepley   Not collective
21162adcc780SMatthew G. Knepley 
2117d8d19677SJose E. Roman   Input Parameters:
2118bb7acecfSBarry Smith + dms - The `DM` objects
2119bb7acecfSBarry Smith - n - The number of `DM`s
21202adcc780SMatthew G. Knepley 
21212adcc780SMatthew G. Knepley   Output Parameters:
2122bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2123bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21242adcc780SMatthew G. Knepley 
212520f4b53cSBarry Smith   Level: intermediate
212620f4b53cSBarry Smith 
2127bb7acecfSBarry Smith   Note:
2128bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21295d3b26e6SMatthew G. Knepley 
21301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21312adcc780SMatthew G. Knepley @*/
2132d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2133d71ae5a4SJacob Faibussowitsch {
21342adcc780SMatthew G. Knepley   PetscInt i;
21352adcc780SMatthew G. Knepley 
21362adcc780SMatthew G. Knepley   PetscFunctionBegin;
21372adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2138ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21392adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2140a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2141bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2142bb7acecfSBarry Smith   if (n) {
2143b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2144dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21452adcc780SMatthew G. Knepley   }
21463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21472adcc780SMatthew G. Knepley }
21482adcc780SMatthew G. Knepley 
214916621825SDmitry Karpeev /*@C
2150bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2151bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2152bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2153bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2154bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
215516621825SDmitry Karpeev 
215620f4b53cSBarry Smith   Not Collective
215716621825SDmitry Karpeev 
215816621825SDmitry Karpeev   Input Parameter:
2159bb7acecfSBarry Smith . dm - the `DM` object
216016621825SDmitry Karpeev 
216116621825SDmitry Karpeev   Output Parameters:
216220f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
216320f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21640298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21650298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
216620f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
216716621825SDmitry Karpeev 
216816621825SDmitry Karpeev   Level: intermediate
216916621825SDmitry Karpeev 
2170bb7acecfSBarry Smith   Note:
217116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2172bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2173bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
217416621825SDmitry Karpeev 
2175bb7acecfSBarry Smith   Questions:
217620f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2177bb7acecfSBarry Smith 
21781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
217916621825SDmitry Karpeev @*/
2180d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2181d71ae5a4SJacob Faibussowitsch {
2182be081cd6SPeter Brune   DMSubDomainHookLink link;
2183be081cd6SPeter Brune   PetscInt            i, l;
218416621825SDmitry Karpeev 
218516621825SDmitry Karpeev   PetscFunctionBegin;
218616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21879371c9d4SSatish Balay   if (n) {
21889371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
21899371c9d4SSatish Balay     *n = 0;
21909371c9d4SSatish Balay   }
21919371c9d4SSatish Balay   if (namelist) {
21929371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
21939371c9d4SSatish Balay     *namelist = NULL;
21949371c9d4SSatish Balay   }
21959371c9d4SSatish Balay   if (innerislist) {
21969371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
21979371c9d4SSatish Balay     *innerislist = NULL;
21989371c9d4SSatish Balay   }
21999371c9d4SSatish Balay   if (outerislist) {
22009371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
22019371c9d4SSatish Balay     *outerislist = NULL;
22029371c9d4SSatish Balay   }
22039371c9d4SSatish Balay   if (dmlist) {
22049371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
22059371c9d4SSatish Balay     *dmlist = NULL;
22069371c9d4SSatish Balay   }
2207f3f0edfdSDmitry Karpeev   /*
2208f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2209f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2210f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2211f3f0edfdSDmitry Karpeev    */
22127a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
221316621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2214dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
221514a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2216f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2217be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2218be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22199566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2220be081cd6SPeter Brune         }
2221648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2222e7c4fc90SDmitry Karpeev       }
222314a18fd3SPeter Brune     }
2224bb7acecfSBarry Smith     if (n) *n = l;
222514a18fd3SPeter Brune   }
22263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2227e30e807fSPeter Brune }
2228e30e807fSPeter Brune 
2229e30e807fSPeter Brune /*@C
2230e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2231e30e807fSPeter Brune 
223220f4b53cSBarry Smith   Not Collective
2233e30e807fSPeter Brune 
2234e30e807fSPeter Brune   Input Parameters:
2235bb7acecfSBarry Smith + dm - the `DM` object
2236e30e807fSPeter Brune . n  - the number of subdomain scatters
2237e30e807fSPeter Brune - subdms - the local subdomains
2238e30e807fSPeter Brune 
2239e30e807fSPeter Brune   Output Parameters:
22406b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2241e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2242e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2243e30e807fSPeter Brune 
224420f4b53cSBarry Smith   Level: developer
224520f4b53cSBarry Smith 
2246bb7acecfSBarry Smith   Note:
2247bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2248e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2249e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2250e30e807fSPeter Brune   solution and residual data.
2251e30e807fSPeter Brune 
2252bb7acecfSBarry Smith   Questions:
2253bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2254bb7acecfSBarry Smith 
22551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2256e30e807fSPeter Brune @*/
2257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2258d71ae5a4SJacob Faibussowitsch {
2259e30e807fSPeter Brune   PetscFunctionBegin;
2260e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2261e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2262dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2264e7c4fc90SDmitry Karpeev }
2265e7c4fc90SDmitry Karpeev 
226647c6ae99SBarry Smith /*@
2267bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
226847c6ae99SBarry Smith 
226920f4b53cSBarry Smith   Collective
227047c6ae99SBarry Smith 
2271d8d19677SJose E. Roman   Input Parameters:
2272bb7acecfSBarry Smith + dm   - the `DM` object
2273bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
227447c6ae99SBarry Smith 
227547c6ae99SBarry Smith   Output Parameter:
227620f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2277ae0a1c52SMatthew G Knepley 
227820f4b53cSBarry Smith   Options Database Key:
2279412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2280412e9a14SMatthew G. Knepley 
228147c6ae99SBarry Smith   Level: developer
228247c6ae99SBarry Smith 
228320f4b53cSBarry Smith   Note:
228420f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
228520f4b53cSBarry Smith 
22861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
228747c6ae99SBarry Smith @*/
2288d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2289d71ae5a4SJacob Faibussowitsch {
2290c833c3b5SJed Brown   DMRefineHookLink link;
229147c6ae99SBarry Smith 
229247c6ae99SBarry Smith   PetscFunctionBegin;
2293732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2295dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22964057135bSMatthew G Knepley   if (*dmf) {
229743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22988865f1eaSKarl Rupp 
22999566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23008865f1eaSKarl Rupp 
2301644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23020598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2303656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23048865f1eaSKarl Rupp 
23059566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2306c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23071baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2308c833c3b5SJed Brown     }
2309c833c3b5SJed Brown   }
23109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2312c833c3b5SJed Brown }
2313c833c3b5SJed Brown 
2314bb9467b5SJed Brown /*@C
2315c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2316c833c3b5SJed Brown 
231720f4b53cSBarry Smith    Logically Collective; No Fortran Support
2318c833c3b5SJed Brown 
23194165533cSJose E. Roman    Input Parameters:
2320bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2321bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2322f826b5fcSPierre Jolivet .  interphook - function to run to update data on finer levels (once per `SNESSolve()`)
232320f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2324c833c3b5SJed Brown 
232520f4b53cSBarry Smith    Calling sequence of `refinehook`:
232620f4b53cSBarry Smith $  PetscErrorCode refinehook(DM coarse, DM fine, void *ctx);
2327bb7acecfSBarry Smith +  coarse - coarse level `DM`
2328bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2329c833c3b5SJed Brown -  ctx - optional user-defined function context
2330c833c3b5SJed Brown 
233120f4b53cSBarry Smith    Calling sequence of `interphook`:
233220f4b53cSBarry Smith $  PetscErrorCode interphook(DM coarse, Mat interp, DM fine, void *ctx)
2333bb7acecfSBarry Smith +  coarse - coarse level `DM`
2334c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2335bb7acecfSBarry Smith .  fine - fine level `DM` to update
2336c833c3b5SJed Brown -  ctx - optional user-defined function context
2337c833c3b5SJed Brown 
2338c833c3b5SJed Brown    Level: advanced
2339c833c3b5SJed Brown 
2340c833c3b5SJed Brown    Notes:
2341bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2342bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2343bb7acecfSBarry Smith 
2344bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2345c833c3b5SJed Brown 
2346c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2347c833c3b5SJed Brown 
23481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2349c833c3b5SJed Brown @*/
2350d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2351d71ae5a4SJacob Faibussowitsch {
2352c833c3b5SJed Brown   DMRefineHookLink link, *p;
2353c833c3b5SJed Brown 
2354c833c3b5SJed Brown   PetscFunctionBegin;
2355c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23563d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23573ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23583d8e3701SJed Brown   }
23599566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2360c833c3b5SJed Brown   link->refinehook = refinehook;
2361c833c3b5SJed Brown   link->interphook = interphook;
2362c833c3b5SJed Brown   link->ctx        = ctx;
23630298fd71SBarry Smith   link->next       = NULL;
2364c833c3b5SJed Brown   *p               = link;
23653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2366c833c3b5SJed Brown }
2367c833c3b5SJed Brown 
23683d8e3701SJed Brown /*@C
2369bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2370bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23713d8e3701SJed Brown 
237220f4b53cSBarry Smith    Logically Collective; No Fortran Support
23733d8e3701SJed Brown 
23744165533cSJose E. Roman    Input Parameters:
2375bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2376bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2377bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
237820f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23793d8e3701SJed Brown 
23803d8e3701SJed Brown    Level: advanced
23813d8e3701SJed Brown 
2382bb7acecfSBarry Smith    Note:
23833d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23843d8e3701SJed Brown 
23851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23863d8e3701SJed Brown @*/
2387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2388d71ae5a4SJacob Faibussowitsch {
23893d8e3701SJed Brown   DMRefineHookLink link, *p;
23903d8e3701SJed Brown 
23913d8e3701SJed Brown   PetscFunctionBegin;
23923d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23933d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23943d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23953d8e3701SJed Brown       link = *p;
23963d8e3701SJed Brown       *p   = link->next;
23979566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23983d8e3701SJed Brown       break;
23993d8e3701SJed Brown     }
24003d8e3701SJed Brown   }
24013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24023d8e3701SJed Brown }
24033d8e3701SJed Brown 
2404c833c3b5SJed Brown /*@
2405bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2406c833c3b5SJed Brown 
2407c833c3b5SJed Brown    Collective if any hooks are
2408c833c3b5SJed Brown 
24094165533cSJose E. Roman    Input Parameters:
2410bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2411bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2412bb7acecfSBarry Smith -  fine - finer `DM` to update
2413c833c3b5SJed Brown 
2414c833c3b5SJed Brown    Level: developer
2415c833c3b5SJed Brown 
2416bb7acecfSBarry Smith    Developer Note:
2417bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2418bb7acecfSBarry Smith    an API with consistent terminology.
2419bb7acecfSBarry Smith 
24201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2421c833c3b5SJed Brown @*/
2422d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2423d71ae5a4SJacob Faibussowitsch {
2424c833c3b5SJed Brown   DMRefineHookLink link;
2425c833c3b5SJed Brown 
2426c833c3b5SJed Brown   PetscFunctionBegin;
2427c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24281baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24294057135bSMatthew G Knepley   }
24303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
243147c6ae99SBarry Smith }
243247c6ae99SBarry Smith 
2433eb3f98d2SBarry Smith /*@
24341f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24351f3379b2SToby Isaac 
243620f4b53cSBarry Smith    Collective
24371f3379b2SToby Isaac 
24384165533cSJose E. Roman    Input Parameters:
2439bb7acecfSBarry Smith +  coarse - coarse `DM`
2440bb7acecfSBarry Smith .  fine   - fine `DM`
2441bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2442bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2443bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24441f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24451f3379b2SToby Isaac 
24464165533cSJose E. Roman    Output Parameter:
24471f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24481f3379b2SToby Isaac 
24491f3379b2SToby Isaac    Level: developer
24501f3379b2SToby Isaac 
2451bb7acecfSBarry Smith    Note:
2452bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24531f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24541f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24551f3379b2SToby Isaac    slope-limiting reconstruction.
24561f3379b2SToby Isaac 
2457bb7acecfSBarry Smith    Developer Note:
2458bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2459bb7acecfSBarry Smith 
24601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24611f3379b2SToby Isaac @*/
2462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2463d71ae5a4SJacob Faibussowitsch {
24641f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24651f3379b2SToby Isaac 
24661f3379b2SToby Isaac   PetscFunctionBegin;
24671f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24681f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24691f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24701f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24711f3379b2SToby Isaac 
24729566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24731f3379b2SToby Isaac   if (interpsol) {
24749566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24751f3379b2SToby Isaac   } else if (interp) {
24769566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
247798921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24791f3379b2SToby Isaac }
24801f3379b2SToby Isaac 
24811f3379b2SToby Isaac /*@
2482bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2483eb3f98d2SBarry Smith 
2484eb3f98d2SBarry Smith     Not Collective
2485eb3f98d2SBarry Smith 
2486eb3f98d2SBarry Smith     Input Parameter:
2487bb7acecfSBarry Smith .   dm - the `DM` object
2488eb3f98d2SBarry Smith 
2489eb3f98d2SBarry Smith     Output Parameter:
2490eb3f98d2SBarry Smith .   level - number of refinements
2491eb3f98d2SBarry Smith 
2492eb3f98d2SBarry Smith     Level: developer
2493eb3f98d2SBarry Smith 
2494bb7acecfSBarry Smith     Note:
2495bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2496bb7acecfSBarry Smith 
24971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2498eb3f98d2SBarry Smith @*/
2499d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2500d71ae5a4SJacob Faibussowitsch {
2501eb3f98d2SBarry Smith   PetscFunctionBegin;
2502eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2503eb3f98d2SBarry Smith   *level = dm->levelup;
25043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2505eb3f98d2SBarry Smith }
2506eb3f98d2SBarry Smith 
2507fef3a512SBarry Smith /*@
2508bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2509fef3a512SBarry Smith 
2510fef3a512SBarry Smith     Not Collective
2511fef3a512SBarry Smith 
2512d8d19677SJose E. Roman     Input Parameters:
2513bb7acecfSBarry Smith +   dm - the `DM` object
2514fef3a512SBarry Smith -   level - number of refinements
2515fef3a512SBarry Smith 
2516fef3a512SBarry Smith     Level: advanced
2517fef3a512SBarry Smith 
251895452b02SPatrick Sanan     Notes:
2519bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2520fef3a512SBarry Smith 
2521bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2522bb7acecfSBarry Smith 
25231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2524fef3a512SBarry Smith @*/
2525d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2526d71ae5a4SJacob Faibussowitsch {
2527fef3a512SBarry Smith   PetscFunctionBegin;
2528fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2529fef3a512SBarry Smith   dm->levelup = level;
25303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2531fef3a512SBarry Smith }
2532fef3a512SBarry Smith 
2533d410b0cfSMatthew G. Knepley /*@
2534bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2535d410b0cfSMatthew G. Knepley 
253620f4b53cSBarry Smith   Collective
2537d410b0cfSMatthew G. Knepley 
2538f1a722f8SMatthew G. Knepley   Input Parameters:
2539bb7acecfSBarry Smith + dm     - the `DM` object
2540d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2541d410b0cfSMatthew G. Knepley 
2542d410b0cfSMatthew G. Knepley   Output Parameter:
254320f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2544d410b0cfSMatthew G. Knepley 
2545d410b0cfSMatthew G. Knepley   Level: developer
2546d410b0cfSMatthew G. Knepley 
254720f4b53cSBarry Smith   Note:
254820f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
254920f4b53cSBarry Smith 
25501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2551d410b0cfSMatthew G. Knepley @*/
2552d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2553d71ae5a4SJacob Faibussowitsch {
2554d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2555d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2556dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2557d410b0cfSMatthew G. Knepley   if (*dme) {
2558d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25599566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2560d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25619566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2562d410b0cfSMatthew G. Knepley   }
25633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2564d410b0cfSMatthew G. Knepley }
2565d410b0cfSMatthew G. Knepley 
2566d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2567d71ae5a4SJacob Faibussowitsch {
2568ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2569ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2570ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2571ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2573ca3d3a14SMatthew G. Knepley }
2574ca3d3a14SMatthew G. Knepley 
2575d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2576d71ae5a4SJacob Faibussowitsch {
2577ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2578ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2579ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2580ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2582ca3d3a14SMatthew G. Knepley }
2583ca3d3a14SMatthew G. Knepley 
2584ca3d3a14SMatthew G. Knepley /*@
2585bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2586ca3d3a14SMatthew G. Knepley 
2587ca3d3a14SMatthew G. Knepley   Input Parameter:
258820f4b53cSBarry Smith . dm - The `DM`
2589ca3d3a14SMatthew G. Knepley 
2590ca3d3a14SMatthew G. Knepley   Output Parameter:
259120f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2592ca3d3a14SMatthew G. Knepley 
2593ca3d3a14SMatthew G. Knepley   Level: developer
2594ca3d3a14SMatthew G. Knepley 
25951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2596ca3d3a14SMatthew G. Knepley @*/
2597d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2598d71ae5a4SJacob Faibussowitsch {
2599ca3d3a14SMatthew G. Knepley   Vec tv;
2600ca3d3a14SMatthew G. Knepley 
2601ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2602ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2603534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
26049566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2605ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2607ca3d3a14SMatthew G. Knepley }
2608ca3d3a14SMatthew G. Knepley 
2609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2610d71ae5a4SJacob Faibussowitsch {
2611ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2612ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2613ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2614ca3d3a14SMatthew G. Knepley 
2615ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26169566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26179566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26189566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26199566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26209566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26219566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26229566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26239566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2624ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2626ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2627ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2628ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2630ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26319566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26329566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2633ca3d3a14SMatthew G. Knepley     }
2634ca3d3a14SMatthew G. Knepley   }
26359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26369566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26379566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2638ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2639ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26409566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2641ca3d3a14SMatthew G. Knepley       if (dof) {
2642ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2643ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2644ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2645ca3d3a14SMatthew G. Knepley 
2646ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26479566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26489566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26499566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2650ca3d3a14SMatthew G. Knepley       }
2651ca3d3a14SMatthew G. Knepley     }
2652ca3d3a14SMatthew G. Knepley   }
26539566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2655ca3d3a14SMatthew G. Knepley }
2656ca3d3a14SMatthew G. Knepley 
2657d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2658d71ae5a4SJacob Faibussowitsch {
2659ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2660ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2661ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2662ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2663ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2664ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2665ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26669566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2668ca3d3a14SMatthew G. Knepley }
2669ca3d3a14SMatthew G. Knepley 
2670bb9467b5SJed Brown /*@C
2671bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2672baf369e7SPeter Brune 
267320f4b53cSBarry Smith    Logically Collective
2674baf369e7SPeter Brune 
26754165533cSJose E. Roman    Input Parameters:
2676bb7acecfSBarry Smith +  dm - the `DM`
2677bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2678bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
267920f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2680baf369e7SPeter Brune 
268120f4b53cSBarry Smith    Calling sequence of `beginhook`:
268220f4b53cSBarry Smith $  PetscErrorCode  beginhook(DM fine, VecScatter out, VecScatter in, DM coarse, void *ctx)
2683baf369e7SPeter Brune +  dm - global DM
2684baf369e7SPeter Brune .  g - global vector
2685baf369e7SPeter Brune .  mode - mode
2686baf369e7SPeter Brune .  l - local vector
2687baf369e7SPeter Brune -  ctx - optional user-defined function context
2688baf369e7SPeter Brune 
268920f4b53cSBarry Smith    Calling sequence of `endhook`:
269020f4b53cSBarry Smith $  PetscErrorCode  endhook(DM fine, VecScatter out, VecScatter in, DM coarse, void *ctx)
2691baf369e7SPeter Brune +  global - global DM
2692baf369e7SPeter Brune -  ctx - optional user-defined function context
2693baf369e7SPeter Brune 
2694baf369e7SPeter Brune    Level: advanced
2695baf369e7SPeter Brune 
2696bb7acecfSBarry Smith    Note:
2697bb7acecfSBarry 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.
2698bb7acecfSBarry Smith 
26991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2700baf369e7SPeter Brune @*/
2701d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2702d71ae5a4SJacob Faibussowitsch {
2703baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2704baf369e7SPeter Brune 
2705baf369e7SPeter Brune   PetscFunctionBegin;
2706baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2707baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27089566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2709baf369e7SPeter Brune   link->beginhook = beginhook;
2710baf369e7SPeter Brune   link->endhook   = endhook;
2711baf369e7SPeter Brune   link->ctx       = ctx;
27120298fd71SBarry Smith   link->next      = NULL;
2713baf369e7SPeter Brune   *p              = link;
27143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2715baf369e7SPeter Brune }
2716baf369e7SPeter Brune 
2717d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2718d71ae5a4SJacob Faibussowitsch {
27194c274da1SToby Isaac   Mat          cMat;
272079769bd5SJed Brown   Vec          cVec, cBias;
27214c274da1SToby Isaac   PetscSection section, cSec;
27224c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27234c274da1SToby Isaac 
27244c274da1SToby Isaac   PetscFunctionBegin;
27254c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27269566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27274c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27285db9a05bSToby Isaac     PetscInt nRows;
27295db9a05bSToby Isaac 
27309566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27313ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27329566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27339566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27349566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27359566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27374c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27389566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27394c274da1SToby Isaac       if (dof) {
27404c274da1SToby Isaac         PetscScalar *vals;
27419566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27429566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27434c274da1SToby Isaac       }
27444c274da1SToby Isaac     }
27459566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27464c274da1SToby Isaac   }
27473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27484c274da1SToby Isaac }
27494c274da1SToby Isaac 
275047c6ae99SBarry Smith /*@
275101729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
275201729b5cSPatrick Sanan 
275320f4b53cSBarry Smith     Neighbor-wise Collective
275401729b5cSPatrick Sanan 
275501729b5cSPatrick Sanan     Input Parameters:
2756bb7acecfSBarry Smith +   dm - the `DM` object
275701729b5cSPatrick Sanan .   g - the global vector
2758bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
275901729b5cSPatrick Sanan -   l - the local vector
276001729b5cSPatrick Sanan 
276120f4b53cSBarry Smith     Level: beginner
276220f4b53cSBarry Smith 
276301729b5cSPatrick Sanan     Notes:
2764bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2765bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2766bb7acecfSBarry Smith 
2767bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
276801729b5cSPatrick Sanan 
27691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2770bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2771bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
277201729b5cSPatrick Sanan @*/
2773d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2774d71ae5a4SJacob Faibussowitsch {
277501729b5cSPatrick Sanan   PetscFunctionBegin;
27769566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27779566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
277901729b5cSPatrick Sanan }
278001729b5cSPatrick Sanan 
278101729b5cSPatrick Sanan /*@
278247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
278347c6ae99SBarry Smith 
278420f4b53cSBarry Smith     Neighbor-wise Collective
278547c6ae99SBarry Smith 
278647c6ae99SBarry Smith     Input Parameters:
2787bb7acecfSBarry Smith +   dm - the `DM` object
278847c6ae99SBarry Smith .   g - the global vector
2789bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
279047c6ae99SBarry Smith -   l - the local vector
279147c6ae99SBarry Smith 
279201729b5cSPatrick Sanan     Level: intermediate
279347c6ae99SBarry Smith 
2794bb7acecfSBarry Smith     Notes:
2795bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2796bb7acecfSBarry Smith 
2797bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2798bb7acecfSBarry Smith 
2799bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2800bb7acecfSBarry Smith 
2801bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2802bb7acecfSBarry Smith 
28031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
280447c6ae99SBarry Smith @*/
2805d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2806d71ae5a4SJacob Faibussowitsch {
28077128ae9fSMatthew G Knepley   PetscSF                 sf;
2808baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
280947c6ae99SBarry Smith 
281047c6ae99SBarry Smith   PetscFunctionBegin;
2811171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2812baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28131baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2814baf369e7SPeter Brune   }
28159566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28167128ae9fSMatthew G Knepley   if (sf) {
2817ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2818ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2819d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28207128ae9fSMatthew G Knepley 
28217a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28229566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28239566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28249566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28259566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28269566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28277128ae9fSMatthew G Knepley   } else {
28289566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28297128ae9fSMatthew G Knepley   }
28303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
283147c6ae99SBarry Smith }
283247c6ae99SBarry Smith 
283347c6ae99SBarry Smith /*@
283447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
283547c6ae99SBarry Smith 
283620f4b53cSBarry Smith     Neighbor-wise Collective
283747c6ae99SBarry Smith 
283847c6ae99SBarry Smith     Input Parameters:
2839bb7acecfSBarry Smith +   dm - the `DM` object
284047c6ae99SBarry Smith .   g - the global vector
2841bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
284247c6ae99SBarry Smith -   l - the local vector
284347c6ae99SBarry Smith 
284401729b5cSPatrick Sanan     Level: intermediate
284547c6ae99SBarry Smith 
2846bb7acecfSBarry Smith     Note:
2847bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2848bb7acecfSBarry Smith 
28491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
285047c6ae99SBarry Smith @*/
2851d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2852d71ae5a4SJacob Faibussowitsch {
28537128ae9fSMatthew G Knepley   PetscSF                 sf;
2854ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2855ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2856ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2857baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2858d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
285947c6ae99SBarry Smith 
286047c6ae99SBarry Smith   PetscFunctionBegin;
2861171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28629566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28639566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28647128ae9fSMatthew G Knepley   if (sf) {
28657a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28667128ae9fSMatthew G Knepley 
28679566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28689566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28699566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28709566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28719566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28729566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28737128ae9fSMatthew G Knepley   } else {
28749566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28757128ae9fSMatthew G Knepley   }
28769566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2877baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28789566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2879baf369e7SPeter Brune   }
28803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
288147c6ae99SBarry Smith }
288247c6ae99SBarry Smith 
2883d4d07f1eSToby Isaac /*@C
2884d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2885d4d07f1eSToby Isaac 
288620f4b53cSBarry Smith    Logically Collective
2887d4d07f1eSToby Isaac 
28884165533cSJose E. Roman    Input Parameters:
2889bb7acecfSBarry Smith +  dm - the `DM`
2890bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2891bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
289220f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2893d4d07f1eSToby Isaac 
289420f4b53cSBarry Smith    Calling sequence of `beginhook`:
289520f4b53cSBarry Smith $  PetscErrorCode  beginhook(DM fine, Vec l, InsertMode mode, Vec g, void *ctx)
2896bb7acecfSBarry Smith +  dm - global `DM`
2897d4d07f1eSToby Isaac .  l - local vector
2898d4d07f1eSToby Isaac .  mode - mode
2899d4d07f1eSToby Isaac .  g - global vector
2900d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2901d4d07f1eSToby Isaac 
290220f4b53cSBarry Smith    Calling sequence of `endhook`:
290320f4b53cSBarry Smith $  PetscErrorCode  endhook(DM fine, Vec l, InsertMode mode, Vec g, void *ctx)
2904bb7acecfSBarry Smith +  global - global `DM`
2905d4d07f1eSToby Isaac .  l - local vector
2906d4d07f1eSToby Isaac .  mode - mode
2907d4d07f1eSToby Isaac .  g - global vector
2908d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2909d4d07f1eSToby Isaac 
2910d4d07f1eSToby Isaac    Level: advanced
2911d4d07f1eSToby Isaac 
29121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2913d4d07f1eSToby Isaac @*/
2914d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2915d71ae5a4SJacob Faibussowitsch {
2916d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2917d4d07f1eSToby Isaac 
2918d4d07f1eSToby Isaac   PetscFunctionBegin;
2919d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2920d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29219566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2922d4d07f1eSToby Isaac   link->beginhook = beginhook;
2923d4d07f1eSToby Isaac   link->endhook   = endhook;
2924d4d07f1eSToby Isaac   link->ctx       = ctx;
2925d4d07f1eSToby Isaac   link->next      = NULL;
2926d4d07f1eSToby Isaac   *p              = link;
29273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2928d4d07f1eSToby Isaac }
2929d4d07f1eSToby Isaac 
2930d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2931d71ae5a4SJacob Faibussowitsch {
29324c274da1SToby Isaac   Mat          cMat;
29334c274da1SToby Isaac   Vec          cVec;
29344c274da1SToby Isaac   PetscSection section, cSec;
29354c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29364c274da1SToby Isaac 
29374c274da1SToby Isaac   PetscFunctionBegin;
29384c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29399566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29404c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29415db9a05bSToby Isaac     PetscInt nRows;
29425db9a05bSToby Isaac 
29439566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29443ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29459566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29469566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29484c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29504c274da1SToby Isaac       if (dof) {
29514c274da1SToby Isaac         PetscInt     d;
29524c274da1SToby Isaac         PetscScalar *vals;
29539566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29549566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29554c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29564c274da1SToby Isaac          * we just extracted */
2957ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29584c274da1SToby Isaac       }
29594c274da1SToby Isaac     }
29609566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29619566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29624c274da1SToby Isaac   }
29633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29644c274da1SToby Isaac }
296501729b5cSPatrick Sanan /*@
296601729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
296701729b5cSPatrick Sanan 
296820f4b53cSBarry Smith     Neighbor-wise Collective
296901729b5cSPatrick Sanan 
297001729b5cSPatrick Sanan     Input Parameters:
2971bb7acecfSBarry Smith +   dm - the `DM` object
297201729b5cSPatrick Sanan .   l - the local vector
2973bb7acecfSBarry 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.
297401729b5cSPatrick Sanan -   g - the global vector
297501729b5cSPatrick Sanan 
297620f4b53cSBarry Smith     Level: beginner
297720f4b53cSBarry Smith 
297801729b5cSPatrick Sanan     Notes:
297901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2980bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
298101729b5cSPatrick Sanan 
2982bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2983bb7acecfSBarry Smith 
2984bb7acecfSBarry 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.
2985bb7acecfSBarry Smith 
2986bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
298701729b5cSPatrick Sanan 
29881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
298901729b5cSPatrick Sanan @*/
2990d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2991d71ae5a4SJacob Faibussowitsch {
299201729b5cSPatrick Sanan   PetscFunctionBegin;
29939566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29949566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
29953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
299601729b5cSPatrick Sanan }
29974c274da1SToby Isaac 
299847c6ae99SBarry Smith /*@
299901729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
30009a42bb27SBarry Smith 
300120f4b53cSBarry Smith     Neighbor-wise Collective
30029a42bb27SBarry Smith 
30039a42bb27SBarry Smith     Input Parameters:
3004bb7acecfSBarry Smith +   dm - the `DM` object
3005f6813fd5SJed Brown .   l - the local vector
3006aa624791SPierre 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.
30071eb28f2eSBarry Smith -   g - the global vector
30089a42bb27SBarry Smith 
300920f4b53cSBarry Smith     Level: intermediate
301020f4b53cSBarry Smith 
301195452b02SPatrick Sanan     Notes:
3012bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3013bb7acecfSBarry Smith 
3014bb7acecfSBarry 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.
3015bb7acecfSBarry Smith 
3016bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
3017bb7acecfSBarry Smith 
3018bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3019bb7acecfSBarry Smith 
3020bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30219a42bb27SBarry Smith 
30221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30239a42bb27SBarry Smith @*/
3024d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3025d71ae5a4SJacob Faibussowitsch {
30267128ae9fSMatthew G Knepley   PetscSF                 sf;
302784330215SMatthew G. Knepley   PetscSection            s, gs;
3028d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3029ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3030ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3031ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3032fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3033d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30349a42bb27SBarry Smith 
30359a42bb27SBarry Smith   PetscFunctionBegin;
3036171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3037d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30381baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3039d4d07f1eSToby Isaac   }
30409566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30419566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30429566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30437128ae9fSMatthew G Knepley   switch (mode) {
30447128ae9fSMatthew G Knepley   case INSERT_VALUES:
30457128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3046d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3047d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3048d71ae5a4SJacob Faibussowitsch     break;
30497128ae9fSMatthew G Knepley   case ADD_VALUES:
30507128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3051d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3052d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3053d71ae5a4SJacob Faibussowitsch     break;
3054d71ae5a4SJacob Faibussowitsch   default:
3055d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30567128ae9fSMatthew G Knepley   }
3057ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30589566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3059ca3d3a14SMatthew G. Knepley     if (transform) {
30609566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30619566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30629566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30639566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3064fa88e482SJed Brown     } else if (isInsert) {
30659566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3066fa88e482SJed Brown     } else {
30679566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3068fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3069ca3d3a14SMatthew G. Knepley     }
3070fa88e482SJed Brown     if (s && isInsert) {
30719566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3072fa88e482SJed Brown     } else {
30739566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3074fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3075fa88e482SJed Brown     }
3076ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30779566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
307884330215SMatthew G. Knepley     } else if (s && isInsert) {
307984330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
308084330215SMatthew G. Knepley 
30819566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30839566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
308484330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3085b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
308684330215SMatthew G. Knepley 
30879566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30909566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30919566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30929566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3093b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
309403442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30957a8be351SBarry 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);
3096b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3097b3b16f48SMatthew G. Knepley         if (!gcdof) {
309884330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3099b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3100b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
310184330215SMatthew G. Knepley           const PetscInt *cdofs;
310284330215SMatthew G. Knepley           PetscInt        cind = 0;
310384330215SMatthew G. Knepley 
31049566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3105b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31069371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31079371c9d4SSatish Balay               ++cind;
31089371c9d4SSatish Balay               continue;
31099371c9d4SSatish Balay             }
3110b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
311184330215SMatthew G. Knepley           }
31127a8be351SBarry 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);
311384330215SMatthew G. Knepley       }
3114ca3d3a14SMatthew G. Knepley     }
3115fa88e482SJed Brown     if (g_inplace) {
31169566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3117fa88e482SJed Brown     } else {
31189566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3119fa88e482SJed Brown     }
3120ca3d3a14SMatthew G. Knepley     if (transform) {
31219566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31229566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3123fa88e482SJed Brown     } else if (l_inplace) {
31249566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3125ca3d3a14SMatthew G. Knepley     } else {
31269566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3127ca3d3a14SMatthew G. Knepley     }
31287128ae9fSMatthew G Knepley   } else {
31299566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31307128ae9fSMatthew G Knepley   }
31313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31329a42bb27SBarry Smith }
31339a42bb27SBarry Smith 
31349a42bb27SBarry Smith /*@
31359a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
313647c6ae99SBarry Smith 
313720f4b53cSBarry Smith     Neighbor-wise Collective
313847c6ae99SBarry Smith 
313947c6ae99SBarry Smith     Input Parameters:
3140bb7acecfSBarry Smith +   dm - the `DM` object
3141f6813fd5SJed Brown .   l - the local vector
3142bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3143f6813fd5SJed Brown -   g - the global vector
314447c6ae99SBarry Smith 
314501729b5cSPatrick Sanan     Level: intermediate
314647c6ae99SBarry Smith 
3147bb7acecfSBarry Smith     Note:
3148bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3149bb7acecfSBarry Smith 
31501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
315147c6ae99SBarry Smith @*/
3152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3153d71ae5a4SJacob Faibussowitsch {
31547128ae9fSMatthew G Knepley   PetscSF                 sf;
315584330215SMatthew G. Knepley   PetscSection            s;
3156d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3157ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
315847c6ae99SBarry Smith 
315947c6ae99SBarry Smith   PetscFunctionBegin;
3160171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31619566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31629566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31637128ae9fSMatthew G Knepley   switch (mode) {
31647128ae9fSMatthew G Knepley   case INSERT_VALUES:
3165d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3166d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3167d71ae5a4SJacob Faibussowitsch     break;
31687128ae9fSMatthew G Knepley   case ADD_VALUES:
3169d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3170d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3171d71ae5a4SJacob Faibussowitsch     break;
3172d71ae5a4SJacob Faibussowitsch   default:
3173d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31747128ae9fSMatthew G Knepley   }
317584330215SMatthew G. Knepley   if (sf && !isInsert) {
3176ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3177ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3178ca3d3a14SMatthew G. Knepley     Vec                tmpl;
317984330215SMatthew G. Knepley 
31809566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3181ca3d3a14SMatthew G. Knepley     if (transform) {
31829566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31839566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3184ca3d3a14SMatthew G. Knepley     } else {
31859566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3186ca3d3a14SMatthew G. Knepley     }
31879566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31889566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3189ca3d3a14SMatthew G. Knepley     if (transform) {
31909566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31919566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3192ca3d3a14SMatthew G. Knepley     } else {
31939566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3194ca3d3a14SMatthew G. Knepley     }
31959566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
319684330215SMatthew G. Knepley   } else if (s && isInsert) {
31977128ae9fSMatthew G Knepley   } else {
31989566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31997128ae9fSMatthew G Knepley   }
3200d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32019566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3202d4d07f1eSToby Isaac   }
32033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
320447c6ae99SBarry Smith }
320547c6ae99SBarry Smith 
3206f089877aSRichard Tran Mills /*@
3207bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3208bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3209bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3210f089877aSRichard Tran Mills 
321120f4b53cSBarry Smith    Neighbor-wise Collective
3212f089877aSRichard Tran Mills 
3213f089877aSRichard Tran Mills    Input Parameters:
3214bb7acecfSBarry Smith +  dm - the `DM` object
3215bc0a1609SRichard Tran Mills .  g - the original local vector
3216bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3217f089877aSRichard Tran Mills 
3218bc0a1609SRichard Tran Mills    Output Parameter:
3219bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3220f089877aSRichard Tran Mills 
3221f089877aSRichard Tran Mills    Level: intermediate
3222f089877aSRichard Tran Mills 
32231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3224f089877aSRichard Tran Mills @*/
3225d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3226d71ae5a4SJacob Faibussowitsch {
3227f089877aSRichard Tran Mills   PetscFunctionBegin;
3228f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32299f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32309f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32319f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3233f089877aSRichard Tran Mills }
3234f089877aSRichard Tran Mills 
3235f089877aSRichard Tran Mills /*@
3236bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3237bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3238f089877aSRichard Tran Mills 
323920f4b53cSBarry Smith    Neighbor-wise Collective
3240f089877aSRichard Tran Mills 
3241f089877aSRichard Tran Mills    Input Parameters:
3242bb7acecfSBarry Smith +  da - the `DM` object
3243bc0a1609SRichard Tran Mills .  g - the original local vector
3244bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3245f089877aSRichard Tran Mills 
3246bc0a1609SRichard Tran Mills    Output Parameter:
3247bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3248f089877aSRichard Tran Mills 
3249f089877aSRichard Tran Mills    Level: intermediate
3250f089877aSRichard Tran Mills 
32511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3252f089877aSRichard Tran Mills @*/
3253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3254d71ae5a4SJacob Faibussowitsch {
3255f089877aSRichard Tran Mills   PetscFunctionBegin;
3256f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32579f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32589f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32599f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3261f089877aSRichard Tran Mills }
3262f089877aSRichard Tran Mills 
326347c6ae99SBarry Smith /*@
3264bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
326547c6ae99SBarry Smith 
326620f4b53cSBarry Smith     Collective
326747c6ae99SBarry Smith 
3268d8d19677SJose E. Roman     Input Parameters:
3269bb7acecfSBarry Smith +   dm - the `DM` object
327020f4b53cSBarry Smith -   comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
327147c6ae99SBarry Smith 
327247c6ae99SBarry Smith     Output Parameter:
3273bb7acecfSBarry Smith .   dmc - the coarsened `DM`
327447c6ae99SBarry Smith 
327547c6ae99SBarry Smith     Level: developer
327647c6ae99SBarry Smith 
32771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
327847c6ae99SBarry Smith @*/
3279d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3280d71ae5a4SJacob Faibussowitsch {
3281b17ce1afSJed Brown   DMCoarsenHookLink link;
328247c6ae99SBarry Smith 
328347c6ae99SBarry Smith   PetscFunctionBegin;
3284171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32859566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3286dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3287b9d85ea2SLisandro Dalcin   if (*dmc) {
3288a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32899566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
329043842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32919566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3292644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
32930598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3294656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
32959566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3296b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
32979566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3298b17ce1afSJed Brown     }
3299b9d85ea2SLisandro Dalcin   }
33009566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33017a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3303b17ce1afSJed Brown }
3304b17ce1afSJed Brown 
3305bb9467b5SJed Brown /*@C
3306b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3307b17ce1afSJed Brown 
330820f4b53cSBarry Smith    Logically Collective; No Fortran Support
3309b17ce1afSJed Brown 
33104165533cSJose E. Roman    Input Parameters:
3311bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3312b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3313bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
331420f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3315b17ce1afSJed Brown 
331620f4b53cSBarry Smith    Calling sequence of `coarsenhook`:
331720f4b53cSBarry Smith $  PetscErrorCode  coarsenhook(DM fine, DM coarse, void *ctx);
3318bb7acecfSBarry Smith +  fine - fine level `DM`
3319bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3320b17ce1afSJed Brown -  ctx - optional user-defined function context
3321b17ce1afSJed Brown 
332220f4b53cSBarry Smith    Calling sequence of `restricthook`:
332320f4b53cSBarry Smith $  PetscErrorCode  restricthook(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx)
3324bb7acecfSBarry Smith +  fine - fine level `DM`
3325bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3326c833c3b5SJed Brown .  rscale - scaling vector for restriction
3327c833c3b5SJed Brown .  inject - matrix restricting by injection
3328b17ce1afSJed Brown .  coarse - coarse level DM to update
3329b17ce1afSJed Brown -  ctx - optional user-defined function context
3330b17ce1afSJed Brown 
3331b17ce1afSJed Brown    Level: advanced
3332b17ce1afSJed Brown 
3333b17ce1afSJed Brown    Notes:
3334bb7acecfSBarry 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`.
3335b17ce1afSJed Brown 
3336b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3337b17ce1afSJed Brown 
3338b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3339bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3340b17ce1afSJed Brown 
3341bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3342bb7acecfSBarry Smith 
33431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3344b17ce1afSJed Brown @*/
3345d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3346d71ae5a4SJacob Faibussowitsch {
3347b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3348b17ce1afSJed Brown 
3349b17ce1afSJed Brown   PetscFunctionBegin;
3350b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33511e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33523ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33531e3d8eccSJed Brown   }
33549566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3355b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3356b17ce1afSJed Brown   link->restricthook = restricthook;
3357b17ce1afSJed Brown   link->ctx          = ctx;
33580298fd71SBarry Smith   link->next         = NULL;
3359b17ce1afSJed Brown   *p                 = link;
33603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3361b17ce1afSJed Brown }
3362b17ce1afSJed Brown 
3363dc822a44SJed Brown /*@C
3364bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3365dc822a44SJed Brown 
336620f4b53cSBarry Smith    Logically Collective; No Fortran Support
3367dc822a44SJed Brown 
33684165533cSJose E. Roman    Input Parameters:
3369bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3370dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3371bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
337220f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3373dc822a44SJed Brown 
3374dc822a44SJed Brown    Level: advanced
3375dc822a44SJed Brown 
3376bb7acecfSBarry Smith    Note:
3377dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3378dc822a44SJed Brown 
33791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3380dc822a44SJed Brown @*/
3381d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3382d71ae5a4SJacob Faibussowitsch {
3383dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3384dc822a44SJed Brown 
3385dc822a44SJed Brown   PetscFunctionBegin;
3386dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3387dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3388dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3389dc822a44SJed Brown       link = *p;
3390dc822a44SJed Brown       *p   = link->next;
33919566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3392dc822a44SJed Brown       break;
3393dc822a44SJed Brown     }
3394dc822a44SJed Brown   }
33953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3396dc822a44SJed Brown }
3397dc822a44SJed Brown 
3398b17ce1afSJed Brown /*@
3399bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3400b17ce1afSJed Brown 
3401b17ce1afSJed Brown    Collective if any hooks are
3402b17ce1afSJed Brown 
34034165533cSJose E. Roman    Input Parameters:
3404bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3405bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3406e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3407bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
340820f4b53cSBarry Smith -  coarse - coarser `DM` to update
3409b17ce1afSJed Brown 
3410b17ce1afSJed Brown    Level: developer
3411b17ce1afSJed Brown 
3412bb7acecfSBarry Smith    Developer Note:
3413bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3414bb7acecfSBarry Smith 
34151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3416b17ce1afSJed Brown @*/
3417d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3418d71ae5a4SJacob Faibussowitsch {
3419b17ce1afSJed Brown   DMCoarsenHookLink link;
3420b17ce1afSJed Brown 
3421b17ce1afSJed Brown   PetscFunctionBegin;
3422b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34231baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3424b17ce1afSJed Brown   }
34253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
342647c6ae99SBarry Smith }
342747c6ae99SBarry Smith 
3428bb9467b5SJed Brown /*@C
3429be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34305dbd56e3SPeter Brune 
343120f4b53cSBarry Smith    Logically Collective; No Fortran Support
34325dbd56e3SPeter Brune 
34334165533cSJose E. Roman    Input Parameters:
3434bb7acecfSBarry Smith +  global - global `DM`
3435bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34365dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
343720f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34385dbd56e3SPeter Brune 
343920f4b53cSBarry Smith    Calling sequence of `ddhook`:
344020f4b53cSBarry Smith $  PetscErrorCode ddhook(DM global, DM block, void *ctx)
3441bb7acecfSBarry Smith +  global - global `DM`
3442bb7acecfSBarry Smith .  block  - block `DM`
3443ec4806b8SPeter Brune -  ctx - optional user-defined function context
3444ec4806b8SPeter Brune 
344520f4b53cSBarry Smith    Calling sequence of `restricthook`:
344620f4b53cSBarry Smith $  PetscErrorCode restricthook(DM global, VecScatter out, VecScatter in, DM block, void *ctx)
3447bb7acecfSBarry Smith +  global - global `DM`
34485dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34495dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3450bb7acecfSBarry Smith .  block  - block `DM`
34515dbd56e3SPeter Brune -  ctx - optional user-defined function context
34525dbd56e3SPeter Brune 
34535dbd56e3SPeter Brune    Level: advanced
34545dbd56e3SPeter Brune 
34555dbd56e3SPeter Brune    Notes:
3456bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34575dbd56e3SPeter Brune 
34585dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34595dbd56e3SPeter Brune 
34605dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3461bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34625dbd56e3SPeter Brune 
34631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34645dbd56e3SPeter Brune @*/
3465d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3466d71ae5a4SJacob Faibussowitsch {
3467be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34685dbd56e3SPeter Brune 
34695dbd56e3SPeter Brune   PetscFunctionBegin;
34705dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3471b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34723ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3473b3a6b972SJed Brown   }
34749566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34755dbd56e3SPeter Brune   link->restricthook = restricthook;
3476be081cd6SPeter Brune   link->ddhook       = ddhook;
34775dbd56e3SPeter Brune   link->ctx          = ctx;
34780298fd71SBarry Smith   link->next         = NULL;
34795dbd56e3SPeter Brune   *p                 = link;
34803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34815dbd56e3SPeter Brune }
34825dbd56e3SPeter Brune 
3483b3a6b972SJed Brown /*@C
3484b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3485b3a6b972SJed Brown 
348620f4b53cSBarry Smith    Logically Collective; No Fortran Support
3487b3a6b972SJed Brown 
34884165533cSJose E. Roman    Input Parameters:
3489bb7acecfSBarry Smith +  global - global `DM`
3490bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3491b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
349220f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3493b3a6b972SJed Brown 
3494b3a6b972SJed Brown    Level: advanced
3495b3a6b972SJed Brown 
34961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3497b3a6b972SJed Brown @*/
3498d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3499d71ae5a4SJacob Faibussowitsch {
3500b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3501b3a6b972SJed Brown 
3502b3a6b972SJed Brown   PetscFunctionBegin;
3503b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3504b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3505b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3506b3a6b972SJed Brown       link = *p;
3507b3a6b972SJed Brown       *p   = link->next;
35089566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3509b3a6b972SJed Brown       break;
3510b3a6b972SJed Brown     }
3511b3a6b972SJed Brown   }
35123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3513b3a6b972SJed Brown }
3514b3a6b972SJed Brown 
35155dbd56e3SPeter Brune /*@
3516bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35175dbd56e3SPeter Brune 
35185dbd56e3SPeter Brune    Collective if any hooks are
35195dbd56e3SPeter Brune 
35204165533cSJose E. Roman    Input Parameters:
3521bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3522be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3523be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3524bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35255dbd56e3SPeter Brune 
35265dbd56e3SPeter Brune    Level: developer
35275dbd56e3SPeter Brune 
35281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`
35295dbd56e3SPeter Brune @*/
3530d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3531d71ae5a4SJacob Faibussowitsch {
3532be081cd6SPeter Brune   DMSubDomainHookLink link;
35335dbd56e3SPeter Brune 
35345dbd56e3SPeter Brune   PetscFunctionBegin;
3535be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35361baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35375dbd56e3SPeter Brune   }
35383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35395dbd56e3SPeter Brune }
35405dbd56e3SPeter Brune 
35415fe1f584SPeter Brune /*@
3542bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35435fe1f584SPeter Brune 
35445fe1f584SPeter Brune     Not Collective
35455fe1f584SPeter Brune 
35465fe1f584SPeter Brune     Input Parameter:
3547bb7acecfSBarry Smith .   dm - the `DM` object
35485fe1f584SPeter Brune 
35495fe1f584SPeter Brune     Output Parameter:
35506a7d9d85SPeter Brune .   level - number of coarsenings
35515fe1f584SPeter Brune 
35525fe1f584SPeter Brune     Level: developer
35535fe1f584SPeter Brune 
35541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35555fe1f584SPeter Brune @*/
3556d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3557d71ae5a4SJacob Faibussowitsch {
35585fe1f584SPeter Brune   PetscFunctionBegin;
35595fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3560b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
35615fe1f584SPeter Brune   *level = dm->leveldown;
35623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35635fe1f584SPeter Brune }
35645fe1f584SPeter Brune 
35659a64c4a8SMatthew G. Knepley /*@
3566bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35679a64c4a8SMatthew G. Knepley 
356820f4b53cSBarry Smith     Collective
35699a64c4a8SMatthew G. Knepley 
35709a64c4a8SMatthew G. Knepley     Input Parameters:
3571bb7acecfSBarry Smith +   dm - the `DM` object
35729a64c4a8SMatthew G. Knepley -   level - number of coarsenings
35739a64c4a8SMatthew G. Knepley 
35749a64c4a8SMatthew G. Knepley     Level: developer
35759a64c4a8SMatthew G. Knepley 
3576bb7acecfSBarry Smith     Note:
3577bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3578bb7acecfSBarry Smith 
35791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35809a64c4a8SMatthew G. Knepley @*/
3581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3582d71ae5a4SJacob Faibussowitsch {
35839a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35849a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35859a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35879a64c4a8SMatthew G. Knepley }
35889a64c4a8SMatthew G. Knepley 
358947c6ae99SBarry Smith /*@C
3590bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
359147c6ae99SBarry Smith 
359220f4b53cSBarry Smith     Collective
359347c6ae99SBarry Smith 
3594d8d19677SJose E. Roman     Input Parameters:
3595bb7acecfSBarry Smith +   dm - the `DM` object
359647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
359747c6ae99SBarry Smith 
359847c6ae99SBarry Smith     Output Parameter:
3599bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
360047c6ae99SBarry Smith 
360147c6ae99SBarry Smith     Level: developer
360247c6ae99SBarry Smith 
36031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
360447c6ae99SBarry Smith @*/
3605d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3606d71ae5a4SJacob Faibussowitsch {
360747c6ae99SBarry Smith   PetscFunctionBegin;
3608171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36097a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36103ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
3611b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
361247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3613dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
361447c6ae99SBarry Smith   } else if (dm->ops->refine) {
361547c6ae99SBarry Smith     PetscInt i;
361647c6ae99SBarry Smith 
36179566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
361848a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3619ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
36203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
362147c6ae99SBarry Smith }
362247c6ae99SBarry Smith 
362347c6ae99SBarry Smith /*@C
3624bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
362547c6ae99SBarry Smith 
362620f4b53cSBarry Smith     Collective
362747c6ae99SBarry Smith 
3628d8d19677SJose E. Roman     Input Parameters:
3629bb7acecfSBarry Smith +   dm - the `DM` object
363047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
363147c6ae99SBarry Smith 
363247c6ae99SBarry Smith     Output Parameter:
3633bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
363447c6ae99SBarry Smith 
363547c6ae99SBarry Smith     Level: developer
363647c6ae99SBarry Smith 
36371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363847c6ae99SBarry Smith @*/
3639d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3640d71ae5a4SJacob Faibussowitsch {
364147c6ae99SBarry Smith   PetscFunctionBegin;
3642171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36437a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36443ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
364547c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
364647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3647dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
364847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
364947c6ae99SBarry Smith     PetscInt i;
365047c6ae99SBarry Smith 
36519566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
365248a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3653ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
36543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365547c6ae99SBarry Smith }
365647c6ae99SBarry Smith 
36571a266240SBarry Smith /*@C
3658bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36591a266240SBarry Smith 
3660bb7acecfSBarry Smith     Logically Collective if the function is collective
36611a266240SBarry Smith 
36621a266240SBarry Smith     Input Parameters:
3663bb7acecfSBarry Smith +   dm - the `DM` object
36641a266240SBarry Smith -   destroy - the destroy function
36651a266240SBarry Smith 
36661a266240SBarry Smith     Level: intermediate
36671a266240SBarry Smith 
36681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3669f07f9ceaSJed Brown @*/
3670d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3671d71ae5a4SJacob Faibussowitsch {
36721a266240SBarry Smith   PetscFunctionBegin;
3673171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36741a266240SBarry Smith   dm->ctxdestroy = destroy;
36753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36761a266240SBarry Smith }
36771a266240SBarry Smith 
3678b07ff414SBarry Smith /*@
3679bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
368047c6ae99SBarry Smith 
368147c6ae99SBarry Smith     Not Collective
368247c6ae99SBarry Smith 
368347c6ae99SBarry Smith     Input Parameters:
3684bb7acecfSBarry Smith +   dm - the `DM` object
368547c6ae99SBarry Smith -   ctx - the user context
368647c6ae99SBarry Smith 
368747c6ae99SBarry Smith     Level: intermediate
368847c6ae99SBarry Smith 
3689bb7acecfSBarry Smith     Note:
369035cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3691bb7acecfSBarry Smith 
36921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
369347c6ae99SBarry Smith @*/
3694d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3695d71ae5a4SJacob Faibussowitsch {
369647c6ae99SBarry Smith   PetscFunctionBegin;
3697171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369847c6ae99SBarry Smith   dm->ctx = ctx;
36993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
370047c6ae99SBarry Smith }
370147c6ae99SBarry Smith 
370247c6ae99SBarry Smith /*@
3703bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
370447c6ae99SBarry Smith 
370547c6ae99SBarry Smith     Not Collective
370647c6ae99SBarry Smith 
370747c6ae99SBarry Smith     Input Parameter:
3708bb7acecfSBarry Smith .   dm - the `DM` object
370947c6ae99SBarry Smith 
371047c6ae99SBarry Smith     Output Parameter:
371147c6ae99SBarry Smith .   ctx - the user context
371247c6ae99SBarry Smith 
371347c6ae99SBarry Smith     Level: intermediate
371447c6ae99SBarry Smith 
3715bb7acecfSBarry Smith     Note:
371635cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3717bb7acecfSBarry Smith 
37181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
371947c6ae99SBarry Smith @*/
3720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3721d71ae5a4SJacob Faibussowitsch {
372247c6ae99SBarry Smith   PetscFunctionBegin;
3723171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37241b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
372647c6ae99SBarry Smith }
372747c6ae99SBarry Smith 
372808da532bSDmitry Karpeev /*@C
3729bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
373008da532bSDmitry Karpeev 
373120f4b53cSBarry Smith     Logically Collective
373208da532bSDmitry Karpeev 
3733d8d19677SJose E. Roman     Input Parameters:
373408da532bSDmitry Karpeev +   dm - the DM object
373520f4b53cSBarry Smith -   f - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
373608da532bSDmitry Karpeev 
373708da532bSDmitry Karpeev     Level: intermediate
373808da532bSDmitry Karpeev 
37391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3740db781477SPatrick Sanan          `DMSetJacobian()`
374108da532bSDmitry Karpeev @*/
3742d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3743d71ae5a4SJacob Faibussowitsch {
374408da532bSDmitry Karpeev   PetscFunctionBegin;
37455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374808da532bSDmitry Karpeev }
374908da532bSDmitry Karpeev 
375008da532bSDmitry Karpeev /*@
3751bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
375208da532bSDmitry Karpeev 
375308da532bSDmitry Karpeev     Not Collective
375408da532bSDmitry Karpeev 
375508da532bSDmitry Karpeev     Input Parameter:
3756bb7acecfSBarry Smith .   dm - the `DM` object to destroy
375708da532bSDmitry Karpeev 
375808da532bSDmitry Karpeev     Output Parameter:
3759bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
376008da532bSDmitry Karpeev 
376108da532bSDmitry Karpeev     Level: developer
376208da532bSDmitry Karpeev 
37631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
376408da532bSDmitry Karpeev @*/
3765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3766d71ae5a4SJacob Faibussowitsch {
376708da532bSDmitry Karpeev   PetscFunctionBegin;
37685a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3769534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
377008da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377208da532bSDmitry Karpeev }
377308da532bSDmitry Karpeev 
377408da532bSDmitry Karpeev /*@C
3775bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
377608da532bSDmitry Karpeev 
377720f4b53cSBarry Smith     Logically Collective
377808da532bSDmitry Karpeev 
3779f899ff85SJose E. Roman     Input Parameter:
3780bb7acecfSBarry Smith .   dm - the `DM` object
378108da532bSDmitry Karpeev 
378208da532bSDmitry Karpeev     Output parameters:
378308da532bSDmitry Karpeev +   xl - lower bound
378408da532bSDmitry Karpeev -   xu - upper bound
378508da532bSDmitry Karpeev 
3786907376e6SBarry Smith     Level: advanced
3787907376e6SBarry Smith 
378820f4b53cSBarry Smith     Note:
378995452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
379008da532bSDmitry Karpeev 
37911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379208da532bSDmitry Karpeev @*/
3793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3794d71ae5a4SJacob Faibussowitsch {
379508da532bSDmitry Karpeev   PetscFunctionBegin;
37965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37985a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3799dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380108da532bSDmitry Karpeev }
380208da532bSDmitry Karpeev 
3803b0ae01b7SPeter Brune /*@
3804bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3805b0ae01b7SPeter Brune 
3806b0ae01b7SPeter Brune     Not Collective
3807b0ae01b7SPeter Brune 
3808b0ae01b7SPeter Brune     Input Parameter:
3809b0ae01b7SPeter Brune .   dm - the DM object
3810b0ae01b7SPeter Brune 
3811b0ae01b7SPeter Brune     Output Parameter:
3812bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3813b0ae01b7SPeter Brune 
3814b0ae01b7SPeter Brune     Level: developer
3815b0ae01b7SPeter Brune 
38161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3817b0ae01b7SPeter Brune @*/
3818d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3819d71ae5a4SJacob Faibussowitsch {
3820b0ae01b7SPeter Brune   PetscFunctionBegin;
38215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3822534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3823b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3825b0ae01b7SPeter Brune }
3826b0ae01b7SPeter Brune 
38273ad4599aSBarry Smith /*@
3828bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38293ad4599aSBarry Smith 
38303ad4599aSBarry Smith     Not Collective
38313ad4599aSBarry Smith 
38323ad4599aSBarry Smith     Input Parameter:
3833bb7acecfSBarry Smith .   dm - the `DM` object
38343ad4599aSBarry Smith 
38353ad4599aSBarry Smith     Output Parameter:
3836bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38373ad4599aSBarry Smith 
38383ad4599aSBarry Smith     Level: developer
38393ad4599aSBarry Smith 
38401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38413ad4599aSBarry Smith @*/
3842d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3843d71ae5a4SJacob Faibussowitsch {
38443ad4599aSBarry Smith   PetscFunctionBegin;
38455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3846534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
38473ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38493ad4599aSBarry Smith }
38503ad4599aSBarry Smith 
3851a7058e45SLawrence Mitchell /*@
3852bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3853a7058e45SLawrence Mitchell 
3854a7058e45SLawrence Mitchell     Not Collective
3855a7058e45SLawrence Mitchell 
3856a7058e45SLawrence Mitchell     Input Parameter:
3857bb7acecfSBarry Smith .   dm - the `DM` object
3858a7058e45SLawrence Mitchell 
3859a7058e45SLawrence Mitchell     Output Parameter:
3860bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3861a7058e45SLawrence Mitchell 
3862a7058e45SLawrence Mitchell     Level: developer
3863a7058e45SLawrence Mitchell 
38641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3865a7058e45SLawrence Mitchell @*/
3866d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3867d71ae5a4SJacob Faibussowitsch {
3868a7058e45SLawrence Mitchell   PetscFunctionBegin;
38695a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3870534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3871dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3872ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3874a7058e45SLawrence Mitchell }
3875a7058e45SLawrence Mitchell 
38760298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3877264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3878264ace61SBarry Smith 
3879264ace61SBarry Smith /*@C
3880bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3881264ace61SBarry Smith 
388220f4b53cSBarry Smith   Collective
3883264ace61SBarry Smith 
3884264ace61SBarry Smith   Input Parameters:
3885bb7acecfSBarry Smith + dm     - The `DM` object
3886bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3887264ace61SBarry Smith 
3888264ace61SBarry Smith   Options Database Key:
3889bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3890264ace61SBarry Smith 
3891264ace61SBarry Smith   Level: intermediate
3892264ace61SBarry Smith 
3893bb7acecfSBarry Smith   Note:
38940462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3895bb7acecfSBarry Smith 
38961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3897264ace61SBarry Smith @*/
3898d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3899d71ae5a4SJacob Faibussowitsch {
3900264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3901264ace61SBarry Smith   PetscBool match;
3902264ace61SBarry Smith 
3903264ace61SBarry Smith   PetscFunctionBegin;
3904264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39059566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39063ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3907264ace61SBarry Smith 
39089566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39099566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39107a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3911264ace61SBarry Smith 
3912dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39139566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39149566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39159566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3917264ace61SBarry Smith }
3918264ace61SBarry Smith 
3919264ace61SBarry Smith /*@C
3920bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3921264ace61SBarry Smith 
3922264ace61SBarry Smith   Not Collective
3923264ace61SBarry Smith 
3924264ace61SBarry Smith   Input Parameter:
3925bb7acecfSBarry Smith . dm  - The `DM`
3926264ace61SBarry Smith 
3927264ace61SBarry Smith   Output Parameter:
3928bb7acecfSBarry Smith . type - The `DMType` name
3929264ace61SBarry Smith 
3930264ace61SBarry Smith   Level: intermediate
3931264ace61SBarry Smith 
39321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3933264ace61SBarry Smith @*/
3934d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3935d71ae5a4SJacob Faibussowitsch {
3936264ace61SBarry Smith   PetscFunctionBegin;
3937264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3938c959eef4SJed Brown   PetscValidPointer(type, 2);
39399566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3940264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3942264ace61SBarry Smith }
3943264ace61SBarry Smith 
394467a56275SMatthew G Knepley /*@C
3945bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
394667a56275SMatthew G Knepley 
394720f4b53cSBarry Smith   Collective
394867a56275SMatthew G Knepley 
394967a56275SMatthew G Knepley   Input Parameters:
3950bb7acecfSBarry Smith + dm - the `DM`
3951bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
395267a56275SMatthew G Knepley 
395367a56275SMatthew G Knepley   Output Parameter:
3954bb7acecfSBarry Smith . M - pointer to new `DM`
395567a56275SMatthew G Knepley 
395620f4b53cSBarry Smith   Level: intermediate
395720f4b53cSBarry Smith 
395867a56275SMatthew G Knepley   Notes:
3959bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3960bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3961bb7acecfSBarry Smith   of the input `DM`.
396267a56275SMatthew G Knepley 
39631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
396467a56275SMatthew G Knepley @*/
3965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3966d71ae5a4SJacob Faibussowitsch {
396767a56275SMatthew G Knepley   DM        B;
396867a56275SMatthew G Knepley   char      convname[256];
3969c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
397067a56275SMatthew G Knepley 
397167a56275SMatthew G Knepley   PetscFunctionBegin;
397267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397367a56275SMatthew G Knepley   PetscValidType(dm, 1);
397467a56275SMatthew G Knepley   PetscValidPointer(M, 3);
39759566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39769566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3977c067b6caSMatthew G. Knepley   if (sametype) {
3978c067b6caSMatthew G. Knepley     *M = dm;
39799566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39803ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3981c067b6caSMatthew G. Knepley   } else {
39820298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley     /*
398567a56275SMatthew G Knepley        Order of precedence:
398667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
398767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
398867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
398967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
399067a56275SMatthew G Knepley        5) Use a really basic converter.
399167a56275SMatthew G Knepley     */
399267a56275SMatthew G Knepley 
399367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39949566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39959566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39969566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39979566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
400067a56275SMatthew G Knepley     if (conv) goto foundconv;
400167a56275SMatthew G Knepley 
400267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40039566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40049566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40059566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40069566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40079566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40089566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40099566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40109566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
401167a56275SMatthew G Knepley     if (conv) {
40129566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
401367a56275SMatthew G Knepley       goto foundconv;
401467a56275SMatthew G Knepley     }
401567a56275SMatthew G Knepley 
401667a56275SMatthew G Knepley #if 0
401767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
401867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40199566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
402067a56275SMatthew G Knepley     if (conv) goto foundconv;
402167a56275SMatthew G Knepley 
402267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
402367a56275SMatthew G Knepley     if (dm->ops->convert) {
402467a56275SMatthew G Knepley       conv = dm->ops->convert;
402567a56275SMatthew G Knepley     }
402667a56275SMatthew G Knepley     if (conv) goto foundconv;
402767a56275SMatthew G Knepley #endif
402867a56275SMatthew G Knepley 
402967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
403098921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
403167a56275SMatthew G Knepley 
403267a56275SMatthew G Knepley   foundconv:
40339566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40349566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
403512fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
403690b157c4SStefano Zampini     {
40374fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40386858538eSMatthew G. Knepley 
40394fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40404fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4041c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40429566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40439566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40449566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40459566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
404612fa691eSMatthew G. Knepley     }
40479566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
404867a56275SMatthew G Knepley   }
40499566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
405167a56275SMatthew G Knepley }
4052264ace61SBarry Smith 
4053264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4054264ace61SBarry Smith 
4055264ace61SBarry Smith /*@C
4056bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40571c84c290SBarry Smith 
40581c84c290SBarry Smith   Not Collective
40591c84c290SBarry Smith 
40601c84c290SBarry Smith   Input Parameters:
406120f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
406220f4b53cSBarry Smith - function - The creation routine itself
406320f4b53cSBarry Smith 
406420f4b53cSBarry Smith   Level: advanced
40651c84c290SBarry Smith 
40661c84c290SBarry Smith   Notes:
406720f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40681c84c290SBarry Smith 
40691c84c290SBarry Smith   Sample usage:
40701c84c290SBarry Smith .vb
4071bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40721c84c290SBarry Smith .ve
40731c84c290SBarry Smith 
407420f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40751c84c290SBarry Smith .vb
40761c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40771c84c290SBarry Smith     DMSetType(DM,"my_da");
40781c84c290SBarry Smith .ve
40791c84c290SBarry Smith    or at runtime via the option
40801c84c290SBarry Smith .vb
40811c84c290SBarry Smith     -da_type my_da
40821c84c290SBarry Smith .ve
4083264ace61SBarry Smith 
40841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4085264ace61SBarry Smith @*/
4086d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4087d71ae5a4SJacob Faibussowitsch {
4088264ace61SBarry Smith   PetscFunctionBegin;
40899566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40909566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4092264ace61SBarry Smith }
4093264ace61SBarry Smith 
4094b859378eSBarry Smith /*@C
4095bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4096b859378eSBarry Smith 
409720f4b53cSBarry Smith   Collective
4098b859378eSBarry Smith 
4099b859378eSBarry Smith   Input Parameters:
4100bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4101bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4102bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4103bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4104b859378eSBarry Smith 
4105b859378eSBarry Smith    Level: intermediate
4106b859378eSBarry Smith 
4107b859378eSBarry Smith   Notes:
410855849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4109b859378eSBarry Smith 
4110bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4111bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4112bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4113cd7e8a5eSksagiyam 
41141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4115b859378eSBarry Smith @*/
4116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4117d71ae5a4SJacob Faibussowitsch {
41189331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4119b859378eSBarry Smith 
4120b859378eSBarry Smith   PetscFunctionBegin;
4121b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4122b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41239566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41249566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41259566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41269566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41279331c7a4SMatthew G. Knepley   if (isbinary) {
41289331c7a4SMatthew G. Knepley     PetscInt classid;
41299331c7a4SMatthew G. Knepley     char     type[256];
4130b859378eSBarry Smith 
41319566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41327a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41339566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41349566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4135dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41369331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4137dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41389331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4141b859378eSBarry Smith }
4142b859378eSBarry Smith 
41437da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41447da65231SMatthew G Knepley 
4145d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4146d71ae5a4SJacob Faibussowitsch {
41471d47ebbbSSatish Balay   PetscInt f;
41481b30c384SMatthew G Knepley 
41497da65231SMatthew G Knepley   PetscFunctionBegin;
415063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
415148a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41537da65231SMatthew G Knepley }
41547da65231SMatthew G Knepley 
4155d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4156d71ae5a4SJacob Faibussowitsch {
41571b30c384SMatthew G Knepley   PetscInt f, g;
41587da65231SMatthew G Knepley 
41597da65231SMatthew G Knepley   PetscFunctionBegin;
416063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41611d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41629566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
416348a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41649566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41657da65231SMatthew G Knepley   }
41663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41677da65231SMatthew G Knepley }
4168e7c4fc90SDmitry Karpeev 
4169d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4170d71ae5a4SJacob Faibussowitsch {
41710c5b8624SToby Isaac   PetscInt           localSize, bs;
41720c5b8624SToby Isaac   PetscMPIInt        size;
41730c5b8624SToby Isaac   Vec                x, xglob;
41740c5b8624SToby Isaac   const PetscScalar *xarray;
4175e759306cSMatthew G. Knepley 
4176e759306cSMatthew G. Knepley   PetscFunctionBegin;
41779566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41789566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41799566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
41809566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
41819566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
41820c5b8624SToby Isaac   if (size > 1) {
41839566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
41849566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
41859566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
41869566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
41870c5b8624SToby Isaac   } else {
41880c5b8624SToby Isaac     xglob = x;
41890c5b8624SToby Isaac   }
41909566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
41910c5b8624SToby Isaac   if (size > 1) {
41929566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
41939566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
41940c5b8624SToby Isaac   }
41959566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
41963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4197e759306cSMatthew G. Knepley }
4198e759306cSMatthew G. Knepley 
419988ed4aceSMatthew G Knepley /*@
4200bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4201061576a5SJed Brown 
4202061576a5SJed Brown   Input Parameter:
4203bb7acecfSBarry Smith . dm - The `DM`
4204061576a5SJed Brown 
4205061576a5SJed Brown   Output Parameter:
4206bb7acecfSBarry Smith . section - The `PetscSection`
4207061576a5SJed Brown 
420820f4b53cSBarry Smith   Options Database Key:
4209bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4210061576a5SJed Brown 
4211061576a5SJed Brown   Level: advanced
4212061576a5SJed Brown 
4213061576a5SJed Brown   Notes:
4214bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4215061576a5SJed Brown 
4216bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4217061576a5SJed Brown 
42181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4219061576a5SJed Brown @*/
4220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4221d71ae5a4SJacob Faibussowitsch {
4222061576a5SJed Brown   PetscFunctionBegin;
42239566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4225061576a5SJed Brown }
4226061576a5SJed Brown 
4227061576a5SJed Brown /*@
4228bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
422988ed4aceSMatthew G Knepley 
423088ed4aceSMatthew G Knepley   Input Parameter:
4231bb7acecfSBarry Smith . dm - The `DM`
423288ed4aceSMatthew G Knepley 
423388ed4aceSMatthew G Knepley   Output Parameter:
4234bb7acecfSBarry Smith . section - The `PetscSection`
423588ed4aceSMatthew G Knepley 
423620f4b53cSBarry Smith   Options Database Key:
4237bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4238e5893cccSMatthew G. Knepley 
423988ed4aceSMatthew G Knepley   Level: intermediate
424088ed4aceSMatthew G Knepley 
4241bb7acecfSBarry Smith   Note:
4242bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
424388ed4aceSMatthew G Knepley 
42441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
424588ed4aceSMatthew G Knepley @*/
4246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4247d71ae5a4SJacob Faibussowitsch {
424888ed4aceSMatthew G Knepley   PetscFunctionBegin;
424988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42511bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4252e5e52638SMatthew G. Knepley     PetscInt d;
4253e5e52638SMatthew G. Knepley 
425445480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
425545480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
425645480ffeSMatthew G. Knepley       PetscViewer       viewer;
425745480ffeSMatthew G. Knepley       PetscViewerFormat format;
425845480ffeSMatthew G. Knepley       PetscBool         flg;
425945480ffeSMatthew G. Knepley 
42609566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42619566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
426245480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42639566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42649566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
426545480ffeSMatthew G. Knepley       }
426645480ffeSMatthew G. Knepley       if (flg) {
42679566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42689566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42699566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
427045480ffeSMatthew G. Knepley       }
427145480ffeSMatthew G. Knepley     }
4272dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42739566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42742f0f8703SMatthew G. Knepley   }
42751bb6d2a8SBarry Smith   *section = dm->localSection;
42763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
427788ed4aceSMatthew G Knepley }
427888ed4aceSMatthew G Knepley 
427988ed4aceSMatthew G Knepley /*@
4280bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4281061576a5SJed Brown 
4282061576a5SJed Brown   Input Parameters:
4283bb7acecfSBarry Smith + dm - The `DM`
4284bb7acecfSBarry Smith - section - The `PetscSection`
4285061576a5SJed Brown 
4286061576a5SJed Brown   Level: advanced
4287061576a5SJed Brown 
4288061576a5SJed Brown   Notes:
4289bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4290061576a5SJed Brown 
4291bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4292061576a5SJed Brown 
42931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4294061576a5SJed Brown @*/
4295d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4296d71ae5a4SJacob Faibussowitsch {
4297061576a5SJed Brown   PetscFunctionBegin;
42989566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
42993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4300061576a5SJed Brown }
4301061576a5SJed Brown 
4302061576a5SJed Brown /*@
4303bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
430488ed4aceSMatthew G Knepley 
430588ed4aceSMatthew G Knepley   Input Parameters:
4306bb7acecfSBarry Smith + dm - The `DM`
4307bb7acecfSBarry Smith - section - The `PetscSection`
430888ed4aceSMatthew G Knepley 
430988ed4aceSMatthew G Knepley   Level: intermediate
431088ed4aceSMatthew G Knepley 
4311bb7acecfSBarry Smith   Note:
4312bb7acecfSBarry Smith   Any existing Section will be destroyed
431388ed4aceSMatthew G Knepley 
43141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
431588ed4aceSMatthew G Knepley @*/
4316d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4317d71ae5a4SJacob Faibussowitsch {
4318c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4319af122d2aSMatthew G Knepley   PetscInt f;
432088ed4aceSMatthew G Knepley 
432188ed4aceSMatthew G Knepley   PetscFunctionBegin;
432288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4323b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43249566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43259566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43261bb6d2a8SBarry Smith   dm->localSection = section;
43279566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4328af122d2aSMatthew G Knepley   if (numFields) {
43299566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4330af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43310f21e855SMatthew G. Knepley       PetscObject disc;
4332af122d2aSMatthew G Knepley       const char *name;
4333af122d2aSMatthew G Knepley 
43349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43359566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43369566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4337af122d2aSMatthew G Knepley     }
4338af122d2aSMatthew G Knepley   }
4339e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43409566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
43413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
434288ed4aceSMatthew G Knepley }
434388ed4aceSMatthew G Knepley 
43449435951eSToby Isaac /*@
4345bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43469435951eSToby Isaac 
434720f4b53cSBarry Smith   not Collective
4348e228b242SToby Isaac 
43499435951eSToby Isaac   Input Parameter:
4350bb7acecfSBarry Smith . dm - The `DM`
43519435951eSToby Isaac 
4352d8d19677SJose E. Roman   Output Parameters:
435320f4b53cSBarry 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.
435420f4b53cSBarry 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.
435579769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43569435951eSToby Isaac 
43579435951eSToby Isaac   Level: advanced
43589435951eSToby Isaac 
4359bb7acecfSBarry Smith   Note:
4360bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43619435951eSToby Isaac 
43621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
43639435951eSToby Isaac @*/
4364d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4365d71ae5a4SJacob Faibussowitsch {
43669435951eSToby Isaac   PetscFunctionBegin;
43679435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4368dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
43693b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43703b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
437179769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43739435951eSToby Isaac }
43749435951eSToby Isaac 
43759435951eSToby Isaac /*@
4376bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
43779435951eSToby Isaac 
437820f4b53cSBarry Smith   Collective
4379e228b242SToby Isaac 
43809435951eSToby Isaac   Input Parameters:
4381bb7acecfSBarry Smith + dm - The `DM`
4382bb7acecfSBarry 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).
438320f4b53cSBarry 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).
438420f4b53cSBarry 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).
43859435951eSToby Isaac 
43869435951eSToby Isaac   Level: advanced
43879435951eSToby Isaac 
438820f4b53cSBarry Smith   Notes:
438920f4b53cSBarry 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()`.
439020f4b53cSBarry Smith 
439120f4b53cSBarry 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.
439220f4b53cSBarry Smith 
4393bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
43949435951eSToby Isaac 
43951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
43969435951eSToby Isaac @*/
4397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4398d71ae5a4SJacob Faibussowitsch {
4399e228b242SToby Isaac   PetscMPIInt result;
44009435951eSToby Isaac 
44019435951eSToby Isaac   PetscFunctionBegin;
44029435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4403e228b242SToby Isaac   if (section) {
4404e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44059566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44067a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4407e228b242SToby Isaac   }
4408e228b242SToby Isaac   if (mat) {
4409e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44109566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44117a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4412e228b242SToby Isaac   }
441379769bd5SJed Brown   if (bias) {
441479769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44159566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
441679769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
441779769bd5SJed Brown   }
44189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44199566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44203b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44219566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44229566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44233b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44249566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44259566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
442679769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44289435951eSToby Isaac }
44299435951eSToby Isaac 
4430497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4431507e4973SMatthew G. Knepley /*
4432bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4433507e4973SMatthew G. Knepley 
4434507e4973SMatthew G. Knepley   Input Parameters:
4435bb7acecfSBarry Smith + dm - The `DM`
4436bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4437bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4438507e4973SMatthew G. Knepley 
4439507e4973SMatthew G. Knepley   Level: intermediate
4440507e4973SMatthew G. Knepley 
44411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4442507e4973SMatthew G. Knepley */
4443d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4444d71ae5a4SJacob Faibussowitsch {
4445507e4973SMatthew G. Knepley   MPI_Comm        comm;
4446507e4973SMatthew G. Knepley   PetscLayout     layout;
4447507e4973SMatthew G. Knepley   const PetscInt *ranges;
4448507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4449507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4450507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4451507e4973SMatthew G. Knepley 
4452507e4973SMatthew G. Knepley   PetscFunctionBegin;
44539566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4454507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44579566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44589566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44599566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44609566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44619566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44629566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44639566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4464507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4465f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4466507e4973SMatthew G. Knepley 
44679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44699566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44709566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44719566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4473507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
44749371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
44759371c9d4SSatish 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));
44769371c9d4SSatish Balay       valid = PETSC_FALSE;
44779371c9d4SSatish Balay     }
44789371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
44799371c9d4SSatish 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));
44809371c9d4SSatish Balay       valid = PETSC_FALSE;
44819371c9d4SSatish Balay     }
4482507e4973SMatthew G. Knepley     if (gdof < 0) {
4483507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4484507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4485507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4486507e4973SMatthew G. Knepley 
44879566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4488507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
44899371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
44909371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
44919371c9d4SSatish Balay           valid = PETSC_FALSE;
44929371c9d4SSatish Balay           break;
44939371c9d4SSatish Balay         }
4494507e4973SMatthew G. Knepley       }
4495507e4973SMatthew G. Knepley     }
4496507e4973SMatthew G. Knepley   }
44979566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
44989566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
44991c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4500507e4973SMatthew G. Knepley   if (!gvalid) {
45019566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4502507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4503507e4973SMatthew G. Knepley   }
45043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4505507e4973SMatthew G. Knepley }
4506f741bcd2SMatthew G. Knepley #endif
4507507e4973SMatthew G. Knepley 
45085f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45096725e60dSJed Brown {
45106725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45116725e60dSJed Brown   PetscFunctionBegin;
45126725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45136725e60dSJed Brown   PetscValidPointer(sf, 2);
45145f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45156725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45166725e60dSJed Brown   else *sf = dm->sf;
45173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45186725e60dSJed Brown }
45196725e60dSJed Brown 
452088ed4aceSMatthew G Knepley /*@
4521bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
452288ed4aceSMatthew G Knepley 
452320f4b53cSBarry Smith   Collective
45248b1ab98fSJed Brown 
452588ed4aceSMatthew G Knepley   Input Parameter:
4526bb7acecfSBarry Smith . dm - The `DM`
452788ed4aceSMatthew G Knepley 
452888ed4aceSMatthew G Knepley   Output Parameter:
4529bb7acecfSBarry Smith . section - The `PetscSection`
453088ed4aceSMatthew G Knepley 
453188ed4aceSMatthew G Knepley   Level: intermediate
453288ed4aceSMatthew G Knepley 
4533bb7acecfSBarry Smith   Note:
4534bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
453588ed4aceSMatthew G Knepley 
45361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
453788ed4aceSMatthew G Knepley @*/
4538d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4539d71ae5a4SJacob Faibussowitsch {
454088ed4aceSMatthew G Knepley   PetscFunctionBegin;
454188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
454288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45431bb6d2a8SBarry Smith   if (!dm->globalSection) {
4544fd59a867SMatthew G. Knepley     PetscSection s;
45456725e60dSJed Brown     PetscSF      sf;
4546fd59a867SMatthew G. Knepley 
45479566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45487a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45497a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45505f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45516725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45529566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45549566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
455588ed4aceSMatthew G Knepley   }
45561bb6d2a8SBarry Smith   *section = dm->globalSection;
45573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
455888ed4aceSMatthew G Knepley }
455988ed4aceSMatthew G Knepley 
4560b21d0597SMatthew G Knepley /*@
4561bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4562b21d0597SMatthew G Knepley 
4563b21d0597SMatthew G Knepley   Input Parameters:
4564bb7acecfSBarry Smith + dm - The `DM`
45652fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4566b21d0597SMatthew G Knepley 
4567b21d0597SMatthew G Knepley   Level: intermediate
4568b21d0597SMatthew G Knepley 
4569bb7acecfSBarry Smith   Note:
4570bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4571b21d0597SMatthew G Knepley 
45721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4573b21d0597SMatthew G Knepley @*/
4574d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4575d71ae5a4SJacob Faibussowitsch {
4576b21d0597SMatthew G Knepley   PetscFunctionBegin;
4577b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45785080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45799566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45809566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45811bb6d2a8SBarry Smith   dm->globalSection = section;
4582497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45839566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4584507e4973SMatthew G. Knepley #endif
45853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4586b21d0597SMatthew G Knepley }
4587b21d0597SMatthew G Knepley 
458888ed4aceSMatthew G Knepley /*@
4589bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4590bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
459188ed4aceSMatthew G Knepley 
459288ed4aceSMatthew G Knepley   Input Parameter:
4593bb7acecfSBarry Smith . dm - The `DM`
459488ed4aceSMatthew G Knepley 
459588ed4aceSMatthew G Knepley   Output Parameter:
4596bb7acecfSBarry Smith . sf - The `PetscSF`
459788ed4aceSMatthew G Knepley 
459888ed4aceSMatthew G Knepley   Level: intermediate
459988ed4aceSMatthew G Knepley 
4600bb7acecfSBarry Smith   Note:
4601bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
460288ed4aceSMatthew G Knepley 
46031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
460488ed4aceSMatthew G Knepley @*/
4605d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4606d71ae5a4SJacob Faibussowitsch {
460788ed4aceSMatthew G Knepley   PetscInt nroots;
460888ed4aceSMatthew G Knepley 
460988ed4aceSMatthew G Knepley   PetscFunctionBegin;
461088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
461188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
461248a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46139566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
461488ed4aceSMatthew G Knepley   if (nroots < 0) {
461588ed4aceSMatthew G Knepley     PetscSection section, gSection;
461688ed4aceSMatthew G Knepley 
46179566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
461831ea6d37SMatthew G Knepley     if (section) {
46199566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46209566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
462131ea6d37SMatthew G Knepley     } else {
46220298fd71SBarry Smith       *sf = NULL;
46233ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
462431ea6d37SMatthew G Knepley     }
462588ed4aceSMatthew G Knepley   }
46261bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
462888ed4aceSMatthew G Knepley }
462988ed4aceSMatthew G Knepley 
463088ed4aceSMatthew G Knepley /*@
4631bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
463288ed4aceSMatthew G Knepley 
463388ed4aceSMatthew G Knepley   Input Parameters:
4634bb7acecfSBarry Smith + dm - The `DM`
4635bb7acecfSBarry Smith - sf - The `PetscSF`
463688ed4aceSMatthew G Knepley 
463788ed4aceSMatthew G Knepley   Level: intermediate
463888ed4aceSMatthew G Knepley 
4639bb7acecfSBarry Smith   Note:
4640bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
464188ed4aceSMatthew G Knepley 
46421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
464388ed4aceSMatthew G Knepley @*/
4644d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4645d71ae5a4SJacob Faibussowitsch {
464688ed4aceSMatthew G Knepley   PetscFunctionBegin;
464788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4648b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46499566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46509566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46511bb6d2a8SBarry Smith   dm->sectionSF = sf;
46523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
465388ed4aceSMatthew G Knepley }
465488ed4aceSMatthew G Knepley 
465588ed4aceSMatthew G Knepley /*@C
4656bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
465788ed4aceSMatthew G Knepley   describing the data layout.
465888ed4aceSMatthew G Knepley 
465988ed4aceSMatthew G Knepley   Input Parameters:
4660bb7acecfSBarry Smith + dm - The `DM`
4661bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4662bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
466388ed4aceSMatthew G Knepley 
46641bb6d2a8SBarry Smith   Level: developer
46651bb6d2a8SBarry Smith 
4666bb7acecfSBarry Smith   Note:
4667bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4668bb7acecfSBarry Smith 
4669bb7acecfSBarry Smith   Developer Note:
4670bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4671bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4672bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46731bb6d2a8SBarry Smith 
46741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
467588ed4aceSMatthew G Knepley @*/
4676d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4677d71ae5a4SJacob Faibussowitsch {
467888ed4aceSMatthew G Knepley   PetscFunctionBegin;
467988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46809566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
46813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
468288ed4aceSMatthew G Knepley }
4683af122d2aSMatthew G Knepley 
4684b21d0597SMatthew G Knepley /*@
4685bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4686bb7acecfSBarry Smith 
4687bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4688b21d0597SMatthew G Knepley 
4689b21d0597SMatthew G Knepley   Input Parameter:
4690bb7acecfSBarry Smith . dm - The `DM`
4691b21d0597SMatthew G Knepley 
4692b21d0597SMatthew G Knepley   Output Parameter:
4693bb7acecfSBarry Smith . sf - The `PetscSF`
4694b21d0597SMatthew G Knepley 
4695b21d0597SMatthew G Knepley   Level: intermediate
4696b21d0597SMatthew G Knepley 
4697bb7acecfSBarry Smith   Note:
4698bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4699b21d0597SMatthew G Knepley 
47001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4701b21d0597SMatthew G Knepley @*/
4702d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4703d71ae5a4SJacob Faibussowitsch {
4704b21d0597SMatthew G Knepley   PetscFunctionBegin;
4705b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4706b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4707b21d0597SMatthew G Knepley   *sf = dm->sf;
47083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4709b21d0597SMatthew G Knepley }
4710b21d0597SMatthew G Knepley 
4711057b4bcdSMatthew G Knepley /*@
4712bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4713bb7acecfSBarry Smith 
471420f4b53cSBarry Smith   Collective
4715057b4bcdSMatthew G Knepley 
4716057b4bcdSMatthew G Knepley   Input Parameters:
4717bb7acecfSBarry Smith + dm - The `DM`
4718bb7acecfSBarry Smith - sf - The `PetscSF`
4719057b4bcdSMatthew G Knepley 
4720057b4bcdSMatthew G Knepley   Level: intermediate
4721057b4bcdSMatthew G Knepley 
47221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4723057b4bcdSMatthew G Knepley @*/
4724d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4725d71ae5a4SJacob Faibussowitsch {
4726057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4727057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4728b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47299566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47309566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4731057b4bcdSMatthew G Knepley   dm->sf = sf;
47323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4733057b4bcdSMatthew G Knepley }
4734057b4bcdSMatthew G Knepley 
47354f37162bSMatthew G. Knepley /*@
4736bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47374f37162bSMatthew G. Knepley 
47384f37162bSMatthew G. Knepley   Input Parameter:
4739bb7acecfSBarry Smith . dm - The `DM`
47404f37162bSMatthew G. Knepley 
47414f37162bSMatthew G. Knepley   Output Parameter:
4742bb7acecfSBarry Smith . sf - The `PetscSF`
47434f37162bSMatthew G. Knepley 
47444f37162bSMatthew G. Knepley   Level: intermediate
47454f37162bSMatthew G. Knepley 
4746bb7acecfSBarry Smith   Note:
4747bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47484f37162bSMatthew G. Knepley 
47491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47504f37162bSMatthew G. Knepley @*/
4751d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4752d71ae5a4SJacob Faibussowitsch {
47534f37162bSMatthew G. Knepley   PetscFunctionBegin;
47544f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47554f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47564f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47584f37162bSMatthew G. Knepley }
47594f37162bSMatthew G. Knepley 
47604f37162bSMatthew G. Knepley /*@
47614f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47624f37162bSMatthew G. Knepley 
47634f37162bSMatthew G. Knepley   Input Parameters:
47644f37162bSMatthew G. Knepley + dm - The DM
47654f37162bSMatthew G. Knepley - sf - The PetscSF
47664f37162bSMatthew G. Knepley 
47674f37162bSMatthew G. Knepley   Level: intermediate
47684f37162bSMatthew G. Knepley 
47691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47704f37162bSMatthew G. Knepley @*/
4771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4772d71ae5a4SJacob Faibussowitsch {
47734f37162bSMatthew G. Knepley   PetscFunctionBegin;
47744f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47754f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47769566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47779566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47784f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47804f37162bSMatthew G. Knepley }
47814f37162bSMatthew G. Knepley 
4782d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4783d71ae5a4SJacob Faibussowitsch {
478434aa8a36SMatthew G. Knepley   PetscClassId id;
478534aa8a36SMatthew G. Knepley 
478634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
478834aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47899566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
479034aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47919566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
479217c1d62eSMatthew G. Knepley   } else {
47939566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
479434aa8a36SMatthew G. Knepley   }
47953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
479634aa8a36SMatthew G. Knepley }
479734aa8a36SMatthew G. Knepley 
4798d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4799d71ae5a4SJacob Faibussowitsch {
480044a7f3ddSMatthew G. Knepley   RegionField *tmpr;
480144a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
480244a7f3ddSMatthew G. Knepley 
480344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48043ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48059566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
480644a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48079371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48089371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48099371c9d4SSatish Balay     tmpr[f].label       = NULL;
48109371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48119371c9d4SSatish Balay   }
48129566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
481344a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
481444a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
481644a7f3ddSMatthew G. Knepley }
481744a7f3ddSMatthew G. Knepley 
481844a7f3ddSMatthew G. Knepley /*@
481920f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
482044a7f3ddSMatthew G. Knepley 
482120f4b53cSBarry Smith   Logically Collective
482244a7f3ddSMatthew G. Knepley 
482344a7f3ddSMatthew G. Knepley   Input Parameter:
482420f4b53cSBarry Smith . dm - The `DM`
482544a7f3ddSMatthew G. Knepley 
482644a7f3ddSMatthew G. Knepley   Level: intermediate
482744a7f3ddSMatthew G. Knepley 
48281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
482944a7f3ddSMatthew G. Knepley @*/
4830d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4831d71ae5a4SJacob Faibussowitsch {
483244a7f3ddSMatthew G. Knepley   PetscInt f;
483344a7f3ddSMatthew G. Knepley 
483444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
483544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483644a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48379566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48389566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
483944a7f3ddSMatthew G. Knepley   }
48409566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
484144a7f3ddSMatthew G. Knepley   dm->fields = NULL;
484244a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
484444a7f3ddSMatthew G. Knepley }
484544a7f3ddSMatthew G. Knepley 
4846689b5837SMatthew G. Knepley /*@
484720f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4848689b5837SMatthew G. Knepley 
484920f4b53cSBarry Smith   Not Collective
4850689b5837SMatthew G. Knepley 
4851689b5837SMatthew G. Knepley   Input Parameter:
485220f4b53cSBarry Smith . dm - The `DM`
4853689b5837SMatthew G. Knepley 
4854689b5837SMatthew G. Knepley   Output Parameter:
4855689b5837SMatthew G. Knepley . Nf - The number of fields
4856689b5837SMatthew G. Knepley 
4857689b5837SMatthew G. Knepley   Level: intermediate
4858689b5837SMatthew G. Knepley 
48591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4860689b5837SMatthew G. Knepley @*/
4861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4862d71ae5a4SJacob Faibussowitsch {
48630f21e855SMatthew G. Knepley   PetscFunctionBegin;
48640f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4865534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
486644a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
48673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4868af122d2aSMatthew G Knepley }
4869af122d2aSMatthew G Knepley 
4870689b5837SMatthew G. Knepley /*@
487120f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4872689b5837SMatthew G. Knepley 
487320f4b53cSBarry Smith   Logically Collective
4874689b5837SMatthew G. Knepley 
4875689b5837SMatthew G. Knepley   Input Parameters:
487620f4b53cSBarry Smith + dm - The `DM`
4877689b5837SMatthew G. Knepley - Nf - The number of fields
4878689b5837SMatthew G. Knepley 
4879689b5837SMatthew G. Knepley   Level: intermediate
4880689b5837SMatthew G. Knepley 
48811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4882689b5837SMatthew G. Knepley @*/
4883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4884d71ae5a4SJacob Faibussowitsch {
48850f21e855SMatthew G. Knepley   PetscInt Nf, f;
4886af122d2aSMatthew G Knepley 
4887af122d2aSMatthew G Knepley   PetscFunctionBegin;
4888af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48899566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48900f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48910f21e855SMatthew G. Knepley     PetscContainer obj;
48920f21e855SMatthew G. Knepley 
48939566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
48949566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
48959566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4896af122d2aSMatthew G Knepley   }
48973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4898af122d2aSMatthew G Knepley }
4899af122d2aSMatthew G Knepley 
4900c1929be8SMatthew G. Knepley /*@
4901bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4902c1929be8SMatthew G. Knepley 
490320f4b53cSBarry Smith   Not Collective
4904c1929be8SMatthew G. Knepley 
4905c1929be8SMatthew G. Knepley   Input Parameters:
4906bb7acecfSBarry Smith + dm - The `DM`
4907c1929be8SMatthew G. Knepley - f  - The field number
4908c1929be8SMatthew G. Knepley 
490944a7f3ddSMatthew G. Knepley   Output Parameters:
491020f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
491120f4b53cSBarry Smith - disc - The discretization object (pass in `NULL` if not needed)
4912c1929be8SMatthew G. Knepley 
491344a7f3ddSMatthew G. Knepley   Level: intermediate
4914c1929be8SMatthew G. Knepley 
49151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4916c1929be8SMatthew G. Knepley @*/
4917d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4918d71ae5a4SJacob Faibussowitsch {
4919af122d2aSMatthew G Knepley   PetscFunctionBegin;
4920af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4921bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
49227a8be351SBarry 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);
492344a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4924bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4926decb47aaSMatthew G. Knepley }
4927decb47aaSMatthew G. Knepley 
4928083401c6SMatthew G. Knepley /* Does not clear the DS */
4929d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4930d71ae5a4SJacob Faibussowitsch {
4931083401c6SMatthew G. Knepley   PetscFunctionBegin;
49329566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49339566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49349566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4935083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4936bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49379566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4938bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4940083401c6SMatthew G. Knepley }
4941083401c6SMatthew G. Knepley 
494246560f82SMatthew G. Knepley /*@C
4943bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4944bb7acecfSBarry Smith   the field numbering.
4945c1929be8SMatthew G. Knepley 
494620f4b53cSBarry Smith   Logically Collective
4947c1929be8SMatthew G. Knepley 
4948c1929be8SMatthew G. Knepley   Input Parameters:
4949bb7acecfSBarry Smith + dm    - The `DM`
4950c1929be8SMatthew G. Knepley . f     - The field number
495120f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4952bb7acecfSBarry Smith - disc - The discretization object
4953c1929be8SMatthew G. Knepley 
495444a7f3ddSMatthew G. Knepley   Level: intermediate
4955c1929be8SMatthew G. Knepley 
49561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
4957c1929be8SMatthew G. Knepley @*/
4958d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4959d71ae5a4SJacob Faibussowitsch {
4960decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4961decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4962e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4963bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49647a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4965bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4966bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49679566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
49683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
496944a7f3ddSMatthew G. Knepley }
497044a7f3ddSMatthew G. Knepley 
497146560f82SMatthew G. Knepley /*@C
4972bb7acecfSBarry 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)
4973bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
497444a7f3ddSMatthew G. Knepley 
497520f4b53cSBarry Smith   Logically Collective
497644a7f3ddSMatthew G. Knepley 
497744a7f3ddSMatthew G. Knepley   Input Parameters:
4978bb7acecfSBarry Smith + dm    - The `DM`
497920f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4980bb7acecfSBarry Smith - disc - The discretization object
498144a7f3ddSMatthew G. Knepley 
498244a7f3ddSMatthew G. Knepley   Level: intermediate
498344a7f3ddSMatthew G. Knepley 
4984bb7acecfSBarry Smith   Notes:
4985bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4986bb7acecfSBarry Smith 
4987da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4988bb7acecfSBarry 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
4989bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4990bb7acecfSBarry Smith 
49911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
499244a7f3ddSMatthew G. Knepley @*/
4993d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
4994d71ae5a4SJacob Faibussowitsch {
499544a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
499644a7f3ddSMatthew G. Knepley 
499744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
499844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4999064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5000bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50019566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
500244a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5003bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5005bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5006bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50079566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5009af122d2aSMatthew G Knepley }
50106636e97aSMatthew G Knepley 
5011e5e52638SMatthew G. Knepley /*@
5012e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5013e0b68406SMatthew Knepley 
501420f4b53cSBarry Smith   Logically Collective
5015e0b68406SMatthew Knepley 
5016e0b68406SMatthew Knepley   Input Parameters:
5017bb7acecfSBarry Smith + dm          - The `DM`
5018e0b68406SMatthew Knepley . f           - The field index
5019bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5020e0b68406SMatthew Knepley 
5021e0b68406SMatthew Knepley   Level: intermediate
5022e0b68406SMatthew Knepley 
50231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5024e0b68406SMatthew Knepley @*/
5025d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5026d71ae5a4SJacob Faibussowitsch {
5027e0b68406SMatthew Knepley   PetscFunctionBegin;
502863a3b9bcSJacob 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);
5029e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5031e0b68406SMatthew Knepley }
5032e0b68406SMatthew Knepley 
5033e0b68406SMatthew Knepley /*@
5034e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5035e0b68406SMatthew Knepley 
503620f4b53cSBarry Smith   Not Collective
5037e0b68406SMatthew Knepley 
5038e0b68406SMatthew Knepley   Input Parameters:
5039bb7acecfSBarry Smith + dm          - The `DM`
5040e0b68406SMatthew Knepley - f           - The field index
5041e0b68406SMatthew Knepley 
5042e0b68406SMatthew Knepley   Output Parameter:
5043e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5044e0b68406SMatthew Knepley 
5045e0b68406SMatthew Knepley   Level: intermediate
5046e0b68406SMatthew Knepley 
50471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5048e0b68406SMatthew Knepley @*/
5049d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5050d71ae5a4SJacob Faibussowitsch {
5051e0b68406SMatthew Knepley   PetscFunctionBegin;
505263a3b9bcSJacob 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);
5053e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
50543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5055e0b68406SMatthew Knepley }
5056e0b68406SMatthew Knepley 
5057e0b68406SMatthew Knepley /*@
5058bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5059e5e52638SMatthew G. Knepley 
506020f4b53cSBarry Smith   Collective
5061e5e52638SMatthew G. Knepley 
5062e5e52638SMatthew G. Knepley   Input Parameter:
5063bb7acecfSBarry Smith . dm - The `DM`
5064e5e52638SMatthew G. Knepley 
5065e5e52638SMatthew G. Knepley   Output Parameter:
5066bb7acecfSBarry Smith . newdm - The `DM`
5067e5e52638SMatthew G. Knepley 
5068e5e52638SMatthew G. Knepley   Level: advanced
5069e5e52638SMatthew G. Knepley 
50701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5071e5e52638SMatthew G. Knepley @*/
5072d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5073d71ae5a4SJacob Faibussowitsch {
5074e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5075e5e52638SMatthew G. Knepley 
5076e5e52638SMatthew G. Knepley   PetscFunctionBegin;
50773ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
50789566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50799566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5080e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5081e5e52638SMatthew G. Knepley     DMLabel     label;
5082e5e52638SMatthew G. Knepley     PetscObject field;
508334aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5084e5e52638SMatthew G. Knepley 
50859566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50869566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50879566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50889566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
508934aa8a36SMatthew G. Knepley   }
50903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
509134aa8a36SMatthew G. Knepley }
509234aa8a36SMatthew G. Knepley 
509334aa8a36SMatthew G. Knepley /*@
509434aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
509534aa8a36SMatthew G. Knepley 
509620f4b53cSBarry Smith   Not Collective
509734aa8a36SMatthew G. Knepley 
509834aa8a36SMatthew G. Knepley   Input Parameters:
509920f4b53cSBarry Smith + dm - The `DM` object
510020f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
510134aa8a36SMatthew G. Knepley 
5102d8d19677SJose E. Roman   Output Parameters:
510334aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
510434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
510534aa8a36SMatthew G. Knepley 
510634aa8a36SMatthew G. Knepley   Level: developer
510734aa8a36SMatthew G. Knepley 
510820f4b53cSBarry Smith   Notes:
510920f4b53cSBarry Smith .vb
511020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
511120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
511220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
511320f4b53cSBarry Smith .ve
511420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
511520f4b53cSBarry Smith 
51161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
511734aa8a36SMatthew G. Knepley @*/
5118d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5119d71ae5a4SJacob Faibussowitsch {
512034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
512134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5122534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
5123534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
512434aa8a36SMatthew G. Knepley   if (f < 0) {
512534aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
512634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
512734aa8a36SMatthew G. Knepley   } else {
512834aa8a36SMatthew G. Knepley     PetscInt Nf;
512934aa8a36SMatthew G. Knepley 
51309566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51317a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
513234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
513334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
513434aa8a36SMatthew G. Knepley   }
51353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
513634aa8a36SMatthew G. Knepley }
513734aa8a36SMatthew G. Knepley 
513834aa8a36SMatthew G. Knepley /*@
513934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
514034aa8a36SMatthew G. Knepley 
514120f4b53cSBarry Smith   Not Collective
514234aa8a36SMatthew G. Knepley 
514334aa8a36SMatthew G. Knepley   Input Parameters:
514420f4b53cSBarry Smith + dm         - The `DM` object
514534aa8a36SMatthew G. Knepley . f          - The field number
514634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
514734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
514834aa8a36SMatthew G. Knepley 
514934aa8a36SMatthew G. Knepley   Level: developer
515034aa8a36SMatthew G. Knepley 
515120f4b53cSBarry Smith   Notes:
515220f4b53cSBarry Smith .vb
515320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
515420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
515520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
515620f4b53cSBarry Smith .ve
515720f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
515820f4b53cSBarry Smith 
51591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
516034aa8a36SMatthew G. Knepley @*/
5161d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5162d71ae5a4SJacob Faibussowitsch {
516334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
516434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
516534aa8a36SMatthew G. Knepley   if (f < 0) {
516634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
516734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
516834aa8a36SMatthew G. Knepley   } else {
516934aa8a36SMatthew G. Knepley     PetscInt Nf;
517034aa8a36SMatthew G. Knepley 
51719566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51727a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
517334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
517434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5175e5e52638SMatthew G. Knepley   }
51763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5177e5e52638SMatthew G. Knepley }
5178e5e52638SMatthew G. Knepley 
5179b0441da4SMatthew G. Knepley /*@
5180b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5181b0441da4SMatthew G. Knepley 
5182b0441da4SMatthew G. Knepley   Not collective
5183b0441da4SMatthew G. Knepley 
5184f899ff85SJose E. Roman   Input Parameter:
518520f4b53cSBarry Smith . dm - The `DM` object
5186b0441da4SMatthew G. Knepley 
5187d8d19677SJose E. Roman   Output Parameters:
5188b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5189b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5190b0441da4SMatthew G. Knepley 
5191b0441da4SMatthew G. Knepley   Level: developer
5192b0441da4SMatthew G. Knepley 
519320f4b53cSBarry Smith   Notes:
519420f4b53cSBarry Smith .vb
519520f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
519620f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
519720f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
519820f4b53cSBarry Smith .ve
519920f4b53cSBarry Smith 
52001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5201b0441da4SMatthew G. Knepley @*/
5202d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5203d71ae5a4SJacob Faibussowitsch {
5204b0441da4SMatthew G. Knepley   PetscInt Nf;
5205b0441da4SMatthew G. Knepley 
5206b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5207b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5208064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5209064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
52109566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5211b0441da4SMatthew G. Knepley   if (!Nf) {
52129566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5213b0441da4SMatthew G. Knepley   } else {
52149566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5215b0441da4SMatthew G. Knepley   }
52163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5217b0441da4SMatthew G. Knepley }
5218b0441da4SMatthew G. Knepley 
5219b0441da4SMatthew G. Knepley /*@
5220b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5221b0441da4SMatthew G. Knepley 
522220f4b53cSBarry Smith   Not Collective
5223b0441da4SMatthew G. Knepley 
5224b0441da4SMatthew G. Knepley   Input Parameters:
522520f4b53cSBarry Smith + dm         - The `DM` object
5226b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5227b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5228b0441da4SMatthew G. Knepley 
5229b0441da4SMatthew G. Knepley   Level: developer
5230b0441da4SMatthew G. Knepley 
523120f4b53cSBarry Smith   Notes:
523220f4b53cSBarry Smith .vb
523320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
523420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
523520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
523620f4b53cSBarry Smith .ve
523720f4b53cSBarry Smith 
52381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5239b0441da4SMatthew G. Knepley @*/
5240d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5241d71ae5a4SJacob Faibussowitsch {
5242b0441da4SMatthew G. Knepley   PetscInt Nf;
5243b0441da4SMatthew G. Knepley 
5244b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5245b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52469566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5247b0441da4SMatthew G. Knepley   if (!Nf) {
52489566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5249b0441da4SMatthew G. Knepley   } else {
52509566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5251e5e52638SMatthew G. Knepley   }
52523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5253e5e52638SMatthew G. Knepley }
5254e5e52638SMatthew G. Knepley 
5255d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5256d71ae5a4SJacob Faibussowitsch {
5257799db056SMatthew G. Knepley   DM           plex;
5258799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5259799db056SMatthew G. Knepley   const char **names;
5260799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5261799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5262799db056SMatthew G. Knepley   size_t       len;
5263799db056SMatthew G. Knepley   MPI_Comm     comm;
5264799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5265783e2ec8SMatthew G. Knepley 
5266783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5267799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5268799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5269799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5270799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5271799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5272799db056SMatthew G. Knepley     PetscDS  dsBC;
5273799db056SMatthew G. Knepley     PetscInt numBd;
5274799db056SMatthew G. Knepley 
527507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5276799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5277799db056SMatthew G. Knepley     maxLabels += numBd;
5278799db056SMatthew G. Knepley   }
5279799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5280799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5281799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5282799db056SMatthew G. Knepley     PetscDS  dsBC;
5283799db056SMatthew G. Knepley     PetscInt numBd, bd;
5284799db056SMatthew G. Knepley 
528507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5286799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5287799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5288799db056SMatthew G. Knepley       DMLabel      label;
5289799db056SMatthew G. Knepley       PetscInt     field;
5290799db056SMatthew G. Knepley       PetscObject  obj;
5291799db056SMatthew G. Knepley       PetscClassId id;
5292799db056SMatthew G. Knepley 
5293799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52949566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52959566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5296799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
52979371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
52989371c9d4SSatish Balay         if (labels[l] == label) break;
5299799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5300783e2ec8SMatthew G. Knepley     }
5301799db056SMatthew G. Knepley   }
5302799db056SMatthew G. Knepley   /* Get label names */
5303799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5304799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53059371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53069371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53079371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53089371c9d4SSatish Balay   }
5309799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5310712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5311799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5312c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5313799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5314799db056SMatthew G. Knepley   /* Put all names on all processes */
5315799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5316799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5317799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5318799db056SMatthew G. Knepley   gNl = displs[size];
53199371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53209371c9d4SSatish Balay     counts[p] *= gmaxLen;
53219371c9d4SSatish Balay     displs[p] *= gmaxLen;
53229371c9d4SSatish Balay   }
5323799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5324799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5325799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5326799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5327799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5328799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5329799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53309371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53319371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53329566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5333799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53349566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5335799db056SMatthew G. Knepley     ++gl;
5336783e2ec8SMatthew G. Knepley   }
5337799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5339783e2ec8SMatthew G. Knepley }
5340783e2ec8SMatthew G. Knepley 
5341d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5342d71ae5a4SJacob Faibussowitsch {
5343e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5344e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5345e5e52638SMatthew G. Knepley 
5346e5e52638SMatthew G. Knepley   PetscFunctionBegin;
53473ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
53489566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5349e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53509371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53519371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53529371c9d4SSatish Balay     tmpd[s].label  = NULL;
53539371c9d4SSatish Balay     tmpd[s].fields = NULL;
53549371c9d4SSatish Balay   }
53559566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5356e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5357e5e52638SMatthew G. Knepley   dm->probs = tmpd;
53583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5359e5e52638SMatthew G. Knepley }
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley /*@
536220f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5363e5e52638SMatthew G. Knepley 
536420f4b53cSBarry Smith   Not Collective
5365e5e52638SMatthew G. Knepley 
5366e5e52638SMatthew G. Knepley   Input Parameter:
536720f4b53cSBarry Smith . dm - The `DM`
5368e5e52638SMatthew G. Knepley 
5369e5e52638SMatthew G. Knepley   Output Parameter:
537020f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5371e5e52638SMatthew G. Knepley 
5372e5e52638SMatthew G. Knepley   Level: intermediate
5373e5e52638SMatthew G. Knepley 
53741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5375e5e52638SMatthew G. Knepley @*/
5376d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5377d71ae5a4SJacob Faibussowitsch {
5378e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5379e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5380534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5381e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
53823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5383e5e52638SMatthew G. Knepley }
5384e5e52638SMatthew G. Knepley 
5385e5e52638SMatthew G. Knepley /*@
538620f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5387e5e52638SMatthew G. Knepley 
538820f4b53cSBarry Smith   Logically Collective
5389e5e52638SMatthew G. Knepley 
5390e5e52638SMatthew G. Knepley   Input Parameter:
539120f4b53cSBarry Smith . dm - The `DM`
5392e5e52638SMatthew G. Knepley 
5393e5e52638SMatthew G. Knepley   Level: intermediate
5394e5e52638SMatthew G. Knepley 
53951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5396e5e52638SMatthew G. Knepley @*/
5397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5398d71ae5a4SJacob Faibussowitsch {
5399e5e52638SMatthew G. Knepley   PetscInt s;
5400e5e52638SMatthew G. Knepley 
5401e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5402e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5403e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54049566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
540507218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54069566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54079566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5408e5e52638SMatthew G. Knepley   }
54099566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5410e5e52638SMatthew G. Knepley   dm->probs = NULL;
5411e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5413e5e52638SMatthew G. Knepley }
5414e5e52638SMatthew G. Knepley 
5415e5e52638SMatthew G. Knepley /*@
541620f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5417e5e52638SMatthew G. Knepley 
541820f4b53cSBarry Smith   Not Collective
5419e5e52638SMatthew G. Knepley 
5420e5e52638SMatthew G. Knepley   Input Parameter:
542120f4b53cSBarry Smith . dm - The `DM`
5422e5e52638SMatthew G. Knepley 
5423e5e52638SMatthew G. Knepley   Output Parameter:
542407218a29SMatthew G. Knepley . ds - The default `PetscDS`
5425e5e52638SMatthew G. Knepley 
5426e5e52638SMatthew G. Knepley   Level: intermediate
5427e5e52638SMatthew G. Knepley 
54281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5429e5e52638SMatthew G. Knepley @*/
543007218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5431d71ae5a4SJacob Faibussowitsch {
5432e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5433e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
543407218a29SMatthew G. Knepley   PetscValidPointer(ds, 2);
543507218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
543607218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
54373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5438e5e52638SMatthew G. Knepley }
5439e5e52638SMatthew G. Knepley 
5440e5e52638SMatthew G. Knepley /*@
544120f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5442e5e52638SMatthew G. Knepley 
544320f4b53cSBarry Smith   Not Collective
5444e5e52638SMatthew G. Knepley 
5445e5e52638SMatthew G. Knepley   Input Parameters:
544620f4b53cSBarry Smith + dm    - The `DM`
544720f4b53cSBarry Smith - point - Cell for the `PetscDS`
5448e5e52638SMatthew G. Knepley 
544907218a29SMatthew G. Knepley   Output Parameters:
545007218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
545107218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5452e5e52638SMatthew G. Knepley 
5453e5e52638SMatthew G. Knepley   Level: developer
5454e5e52638SMatthew G. Knepley 
54551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5456e5e52638SMatthew G. Knepley @*/
545707218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5458d71ae5a4SJacob Faibussowitsch {
545907218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5460e5e52638SMatthew G. Knepley   PetscInt s;
5461e5e52638SMatthew G. Knepley 
5462e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5463e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
546407218a29SMatthew G. Knepley   if (ds) PetscValidPointer(ds, 3);
546507218a29SMatthew G. Knepley   if (dsIn) PetscValidPointer(dsIn, 4);
546663a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
546707218a29SMatthew G. Knepley   if (ds) *ds = NULL;
546807218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5469e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5470e5e52638SMatthew G. Knepley     PetscInt val;
5471e5e52638SMatthew G. Knepley 
54729371c9d4SSatish Balay     if (!dm->probs[s].label) {
547307218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
54749371c9d4SSatish Balay     } else {
54759566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
54769371c9d4SSatish Balay       if (val >= 0) {
547707218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
547807218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
54799371c9d4SSatish Balay         break;
54809371c9d4SSatish Balay       }
5481e5e52638SMatthew G. Knepley     }
5482e5e52638SMatthew G. Knepley   }
548307218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
54843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5485e5e52638SMatthew G. Knepley }
5486e5e52638SMatthew G. Knepley 
5487e5e52638SMatthew G. Knepley /*@
548820f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5489e5e52638SMatthew G. Knepley 
549020f4b53cSBarry Smith   Not Collective
5491e5e52638SMatthew G. Knepley 
5492e5e52638SMatthew G. Knepley   Input Parameters:
549320f4b53cSBarry Smith + dm    - The `DM`
549420f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5495e5e52638SMatthew G. Knepley 
5496b3cf3223SMatthew G. Knepley   Output Parameters:
549720f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
549807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
549907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5500e5e52638SMatthew G. Knepley 
5501e5e52638SMatthew G. Knepley   Level: advanced
5502e5e52638SMatthew G. Knepley 
550320f4b53cSBarry Smith   Note:
550420f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
550520f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
550607218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
550720f4b53cSBarry Smith 
55081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5509e5e52638SMatthew G. Knepley @*/
551007218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5511d71ae5a4SJacob Faibussowitsch {
5512e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5513e5e52638SMatthew G. Knepley 
5514e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5515e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5516e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55179371c9d4SSatish Balay   if (fields) {
55189371c9d4SSatish Balay     PetscValidPointer(fields, 3);
55199371c9d4SSatish Balay     *fields = NULL;
55209371c9d4SSatish Balay   }
55219371c9d4SSatish Balay   if (ds) {
55229371c9d4SSatish Balay     PetscValidPointer(ds, 4);
55239371c9d4SSatish Balay     *ds = NULL;
55249371c9d4SSatish Balay   }
552507218a29SMatthew G. Knepley   if (dsIn) {
552607218a29SMatthew G. Knepley     PetscValidPointer(dsIn, 5);
552707218a29SMatthew G. Knepley     *dsIn = NULL;
552807218a29SMatthew G. Knepley   }
5529e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5530154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5531b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5532b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
553307218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55343ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5535b3cf3223SMatthew G. Knepley     }
5536e5e52638SMatthew G. Knepley   }
55373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5538e5e52638SMatthew G. Knepley }
5539e5e52638SMatthew G. Knepley 
5540e5e52638SMatthew G. Knepley /*@
5541bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5542083401c6SMatthew G. Knepley 
554320f4b53cSBarry Smith   Collective
5544083401c6SMatthew G. Knepley 
5545083401c6SMatthew G. Knepley   Input Parameters:
5546bb7acecfSBarry Smith + dm     - The `DM`
554720f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
554807218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
554907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
555007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5551083401c6SMatthew G. Knepley 
555220f4b53cSBarry Smith   Level: advanced
555320f4b53cSBarry Smith 
5554bb7acecfSBarry Smith   Note:
5555bb7acecfSBarry 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,
5556083401c6SMatthew G. Knepley   the fields argument is ignored.
5557083401c6SMatthew G. Knepley 
55581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5559083401c6SMatthew G. Knepley @*/
556007218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5561d71ae5a4SJacob Faibussowitsch {
5562083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5563083401c6SMatthew G. Knepley 
5564083401c6SMatthew G. Knepley   PetscFunctionBegin;
5565083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5566083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
556707218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5568064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
556907218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5570083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5571083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55729566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
557307218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5574083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
557507218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
55763ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5577083401c6SMatthew G. Knepley     }
5578083401c6SMatthew G. Knepley   }
55799566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
55809566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
55819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
55829566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
558307218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5584083401c6SMatthew G. Knepley   if (!label) {
5585083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5586083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5587083401c6SMatthew G. Knepley     Nds = 0;
5588083401c6SMatthew G. Knepley   }
5589083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5590083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5591083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
559207218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
55933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5594083401c6SMatthew G. Knepley }
5595083401c6SMatthew G. Knepley 
5596083401c6SMatthew G. Knepley /*@
559720f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5598e5e52638SMatthew G. Knepley 
559920f4b53cSBarry Smith   Not Collective
5600e5e52638SMatthew G. Knepley 
5601e5e52638SMatthew G. Knepley   Input Parameters:
560220f4b53cSBarry Smith + dm  - The `DM`
5603e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5604e5e52638SMatthew G. Knepley 
5605e5e52638SMatthew G. Knepley   Output Parameters:
560620f4b53cSBarry Smith + label  - The region label, or `NULL`
560720f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
560807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
560907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5610e5e52638SMatthew G. Knepley 
5611e5e52638SMatthew G. Knepley   Level: advanced
5612e5e52638SMatthew G. Knepley 
56131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5614e5e52638SMatthew G. Knepley @*/
561507218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5616d71ae5a4SJacob Faibussowitsch {
5617e5e52638SMatthew G. Knepley   PetscInt Nds;
5618e5e52638SMatthew G. Knepley 
5619e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5620e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56219566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
562263a3b9bcSJacob 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);
5623e5e52638SMatthew G. Knepley   if (label) {
5624e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5625e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5626e5e52638SMatthew G. Knepley   }
5627b3cf3223SMatthew G. Knepley   if (fields) {
5628b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5629b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5630b3cf3223SMatthew G. Knepley   }
5631e5e52638SMatthew G. Knepley   if (ds) {
5632b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5633e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5634e5e52638SMatthew G. Knepley   }
563507218a29SMatthew G. Knepley   if (dsIn) {
563607218a29SMatthew G. Knepley     PetscValidPointer(dsIn, 6);
563707218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
563807218a29SMatthew G. Knepley   }
56393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5640e5e52638SMatthew G. Knepley }
5641e5e52638SMatthew G. Knepley 
5642e5e52638SMatthew G. Knepley /*@
564320f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5644e5e52638SMatthew G. Knepley 
564520f4b53cSBarry Smith   Not Collective
5646e5e52638SMatthew G. Knepley 
5647e5e52638SMatthew G. Knepley   Input Parameters:
564820f4b53cSBarry Smith + dm     - The `DM`
5649083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
565020f4b53cSBarry Smith . label  - The region label, or `NULL`
565107218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
565207218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
565307218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5654e5e52638SMatthew G. Knepley 
5655e5e52638SMatthew G. Knepley   Level: advanced
5656e5e52638SMatthew G. Knepley 
56571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5658e5e52638SMatthew G. Knepley @*/
565907218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5660d71ae5a4SJacob Faibussowitsch {
5661083401c6SMatthew G. Knepley   PetscInt Nds;
5662e5e52638SMatthew G. Knepley 
5663e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5664e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5665ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56669566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
566763a3b9bcSJacob 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);
56689566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56699566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5670083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5671083401c6SMatthew G. Knepley   if (fields) {
5672083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56739566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56749566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5675083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5676e5e52638SMatthew G. Knepley   }
5677083401c6SMatthew G. Knepley   if (ds) {
5678083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56799566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56809566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5681083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5682083401c6SMatthew G. Knepley   }
568307218a29SMatthew G. Knepley   if (dsIn) {
568407218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
568507218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
568607218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
568707218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
568807218a29SMatthew G. Knepley   }
56893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5690e5e52638SMatthew G. Knepley }
5691e5e52638SMatthew G. Knepley 
5692e5e52638SMatthew G. Knepley /*@
569320f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
56941d3af9e0SMatthew G. Knepley 
569520f4b53cSBarry Smith   Not Collective
56961d3af9e0SMatthew G. Knepley 
56971d3af9e0SMatthew G. Knepley   Input Parameters:
569820f4b53cSBarry Smith + dm  - The `DM`
569920f4b53cSBarry Smith - ds  - The `PetscDS` defined on the given region
57001d3af9e0SMatthew G. Knepley 
57011d3af9e0SMatthew G. Knepley   Output Parameter:
57021d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57031d3af9e0SMatthew G. Knepley 
57041d3af9e0SMatthew G. Knepley   Level: advanced
57051d3af9e0SMatthew G. Knepley 
57061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57071d3af9e0SMatthew G. Knepley @*/
5708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5709d71ae5a4SJacob Faibussowitsch {
57101d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57111d3af9e0SMatthew G. Knepley 
57121d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57131d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57141d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5715dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
57169566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57179371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57189371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57191d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57201d3af9e0SMatthew G. Knepley   else *num = n;
57213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57221d3af9e0SMatthew G. Knepley }
57231d3af9e0SMatthew G. Knepley 
57242df84da0SMatthew G. Knepley /*@C
5725bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57262df84da0SMatthew G. Knepley 
572720f4b53cSBarry Smith   Not Collective
57282df84da0SMatthew G. Knepley 
5729f1a722f8SMatthew G. Knepley   Input Parameters:
5730bb7acecfSBarry Smith + dm     - The `DM`
57312df84da0SMatthew G. Knepley . Nc     - The number of components for the field
573220f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5733bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57342df84da0SMatthew G. Knepley 
57352df84da0SMatthew G. Knepley   Output Parameter:
5736bb7acecfSBarry Smith . fem - The `PetscFE`
57372df84da0SMatthew G. Knepley 
573820f4b53cSBarry Smith   Level: intermediate
573920f4b53cSBarry Smith 
5740bb7acecfSBarry Smith   Note:
5741bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57422df84da0SMatthew G. Knepley 
57431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57442df84da0SMatthew G. Knepley @*/
5745d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5746d71ae5a4SJacob Faibussowitsch {
57472df84da0SMatthew G. Knepley   DMPolytopeType ct;
57482df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57492df84da0SMatthew G. Knepley 
57502df84da0SMatthew G. Knepley   PetscFunctionBegin;
57512df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57522df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57532df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57542df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57552df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57569566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57579566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57599566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57612df84da0SMatthew G. Knepley }
57622df84da0SMatthew G. Knepley 
57631d3af9e0SMatthew G. Knepley /*@
5764bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5765e5e52638SMatthew G. Knepley 
576620f4b53cSBarry Smith   Collective
5767e5e52638SMatthew G. Knepley 
5768e5e52638SMatthew G. Knepley   Input Parameter:
5769bb7acecfSBarry Smith . dm - The `DM`
5770e5e52638SMatthew G. Knepley 
577120f4b53cSBarry Smith   Options Database Key:
5772bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
577345480ffeSMatthew G. Knepley 
577420f4b53cSBarry Smith   Level: intermediate
577520f4b53cSBarry Smith 
57761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5777e5e52638SMatthew G. Knepley @*/
5778d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5779d71ae5a4SJacob Faibussowitsch {
5780e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5781083401c6SMatthew G. Knepley   PetscDS   dsDef;
5782083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5783f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5784f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5785e5e52638SMatthew G. Knepley 
5786e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5787e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57883ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
57899566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
57909566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5791083401c6SMatthew G. Knepley   /* Determine how many regions we have */
57929566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5793083401c6SMatthew G. Knepley   Nl   = 0;
5794083401c6SMatthew G. Knepley   Ndef = 0;
5795083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5796083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5797083401c6SMatthew G. Knepley     PetscInt l;
5798083401c6SMatthew G. Knepley 
5799f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5800f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5801f918ec44SMatthew G. Knepley     {
5802f918ec44SMatthew G. Knepley       PetscClassId id;
5803f918ec44SMatthew G. Knepley 
58049566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5805f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5806f918ec44SMatthew G. Knepley         Ceed ceed;
5807f918ec44SMatthew G. Knepley 
58089566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58099566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5810f918ec44SMatthew G. Knepley       }
5811f918ec44SMatthew G. Knepley     }
5812f918ec44SMatthew G. Knepley #endif
58139371c9d4SSatish Balay     if (!label) {
58149371c9d4SSatish Balay       ++Ndef;
58159371c9d4SSatish Balay       continue;
58169371c9d4SSatish Balay     }
58179371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58189371c9d4SSatish Balay       if (label == labelSet[l]) break;
5819083401c6SMatthew G. Knepley     if (l < Nl) continue;
5820083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5821083401c6SMatthew G. Knepley   }
5822083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
582307218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5824083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5825b3cf3223SMatthew G. Knepley     IS        fields;
5826b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5827b3cf3223SMatthew G. Knepley 
58289371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58299371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58307a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58319566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58329371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58339371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58349566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58369566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58379566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
583888f0c812SMatthew G. Knepley 
58399566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
584007218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
58419566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58429566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58432df9ee95SMatthew G. Knepley   }
584407218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
58459566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5846083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5847083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58480122748bSMatthew G. Knepley     DM              plex;
5849083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5850083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5851083401c6SMatthew G. Knepley     PetscInt       *fields;
5852083401c6SMatthew G. Knepley     const PetscInt *cells;
5853083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58540122748bSMatthew G. Knepley 
58559566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58569566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58579566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58589566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58595fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5860083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5861083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5862083401c6SMatthew G. Knepley       IS      pointIS;
5863083401c6SMatthew G. Knepley 
58649566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58659566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58669566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58679566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5868083401c6SMatthew G. Knepley     }
58699566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5870083401c6SMatthew G. Knepley 
58719566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58729566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58749566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58759566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58769566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58779566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5878083401c6SMatthew G. Knepley 
58799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58809371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58819371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58829566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58839566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58849566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58859566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5886083401c6SMatthew G. Knepley 
58879566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
588807218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
58899566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
58909566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
58919566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58929566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
58939566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5894083401c6SMatthew G. Knepley   }
5895083401c6SMatthew G. Knepley   /* Create label DSes
5896083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5897083401c6SMatthew G. Knepley   */
5898083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5899083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5900083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
590107218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5902083401c6SMatthew G. Knepley     IS        fields;
5903083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5904083401c6SMatthew G. Knepley 
59059566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59069371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59079371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59089566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59099371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59109371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59119566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59129566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59139566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59149566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59159566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5916083401c6SMatthew G. Knepley     {
5917083401c6SMatthew G. Knepley       DMPolytopeType ct;
5918083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59195fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59200122748bSMatthew G. Knepley 
59219566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5922665f567fSMatthew G. Knepley       if (lStart >= 0) {
59239566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5924412e9a14SMatthew G. Knepley         switch (ct) {
5925412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5926412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5927412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5928d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5929d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5930d71ae5a4SJacob Faibussowitsch           break;
5931d71ae5a4SJacob Faibussowitsch         default:
5932d71ae5a4SJacob Faibussowitsch           break;
5933412e9a14SMatthew G. Knepley         }
5934665f567fSMatthew G. Knepley       }
5935712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
593607218a29SMatthew G. Knepley       if (isCohesive) {
593707218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
593807218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
593907218a29SMatthew G. Knepley       }
59405fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59415fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59425fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59439566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59449566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
594507218a29SMatthew G. Knepley             if (dsIn) {
594607218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
594707218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
594807218a29SMatthew G. Knepley             }
59495fedec97SMatthew G. Knepley           }
59505fedec97SMatthew G. Knepley           ++nf;
59515fedec97SMatthew G. Knepley         }
59525fedec97SMatthew G. Knepley       }
5953e5e52638SMatthew G. Knepley     }
595407218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
595507218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
59569566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
595707218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
5958e5e52638SMatthew G. Knepley   }
59599566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5960e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5961083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5962083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
596307218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
5964083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5965083401c6SMatthew G. Knepley     const PetscInt *fld;
59665fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59675fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5968e5e52638SMatthew G. Knepley 
59699566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59709566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59719566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59729566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5973083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5974083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59755fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5976e5e52638SMatthew G. Knepley       PetscClassId id;
5977e5e52638SMatthew G. Knepley 
59785fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59799566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59805fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
598107218a29SMatthew G. Knepley       if (isCohesive) {
598207218a29SMatthew G. Knepley         if (!isCohesiveField) {
598307218a29SMatthew G. Knepley           PetscObject bdDisc;
598407218a29SMatthew G. Knepley 
598507218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
598607218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
598707218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
598807218a29SMatthew G. Knepley         } else {
59899566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
599007218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
599107218a29SMatthew G. Knepley         }
599207218a29SMatthew G. Knepley       } else {
599307218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
599407218a29SMatthew G. Knepley       }
5995083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59969566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5997e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5998e5e52638SMatthew G. Knepley     }
59999566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6000e5e52638SMatthew G. Knepley   }
6001f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6003f9244615SMatthew G. Knepley   if (flg) {
60043b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60053b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
600607218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60073b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60083b4aee56SMatthew G. Knepley 
60099566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
601007218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
601107218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
601207218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
601307218a29SMatthew G. Knepley       }
60143b4aee56SMatthew G. Knepley     }
6015f9244615SMatthew G. Knepley   }
6016e5e52638SMatthew G. Knepley   /* Setup DSes */
6017e5e52638SMatthew G. Knepley   if (doSetup) {
601807218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
601907218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
602007218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
602107218a29SMatthew G. Knepley     }
6022e5e52638SMatthew G. Knepley   }
60233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6024e5e52638SMatthew G. Knepley }
6025e5e52638SMatthew G. Knepley 
6026e5e52638SMatthew G. Knepley /*@
6027bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
60287f96f943SMatthew G. Knepley 
602920f4b53cSBarry Smith   Collective
6030f2cacb80SMatthew G. Knepley 
60317f96f943SMatthew G. Knepley   Input Parameters:
6032bb7acecfSBarry Smith + dm   - The `DM`
60337f96f943SMatthew G. Knepley - time - The time
60347f96f943SMatthew G. Knepley 
60357f96f943SMatthew G. Knepley   Output Parameters:
603620f4b53cSBarry Smith + u    - The vector will be filled with exact solution values, or `NULL`
603720f4b53cSBarry Smith - u_t  - The vector will be filled with the time derivative of exact solution values, or `NULL`
603820f4b53cSBarry Smith 
603920f4b53cSBarry Smith   Level: developer
60407f96f943SMatthew G. Knepley 
6041bb7acecfSBarry Smith   Note:
6042bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
60437f96f943SMatthew G. Knepley 
60441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
60457f96f943SMatthew G. Knepley @*/
6046d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6047d71ae5a4SJacob Faibussowitsch {
60487f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
60497f96f943SMatthew G. Knepley   void   **ectxs;
6050f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
60517f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
60527f96f943SMatthew G. Knepley 
60537f96f943SMatthew G. Knepley   PetscFunctionBegin;
6054f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6055f60fa741SMatthew G. Knepley   if (u) {
6056f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6057f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6058f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6059f60fa741SMatthew G. Knepley   }
6060f60fa741SMatthew G. Knepley   if (u_t) {
6061f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6062f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6063f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6064f60fa741SMatthew G. Knepley   }
60659566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
60669566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
60679566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60687f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
60697f96f943SMatthew G. Knepley     PetscDS         ds;
60707f96f943SMatthew G. Knepley     DMLabel         label;
60717f96f943SMatthew G. Knepley     IS              fieldIS;
60727f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60737f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60747f96f943SMatthew G. Knepley 
607507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
60769566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
60779566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
60789566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
60799566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6080f2cacb80SMatthew G. Knepley     if (u) {
6081f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6082f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6083f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
60847f96f943SMatthew G. Knepley     }
6085f2cacb80SMatthew G. Knepley     if (u_t) {
60869566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60879566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6088f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6089f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6090f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6091f2cacb80SMatthew G. Knepley     }
60929566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6093f2cacb80SMatthew G. Knepley   }
6094f2cacb80SMatthew G. Knepley   if (u) {
60959566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60969566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6097f2cacb80SMatthew G. Knepley   }
6098f2cacb80SMatthew G. Knepley   if (u_t) {
60999566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
61009566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6101f2cacb80SMatthew G. Knepley   }
61029566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6103f60fa741SMatthew G. Knepley   if (u) {
6104f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6105f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6106f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6107f60fa741SMatthew G. Knepley   }
6108f60fa741SMatthew G. Knepley   if (u_t) {
6109f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6110f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6111f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6112f60fa741SMatthew G. Knepley   }
61133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61147f96f943SMatthew G. Knepley }
61157f96f943SMatthew G. Knepley 
611607218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6117d71ae5a4SJacob Faibussowitsch {
611807218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
611945480ffeSMatthew G. Knepley 
612045480ffeSMatthew G. Knepley   PetscFunctionBegin;
61219566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
612207218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
612307218a29SMatthew G. Knepley   if (dsIn) {
612407218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
612507218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
612645480ffeSMatthew G. Knepley   }
612707218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
61289566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
612907218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
61303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
613145480ffeSMatthew G. Knepley }
613245480ffeSMatthew G. Knepley 
61337f96f943SMatthew G. Knepley /*@
6134bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6135e5e52638SMatthew G. Knepley 
613620f4b53cSBarry Smith   Collective
6137e5e52638SMatthew G. Knepley 
6138e5e52638SMatthew G. Knepley   Input Parameter:
6139bb7acecfSBarry Smith . dm - The `DM`
6140e5e52638SMatthew G. Knepley 
6141e5e52638SMatthew G. Knepley   Output Parameter:
6142bb7acecfSBarry Smith . newdm - The `DM`
6143e5e52638SMatthew G. Knepley 
6144e5e52638SMatthew G. Knepley   Level: advanced
6145e5e52638SMatthew G. Knepley 
61461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6147e5e52638SMatthew G. Knepley @*/
6148d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6149d71ae5a4SJacob Faibussowitsch {
6150e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6151e5e52638SMatthew G. Knepley 
6152e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61533ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
61549566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61559566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6156e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6157e5e52638SMatthew G. Knepley     DMLabel  label;
6158b3cf3223SMatthew G. Knepley     IS       fields;
615907218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6160783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6161e5e52638SMatthew G. Knepley 
616207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6163b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
616407218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6165d5b43468SJose E. Roman     /* Complete new labels in the new DS */
616607218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
61679566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6168783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6169b8025e53SMatthew G. Knepley       PetscWeakForm wf;
617045480ffeSMatthew G. Knepley       DMLabel       label;
6171783e2ec8SMatthew G. Knepley       PetscInt      field;
6172783e2ec8SMatthew G. Knepley 
61739566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61749566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6175783e2ec8SMatthew G. Knepley     }
6176e5e52638SMatthew G. Knepley   }
6177799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
61783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6179e5e52638SMatthew G. Knepley }
6180e5e52638SMatthew G. Knepley 
6181e5e52638SMatthew G. Knepley /*@
6182bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6183e5e52638SMatthew G. Knepley 
618420f4b53cSBarry Smith   Collective
6185e5e52638SMatthew G. Knepley 
6186e5e52638SMatthew G. Knepley   Input Parameter:
6187bb7acecfSBarry Smith . dm - The `DM`
6188e5e52638SMatthew G. Knepley 
6189e5e52638SMatthew G. Knepley   Output Parameter:
6190bb7acecfSBarry Smith . newdm - The `DM`
6191e5e52638SMatthew G. Knepley 
6192e5e52638SMatthew G. Knepley   Level: advanced
6193e5e52638SMatthew G. Knepley 
6194bb7acecfSBarry Smith   Developer Note:
6195bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6196bb7acecfSBarry Smith 
61971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6198e5e52638SMatthew G. Knepley @*/
6199d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6200d71ae5a4SJacob Faibussowitsch {
6201e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62029566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
62039566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
62043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6205e5e52638SMatthew G. Knepley }
6206e5e52638SMatthew G. Knepley 
6207c73cfb54SMatthew G. Knepley /*@
6208bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6209c73cfb54SMatthew G. Knepley 
621020f4b53cSBarry Smith   Not Collective
6211c73cfb54SMatthew G. Knepley 
6212c73cfb54SMatthew G. Knepley   Input Parameter:
6213bb7acecfSBarry Smith . dm - The `DM`
6214c73cfb54SMatthew G. Knepley 
6215c73cfb54SMatthew G. Knepley   Output Parameter:
6216c73cfb54SMatthew G. Knepley . dim - The topological dimension
6217c73cfb54SMatthew G. Knepley 
6218c73cfb54SMatthew G. Knepley   Level: beginner
6219c73cfb54SMatthew G. Knepley 
62201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6221c73cfb54SMatthew G. Knepley @*/
6222d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6223d71ae5a4SJacob Faibussowitsch {
6224c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6225c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6226534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6227c73cfb54SMatthew G. Knepley   *dim = dm->dim;
62283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6229c73cfb54SMatthew G. Knepley }
6230c73cfb54SMatthew G. Knepley 
6231c73cfb54SMatthew G. Knepley /*@
6232bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6233c73cfb54SMatthew G. Knepley 
623420f4b53cSBarry Smith   Collective
6235c73cfb54SMatthew G. Knepley 
6236c73cfb54SMatthew G. Knepley   Input Parameters:
6237bb7acecfSBarry Smith + dm - The `DM`
6238c73cfb54SMatthew G. Knepley - dim - The topological dimension
6239c73cfb54SMatthew G. Knepley 
6240c73cfb54SMatthew G. Knepley   Level: beginner
6241c73cfb54SMatthew G. Knepley 
62421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6243c73cfb54SMatthew G. Knepley @*/
6244d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6245d71ae5a4SJacob Faibussowitsch {
6246e5e52638SMatthew G. Knepley   PetscDS  ds;
624745480ffeSMatthew G. Knepley   PetscInt Nds, n;
6248f17e8794SMatthew G. Knepley 
6249c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6250c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6251c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6252c73cfb54SMatthew G. Knepley   dm->dim = dim;
6253d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
62549566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
625545480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
625607218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
62579566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
625845480ffeSMatthew G. Knepley     }
6259d17bd122SMatthew G. Knepley   }
62603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6261c73cfb54SMatthew G. Knepley }
6262c73cfb54SMatthew G. Knepley 
6263793f3fe5SMatthew G. Knepley /*@
6264793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6265793f3fe5SMatthew G. Knepley 
626620f4b53cSBarry Smith   Collective
6267793f3fe5SMatthew G. Knepley 
6268793f3fe5SMatthew G. Knepley   Input Parameters:
6269bb7acecfSBarry Smith + dm - the `DM`
6270793f3fe5SMatthew G. Knepley - dim - the dimension
6271793f3fe5SMatthew G. Knepley 
6272793f3fe5SMatthew G. Knepley   Output Parameters:
6273793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6274aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6275793f3fe5SMatthew G. Knepley 
627620f4b53cSBarry Smith   Level: intermediate
627720f4b53cSBarry Smith 
6278793f3fe5SMatthew G. Knepley   Note:
6279793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6280a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6281793f3fe5SMatthew G. Knepley   then the interval is empty.
6282793f3fe5SMatthew G. Knepley 
62831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6284793f3fe5SMatthew G. Knepley @*/
6285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6286d71ae5a4SJacob Faibussowitsch {
6287793f3fe5SMatthew G. Knepley   PetscInt d;
6288793f3fe5SMatthew G. Knepley 
6289793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6290793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62919566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62927a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6293dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
62943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6295793f3fe5SMatthew G. Knepley }
6296793f3fe5SMatthew G. Knepley 
62976636e97aSMatthew G Knepley /*@
6298bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6299f4d763aaSMatthew G. Knepley 
630020f4b53cSBarry Smith   Collective
63018f700142SStefano Zampini 
6302f4d763aaSMatthew G. Knepley   Input Parameter:
6303bb7acecfSBarry Smith . dm - The original `DM`
6304f4d763aaSMatthew G. Knepley 
6305f4d763aaSMatthew G. Knepley   Output Parameter:
6306bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6307f4d763aaSMatthew G. Knepley 
6308f4d763aaSMatthew G. Knepley   Level: intermediate
6309f4d763aaSMatthew G. Knepley 
6310bb7acecfSBarry Smith   Note:
6311bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6312bb7acecfSBarry 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
6313bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6314bb7acecfSBarry Smith 
63151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6316f4d763aaSMatthew G. Knepley @*/
6317d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6318d71ae5a4SJacob Faibussowitsch {
6319c26acbdeSMatthew G. Knepley   PetscSection section;
63202d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
632114f150ffSMatthew G. Knepley 
632214f150ffSMatthew G. Knepley   PetscFunctionBegin;
632314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
632414f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
63259566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
63269566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6327712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
63282d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6329c26acbdeSMatthew G. Knepley     *odm = dm;
63303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6331c26acbdeSMatthew G. Knepley   }
633214f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6333c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
633414f150ffSMatthew G. Knepley     PetscSF      sf;
633514f150ffSMatthew G. Knepley 
63369566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
63379566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
63389566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
63399566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
63409566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
63419566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
63429566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
63439566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
63449566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
634514f150ffSMatthew G. Knepley   }
634614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
63473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
634814f150ffSMatthew G. Knepley }
6349f4d763aaSMatthew G. Knepley 
6350f4d763aaSMatthew G. Knepley /*@
6351cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6352f4d763aaSMatthew G. Knepley 
6353f4d763aaSMatthew G. Knepley   Input Parameter:
6354bb7acecfSBarry Smith . dm - The original `DM`
6355f4d763aaSMatthew G. Knepley 
6356cdb7a50dSMatthew G. Knepley   Output Parameters:
6357cdb7a50dSMatthew G. Knepley + num - The output sequence number
6358cdb7a50dSMatthew G. Knepley - val - The output sequence value
6359f4d763aaSMatthew G. Knepley 
6360f4d763aaSMatthew G. Knepley   Level: intermediate
6361f4d763aaSMatthew G. Knepley 
6362bb7acecfSBarry Smith   Note:
6363bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6364bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6365bb7acecfSBarry Smith 
6366bb7acecfSBarry Smith   Developer Note:
6367bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6368bb7acecfSBarry Smith   not directly related to the `DM`.
6369f4d763aaSMatthew G. Knepley 
63701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6371f4d763aaSMatthew G. Knepley @*/
6372d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6373d71ae5a4SJacob Faibussowitsch {
6374f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6375f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63769371c9d4SSatish Balay   if (num) {
63779371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
63789371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63799371c9d4SSatish Balay   }
63809371c9d4SSatish Balay   if (val) {
63819371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
63829371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63839371c9d4SSatish Balay   }
63843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6385f4d763aaSMatthew G. Knepley }
6386f4d763aaSMatthew G. Knepley 
6387f4d763aaSMatthew G. Knepley /*@
6388cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6389f4d763aaSMatthew G. Knepley 
6390f4d763aaSMatthew G. Knepley   Input Parameters:
6391bb7acecfSBarry Smith + dm - The original `DM`
6392cdb7a50dSMatthew G. Knepley . num - The output sequence number
6393cdb7a50dSMatthew G. Knepley - val - The output sequence value
6394f4d763aaSMatthew G. Knepley 
6395f4d763aaSMatthew G. Knepley   Level: intermediate
6396f4d763aaSMatthew G. Knepley 
6397bb7acecfSBarry Smith   Note:
6398bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6399bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6400f4d763aaSMatthew G. Knepley 
64011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6402f4d763aaSMatthew G. Knepley @*/
6403d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6404d71ae5a4SJacob Faibussowitsch {
6405f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6406f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6407f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6408cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
64093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6410cdb7a50dSMatthew G. Knepley }
6411cdb7a50dSMatthew G. Knepley 
6412cdb7a50dSMatthew G. Knepley /*@C
6413bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6414cdb7a50dSMatthew G. Knepley 
6415cdb7a50dSMatthew G. Knepley   Input Parameters:
6416bb7acecfSBarry Smith + dm   - The original `DM`
6417cdb7a50dSMatthew G. Knepley . name - The sequence name
6418cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6419cdb7a50dSMatthew G. Knepley 
6420cdb7a50dSMatthew G. Knepley   Output Parameter:
6421cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6422cdb7a50dSMatthew G. Knepley 
6423cdb7a50dSMatthew G. Knepley   Level: intermediate
6424cdb7a50dSMatthew G. Knepley 
6425bb7acecfSBarry Smith   Note:
6426bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6427bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6428bb7acecfSBarry Smith 
6429bb7acecfSBarry Smith   Developer Note:
6430bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6431cdb7a50dSMatthew G. Knepley 
64321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6433cdb7a50dSMatthew G. Knepley @*/
6434d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6435d71ae5a4SJacob Faibussowitsch {
6436cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6437cdb7a50dSMatthew G. Knepley 
6438cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6439cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6440cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6441064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
64429566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6443cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6444cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6445cdb7a50dSMatthew G. Knepley     PetscScalar value;
6446cdb7a50dSMatthew G. Knepley 
64479566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
64484aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6449cdb7a50dSMatthew G. Knepley #endif
6450cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
64513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6452f4d763aaSMatthew G. Knepley }
64538e4ac7eaSMatthew G. Knepley 
64548e4ac7eaSMatthew G. Knepley /*@
6455bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64568e4ac7eaSMatthew G. Knepley 
645720f4b53cSBarry Smith   Not Collective
64588e4ac7eaSMatthew G. Knepley 
64598e4ac7eaSMatthew G. Knepley   Input Parameter:
6460bb7acecfSBarry Smith . dm - The `DM`
64618e4ac7eaSMatthew G. Knepley 
64628e4ac7eaSMatthew G. Knepley   Output Parameter:
6463bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64648e4ac7eaSMatthew G. Knepley 
64658e4ac7eaSMatthew G. Knepley   Level: beginner
64668e4ac7eaSMatthew G. Knepley 
64671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
64688e4ac7eaSMatthew G. Knepley @*/
6469d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6470d71ae5a4SJacob Faibussowitsch {
64718e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64728e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6473534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64748e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64768e4ac7eaSMatthew G. Knepley }
64778e4ac7eaSMatthew G. Knepley 
64788e4ac7eaSMatthew G. Knepley /*@
6479bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64808e4ac7eaSMatthew G. Knepley 
648120f4b53cSBarry Smith   Collective
64828e4ac7eaSMatthew G. Knepley 
64838e4ac7eaSMatthew G. Knepley   Input Parameters:
6484bb7acecfSBarry Smith  + dm - The `DM`
6485bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64868e4ac7eaSMatthew G. Knepley 
6487bb7acecfSBarry Smith   Note:
6488bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64895d3b26e6SMatthew G. Knepley 
64908e4ac7eaSMatthew G. Knepley   Level: beginner
64918e4ac7eaSMatthew G. Knepley 
64921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64938e4ac7eaSMatthew G. Knepley @*/
6494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6495d71ae5a4SJacob Faibussowitsch {
64968e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64978e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64988833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64998e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
65003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65018e4ac7eaSMatthew G. Knepley }
6502c58f1c22SToby Isaac 
6503c58f1c22SToby Isaac /*@C
6504bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6505c58f1c22SToby Isaac 
6506c58f1c22SToby Isaac   Not Collective
6507c58f1c22SToby Isaac 
6508c58f1c22SToby Isaac   Input Parameters:
6509bb7acecfSBarry Smith + dm   - The `DM` object
6510c58f1c22SToby Isaac - name - The label name
6511c58f1c22SToby Isaac 
6512c58f1c22SToby Isaac   Level: intermediate
6513c58f1c22SToby Isaac 
65141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6515c58f1c22SToby Isaac @*/
6516d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6517d71ae5a4SJacob Faibussowitsch {
65185d80c0bfSVaclav Hapla   PetscBool flg;
65195d80c0bfSVaclav Hapla   DMLabel   label;
6520c58f1c22SToby Isaac 
6521c58f1c22SToby Isaac   PetscFunctionBegin;
6522c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6523c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65249566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6525c58f1c22SToby Isaac   if (!flg) {
65269566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65279566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65289566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6529c58f1c22SToby Isaac   }
65303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6531c58f1c22SToby Isaac }
6532c58f1c22SToby Isaac 
6533c58f1c22SToby Isaac /*@C
6534bb7acecfSBarry 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.
65350fdc7489SMatthew Knepley 
65360fdc7489SMatthew Knepley   Not Collective
65370fdc7489SMatthew Knepley 
65380fdc7489SMatthew Knepley   Input Parameters:
6539bb7acecfSBarry Smith + dm   - The `DM` object
65400fdc7489SMatthew Knepley . l    - The index for the label
65410fdc7489SMatthew Knepley - name - The label name
65420fdc7489SMatthew Knepley 
65430fdc7489SMatthew Knepley   Level: intermediate
65440fdc7489SMatthew Knepley 
65451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
65460fdc7489SMatthew Knepley @*/
6547d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6548d71ae5a4SJacob Faibussowitsch {
65490fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
65500fdc7489SMatthew Knepley   DMLabel     label;
65510fdc7489SMatthew Knepley   PetscInt    Nl, m;
65520fdc7489SMatthew Knepley   PetscBool   flg, match;
65530fdc7489SMatthew Knepley   const char *lname;
65540fdc7489SMatthew Knepley 
65550fdc7489SMatthew Knepley   PetscFunctionBegin;
65560fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6557064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
65589566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
65590fdc7489SMatthew Knepley   if (!flg) {
65609566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65619566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65629566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65630fdc7489SMatthew Knepley   }
65649566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
656563a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65660fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65679566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65689566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65690fdc7489SMatthew Knepley     if (match) break;
65700fdc7489SMatthew Knepley   }
65713ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
65720fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65730fdc7489SMatthew Knepley   else prev->next = orig->next;
65740fdc7489SMatthew Knepley   if (!l) {
65750fdc7489SMatthew Knepley     orig->next = dm->labels;
65760fdc7489SMatthew Knepley     dm->labels = orig;
65770fdc7489SMatthew Knepley   } else {
65789371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65799371c9d4SSatish Balay       ;
65800fdc7489SMatthew Knepley     orig->next = prev->next;
65810fdc7489SMatthew Knepley     prev->next = orig;
65820fdc7489SMatthew Knepley   }
65833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65840fdc7489SMatthew Knepley }
65850fdc7489SMatthew Knepley 
65860fdc7489SMatthew Knepley /*@C
6587bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6588c58f1c22SToby Isaac 
6589c58f1c22SToby Isaac   Not Collective
6590c58f1c22SToby Isaac 
6591c58f1c22SToby Isaac   Input Parameters:
6592bb7acecfSBarry Smith + dm   - The `DM` object
6593c58f1c22SToby Isaac . name - The label name
6594c58f1c22SToby Isaac - point - The mesh point
6595c58f1c22SToby Isaac 
6596c58f1c22SToby Isaac   Output Parameter:
6597c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6598c58f1c22SToby Isaac 
6599c58f1c22SToby Isaac   Level: beginner
6600c58f1c22SToby Isaac 
66011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6602c58f1c22SToby Isaac @*/
6603d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6604d71ae5a4SJacob Faibussowitsch {
6605c58f1c22SToby Isaac   DMLabel label;
6606c58f1c22SToby Isaac 
6607c58f1c22SToby Isaac   PetscFunctionBegin;
6608c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6609c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66109566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66117a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
66129566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
66133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6614c58f1c22SToby Isaac }
6615c58f1c22SToby Isaac 
6616c58f1c22SToby Isaac /*@C
6617bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6618c58f1c22SToby Isaac 
6619c58f1c22SToby Isaac   Not Collective
6620c58f1c22SToby Isaac 
6621c58f1c22SToby Isaac   Input Parameters:
6622bb7acecfSBarry Smith + dm   - The `DM` object
6623c58f1c22SToby Isaac . name - The label name
6624c58f1c22SToby Isaac . point - The mesh point
6625c58f1c22SToby Isaac - value - The label value for this point
6626c58f1c22SToby Isaac 
6627c58f1c22SToby Isaac   Output Parameter:
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   Level: beginner
6630c58f1c22SToby Isaac 
66311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6632c58f1c22SToby Isaac @*/
6633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6634d71ae5a4SJacob Faibussowitsch {
6635c58f1c22SToby Isaac   DMLabel label;
6636c58f1c22SToby Isaac 
6637c58f1c22SToby Isaac   PetscFunctionBegin;
6638c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6639c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66409566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6641c58f1c22SToby Isaac   if (!label) {
66429566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
66439566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6644c58f1c22SToby Isaac   }
66459566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
66463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6647c58f1c22SToby Isaac }
6648c58f1c22SToby Isaac 
6649c58f1c22SToby Isaac /*@C
6650bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6651c58f1c22SToby Isaac 
6652c58f1c22SToby Isaac   Not Collective
6653c58f1c22SToby Isaac 
6654c58f1c22SToby Isaac   Input Parameters:
6655bb7acecfSBarry Smith + dm   - The `DM` object
6656c58f1c22SToby Isaac . name - The label name
6657c58f1c22SToby Isaac . point - The mesh point
6658c58f1c22SToby Isaac - value - The label value for this point
6659c58f1c22SToby Isaac 
6660c58f1c22SToby Isaac   Level: beginner
6661c58f1c22SToby Isaac 
66621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6663c58f1c22SToby Isaac @*/
6664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6665d71ae5a4SJacob Faibussowitsch {
6666c58f1c22SToby Isaac   DMLabel label;
6667c58f1c22SToby Isaac 
6668c58f1c22SToby Isaac   PetscFunctionBegin;
6669c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6670c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66719566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66723ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
66739566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
66743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6675c58f1c22SToby Isaac }
6676c58f1c22SToby Isaac 
6677c58f1c22SToby Isaac /*@C
6678bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6679c58f1c22SToby Isaac 
6680c58f1c22SToby Isaac   Not Collective
6681c58f1c22SToby Isaac 
6682c58f1c22SToby Isaac   Input Parameters:
6683bb7acecfSBarry Smith + dm   - The `DM` object
6684c58f1c22SToby Isaac - name - The label name
6685c58f1c22SToby Isaac 
6686c58f1c22SToby Isaac   Output Parameter:
6687c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6688c58f1c22SToby Isaac 
6689c58f1c22SToby Isaac   Level: beginner
6690c58f1c22SToby Isaac 
6691bb7acecfSBarry Smith   Developer Note:
6692bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6693bb7acecfSBarry Smith 
66941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6695c58f1c22SToby Isaac @*/
6696d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6697d71ae5a4SJacob Faibussowitsch {
6698c58f1c22SToby Isaac   DMLabel label;
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac   PetscFunctionBegin;
6701c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6702c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6703534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
67049566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6705c58f1c22SToby Isaac   *size = 0;
67063ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67079566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
67083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6709c58f1c22SToby Isaac }
6710c58f1c22SToby Isaac 
6711c58f1c22SToby Isaac /*@C
6712bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6713c58f1c22SToby Isaac 
6714c58f1c22SToby Isaac   Not Collective
6715c58f1c22SToby Isaac 
6716c58f1c22SToby Isaac   Input Parameters:
6717bb7acecfSBarry Smith + mesh - The `DM` object
6718c58f1c22SToby Isaac - name - The label name
6719c58f1c22SToby Isaac 
6720c58f1c22SToby Isaac   Output Parameter:
672120f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   Level: beginner
6724c58f1c22SToby Isaac 
67251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6726c58f1c22SToby Isaac @*/
6727d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6728d71ae5a4SJacob Faibussowitsch {
6729c58f1c22SToby Isaac   DMLabel label;
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac   PetscFunctionBegin;
6732c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6733c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6734c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
67359566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6736c58f1c22SToby Isaac   *ids = NULL;
6737dab2e251SBlaise Bourdin   if (label) {
67389566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6739dab2e251SBlaise Bourdin   } else {
6740dab2e251SBlaise Bourdin     /* returning an empty IS */
67419566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6742dab2e251SBlaise Bourdin   }
67433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6744c58f1c22SToby Isaac }
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac /*@C
6747c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6748c58f1c22SToby Isaac 
6749c58f1c22SToby Isaac   Not Collective
6750c58f1c22SToby Isaac 
6751c58f1c22SToby Isaac   Input Parameters:
6752bb7acecfSBarry Smith + dm - The `DM` object
6753c58f1c22SToby Isaac . name - The label name
6754c58f1c22SToby Isaac - value - The stratum value
6755c58f1c22SToby Isaac 
6756c58f1c22SToby Isaac   Output Parameter:
6757bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6758c58f1c22SToby Isaac 
6759c58f1c22SToby Isaac   Level: beginner
6760c58f1c22SToby Isaac 
67611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6762c58f1c22SToby Isaac @*/
6763d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6764d71ae5a4SJacob Faibussowitsch {
6765c58f1c22SToby Isaac   DMLabel label;
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac   PetscFunctionBegin;
6768c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6769c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6770534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67719566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6772c58f1c22SToby Isaac   *size = 0;
67733ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67749566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
67753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6776c58f1c22SToby Isaac }
6777c58f1c22SToby Isaac 
6778c58f1c22SToby Isaac /*@C
6779c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6780c58f1c22SToby Isaac 
6781c58f1c22SToby Isaac   Not Collective
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac   Input Parameters:
6784bb7acecfSBarry Smith + dm - The `DM` object
6785c58f1c22SToby Isaac . name - The label name
6786c58f1c22SToby Isaac - value - The stratum value
6787c58f1c22SToby Isaac 
6788c58f1c22SToby Isaac   Output Parameter:
678920f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac   Level: beginner
6792c58f1c22SToby Isaac 
67931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6794c58f1c22SToby Isaac @*/
6795d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6796d71ae5a4SJacob Faibussowitsch {
6797c58f1c22SToby Isaac   DMLabel label;
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   PetscFunctionBegin;
6800c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6801c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6802c58f1c22SToby Isaac   PetscValidPointer(points, 4);
68039566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6804c58f1c22SToby Isaac   *points = NULL;
68053ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68069566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
68073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6808c58f1c22SToby Isaac }
6809c58f1c22SToby Isaac 
68104de306b1SToby Isaac /*@C
68119044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68124de306b1SToby Isaac 
68134de306b1SToby Isaac   Not Collective
68144de306b1SToby Isaac 
68154de306b1SToby Isaac   Input Parameters:
6816bb7acecfSBarry Smith + dm - The `DM` object
68174de306b1SToby Isaac . name - The label name
68184de306b1SToby Isaac . value - The stratum value
68194de306b1SToby Isaac - points - The stratum points
68204de306b1SToby Isaac 
68214de306b1SToby Isaac   Level: beginner
68224de306b1SToby Isaac 
68231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
68244de306b1SToby Isaac @*/
6825d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6826d71ae5a4SJacob Faibussowitsch {
68274de306b1SToby Isaac   DMLabel label;
68284de306b1SToby Isaac 
68294de306b1SToby Isaac   PetscFunctionBegin;
68304de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68314de306b1SToby Isaac   PetscValidCharPointer(name, 2);
68324de306b1SToby Isaac   PetscValidPointer(points, 4);
68339566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68343ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68359566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
68363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
68374de306b1SToby Isaac }
68384de306b1SToby Isaac 
6839c58f1c22SToby Isaac /*@C
6840bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6841c58f1c22SToby Isaac 
6842c58f1c22SToby Isaac   Not Collective
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Input Parameters:
6845bb7acecfSBarry Smith + dm   - The `DM` object
6846c58f1c22SToby Isaac . name - The label name
6847c58f1c22SToby Isaac - value - The label value for this point
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac   Output Parameter:
6850c58f1c22SToby Isaac 
6851c58f1c22SToby Isaac   Level: beginner
6852c58f1c22SToby Isaac 
68531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6854c58f1c22SToby Isaac @*/
6855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6856d71ae5a4SJacob Faibussowitsch {
6857c58f1c22SToby Isaac   DMLabel label;
6858c58f1c22SToby Isaac 
6859c58f1c22SToby Isaac   PetscFunctionBegin;
6860c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6861c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68629566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68633ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68649566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
68653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6866c58f1c22SToby Isaac }
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac /*@
6869bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6870c58f1c22SToby Isaac 
6871c58f1c22SToby Isaac   Not Collective
6872c58f1c22SToby Isaac 
6873c58f1c22SToby Isaac   Input Parameter:
6874bb7acecfSBarry Smith . dm   - The `DM` object
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac   Output Parameter:
6877c58f1c22SToby Isaac . numLabels - the number of Labels
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac   Level: intermediate
6880c58f1c22SToby Isaac 
68811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6882c58f1c22SToby Isaac @*/
6883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6884d71ae5a4SJacob Faibussowitsch {
68855d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6886c58f1c22SToby Isaac   PetscInt    n    = 0;
6887c58f1c22SToby Isaac 
6888c58f1c22SToby Isaac   PetscFunctionBegin;
6889c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6890534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
68919371c9d4SSatish Balay   while (next) {
68929371c9d4SSatish Balay     ++n;
68939371c9d4SSatish Balay     next = next->next;
68949371c9d4SSatish Balay   }
6895c58f1c22SToby Isaac   *numLabels = n;
68963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6897c58f1c22SToby Isaac }
6898c58f1c22SToby Isaac 
6899c58f1c22SToby Isaac /*@C
6900c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6901c58f1c22SToby Isaac 
6902c58f1c22SToby Isaac   Not Collective
6903c58f1c22SToby Isaac 
6904c58f1c22SToby Isaac   Input Parameters:
6905bb7acecfSBarry Smith + dm - The `DM` object
6906c58f1c22SToby Isaac - n  - the label number
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   Output Parameter:
6909c58f1c22SToby Isaac . name - the label name
6910c58f1c22SToby Isaac 
6911c58f1c22SToby Isaac   Level: intermediate
6912c58f1c22SToby Isaac 
6913bb7acecfSBarry Smith   Developer Note:
6914bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6915bb7acecfSBarry Smith 
69161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6917c58f1c22SToby Isaac @*/
6918d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6919d71ae5a4SJacob Faibussowitsch {
69205d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6921c58f1c22SToby Isaac   PetscInt    l    = 0;
6922c58f1c22SToby Isaac 
6923c58f1c22SToby Isaac   PetscFunctionBegin;
6924c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6925c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6926c58f1c22SToby Isaac   while (next) {
6927c58f1c22SToby Isaac     if (l == n) {
69289566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
69293ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
6930c58f1c22SToby Isaac     }
6931c58f1c22SToby Isaac     ++l;
6932c58f1c22SToby Isaac     next = next->next;
6933c58f1c22SToby Isaac   }
693463a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6935c58f1c22SToby Isaac }
6936c58f1c22SToby Isaac 
6937c58f1c22SToby Isaac /*@C
6938bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6939c58f1c22SToby Isaac 
6940c58f1c22SToby Isaac   Not Collective
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   Input Parameters:
6943bb7acecfSBarry Smith + dm   - The `DM` object
6944c58f1c22SToby Isaac - name - The label name
6945c58f1c22SToby Isaac 
6946c58f1c22SToby Isaac   Output Parameter:
6947bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6948c58f1c22SToby Isaac 
6949c58f1c22SToby Isaac   Level: intermediate
6950c58f1c22SToby Isaac 
69511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6952c58f1c22SToby Isaac @*/
6953d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6954d71ae5a4SJacob Faibussowitsch {
69555d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6956d67d17b1SMatthew G. Knepley   const char *lname;
6957c58f1c22SToby Isaac 
6958c58f1c22SToby Isaac   PetscFunctionBegin;
6959c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6960c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6961534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6962c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6963c58f1c22SToby Isaac   while (next) {
69649566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69659566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6966c58f1c22SToby Isaac     if (*hasLabel) break;
6967c58f1c22SToby Isaac     next = next->next;
6968c58f1c22SToby Isaac   }
69693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6970c58f1c22SToby Isaac }
6971c58f1c22SToby Isaac 
6972c58f1c22SToby Isaac /*@C
697320f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
6974c58f1c22SToby Isaac 
6975c58f1c22SToby Isaac   Not Collective
6976c58f1c22SToby Isaac 
6977c58f1c22SToby Isaac   Input Parameters:
6978bb7acecfSBarry Smith + dm   - The `DM` object
6979c58f1c22SToby Isaac - name - The label name
6980c58f1c22SToby Isaac 
6981c58f1c22SToby Isaac   Output Parameter:
698220f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
6983c58f1c22SToby Isaac 
6984bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6985bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6986bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6987bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6988bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6989bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6990bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69916d7c9049SMatthew G. Knepley 
6992c58f1c22SToby Isaac   Level: intermediate
6993c58f1c22SToby Isaac 
69941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6995c58f1c22SToby Isaac @*/
6996d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6997d71ae5a4SJacob Faibussowitsch {
69985d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6999c58f1c22SToby Isaac   PetscBool   hasLabel;
7000d67d17b1SMatthew G. Knepley   const char *lname;
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   PetscFunctionBegin;
7003c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7004c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7005c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7006c58f1c22SToby Isaac   *label = NULL;
7007c58f1c22SToby Isaac   while (next) {
70089566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70099566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7010c58f1c22SToby Isaac     if (hasLabel) {
7011c58f1c22SToby Isaac       *label = next->label;
7012c58f1c22SToby Isaac       break;
7013c58f1c22SToby Isaac     }
7014c58f1c22SToby Isaac     next = next->next;
7015c58f1c22SToby Isaac   }
70163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7017c58f1c22SToby Isaac }
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac /*@C
7020bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7021c58f1c22SToby Isaac 
7022c58f1c22SToby Isaac   Not Collective
7023c58f1c22SToby Isaac 
7024c58f1c22SToby Isaac   Input Parameters:
7025bb7acecfSBarry Smith + dm - The `DM` object
7026c58f1c22SToby Isaac - n  - the label number
7027c58f1c22SToby Isaac 
7028c58f1c22SToby Isaac   Output Parameter:
7029c58f1c22SToby Isaac . label - the label
7030c58f1c22SToby Isaac 
7031c58f1c22SToby Isaac   Level: intermediate
7032c58f1c22SToby Isaac 
70331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7034c58f1c22SToby Isaac @*/
7035d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7036d71ae5a4SJacob Faibussowitsch {
70375d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7038c58f1c22SToby Isaac   PetscInt    l    = 0;
7039c58f1c22SToby Isaac 
7040c58f1c22SToby Isaac   PetscFunctionBegin;
7041c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7042c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7043c58f1c22SToby Isaac   while (next) {
7044c58f1c22SToby Isaac     if (l == n) {
7045c58f1c22SToby Isaac       *label = next->label;
70463ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7047c58f1c22SToby Isaac     }
7048c58f1c22SToby Isaac     ++l;
7049c58f1c22SToby Isaac     next = next->next;
7050c58f1c22SToby Isaac   }
705163a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7052c58f1c22SToby Isaac }
7053c58f1c22SToby Isaac 
7054c58f1c22SToby Isaac /*@C
7055bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Not Collective
7058c58f1c22SToby Isaac 
7059c58f1c22SToby Isaac   Input Parameters:
7060bb7acecfSBarry Smith + dm   - The `DM` object
7061bb7acecfSBarry Smith - label - The `DMLabel`
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   Level: developer
7064c58f1c22SToby Isaac 
70651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7066c58f1c22SToby Isaac @*/
7067d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7068d71ae5a4SJacob Faibussowitsch {
70695d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7070c58f1c22SToby Isaac   PetscBool   hasLabel;
7071d67d17b1SMatthew G. Knepley   const char *lname;
70725d80c0bfSVaclav Hapla   PetscBool   flg;
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac   PetscFunctionBegin;
7075c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70769566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70779566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70787a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70799566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7080c58f1c22SToby Isaac   tmpLabel->label  = label;
7081c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70825d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70835d80c0bfSVaclav Hapla   *p = tmpLabel;
70849566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70859566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70865d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70879566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7088ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
70893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7090c58f1c22SToby Isaac }
7091c58f1c22SToby Isaac 
7092c58f1c22SToby Isaac /*@C
70934a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
70944a7ee7d0SMatthew G. Knepley 
70954a7ee7d0SMatthew G. Knepley   Not Collective
70964a7ee7d0SMatthew G. Knepley 
70974a7ee7d0SMatthew G. Knepley   Input Parameters:
7098bb7acecfSBarry Smith + dm    - The `DM` object
7099bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
71004a7ee7d0SMatthew G. Knepley 
7101bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7102bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7103bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7104bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7105bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7106bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7107bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71084a7ee7d0SMatthew G. Knepley 
71094a7ee7d0SMatthew G. Knepley   Level: intermediate
71104a7ee7d0SMatthew G. Knepley 
71111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
71124a7ee7d0SMatthew G. Knepley @*/
7113d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7114d71ae5a4SJacob Faibussowitsch {
71154a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
71164a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
71174a7ee7d0SMatthew G. Knepley   const char *name, *lname;
71184a7ee7d0SMatthew G. Knepley 
71194a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
71204a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71214a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
71229566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
71234a7ee7d0SMatthew G. Knepley   while (next) {
71249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71259566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
71264a7ee7d0SMatthew G. Knepley     if (hasLabel) {
71279566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
71289566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
71294a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
71309566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
71314a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
71329566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
71334a7ee7d0SMatthew G. Knepley       next->label = label;
71344a7ee7d0SMatthew G. Knepley       break;
71354a7ee7d0SMatthew G. Knepley     }
71364a7ee7d0SMatthew G. Knepley     next = next->next;
71374a7ee7d0SMatthew G. Knepley   }
71383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
71394a7ee7d0SMatthew G. Knepley }
71404a7ee7d0SMatthew G. Knepley 
71414a7ee7d0SMatthew G. Knepley /*@C
7142bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7143c58f1c22SToby Isaac 
7144c58f1c22SToby Isaac   Not Collective
7145c58f1c22SToby Isaac 
7146c58f1c22SToby Isaac   Input Parameters:
7147bb7acecfSBarry Smith + dm   - The `DM` object
7148c58f1c22SToby Isaac - name - The label name
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac   Output Parameter:
715120f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7152bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Level: developer
7155c58f1c22SToby Isaac 
71561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7157c58f1c22SToby Isaac @*/
7158d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7159d71ae5a4SJacob Faibussowitsch {
716095d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7161c58f1c22SToby Isaac   PetscBool   hasLabel;
7162d67d17b1SMatthew G. Knepley   const char *lname;
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac   PetscFunctionBegin;
7165c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7166e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7167e5472504SVaclav Hapla   if (label) {
7168e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7169c58f1c22SToby Isaac     *label = NULL;
7170e5472504SVaclav Hapla   }
71715d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71729566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71739566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7174c58f1c22SToby Isaac     if (hasLabel) {
717595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71769566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
717795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71789566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7179ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
718095d578d6SVaclav Hapla       if (label) *label = link->label;
71819566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71829566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7183c58f1c22SToby Isaac       break;
7184c58f1c22SToby Isaac     }
7185c58f1c22SToby Isaac   }
71863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7187c58f1c22SToby Isaac }
7188c58f1c22SToby Isaac 
7189306894acSVaclav Hapla /*@
7190bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7191306894acSVaclav Hapla 
7192306894acSVaclav Hapla   Not Collective
7193306894acSVaclav Hapla 
7194306894acSVaclav Hapla   Input Parameters:
7195bb7acecfSBarry Smith + dm   - The `DM` object
7196bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
719720f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7198306894acSVaclav Hapla 
7199306894acSVaclav Hapla   Level: developer
7200306894acSVaclav Hapla 
7201bb7acecfSBarry Smith   Note:
7202306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7203bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7204306894acSVaclav Hapla   *label nullified.
7205306894acSVaclav Hapla 
72061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7207306894acSVaclav Hapla @*/
7208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7209d71ae5a4SJacob Faibussowitsch {
721043e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7211306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7212306894acSVaclav Hapla 
7213306894acSVaclav Hapla   PetscFunctionBegin;
7214306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7215306894acSVaclav Hapla   PetscValidPointer(label, 2);
72163ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7217306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7218306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
72195d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
722043e45a93SVaclav Hapla     if (*label == link->label) {
7221306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
722243e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7223306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7224ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
722543e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
72269566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
72279566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7228306894acSVaclav Hapla       break;
7229306894acSVaclav Hapla     }
7230306894acSVaclav Hapla   }
72317a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
72323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7233306894acSVaclav Hapla }
7234306894acSVaclav Hapla 
7235c58f1c22SToby Isaac /*@C
7236c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   Not Collective
7239c58f1c22SToby Isaac 
7240c58f1c22SToby Isaac   Input Parameters:
7241bb7acecfSBarry Smith + dm   - The `DM` object
7242c58f1c22SToby Isaac - name - The label name
7243c58f1c22SToby Isaac 
7244c58f1c22SToby Isaac   Output Parameter:
7245c58f1c22SToby Isaac . output - The flag for output
7246c58f1c22SToby Isaac 
7247c58f1c22SToby Isaac   Level: developer
7248c58f1c22SToby Isaac 
72491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7250c58f1c22SToby Isaac @*/
7251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7252d71ae5a4SJacob Faibussowitsch {
72535d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7254d67d17b1SMatthew G. Knepley   const char *lname;
7255c58f1c22SToby Isaac 
7256c58f1c22SToby Isaac   PetscFunctionBegin;
7257c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7258dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7259dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7260c58f1c22SToby Isaac   while (next) {
7261c58f1c22SToby Isaac     PetscBool flg;
7262c58f1c22SToby Isaac 
72639566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72649566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72659371c9d4SSatish Balay     if (flg) {
72669371c9d4SSatish Balay       *output = next->output;
72673ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
72689371c9d4SSatish Balay     }
7269c58f1c22SToby Isaac     next = next->next;
7270c58f1c22SToby Isaac   }
727198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7272c58f1c22SToby Isaac }
7273c58f1c22SToby Isaac 
7274c58f1c22SToby Isaac /*@C
7275bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7276c58f1c22SToby Isaac 
7277c58f1c22SToby Isaac   Not Collective
7278c58f1c22SToby Isaac 
7279c58f1c22SToby Isaac   Input Parameters:
7280bb7acecfSBarry Smith + dm     - The `DM` object
7281c58f1c22SToby Isaac . name   - The label name
7282bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7283c58f1c22SToby Isaac 
7284c58f1c22SToby Isaac   Level: developer
7285c58f1c22SToby Isaac 
72861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7287c58f1c22SToby Isaac @*/
7288d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7289d71ae5a4SJacob Faibussowitsch {
72905d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7291d67d17b1SMatthew G. Knepley   const char *lname;
7292c58f1c22SToby Isaac 
7293c58f1c22SToby Isaac   PetscFunctionBegin;
7294c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7295534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7296c58f1c22SToby Isaac   while (next) {
7297c58f1c22SToby Isaac     PetscBool flg;
7298c58f1c22SToby Isaac 
72999566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73009566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73019371c9d4SSatish Balay     if (flg) {
73029371c9d4SSatish Balay       next->output = output;
73033ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73049371c9d4SSatish Balay     }
7305c58f1c22SToby Isaac     next = next->next;
7306c58f1c22SToby Isaac   }
730798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7308c58f1c22SToby Isaac }
7309c58f1c22SToby Isaac 
7310c58f1c22SToby Isaac /*@
7311bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7312c58f1c22SToby Isaac 
731320f4b53cSBarry Smith   Collective
7314c58f1c22SToby Isaac 
7315d8d19677SJose E. Roman   Input Parameters:
7316bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7317bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7318bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7319bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7320bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7321c58f1c22SToby Isaac 
7322c58f1c22SToby Isaac   Level: intermediate
7323c58f1c22SToby Isaac 
7324bb7acecfSBarry Smith   Note:
73252cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7326c58f1c22SToby Isaac 
73271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7328c58f1c22SToby Isaac @*/
7329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7330d71ae5a4SJacob Faibussowitsch {
73312cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7332c58f1c22SToby Isaac   const char *name;
7333c58f1c22SToby Isaac   PetscBool   flg;
73345d80c0bfSVaclav Hapla   DMLabelLink link;
7335c58f1c22SToby Isaac 
73365d80c0bfSVaclav Hapla   PetscFunctionBegin;
73375d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73385d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73395d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
73405d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73417a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73423ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
73435d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
73445d80c0bfSVaclav Hapla     label = link->label;
73459566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
73465d80c0bfSVaclav Hapla     if (!all) {
73479566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7348c58f1c22SToby Isaac       if (flg) continue;
73499566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
73507d5acc75SStefano Zampini       if (flg) continue;
73519566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7352ba2698f1SMatthew G. Knepley       if (flg) continue;
73535d80c0bfSVaclav Hapla     }
73549566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
73552cbb9b06SVaclav Hapla     if (labelOld) {
73562cbb9b06SVaclav Hapla       switch (emode) {
7357d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7358d71ae5a4SJacob Faibussowitsch         continue;
7359d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7360d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7361d71ae5a4SJacob Faibussowitsch         break;
7362d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7363d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7364d71ae5a4SJacob Faibussowitsch       default:
7365d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73662cbb9b06SVaclav Hapla       }
73672cbb9b06SVaclav Hapla     }
73685d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73699566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73705d80c0bfSVaclav Hapla     } else {
73715d80c0bfSVaclav Hapla       labelNew = label;
73725d80c0bfSVaclav Hapla     }
73739566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73749566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7375c58f1c22SToby Isaac   }
73763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7377c58f1c22SToby Isaac }
7378461a15a0SLisandro Dalcin 
7379609dae6eSVaclav Hapla /*@C
7380b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7381609dae6eSVaclav Hapla 
738220f4b53cSBarry Smith   Collective; No Fortran Support
7383609dae6eSVaclav Hapla 
7384609dae6eSVaclav Hapla   Input Parameters:
7385bb7acecfSBarry Smith + dm0 - First `DM` object
7386bb7acecfSBarry Smith - dm1 - Second `DM` object
7387609dae6eSVaclav Hapla 
7388609dae6eSVaclav Hapla   Output Parameters
73895efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
739020f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7391609dae6eSVaclav Hapla 
7392609dae6eSVaclav Hapla   Level: intermediate
7393609dae6eSVaclav Hapla 
7394609dae6eSVaclav Hapla   Notes:
7395bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7396bb7acecfSBarry Smith 
739720f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7398bb7acecfSBarry Smith 
739920f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7400609dae6eSVaclav Hapla 
74015efe38ccSVaclav Hapla   The output message is set independently on each rank.
7402bb7acecfSBarry Smith 
7403bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7404bb7acecfSBarry Smith 
740520f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7406bb7acecfSBarry Smith 
740720f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7408609dae6eSVaclav Hapla 
7409609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7410bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7411609dae6eSVaclav Hapla 
74121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7413609dae6eSVaclav Hapla @*/
7414d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7415d71ae5a4SJacob Faibussowitsch {
74165efe38ccSVaclav Hapla   PetscInt    n, i;
7417609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
74185efe38ccSVaclav Hapla   PetscBool   eq;
7419609dae6eSVaclav Hapla   MPI_Comm    comm;
74205efe38ccSVaclav Hapla   PetscMPIInt rank;
7421609dae6eSVaclav Hapla 
7422609dae6eSVaclav Hapla   PetscFunctionBegin;
7423609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7424609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7425609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
74265efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7427609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
74289566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
74299566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
74305efe38ccSVaclav Hapla   {
74315efe38ccSVaclav Hapla     PetscInt n1;
74325efe38ccSVaclav Hapla 
74339566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
74349566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
74355efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
743648a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7437712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
74385efe38ccSVaclav Hapla     if (!eq) goto finish;
74395efe38ccSVaclav Hapla   }
74405efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7441609dae6eSVaclav Hapla     DMLabel     l0, l1;
7442609dae6eSVaclav Hapla     const char *name;
7443609dae6eSVaclav Hapla     char       *msgInner;
7444609dae6eSVaclav Hapla 
7445609dae6eSVaclav Hapla     /* Ignore label order */
74469566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
74479566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
74489566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7449609dae6eSVaclav Hapla     if (!l1) {
745063a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
74515efe38ccSVaclav Hapla       eq = PETSC_FALSE;
74525efe38ccSVaclav Hapla       break;
7453609dae6eSVaclav Hapla     }
74549566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
74559566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
74569566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74575efe38ccSVaclav Hapla     if (!eq) break;
7458609dae6eSVaclav Hapla   }
7459712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7460609dae6eSVaclav Hapla finish:
74615efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7462609dae6eSVaclav Hapla   if (message) {
7463609dae6eSVaclav Hapla     *message = NULL;
746448a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74655efe38ccSVaclav Hapla   } else {
746648a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74679566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74685efe38ccSVaclav Hapla   }
74695efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74705efe38ccSVaclav Hapla   if (equal) *equal = eq;
74717a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
74723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7473609dae6eSVaclav Hapla }
7474609dae6eSVaclav Hapla 
7475d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7476d71ae5a4SJacob Faibussowitsch {
7477461a15a0SLisandro Dalcin   PetscFunctionBegin;
7478461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7479461a15a0SLisandro Dalcin   if (!*label) {
74809566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74819566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7482461a15a0SLisandro Dalcin   }
74839566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
74843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7485461a15a0SLisandro Dalcin }
7486461a15a0SLisandro Dalcin 
74870fdc7489SMatthew Knepley /*
74880fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74890fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74900fdc7489SMatthew Knepley   (label, id) pair in the DM.
74910fdc7489SMatthew Knepley 
74920fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
74930fdc7489SMatthew Knepley   each label.
74940fdc7489SMatthew Knepley */
7495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7496d71ae5a4SJacob Faibussowitsch {
74970fdc7489SMatthew Knepley   DMUniversalLabel ul;
74980fdc7489SMatthew Knepley   PetscBool       *active;
74990fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
75000fdc7489SMatthew Knepley 
75010fdc7489SMatthew Knepley   PetscFunctionBegin;
75029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
75039566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
75049566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
75059566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
75060fdc7489SMatthew Knepley   ul->Nl = 0;
75070fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75080fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
75090fdc7489SMatthew Knepley     const char *name;
75100fdc7489SMatthew Knepley 
75119566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75129566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
75139566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
75140fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
75150fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
75160fdc7489SMatthew Knepley   }
75179566063dSJacob 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));
75180fdc7489SMatthew Knepley   ul->Nv = 0;
75190fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75200fdc7489SMatthew Knepley     DMLabel     label;
75210fdc7489SMatthew Knepley     PetscInt    nv;
75220fdc7489SMatthew Knepley     const char *name;
75230fdc7489SMatthew Knepley 
75240fdc7489SMatthew Knepley     if (!active[l]) continue;
75259566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75269566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75279566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75289566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
75290fdc7489SMatthew Knepley     ul->indices[m] = l;
75300fdc7489SMatthew Knepley     ul->Nv += nv;
75310fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
75320fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
75330fdc7489SMatthew Knepley     ++m;
75340fdc7489SMatthew Knepley   }
75350fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
75360fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
75370fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
75380fdc7489SMatthew Knepley   }
75390fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
75400fdc7489SMatthew Knepley     PetscInt b;
75410fdc7489SMatthew Knepley 
75420fdc7489SMatthew Knepley     ul->masks[l] = 0;
75430fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
75440fdc7489SMatthew Knepley   }
75459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
75460fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75470fdc7489SMatthew Knepley     DMLabel         label;
75480fdc7489SMatthew Knepley     IS              valueIS;
75490fdc7489SMatthew Knepley     const PetscInt *varr;
75500fdc7489SMatthew Knepley     PetscInt        nv, v;
75510fdc7489SMatthew Knepley 
75520fdc7489SMatthew Knepley     if (!active[l]) continue;
75539566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75549566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75559566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
75569566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7557ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75589566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75599566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75609566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75610fdc7489SMatthew Knepley     ++m;
75620fdc7489SMatthew Knepley   }
75639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75640fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75650fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75660fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75670fdc7489SMatthew Knepley 
75680fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75690fdc7489SMatthew Knepley       DMLabel  label;
75700649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75710fdc7489SMatthew Knepley 
75720fdc7489SMatthew Knepley       if (!active[l]) continue;
75739566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75749566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75759566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75769371c9d4SSatish Balay       if (val == defval) {
75779371c9d4SSatish Balay         ++m;
75789371c9d4SSatish Balay         continue;
75799371c9d4SSatish Balay       }
75800649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75810fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75829566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
758363a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75840fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75850fdc7489SMatthew Knepley       ++m;
75860fdc7489SMatthew Knepley     }
75879566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75880fdc7489SMatthew Knepley   }
75899566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75900fdc7489SMatthew Knepley   *universal = ul;
75913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75920fdc7489SMatthew Knepley }
75930fdc7489SMatthew Knepley 
7594d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7595d71ae5a4SJacob Faibussowitsch {
75960fdc7489SMatthew Knepley   PetscInt l;
75970fdc7489SMatthew Knepley 
75980fdc7489SMatthew Knepley   PetscFunctionBegin;
75999566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
76009566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
76019566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
76029566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
76039566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
76040fdc7489SMatthew Knepley   *universal = NULL;
76053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76060fdc7489SMatthew Knepley }
76070fdc7489SMatthew Knepley 
7608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7609d71ae5a4SJacob Faibussowitsch {
76100fdc7489SMatthew Knepley   PetscFunctionBegin;
76110fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
76120fdc7489SMatthew Knepley   *ulabel = ul->label;
76133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76140fdc7489SMatthew Knepley }
76150fdc7489SMatthew Knepley 
7616d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7617d71ae5a4SJacob Faibussowitsch {
76180fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
76190fdc7489SMatthew Knepley 
76200fdc7489SMatthew Knepley   PetscFunctionBegin;
7621064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
76220fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76239566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
76249566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
76250fdc7489SMatthew Knepley   }
76260fdc7489SMatthew Knepley   if (preserveOrder) {
76270fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
76280fdc7489SMatthew Knepley       const char *name;
76290fdc7489SMatthew Knepley       PetscBool   match;
76300fdc7489SMatthew Knepley 
76319566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
76329566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
763363a3b9bcSJacob 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]);
76340fdc7489SMatthew Knepley     }
76350fdc7489SMatthew Knepley   }
76363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76370fdc7489SMatthew Knepley }
76380fdc7489SMatthew Knepley 
7639d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7640d71ae5a4SJacob Faibussowitsch {
76410fdc7489SMatthew Knepley   PetscInt l;
76420fdc7489SMatthew Knepley 
76430fdc7489SMatthew Knepley   PetscFunctionBegin;
76440fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76450fdc7489SMatthew Knepley     DMLabel  label;
76460fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
76470fdc7489SMatthew Knepley 
76480fdc7489SMatthew Knepley     if (lval) {
76499566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
76509566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
76519566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
76520fdc7489SMatthew Knepley     }
76530fdc7489SMatthew Knepley   }
76543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76550fdc7489SMatthew Knepley }
7656a8fb8f29SToby Isaac 
7657a8fb8f29SToby Isaac /*@
7658bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7659bb7acecfSBarry Smith 
766020f4b53cSBarry Smith   Not Collective
7661a8fb8f29SToby Isaac 
7662a8fb8f29SToby Isaac   Input Parameter:
7663bb7acecfSBarry Smith . dm - The `DM` object
7664a8fb8f29SToby Isaac 
7665a8fb8f29SToby Isaac   Output Parameter:
7666bb7acecfSBarry Smith . cdm - The coarse `DM`
7667a8fb8f29SToby Isaac 
7668a8fb8f29SToby Isaac   Level: intermediate
7669a8fb8f29SToby Isaac 
76701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7671a8fb8f29SToby Isaac @*/
7672d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7673d71ae5a4SJacob Faibussowitsch {
7674a8fb8f29SToby Isaac   PetscFunctionBegin;
7675a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7676a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7677a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
76783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7679a8fb8f29SToby Isaac }
7680a8fb8f29SToby Isaac 
7681a8fb8f29SToby Isaac /*@
7682bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7683a8fb8f29SToby Isaac 
7684a8fb8f29SToby Isaac   Input Parameters:
7685bb7acecfSBarry Smith + dm - The `DM` object
7686bb7acecfSBarry Smith - cdm - The coarse `DM`
7687a8fb8f29SToby Isaac 
7688a8fb8f29SToby Isaac   Level: intermediate
7689a8fb8f29SToby Isaac 
7690bb7acecfSBarry Smith   Note:
7691bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7692bb7acecfSBarry Smith 
76931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7694a8fb8f29SToby Isaac @*/
7695d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7696d71ae5a4SJacob Faibussowitsch {
7697a8fb8f29SToby Isaac   PetscFunctionBegin;
7698a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7699a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
770089d734beSBarry Smith   if (dm == cdm) cdm = NULL;
77019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
77029566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7703a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
77043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7705a8fb8f29SToby Isaac }
7706a8fb8f29SToby Isaac 
770788bdff64SToby Isaac /*@
7708bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
770988bdff64SToby Isaac 
771088bdff64SToby Isaac   Input Parameter:
7711bb7acecfSBarry Smith . dm - The `DM` object
771288bdff64SToby Isaac 
771388bdff64SToby Isaac   Output Parameter:
7714bb7acecfSBarry Smith . fdm - The fine `DM`
771588bdff64SToby Isaac 
771688bdff64SToby Isaac   Level: intermediate
771788bdff64SToby Isaac 
77181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
771988bdff64SToby Isaac @*/
7720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7721d71ae5a4SJacob Faibussowitsch {
772288bdff64SToby Isaac   PetscFunctionBegin;
772388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
772488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
772588bdff64SToby Isaac   *fdm = dm->fineMesh;
77263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
772788bdff64SToby Isaac }
772888bdff64SToby Isaac 
772988bdff64SToby Isaac /*@
7730bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
773188bdff64SToby Isaac 
773288bdff64SToby Isaac   Input Parameters:
7733bb7acecfSBarry Smith + dm - The `DM` object
7734bb7acecfSBarry Smith - fdm - The fine `DM`
773588bdff64SToby Isaac 
7736bb7acecfSBarry Smith   Level: developer
773788bdff64SToby Isaac 
7738bb7acecfSBarry Smith   Note:
7739bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7740bb7acecfSBarry Smith 
77411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
774288bdff64SToby Isaac @*/
7743d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7744d71ae5a4SJacob Faibussowitsch {
774588bdff64SToby Isaac   PetscFunctionBegin;
774688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
774788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
774889d734beSBarry Smith   if (dm == fdm) fdm = NULL;
77499566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
77509566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
775188bdff64SToby Isaac   dm->fineMesh = fdm;
77523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
775388bdff64SToby Isaac }
775488bdff64SToby Isaac 
7755a6ba4734SToby Isaac /*@C
7756bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7757a6ba4734SToby Isaac 
775820f4b53cSBarry Smith   Collective
7759783e2ec8SMatthew G. Knepley 
7760a6ba4734SToby Isaac   Input Parameters:
7761bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7762bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7763a6ba4734SToby Isaac . name     - The BC name
776445480ffeSMatthew G. Knepley . label    - The label defining constrained points
7765bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
776645480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7767a6ba4734SToby Isaac . field    - The field to constrain
776845480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7769a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7770a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
777156cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7772a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7773a6ba4734SToby Isaac 
777445480ffeSMatthew G. Knepley   Output Parameter:
777545480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
777645480ffeSMatthew G. Knepley 
7777a6ba4734SToby Isaac   Options Database Keys:
7778a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7779a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7780a6ba4734SToby Isaac 
778120f4b53cSBarry Smith   Level: intermediate
778220f4b53cSBarry Smith 
7783bb7acecfSBarry Smith   Notes:
778437fdd005SBarry 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\:
778556cf3b9cSMatthew G. Knepley 
778620f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
778756cf3b9cSMatthew G. Knepley 
7788bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
778956cf3b9cSMatthew G. Knepley 
779020f4b53cSBarry Smith .vb
779120f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
779220f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
779320f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
779420f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
779520f4b53cSBarry Smith .ve
779656cf3b9cSMatthew G. Knepley + dim - the spatial dimension
779756cf3b9cSMatthew G. Knepley . Nf - the number of fields
779856cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
779956cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
780056cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
780156cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
780256cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
780356cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
780456cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
780556cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
780656cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
780756cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
780856cf3b9cSMatthew G. Knepley . t - current time
780956cf3b9cSMatthew G. Knepley . x - coordinates of the current point
781056cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
781156cf3b9cSMatthew G. Knepley . constants - constant parameters
781256cf3b9cSMatthew G. Knepley - bcval - output values at the current point
781356cf3b9cSMatthew G. Knepley 
78141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7815a6ba4734SToby Isaac @*/
7816d71ae5a4SJacob 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)
7817d71ae5a4SJacob Faibussowitsch {
7818e5e52638SMatthew G. Knepley   PetscDS ds;
7819a6ba4734SToby Isaac 
7820a6ba4734SToby Isaac   PetscFunctionBegin;
7821a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7822783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
782345480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
782445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
782545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
782645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
782701a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
78289566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7829799db056SMatthew G. Knepley   /* Complete label */
7830799db056SMatthew G. Knepley   if (label) {
7831799db056SMatthew G. Knepley     PetscObject  obj;
7832799db056SMatthew G. Knepley     PetscClassId id;
7833799db056SMatthew G. Knepley 
7834799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7835799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7836799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7837799db056SMatthew G. Knepley       DM plex;
7838799db056SMatthew G. Knepley 
7839799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7840799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7841799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7842799db056SMatthew G. Knepley     }
7843799db056SMatthew G. Knepley   }
78449566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
78453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7846a6ba4734SToby Isaac }
7847a6ba4734SToby Isaac 
784845480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7849d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7850d71ae5a4SJacob Faibussowitsch {
7851e5e52638SMatthew G. Knepley   PetscDS     ds;
7852dff059c6SToby Isaac   DMBoundary *lastnext;
7853e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7854e6f8dbb6SToby Isaac 
7855e6f8dbb6SToby Isaac   PetscFunctionBegin;
78569566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7857e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
785847a1f5adSToby Isaac   if (dm->boundary) {
785947a1f5adSToby Isaac     DMBoundary next = dm->boundary;
786047a1f5adSToby Isaac 
786147a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
78623ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
786347a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
786447a1f5adSToby Isaac     while (next) {
786547a1f5adSToby Isaac       DMBoundary b = next;
786647a1f5adSToby Isaac 
786747a1f5adSToby Isaac       next = b->next;
78689566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7869a6ba4734SToby Isaac     }
787047a1f5adSToby Isaac     dm->boundary = NULL;
7871a6ba4734SToby Isaac   }
787247a1f5adSToby Isaac 
7873dff059c6SToby Isaac   lastnext = &(dm->boundary);
7874e6f8dbb6SToby Isaac   while (dsbound) {
7875e6f8dbb6SToby Isaac     DMBoundary dmbound;
7876e6f8dbb6SToby Isaac 
78779566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7878e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
787945480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
788047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7881dff059c6SToby Isaac     *lastnext = dmbound;
7882dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7883dff059c6SToby Isaac     dsbound   = dsbound->next;
7884a6ba4734SToby Isaac   }
78853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7886a6ba4734SToby Isaac }
7887a6ba4734SToby Isaac 
7888bb7acecfSBarry Smith /* TODO: missing manual page */
7889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7890d71ae5a4SJacob Faibussowitsch {
7891b95f2879SToby Isaac   DMBoundary b;
7892a6ba4734SToby Isaac 
7893a6ba4734SToby Isaac   PetscFunctionBegin;
7894a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7895534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7896a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
78979566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7898b95f2879SToby Isaac   b = dm->boundary;
7899a6ba4734SToby Isaac   while (b && !(*isBd)) {
7900e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7901e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7902a6ba4734SToby Isaac     PetscInt   i;
7903a6ba4734SToby Isaac 
790445480ffeSMatthew G. Knepley     if (label) {
79059566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7906a6ba4734SToby Isaac     }
7907a6ba4734SToby Isaac     b = b->next;
7908a6ba4734SToby Isaac   }
79093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7910a6ba4734SToby Isaac }
79114d6f44ffSToby Isaac 
79124d6f44ffSToby Isaac /*@C
7913bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7914a6e0b375SMatthew G. Knepley 
791520f4b53cSBarry Smith   Collective
79164d6f44ffSToby Isaac 
79174d6f44ffSToby Isaac   Input Parameters:
7918bb7acecfSBarry Smith + dm      - The `DM`
79190709b2feSToby Isaac . time    - The time
79204d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
79214d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79224d6f44ffSToby Isaac - mode    - The insertion mode for values
79234d6f44ffSToby Isaac 
79244d6f44ffSToby Isaac   Output Parameter:
79254d6f44ffSToby Isaac . X - vector
79264d6f44ffSToby Isaac 
792720f4b53cSBarry Smith    Calling sequence of `funcs`:
792820f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
79294d6f44ffSToby Isaac +  dim - The spatial dimension
79308ec8862eSJed Brown .  time - The time at which to sample
79314d6f44ffSToby Isaac .  x   - The coordinates
793277b739a6SMatthew Knepley .  Nc  - The number of components
79334d6f44ffSToby Isaac .  u   - The output field values
79344d6f44ffSToby Isaac -  ctx - optional user-defined function context
79354d6f44ffSToby Isaac 
79364d6f44ffSToby Isaac   Level: developer
79374d6f44ffSToby Isaac 
7938bb7acecfSBarry Smith   Developer Notes:
7939bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7940bb7acecfSBarry Smith 
7941bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7942bb7acecfSBarry Smith 
79431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
79444d6f44ffSToby Isaac @*/
7945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7946d71ae5a4SJacob Faibussowitsch {
79474d6f44ffSToby Isaac   Vec localX;
79484d6f44ffSToby Isaac 
79494d6f44ffSToby Isaac   PetscFunctionBegin;
79504d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7951708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
79529566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7953f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79549566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
79559566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79569566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79579566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
7958708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
79593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79604d6f44ffSToby Isaac }
79614d6f44ffSToby Isaac 
7962a6e0b375SMatthew G. Knepley /*@C
7963bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7964a6e0b375SMatthew G. Knepley 
796520f4b53cSBarry Smith   Not Collective
7966a6e0b375SMatthew G. Knepley 
7967a6e0b375SMatthew G. Knepley   Input Parameters:
7968bb7acecfSBarry Smith + dm      - The `DM`
7969a6e0b375SMatthew G. Knepley . time    - The time
7970a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7971a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7972a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7973a6e0b375SMatthew G. Knepley 
7974a6e0b375SMatthew G. Knepley   Output Parameter:
7975a6e0b375SMatthew G. Knepley . localX - vector
7976a6e0b375SMatthew G. Knepley 
797720f4b53cSBarry Smith    Calling sequence of `funcs`:
797820f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7979a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7980a6e0b375SMatthew G. Knepley .  x   - The coordinates
798177b739a6SMatthew Knepley .  Nc  - The number of components
7982a6e0b375SMatthew G. Knepley .  u   - The output field values
7983a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7984a6e0b375SMatthew G. Knepley 
7985a6e0b375SMatthew G. Knepley   Level: developer
7986a6e0b375SMatthew G. Knepley 
7987bb7acecfSBarry Smith   Developer Notes:
7988bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7989bb7acecfSBarry Smith 
7990bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7991bb7acecfSBarry Smith 
79921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7993a6e0b375SMatthew G. Knepley @*/
7994d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
7995d71ae5a4SJacob Faibussowitsch {
79964d6f44ffSToby Isaac   PetscFunctionBegin;
79974d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7998064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
79999566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
80003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80014d6f44ffSToby Isaac }
80024d6f44ffSToby Isaac 
8003a6e0b375SMatthew G. Knepley /*@C
8004bb7acecfSBarry 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.
8005a6e0b375SMatthew G. Knepley 
800620f4b53cSBarry Smith   Collective
8007a6e0b375SMatthew G. Knepley 
8008a6e0b375SMatthew G. Knepley   Input Parameters:
8009bb7acecfSBarry Smith + dm      - The `DM`
8010a6e0b375SMatthew G. Knepley . time    - The time
8011bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8012a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8013bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8014a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8015a6e0b375SMatthew G. Knepley 
8016a6e0b375SMatthew G. Knepley   Output Parameter:
8017a6e0b375SMatthew G. Knepley . X - vector
8018a6e0b375SMatthew G. Knepley 
801920f4b53cSBarry Smith    Calling sequence of `funcs`:
802020f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8021a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8022a6e0b375SMatthew G. Knepley .  x   - The coordinates
802377b739a6SMatthew Knepley .  Nc  - The number of components
8024a6e0b375SMatthew G. Knepley .  u   - The output field values
8025a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8026a6e0b375SMatthew G. Knepley 
8027a6e0b375SMatthew G. Knepley   Level: developer
8028a6e0b375SMatthew G. Knepley 
8029bb7acecfSBarry Smith   Developer Notes:
8030bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8031bb7acecfSBarry Smith 
8032bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8033bb7acecfSBarry Smith 
80341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8035a6e0b375SMatthew G. Knepley @*/
8036d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
8037d71ae5a4SJacob Faibussowitsch {
80382c53366bSMatthew G. Knepley   Vec localX;
80392c53366bSMatthew G. Knepley 
80402c53366bSMatthew G. Knepley   PetscFunctionBegin;
80412c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80429566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8043f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80449566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80459566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80469566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80479566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
80483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80492c53366bSMatthew G. Knepley }
80502c53366bSMatthew G. Knepley 
8051a6e0b375SMatthew G. Knepley /*@C
8052bb7acecfSBarry 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.
8053a6e0b375SMatthew G. Knepley 
805420f4b53cSBarry Smith   Not Collective
8055a6e0b375SMatthew G. Knepley 
8056a6e0b375SMatthew G. Knepley   Input Parameters:
8057bb7acecfSBarry Smith + dm      - The `DM`
8058a6e0b375SMatthew G. Knepley . time    - The time
8059bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8060a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8061a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8062a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8063a6e0b375SMatthew G. Knepley 
8064a6e0b375SMatthew G. Knepley   Output Parameter:
8065a6e0b375SMatthew G. Knepley . localX - vector
8066a6e0b375SMatthew G. Knepley 
806720f4b53cSBarry Smith    Calling sequence of `funcs`:
806820f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8069a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8070a6e0b375SMatthew G. Knepley .  x   - The coordinates
807177b739a6SMatthew Knepley .  Nc  - The number of components
8072a6e0b375SMatthew G. Knepley .  u   - The output field values
8073a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8074a6e0b375SMatthew G. Knepley 
8075a6e0b375SMatthew G. Knepley   Level: developer
8076a6e0b375SMatthew G. Knepley 
8077bb7acecfSBarry Smith   Developer Notes:
8078bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8079bb7acecfSBarry Smith 
8080bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8081bb7acecfSBarry Smith 
80821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8083a6e0b375SMatthew G. Knepley @*/
8084d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
8085d71ae5a4SJacob Faibussowitsch {
80864d6f44ffSToby Isaac   PetscFunctionBegin;
80874d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8088064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
80899566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80914d6f44ffSToby Isaac }
80922716604bSToby Isaac 
8093a6e0b375SMatthew G. Knepley /*@C
8094bb7acecfSBarry 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.
8095a6e0b375SMatthew G. Knepley 
809620f4b53cSBarry Smith   Not Collective
8097a6e0b375SMatthew G. Knepley 
8098a6e0b375SMatthew G. Knepley   Input Parameters:
8099bb7acecfSBarry Smith + dm      - The `DM`
8100a6e0b375SMatthew G. Knepley . time    - The time
810120f4b53cSBarry Smith . localU  - The input field vector; may be `NULL` if projection is defined purely by coordinates
8102a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8103a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8104a6e0b375SMatthew G. Knepley 
8105a6e0b375SMatthew G. Knepley   Output Parameter:
8106a6e0b375SMatthew G. Knepley . localX  - The output vector
8107a6e0b375SMatthew G. Knepley 
810820f4b53cSBarry Smith    Calling sequence of `funcs`:
810920f4b53cSBarry Smith .vb
811020f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
811120f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
811220f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
811320f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
811420f4b53cSBarry Smith .ve
8115a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8116a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8117a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8118a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8119a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8120a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8121a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8122a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8123a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8124a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8125a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8126a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8127a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8128a6e0b375SMatthew G. Knepley .  t            - The current time
8129a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8130a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8131a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8132a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8133a6e0b375SMatthew G. Knepley 
8134bb7acecfSBarry Smith   Note:
8135bb7acecfSBarry 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.
8136bb7acecfSBarry 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
8137bb7acecfSBarry 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
8138a6e0b375SMatthew 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.
8139a6e0b375SMatthew G. Knepley 
8140a6e0b375SMatthew G. Knepley   Level: intermediate
8141a6e0b375SMatthew G. Knepley 
8142bb7acecfSBarry Smith   Developer Notes:
8143bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8144bb7acecfSBarry Smith 
8145bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8146bb7acecfSBarry Smith 
81471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8148a6e0b375SMatthew G. Knepley @*/
8149d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8150d71ae5a4SJacob Faibussowitsch {
81518c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81528c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8153eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
81548c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
81559566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
81563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81578c6c5593SMatthew G. Knepley }
81588c6c5593SMatthew G. Knepley 
8159a6e0b375SMatthew G. Knepley /*@C
8160a6e0b375SMatthew 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.
8161a6e0b375SMatthew G. Knepley 
816220f4b53cSBarry Smith   Not Collective
8163a6e0b375SMatthew G. Knepley 
8164a6e0b375SMatthew G. Knepley   Input Parameters:
8165bb7acecfSBarry Smith + dm      - The `DM`
8166a6e0b375SMatthew G. Knepley . time    - The time
8167bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8168a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8169a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8170bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
817120f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8172a6e0b375SMatthew G. Knepley . localU  - The input field vector
8173a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8174a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8175a6e0b375SMatthew G. Knepley 
8176a6e0b375SMatthew G. Knepley   Output Parameter:
8177a6e0b375SMatthew G. Knepley . localX  - The output vector
8178a6e0b375SMatthew G. Knepley 
817920f4b53cSBarry Smith    Calling sequence of `funcs`:
818020f4b53cSBarry Smith .vb
818120f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
818220f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
818320f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
818420f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
818520f4b53cSBarry Smith .ve
8186a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8187a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8188a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8189a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8190a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8191a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8192a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8193a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8194a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8195a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8196a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8197a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8198a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8199a6e0b375SMatthew G. Knepley .  t            - The current time
8200a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8201a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8202a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8203a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8204a6e0b375SMatthew G. Knepley 
8205bb7acecfSBarry Smith   Note:
8206bb7acecfSBarry 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.
8207bb7acecfSBarry 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
8208bb7acecfSBarry 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
8209a6e0b375SMatthew 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.
8210a6e0b375SMatthew G. Knepley 
8211a6e0b375SMatthew G. Knepley   Level: intermediate
8212a6e0b375SMatthew G. Knepley 
8213bb7acecfSBarry Smith   Developer Notes:
8214bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8215bb7acecfSBarry Smith 
8216bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8217bb7acecfSBarry Smith 
82181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8219a6e0b375SMatthew G. Knepley @*/
8220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8221d71ae5a4SJacob Faibussowitsch {
82228c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82238c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8224064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8225064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
82269566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
82273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82288c6c5593SMatthew G. Knepley }
82298c6c5593SMatthew G. Knepley 
82302716604bSToby Isaac /*@C
8231d29d7c6eSMatthew 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.
8232d29d7c6eSMatthew G. Knepley 
823320f4b53cSBarry Smith   Not Collective
8234d29d7c6eSMatthew G. Knepley 
8235d29d7c6eSMatthew G. Knepley   Input Parameters:
8236bb7acecfSBarry Smith + dm      - The `DM`
8237d29d7c6eSMatthew G. Knepley . time    - The time
8238bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8239d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8240d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8241bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
824220f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8243d29d7c6eSMatthew G. Knepley . U       - The input field vector
8244d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8245d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8246d29d7c6eSMatthew G. Knepley 
8247d29d7c6eSMatthew G. Knepley   Output Parameter:
8248d29d7c6eSMatthew G. Knepley . X       - The output vector
8249d29d7c6eSMatthew G. Knepley 
825020f4b53cSBarry Smith    Calling sequence of `funcs`:
825120f4b53cSBarry Smith .vb
825220f4b53cSBarry Smith   void func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
825320f4b53cSBarry Smith             const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
825420f4b53cSBarry Smith             const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
825520f4b53cSBarry Smith             PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
825620f4b53cSBarry Smith .ve
8257d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8258d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8259d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8260d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8261d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8262d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8263d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8264d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8265d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8266d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8267d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8268d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8269d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8270d29d7c6eSMatthew G. Knepley .  t            - The current time
8271d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8272d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8273d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8274d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8275d29d7c6eSMatthew G. Knepley 
8276bb7acecfSBarry Smith   Note:
8277bb7acecfSBarry 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.
8278bb7acecfSBarry 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
8279bb7acecfSBarry 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
8280d29d7c6eSMatthew 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.
8281d29d7c6eSMatthew G. Knepley 
8282d29d7c6eSMatthew G. Knepley   Level: intermediate
8283d29d7c6eSMatthew G. Knepley 
8284bb7acecfSBarry Smith   Developer Notes:
8285bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8286bb7acecfSBarry Smith 
8287bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8288bb7acecfSBarry Smith 
82891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8290d29d7c6eSMatthew G. Knepley @*/
8291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec X)
8292d71ae5a4SJacob Faibussowitsch {
8293d29d7c6eSMatthew G. Knepley   DM  dmIn;
8294d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8295d29d7c6eSMatthew G. Knepley 
8296d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8297d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8298d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8299d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8300d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8301f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
830272fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
830372fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8304d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8305d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8306d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8307d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8308d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
83093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8310d29d7c6eSMatthew G. Knepley }
8311d29d7c6eSMatthew G. Knepley 
8312d29d7c6eSMatthew G. Knepley /*@C
8313ece3a9fcSMatthew 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.
8314ece3a9fcSMatthew G. Knepley 
831520f4b53cSBarry Smith   Not Collective
8316ece3a9fcSMatthew G. Knepley 
8317ece3a9fcSMatthew G. Knepley   Input Parameters:
8318bb7acecfSBarry Smith + dm      - The `DM`
8319ece3a9fcSMatthew G. Knepley . time    - The time
8320bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8321ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8322ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8323bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
832420f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8325ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8326ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8327ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8328ece3a9fcSMatthew G. Knepley 
8329ece3a9fcSMatthew G. Knepley   Output Parameter:
8330ece3a9fcSMatthew G. Knepley . localX  - The output vector
8331ece3a9fcSMatthew G. Knepley 
833220f4b53cSBarry Smith    Calling sequence of `funcs`:
833320f4b53cSBarry Smith .vb
833420f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
833520f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
833620f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
833720f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
833820f4b53cSBarry Smith .ve
8339ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8340ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8341ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8342ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8343ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8344ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8345ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8346ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8347ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8348ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8349ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8350ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8351ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8352ece3a9fcSMatthew G. Knepley .  t            - The current time
8353ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8354ece3a9fcSMatthew G. Knepley .  n            - The face normal
8355ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8356ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8357ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8358ece3a9fcSMatthew G. Knepley 
8359ece3a9fcSMatthew G. Knepley   Note:
8360bb7acecfSBarry 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.
8361bb7acecfSBarry 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
8362bb7acecfSBarry 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
8363ece3a9fcSMatthew 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.
8364ece3a9fcSMatthew G. Knepley 
8365ece3a9fcSMatthew G. Knepley   Level: intermediate
8366ece3a9fcSMatthew G. Knepley 
8367bb7acecfSBarry Smith   Developer Notes:
8368bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8369bb7acecfSBarry Smith 
8370bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8371bb7acecfSBarry Smith 
83721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8373ece3a9fcSMatthew G. Knepley @*/
8374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8375d71ae5a4SJacob Faibussowitsch {
8376ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8377ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8378064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8379064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83809566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
83813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8382ece3a9fcSMatthew G. Knepley }
8383ece3a9fcSMatthew G. Knepley 
8384ece3a9fcSMatthew G. Knepley /*@C
83852716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83862716604bSToby Isaac 
838720f4b53cSBarry Smith   Collective
8388bb7acecfSBarry Smith 
83892716604bSToby Isaac   Input Parameters:
8390bb7acecfSBarry Smith + dm    - The `DM`
83910709b2feSToby Isaac . time  - The time
83922716604bSToby Isaac . funcs - The functions to evaluate for each field component
83932716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8394574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83952716604bSToby Isaac 
83962716604bSToby Isaac   Output Parameter:
83972716604bSToby Isaac . diff - The diff ||u - u_h||_2
83982716604bSToby Isaac 
83992716604bSToby Isaac   Level: developer
84002716604bSToby Isaac 
8401bb7acecfSBarry Smith   Developer Notes:
8402bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8403bb7acecfSBarry Smith 
8404bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8405bb7acecfSBarry Smith 
84061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84072716604bSToby Isaac @*/
8408d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8409d71ae5a4SJacob Faibussowitsch {
84102716604bSToby Isaac   PetscFunctionBegin;
84112716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8412b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84139566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
84143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84152716604bSToby Isaac }
8416b698f381SToby Isaac 
8417b698f381SToby Isaac /*@C
8418b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8419b698f381SToby Isaac 
842020f4b53cSBarry Smith   Collective
8421d083f849SBarry Smith 
8422b698f381SToby Isaac   Input Parameters:
8423bb7acecfSBarry Smith + dm    - The `DM`
8424b698f381SToby Isaac , time  - The time
8425b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8426b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8427574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8428b698f381SToby Isaac - n     - The vector to project along
8429b698f381SToby Isaac 
8430b698f381SToby Isaac   Output Parameter:
8431b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8432b698f381SToby Isaac 
8433b698f381SToby Isaac   Level: developer
8434b698f381SToby Isaac 
8435bb7acecfSBarry Smith   Developer Notes:
8436bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8437bb7acecfSBarry Smith 
8438bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8439bb7acecfSBarry Smith 
84401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8441b698f381SToby Isaac @*/
8442d71ae5a4SJacob 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)
8443d71ae5a4SJacob Faibussowitsch {
8444b698f381SToby Isaac   PetscFunctionBegin;
8445b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8446b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84479566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
84483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8449b698f381SToby Isaac }
8450b698f381SToby Isaac 
84512a16baeaSToby Isaac /*@C
84522a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84532a16baeaSToby Isaac 
845420f4b53cSBarry Smith   Collective
8455d083f849SBarry Smith 
84562a16baeaSToby Isaac   Input Parameters:
8457bb7acecfSBarry Smith + dm    - The `DM`
84582a16baeaSToby Isaac . time  - The time
84592a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84602a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8461574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84622a16baeaSToby Isaac 
84632a16baeaSToby Isaac   Output Parameter:
84642a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84652a16baeaSToby Isaac 
84662a16baeaSToby Isaac   Level: developer
84672a16baeaSToby Isaac 
8468bb7acecfSBarry Smith   Developer Notes:
8469bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8470bb7acecfSBarry Smith 
8471bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8472bb7acecfSBarry Smith 
84731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84742a16baeaSToby Isaac @*/
8475d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8476d71ae5a4SJacob Faibussowitsch {
84772a16baeaSToby Isaac   PetscFunctionBegin;
84782a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84792a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84809566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84822a16baeaSToby Isaac }
84832a16baeaSToby Isaac 
8484df0b854cSToby Isaac /*@C
8485bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8486502a2867SDave May 
8487502a2867SDave May  Not Collective
8488502a2867SDave May 
8489502a2867SDave May  Input Parameter:
8490bb7acecfSBarry Smith .  dm    - The `DM`
8491502a2867SDave May 
84920a19bb7dSprj-  Output Parameters:
84930a19bb7dSprj- +  nranks - the number of neighbours
84940a19bb7dSprj- -  ranks - the neighbors ranks
8495502a2867SDave May 
84969bdbcad8SBarry Smith  Level: beginner
84979bdbcad8SBarry Smith 
8498bb7acecfSBarry Smith  Note:
8499bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8500502a2867SDave May 
85011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8502502a2867SDave May @*/
8503d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8504d71ae5a4SJacob Faibussowitsch {
8505502a2867SDave May   PetscFunctionBegin;
8506502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85079566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
85083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8509502a2867SDave May }
8510502a2867SDave May 
8511531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8512531c7667SBarry Smith 
8513531c7667SBarry Smith /*
8514531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
85152b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8516531c7667SBarry Smith */
8517d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8518d71ae5a4SJacob Faibussowitsch {
8519531c7667SBarry Smith   PetscFunctionBegin;
8520531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8521531c7667SBarry Smith     Vec x1local;
8522531c7667SBarry Smith     DM  dm;
85239566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85247a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
85259566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
85269566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
85279566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8528531c7667SBarry Smith     x1 = x1local;
8529531c7667SBarry Smith   }
85309566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8531531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8532531c7667SBarry Smith     DM dm;
85339566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85349566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8535531c7667SBarry Smith   }
85363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8537531c7667SBarry Smith }
8538531c7667SBarry Smith 
8539531c7667SBarry Smith /*@
8540bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8541531c7667SBarry Smith 
8542531c7667SBarry Smith     Input Parameter:
8543bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8544531c7667SBarry Smith 
85459bdbcad8SBarry Smith     Level: advanced
85469bdbcad8SBarry Smith 
8547bb7acecfSBarry Smith     Developer Note:
8548bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8549531c7667SBarry Smith 
85501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8551531c7667SBarry Smith @*/
8552d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8553d71ae5a4SJacob Faibussowitsch {
8554531c7667SBarry Smith   PetscFunctionBegin;
8555531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
85563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8557531c7667SBarry Smith }
85588320bc6fSPatrick Sanan 
85598320bc6fSPatrick Sanan /*@
8560bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85618320bc6fSPatrick Sanan 
85628320bc6fSPatrick Sanan     Collective
85638320bc6fSPatrick Sanan 
85648320bc6fSPatrick Sanan     Input Parameters:
8565bb7acecfSBarry Smith +    dm1 - the first `DM`
8566bb7acecfSBarry Smith -    dm2 - the second `DM`
85678320bc6fSPatrick Sanan 
85688320bc6fSPatrick Sanan     Output Parameters:
8569bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8570bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85718320bc6fSPatrick Sanan 
857220f4b53cSBarry Smith     Level: advanced
857320f4b53cSBarry Smith 
85748320bc6fSPatrick Sanan     Notes:
8575bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85763d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85778320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8578bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85793d862458SPatrick Sanan     decomposition, but hold different data.
85808320bc6fSPatrick Sanan 
85818320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8582bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85838320bc6fSPatrick Sanan 
8584bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85858320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85868320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8587bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85888320bc6fSPatrick Sanan 
8589bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8590bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85918320bc6fSPatrick Sanan .vb
85928320bc6fSPatrick Sanan   ...
85939566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85948320bc6fSPatrick Sanan   if (set && compatible)  {
85959566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
85969566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
85979566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
85988320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85998320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86008320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86018320bc6fSPatrick Sanan       }
86028320bc6fSPatrick Sanan     }
86039566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
86049566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
86058320bc6fSPatrick Sanan   } else {
86068320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86078320bc6fSPatrick Sanan   }
86088320bc6fSPatrick Sanan   ...
86098320bc6fSPatrick Sanan .ve
86108320bc6fSPatrick Sanan 
8611bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
86128320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
86138320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
86148320bc6fSPatrick Sanan     always check the "set" output parameter.
86158320bc6fSPatrick Sanan 
8616bb7acecfSBarry Smith     A `DM` is always compatible with itself.
86178320bc6fSPatrick Sanan 
8618bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
86198320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
86208320bc6fSPatrick Sanan     incompatible.
86218320bc6fSPatrick Sanan 
86228320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8623bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
86248320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
86258320bc6fSPatrick Sanan 
8626bb7acecfSBarry Smith     Developer Note:
8627bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
86283d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8629a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8630bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
86318320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
86323d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8633bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
86348320bc6fSPatrick Sanan 
86351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
86368320bc6fSPatrick Sanan @*/
8637d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8638d71ae5a4SJacob Faibussowitsch {
86398320bc6fSPatrick Sanan   PetscMPIInt compareResult;
86408320bc6fSPatrick Sanan   DMType      type, type2;
86418320bc6fSPatrick Sanan   PetscBool   sameType;
86428320bc6fSPatrick Sanan 
86438320bc6fSPatrick Sanan   PetscFunctionBegin;
8644a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
86458320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
86468320bc6fSPatrick Sanan 
86478320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8648a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86498320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86508320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86513ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86528320bc6fSPatrick Sanan   }
86538320bc6fSPatrick Sanan 
86548320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86558320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86568320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86578320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86589566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
86598320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86608320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86618320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86623ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86638320bc6fSPatrick Sanan   }
86648320bc6fSPatrick Sanan 
86658320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8666a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8667dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
86683ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
86698320bc6fSPatrick Sanan   }
86708320bc6fSPatrick Sanan 
8671a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86728320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86739566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86749566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86759566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86768320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8677dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86788320bc6fSPatrick Sanan   } else {
86798320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86808320bc6fSPatrick Sanan   }
86813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86828320bc6fSPatrick Sanan }
8683c0f0dcc3SMatthew G. Knepley 
8684c0f0dcc3SMatthew G. Knepley /*@C
8685bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8686c0f0dcc3SMatthew G. Knepley 
868720f4b53cSBarry Smith   Logically Collective
8688c0f0dcc3SMatthew G. Knepley 
8689c0f0dcc3SMatthew G. Knepley   Input Parameters:
8690bb7acecfSBarry Smith + DM - the `DM`
8691c0f0dcc3SMatthew G. Knepley . f - the monitor function
869220f4b53cSBarry Smith . mctx - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
869320f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8694c0f0dcc3SMatthew G. Knepley 
869520f4b53cSBarry Smith   Options Database Key:
8696bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8697c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8698c0f0dcc3SMatthew G. Knepley 
86999bdbcad8SBarry Smith   Level: intermediate
87009bdbcad8SBarry Smith 
8701bb7acecfSBarry Smith   Note:
8702c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8703bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8704c0f0dcc3SMatthew G. Knepley   order in which they were set.
8705c0f0dcc3SMatthew G. Knepley 
8706bb7acecfSBarry Smith   Fortran Note:
8707bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8708bb7acecfSBarry Smith 
8709bb7acecfSBarry Smith   Developer Note:
8710bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8711c0f0dcc3SMatthew G. Knepley 
87121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8713c0f0dcc3SMatthew G. Knepley @*/
8714d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8715d71ae5a4SJacob Faibussowitsch {
8716c0f0dcc3SMatthew G. Knepley   PetscInt m;
8717c0f0dcc3SMatthew G. Knepley 
8718c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8719c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8720c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8721c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8722c0f0dcc3SMatthew G. Knepley 
87239566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
87243ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8725c0f0dcc3SMatthew G. Knepley   }
87267a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8727c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8728c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8729c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
87303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8731c0f0dcc3SMatthew G. Knepley }
8732c0f0dcc3SMatthew G. Knepley 
8733c0f0dcc3SMatthew G. Knepley /*@
8734bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8735c0f0dcc3SMatthew G. Knepley 
873620f4b53cSBarry Smith   Logically Collective
8737c0f0dcc3SMatthew G. Knepley 
8738c0f0dcc3SMatthew G. Knepley   Input Parameter:
8739c0f0dcc3SMatthew G. Knepley . dm - the DM
8740c0f0dcc3SMatthew G. Knepley 
8741c0f0dcc3SMatthew G. Knepley   Options Database Key:
8742c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8743bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8744c0f0dcc3SMatthew G. Knepley   set via the options database
8745c0f0dcc3SMatthew G. Knepley 
87469bdbcad8SBarry Smith   Level: intermediate
87479bdbcad8SBarry Smith 
8748bb7acecfSBarry Smith   Note:
8749bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8750c0f0dcc3SMatthew G. Knepley 
87511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8752c0f0dcc3SMatthew G. Knepley @*/
8753d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8754d71ae5a4SJacob Faibussowitsch {
8755c0f0dcc3SMatthew G. Knepley   PetscInt m;
8756c0f0dcc3SMatthew G. Knepley 
8757c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8758c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8759c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87609566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8761c0f0dcc3SMatthew G. Knepley   }
8762c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
87633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8764c0f0dcc3SMatthew G. Knepley }
8765c0f0dcc3SMatthew G. Knepley 
8766c0f0dcc3SMatthew G. Knepley /*@C
8767c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8768c0f0dcc3SMatthew G. Knepley 
876920f4b53cSBarry Smith   Collective
8770c0f0dcc3SMatthew G. Knepley 
8771c0f0dcc3SMatthew G. Knepley   Input Parameters:
8772bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8773c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8774c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8775c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8776c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8777bb7acecfSBarry 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
8778c0f0dcc3SMatthew G. Knepley 
8779c0f0dcc3SMatthew G. Knepley   Output Parameter:
8780c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8781c0f0dcc3SMatthew G. Knepley 
8782c0f0dcc3SMatthew G. Knepley   Level: developer
8783c0f0dcc3SMatthew G. Knepley 
87841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8785db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8786db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8787db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8788c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8789db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8790bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8791c0f0dcc3SMatthew G. Knepley @*/
8792d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8793d71ae5a4SJacob Faibussowitsch {
8794c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8795c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8796c0f0dcc3SMatthew G. Knepley 
8797c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8798c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8800c0f0dcc3SMatthew G. Knepley   if (*flg) {
8801c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8802c0f0dcc3SMatthew G. Knepley 
88039566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
88049566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
88059566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
88069566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8807c0f0dcc3SMatthew G. Knepley   }
88083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8809c0f0dcc3SMatthew G. Knepley }
8810c0f0dcc3SMatthew G. Knepley 
8811c0f0dcc3SMatthew G. Knepley /*@
8812c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8813c0f0dcc3SMatthew G. Knepley 
881420f4b53cSBarry Smith    Collective
8815c0f0dcc3SMatthew G. Knepley 
88162fe279fdSBarry Smith    Input Parameter:
8817bb7acecfSBarry Smith .  dm - The `DM`
8818c0f0dcc3SMatthew G. Knepley 
8819c0f0dcc3SMatthew G. Knepley    Level: developer
8820c0f0dcc3SMatthew G. Knepley 
8821bb7acecfSBarry Smith    Question:
8822bb7acecfSBarry Smith    Note should indicate when during the life of the `DM` the monitor is run. It appears to be related to the discretization process seems rather specialized
8823bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8824bb7acecfSBarry Smith 
88251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8826c0f0dcc3SMatthew G. Knepley @*/
8827d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8828d71ae5a4SJacob Faibussowitsch {
8829c0f0dcc3SMatthew G. Knepley   PetscInt m;
8830c0f0dcc3SMatthew G. Knepley 
8831c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
88323ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8833c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
883448a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
88353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8836c0f0dcc3SMatthew G. Knepley }
88372e4af2aeSMatthew G. Knepley 
88382e4af2aeSMatthew G. Knepley /*@
8839bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
88402e4af2aeSMatthew G. Knepley 
884120f4b53cSBarry Smith   Collective
88422e4af2aeSMatthew G. Knepley 
88432e4af2aeSMatthew G. Knepley   Input Parameters:
8844bb7acecfSBarry Smith + dm     - The `DM`
88456b867d5aSJose E. Roman - sol    - The solution vector
88462e4af2aeSMatthew G. Knepley 
88476b867d5aSJose E. Roman   Input/Output Parameter:
884820f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
88496b867d5aSJose E. Roman            contains the error in each field
88506b867d5aSJose E. Roman 
88516b867d5aSJose E. Roman   Output Parameter:
885220f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
885320f4b53cSBarry Smith 
885420f4b53cSBarry Smith   Level: developer
88552e4af2aeSMatthew G. Knepley 
8856bb7acecfSBarry Smith   Note:
8857bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88582e4af2aeSMatthew G. Knepley 
88591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88602e4af2aeSMatthew G. Knepley @*/
8861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8862d71ae5a4SJacob Faibussowitsch {
88632e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88642e4af2aeSMatthew G. Knepley   void    **ctxs;
88652e4af2aeSMatthew G. Knepley   PetscReal time;
88662e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88672e4af2aeSMatthew G. Knepley 
88682e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88699566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88709566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88719566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88722e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88732e4af2aeSMatthew G. Knepley     PetscDS         ds;
88742e4af2aeSMatthew G. Knepley     DMLabel         label;
88752e4af2aeSMatthew G. Knepley     IS              fieldIS;
88762e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88772e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88782e4af2aeSMatthew G. Knepley 
887907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
88809566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88819566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88822e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88832e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88849566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88852e4af2aeSMatthew G. Knepley     }
88869566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88872e4af2aeSMatthew G. Knepley   }
8888ad540459SPierre 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);
88899566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88909566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88912e4af2aeSMatthew G. Knepley   if (errorVec) {
88922e4af2aeSMatthew G. Knepley     DM             edm;
88932e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
88942e4af2aeSMatthew G. Knepley     PetscBool      simplex;
88952e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
88962e4af2aeSMatthew G. Knepley 
88979566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
88989566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
88999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
89012e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
89029566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
89032e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
89042e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
89052e4af2aeSMatthew G. Knepley       PetscQuadrature q;
89062e4af2aeSMatthew G. Knepley       const char     *name;
89072e4af2aeSMatthew G. Knepley 
89089566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
89099566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
89109566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
89119566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
89129566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
89139566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
89149566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
89159566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
89162e4af2aeSMatthew G. Knepley     }
89179566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
89182e4af2aeSMatthew G. Knepley 
89199566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
89209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
89219566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
89229566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
89232e4af2aeSMatthew G. Knepley   }
89249566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
89253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89262e4af2aeSMatthew G. Knepley }
89279a2a23afSMatthew G. Knepley 
89289a2a23afSMatthew G. Knepley /*@
8929bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
89309a2a23afSMatthew G. Knepley 
893120f4b53cSBarry Smith   Not Collective
89329a2a23afSMatthew G. Knepley 
89339a2a23afSMatthew G. Knepley   Input Parameter:
8934bb7acecfSBarry Smith . dm     - The `DM`
89359a2a23afSMatthew G. Knepley 
89369a2a23afSMatthew G. Knepley   Output Parameter:
8937a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
89389a2a23afSMatthew G. Knepley 
89399a2a23afSMatthew G. Knepley   Level: advanced
89409a2a23afSMatthew G. Knepley 
89411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
89429a2a23afSMatthew G. Knepley @*/
8943d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8944d71ae5a4SJacob Faibussowitsch {
89459a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89469a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89479566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89499a2a23afSMatthew G. Knepley }
89509a2a23afSMatthew G. Knepley 
89519a2a23afSMatthew G. Knepley /*@
8952ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89539a2a23afSMatthew G. Knepley 
895420f4b53cSBarry Smith   Not Collective
89559a2a23afSMatthew G. Knepley 
89569a2a23afSMatthew G. Knepley   Input Parameters:
8957bb7acecfSBarry Smith + dm     - The `DM`
8958bb7acecfSBarry Smith . label  - The `DMLabel`
8959ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8960ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89619a2a23afSMatthew G. Knepley 
89629a2a23afSMatthew G. Knepley   Output Parameter:
8963bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89649a2a23afSMatthew G. Knepley 
89659bdbcad8SBarry Smith   Level: advanced
89669bdbcad8SBarry Smith 
8967bb7acecfSBarry Smith   Note:
8968bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
896904c51a94SMatthew G. Knepley 
89701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89719a2a23afSMatthew G. Knepley @*/
8972d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8973d71ae5a4SJacob Faibussowitsch {
8974ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
897504c51a94SMatthew G. Knepley   PetscBool       has;
89769a2a23afSMatthew G. Knepley 
89779a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89789a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89799a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89809a2a23afSMatthew G. Knepley   key.label = label;
89819a2a23afSMatthew G. Knepley   key.value = value;
8982ac17215fSMatthew G. Knepley   key.part  = part;
89839566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89849566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89859566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89879a2a23afSMatthew G. Knepley }
89889a2a23afSMatthew G. Knepley 
89899a2a23afSMatthew G. Knepley /*@
8990bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89919a2a23afSMatthew G. Knepley 
899220f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
89939a2a23afSMatthew G. Knepley 
89949a2a23afSMatthew G. Knepley   Input Parameters:
8995bb7acecfSBarry Smith + dm     - The `DM`
8996bb7acecfSBarry Smith . label  - The `DMLabel`
89979a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8998ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
8999bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
90009a2a23afSMatthew G. Knepley 
90019a2a23afSMatthew G. Knepley   Level: advanced
90029a2a23afSMatthew G. Knepley 
90031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
90049a2a23afSMatthew G. Knepley @*/
9005d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9006d71ae5a4SJacob Faibussowitsch {
90079a2a23afSMatthew G. Knepley   Vec             old;
90089a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
90099a2a23afSMatthew G. Knepley 
90109a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90119a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90129a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90139a2a23afSMatthew G. Knepley   key.label = label;
90149a2a23afSMatthew G. Knepley   key.value = value;
9015ac17215fSMatthew G. Knepley   key.part  = part;
90169566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
90179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
90189566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
90199566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
90209566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
90213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90229a2a23afSMatthew G. Knepley }
90239a2a23afSMatthew G. Knepley 
90249a2a23afSMatthew G. Knepley /*@C
9025bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
90269a2a23afSMatthew G. Knepley 
902720f4b53cSBarry Smith   Not Collective
90289a2a23afSMatthew G. Knepley 
90299a2a23afSMatthew G. Knepley   Input Parameter:
9030bb7acecfSBarry Smith . dm      - The `DM`
90319a2a23afSMatthew G. Knepley 
90329a2a23afSMatthew G. Knepley   Output Parameters:
9033bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
9034bb7acecfSBarry Smith . values  - The label values for each `Vec`
9035bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
90369a2a23afSMatthew G. Knepley 
90379bdbcad8SBarry Smith   Level: advanced
90389bdbcad8SBarry Smith 
9039bb7acecfSBarry Smith   Note:
9040bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
90419a2a23afSMatthew G. Knepley 
90421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
90439a2a23afSMatthew G. Knepley @*/
9044d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9045d71ae5a4SJacob Faibussowitsch {
90469a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
90479a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90489a2a23afSMatthew G. Knepley 
90499a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90509a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90519a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9052dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9053dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
90549566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90569566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
90579371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
90589371c9d4SSatish Balay     labels[i] = keys[i].label;
90599371c9d4SSatish Balay     values[i] = keys[i].value;
90609371c9d4SSatish Balay     parts[i]  = keys[i].part;
90619371c9d4SSatish Balay   }
90629566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90649a2a23afSMatthew G. Knepley }
90659a2a23afSMatthew G. Knepley 
90669a2a23afSMatthew G. Knepley /*@
9067bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90689a2a23afSMatthew G. Knepley 
906920f4b53cSBarry Smith   Not Collective
90709a2a23afSMatthew G. Knepley 
90719a2a23afSMatthew G. Knepley   Input Parameter:
9072bb7acecfSBarry Smith . dm    - The `DM`
90739a2a23afSMatthew G. Knepley 
90749a2a23afSMatthew G. Knepley   Output Parameter:
9075bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90769a2a23afSMatthew G. Knepley 
90779a2a23afSMatthew G. Knepley   Level: advanced
90789a2a23afSMatthew G. Knepley 
9079bb7acecfSBarry Smith   Note:
9080bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9081bb7acecfSBarry Smith 
90821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90839a2a23afSMatthew G. Knepley @*/
9084d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9085d71ae5a4SJacob Faibussowitsch {
90869a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90879a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90889566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90899566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90919a2a23afSMatthew G. Knepley }
9092b5a892a1SMatthew G. Knepley 
9093b5a892a1SMatthew G. Knepley /*@C
9094bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9095b5a892a1SMatthew G. Knepley 
909620f4b53cSBarry Smith   Not Collective
9097b5a892a1SMatthew G. Knepley 
9098b5a892a1SMatthew G. Knepley   Input Parameters:
9099bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9100b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9101b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9102b5a892a1SMatthew G. Knepley 
9103b5a892a1SMatthew G. Knepley   Output Parameters:
9104bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9105b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9106b5a892a1SMatthew G. Knepley 
9107b5a892a1SMatthew G. Knepley   Level: advanced
9108b5a892a1SMatthew G. Knepley 
9109bb7acecfSBarry Smith   Note:
9110bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9111bb7acecfSBarry Smith 
9112bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9113bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9114bb7acecfSBarry Smith 
9115bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9116bb7acecfSBarry Smith 
91171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9118b5a892a1SMatthew G. Knepley @*/
9119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9120d71ae5a4SJacob Faibussowitsch {
9121b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9122b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9123b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9124b5a892a1SMatthew G. Knepley 
9125b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91269371c9d4SSatish Balay   if (!nO) {
91279371c9d4SSatish Balay     *ornt  = 0;
91289371c9d4SSatish Balay     *found = PETSC_TRUE;
91293ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
91309371c9d4SSatish Balay   }
9131b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9132b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9133b5a892a1SMatthew G. Knepley 
91349371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91359371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
91369371c9d4SSatish Balay     if (c == cS) {
91379371c9d4SSatish Balay       *ornt = o;
91389371c9d4SSatish Balay       break;
91399371c9d4SSatish Balay     }
9140b5a892a1SMatthew G. Knepley   }
9141b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
91423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9143b5a892a1SMatthew G. Knepley }
9144b5a892a1SMatthew G. Knepley 
9145b5a892a1SMatthew G. Knepley /*@C
9146bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9147b5a892a1SMatthew G. Knepley 
914820f4b53cSBarry Smith   Not Collective
9149b5a892a1SMatthew G. Knepley 
9150b5a892a1SMatthew G. Knepley   Input Parameters:
9151bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9152b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9153b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9154b5a892a1SMatthew G. Knepley 
91552fe279fdSBarry Smith   Output Parameter:
9156bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9157b5a892a1SMatthew G. Knepley 
9158b5a892a1SMatthew G. Knepley   Level: advanced
9159b5a892a1SMatthew G. Knepley 
9160bb7acecfSBarry Smith   Note:
9161bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9162bb7acecfSBarry Smith 
9163bb7acecfSBarry Smith   Developer Note:
9164bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9165bb7acecfSBarry Smith 
91661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9167b5a892a1SMatthew G. Knepley @*/
9168d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9169d71ae5a4SJacob Faibussowitsch {
9170b5a892a1SMatthew G. Knepley   PetscBool found;
9171b5a892a1SMatthew G. Knepley 
9172b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91739566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91747a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
91753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9176b5a892a1SMatthew G. Knepley }
9177b5a892a1SMatthew G. Knepley 
9178b5a892a1SMatthew G. Knepley /*@C
9179bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9180b5a892a1SMatthew G. Knepley 
918120f4b53cSBarry Smith   Not Collective
9182b5a892a1SMatthew G. Knepley 
9183b5a892a1SMatthew G. Knepley   Input Parameters:
9184bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9185b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9186b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9187b5a892a1SMatthew G. Knepley 
9188b5a892a1SMatthew G. Knepley   Output Parameters:
9189bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9190b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9191b5a892a1SMatthew G. Knepley 
9192b5a892a1SMatthew G. Knepley   Level: advanced
9193b5a892a1SMatthew G. Knepley 
9194bb7acecfSBarry Smith   Note:
9195bb7acecfSBarry Smith   An arrangement is a vertex order
9196bb7acecfSBarry Smith 
9197bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9198bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9199bb7acecfSBarry Smith 
9200bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9201bb7acecfSBarry Smith 
92021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9203b5a892a1SMatthew G. Knepley @*/
9204d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9205d71ae5a4SJacob Faibussowitsch {
9206b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9207b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9208b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9209b5a892a1SMatthew G. Knepley 
9210b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92119371c9d4SSatish Balay   if (!nO) {
92129371c9d4SSatish Balay     *ornt  = 0;
92139371c9d4SSatish Balay     *found = PETSC_TRUE;
92143ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92159371c9d4SSatish Balay   }
9216b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9217b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9218b5a892a1SMatthew G. Knepley 
92199371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92209371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
92219371c9d4SSatish Balay     if (c == cS) {
92229371c9d4SSatish Balay       *ornt = o;
92239371c9d4SSatish Balay       break;
92249371c9d4SSatish Balay     }
9225b5a892a1SMatthew G. Knepley   }
9226b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9228b5a892a1SMatthew G. Knepley }
9229b5a892a1SMatthew G. Knepley 
9230b5a892a1SMatthew G. Knepley /*@C
9231bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9232b5a892a1SMatthew G. Knepley 
923320f4b53cSBarry Smith   Not Collective
9234b5a892a1SMatthew G. Knepley 
9235b5a892a1SMatthew G. Knepley   Input Parameters:
9236bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9237b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9238b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9239b5a892a1SMatthew G. Knepley 
92402fe279fdSBarry Smith   Output Parameter:
9241bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9242b5a892a1SMatthew G. Knepley 
9243b5a892a1SMatthew G. Knepley   Level: advanced
9244b5a892a1SMatthew G. Knepley 
9245bb7acecfSBarry Smith   Note:
9246bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9247bb7acecfSBarry Smith 
9248bb7acecfSBarry Smith   Developer Note:
9249bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9250bb7acecfSBarry Smith 
92511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9252b5a892a1SMatthew G. Knepley @*/
9253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9254d71ae5a4SJacob Faibussowitsch {
9255b5a892a1SMatthew G. Knepley   PetscBool found;
9256b5a892a1SMatthew G. Knepley 
9257b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92589566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
92597a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
92603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9261b5a892a1SMatthew G. Knepley }
9262012bc364SMatthew G. Knepley 
9263012bc364SMatthew G. Knepley /*@C
9264012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9265012bc364SMatthew G. Knepley 
926620f4b53cSBarry Smith   Not Collective
9267012bc364SMatthew G. Knepley 
9268012bc364SMatthew G. Knepley   Input Parameters:
9269bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9270012bc364SMatthew G. Knepley - point - Coordinates of the point
9271012bc364SMatthew G. Knepley 
92722fe279fdSBarry Smith   Output Parameter:
9273012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9274012bc364SMatthew G. Knepley 
9275012bc364SMatthew G. Knepley   Level: advanced
9276012bc364SMatthew G. Knepley 
92771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9278012bc364SMatthew G. Knepley @*/
9279d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9280d71ae5a4SJacob Faibussowitsch {
9281012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9282012bc364SMatthew G. Knepley   PetscInt  d;
9283012bc364SMatthew G. Knepley 
9284012bc364SMatthew G. Knepley   PetscFunctionBegin;
9285012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9286012bc364SMatthew G. Knepley   switch (ct) {
9287012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9288012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9289012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92909371c9d4SSatish Balay       if (point[d] < -1.0) {
92919371c9d4SSatish Balay         *inside = PETSC_FALSE;
92929371c9d4SSatish Balay         break;
92939371c9d4SSatish Balay       }
9294012bc364SMatthew G. Knepley       sum += point[d];
9295012bc364SMatthew G. Knepley     }
92969371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
92979371c9d4SSatish Balay       *inside = PETSC_FALSE;
92989371c9d4SSatish Balay       break;
92999371c9d4SSatish Balay     }
9300012bc364SMatthew G. Knepley     break;
9301012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9302012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9303012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
93049371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
93059371c9d4SSatish Balay         *inside = PETSC_FALSE;
9306012bc364SMatthew G. Knepley         break;
93079371c9d4SSatish Balay       }
93089371c9d4SSatish Balay     break;
9309d71ae5a4SJacob Faibussowitsch   default:
9310d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9311012bc364SMatthew G. Knepley   }
93123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9313012bc364SMatthew G. Knepley }
9314