xref: /petsc/src/dm/interface/dm.c (revision 92a26154bc948bc3af330cd082baf461ef1aa548)
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>
6d2b2dc1eSMatthew G. Knepley #include <petscdmceed.h>
7f19dbd58SToby Isaac #include <petscdmfield.h>
80c312b8eSJed Brown #include <petscsf.h>
92764a2aaSMatthew G. Knepley #include <petscds.h>
1047c6ae99SBarry Smith 
11f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
12f918ec44SMatthew G. Knepley   #include <petscfeceed.h>
13f918ec44SMatthew G. Knepley #endif
14f918ec44SMatthew G. Knepley 
15732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
16d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
1742d0c57aSJames Wright 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_View, DM_AdaptInterpolator, DM_ProjectFunction;
1867a56275SMatthew G Knepley 
19ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
20d1b3049bSMatthew 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};
210e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
22476787b7SMatthew G. Knepley const char *const DMPolytopeTypes[] =
23476787b7SMatthew G. Knepley   {"vertex",  "segment",      "tensor_segment", "triangle", "quadrilateral",  "tensor_quad",  "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "pyramid", "FV_ghost_cell", "interior_ghost_cell",
24476787b7SMatthew G. Knepley    "unknown", "unknown_cell", "unknown_face",   "invalid",  "DMPolytopeType", "DM_POLYTOPE_", NULL};
252cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2660c22052SBarry Smith 
27a4121054SBarry Smith /*@
28bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
29bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
30a4121054SBarry Smith 
31d083f849SBarry Smith   Collective
32a4121054SBarry Smith 
33a4121054SBarry Smith   Input Parameter:
34bb7acecfSBarry Smith . comm - The communicator for the `DM` object
35a4121054SBarry Smith 
36a4121054SBarry Smith   Output Parameter:
37bb7acecfSBarry Smith . dm - The `DM` object
38a4121054SBarry Smith 
39a4121054SBarry Smith   Level: beginner
40a4121054SBarry Smith 
41bb7acecfSBarry Smith   Notes:
42bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
43bb7acecfSBarry Smith 
44bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
45bb7acecfSBarry Smith   error when you try to use the dm.
46bb7acecfSBarry Smith 
471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
48a4121054SBarry Smith @*/
49d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
50d71ae5a4SJacob Faibussowitsch {
51a4121054SBarry Smith   DM      v;
52e5e52638SMatthew G. Knepley   PetscDS ds;
53a4121054SBarry Smith 
54a4121054SBarry Smith   PetscFunctionBegin;
554f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
56377f809aSBarry Smith 
579566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
589566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
5962e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6049be4549SMatthew G. Knepley   v->setupcalled                          = PETSC_FALSE;
6149be4549SMatthew G. Knepley   v->setfromoptionscalled                 = PETSC_FALSE;
620298fd71SBarry Smith   v->ltogmap                              = NULL;
63a4ea9b21SRichard Tran Mills   v->bind_below                           = 0;
641411c6eeSJed Brown   v->bs                                   = 1;
65171400e9SBarry Smith   v->coloringtype                         = IS_COLORING_GLOBAL;
669566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
679566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
68c58f1c22SToby Isaac   v->labels                    = NULL;
6934aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7034aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
71c58f1c22SToby Isaac   v->depthLabel                = NULL;
72ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
731bb6d2a8SBarry Smith   v->localSection              = NULL;
741bb6d2a8SBarry Smith   v->globalSection             = NULL;
753b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
763b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
7779769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
786858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
796858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
806858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8196173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
82435a35e8SMatthew G Knepley   {
83435a35e8SMatthew G Knepley     PetscInt i;
84435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
850298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
86f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
87435a35e8SMatthew G Knepley     }
88435a35e8SMatthew G Knepley   }
899566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9007218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
919566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
929566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9314f150ffSMatthew G. Knepley   v->dmBC              = NULL;
94a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
95f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
96cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
979566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
989566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
994a7a4c06SLawrence Mitchell 
1001411c6eeSJed Brown   *dm = v;
1013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102a4121054SBarry Smith }
103a4121054SBarry Smith 
10438221697SMatthew G. Knepley /*@
105bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
10638221697SMatthew G. Knepley 
107d083f849SBarry Smith   Collective
10838221697SMatthew G. Knepley 
10938221697SMatthew G. Knepley   Input Parameter:
110bb7acecfSBarry Smith . dm - The original `DM` object
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley   Output Parameter:
113bb7acecfSBarry Smith . newdm - The new `DM` object
11438221697SMatthew G. Knepley 
11538221697SMatthew G. Knepley   Level: beginner
11638221697SMatthew G. Knepley 
1171cb8cacdSPatrick Sanan   Notes:
118bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
119bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
120bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1211bb6d2a8SBarry Smith 
122bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12389706ed2SPatrick Sanan 
124bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
125bb7acecfSBarry Smith 
12660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
12738221697SMatthew G. Knepley @*/
128d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
129d71ae5a4SJacob Faibussowitsch {
13038221697SMatthew G. Knepley   PetscSF              sf;
13138221697SMatthew G. Knepley   Vec                  coords;
13238221697SMatthew G. Knepley   void                *ctx;
133ec196627SMatthew G. Knepley   MatOrderingType      otype;
134ec196627SMatthew G. Knepley   DMReorderDefaultFlag flg;
1356858538eSMatthew G. Knepley   PetscInt             dim, cdim, i;
13638221697SMatthew G. Knepley 
13738221697SMatthew G. Knepley   PetscFunctionBegin;
13838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1394f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1409566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1419566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
142ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
143ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
144c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
145fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1469566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1479566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1499566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1509566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1519566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
152dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1533f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1549566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1559566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1569566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
158ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetDefault(dm, &flg));
159ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetDefault(*newdm, flg));
160ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetType(dm, &otype));
161ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetType(*newdm, otype));
1626858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1636858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
164be4c1c3eSMatthew G. Knepley       DM           ncdm;
165be4c1c3eSMatthew G. Knepley       PetscSection cs;
1665a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
167be4c1c3eSMatthew G. Knepley 
1686858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1699566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
170462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1715a0206caSToby Isaac       if (pEndMax >= 0) {
1726858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1736858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1749566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1755e50ca95SJames Wright         if (dm->coordinates[i].dm->periodic.setup) {
1765e50ca95SJames Wright           ncdm->periodic.setup = dm->coordinates[i].dm->periodic.setup;
1775e50ca95SJames Wright           PetscCall(ncdm->periodic.setup(ncdm));
1785e50ca95SJames Wright         }
1796858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1806858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1819566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
182be4c1c3eSMatthew G. Knepley       }
183be4c1c3eSMatthew G. Knepley     }
1846858538eSMatthew G. Knepley   }
1859566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1869566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18838221697SMatthew G. Knepley   if (coords) {
1899566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
19038221697SMatthew G. Knepley   } else {
1919566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1929566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
19338221697SMatthew G. Knepley   }
1946858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1956858538eSMatthew G. Knepley   if (coords) {
1966858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1976858538eSMatthew G. Knepley   } else {
1986858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1996858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
2006858538eSMatthew G. Knepley   }
20190b157c4SStefano Zampini   {
2024fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
2036858538eSMatthew G. Knepley 
2044fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2054fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
206c6b900c6SMatthew G. Knepley   }
20734aa8a36SMatthew G. Knepley   {
20834aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20934aa8a36SMatthew G. Knepley 
2109566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2119566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
21234aa8a36SMatthew G. Knepley   }
2133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21438221697SMatthew G. Knepley }
21538221697SMatthew G. Knepley 
2165d83a8b1SBarry Smith /*@
2175d83a8b1SBarry Smith   DMSetVecType - Sets the type of vector to be created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2189a42bb27SBarry Smith 
21920f4b53cSBarry Smith   Logically Collective
2209a42bb27SBarry Smith 
221147403d9SBarry Smith   Input Parameters:
22232546409SMatthew G. Knepley + dm    - initial distributed array
223bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2249a42bb27SBarry Smith 
22520f4b53cSBarry Smith   Options Database Key:
226147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2279a42bb27SBarry Smith 
2289a42bb27SBarry Smith   Level: intermediate
2299a42bb27SBarry Smith 
23060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
231bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2329a42bb27SBarry Smith @*/
23332546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
234d71ae5a4SJacob Faibussowitsch {
23532546409SMatthew G. Knepley   char *tmp;
23632546409SMatthew G. Knepley 
2379a42bb27SBarry Smith   PetscFunctionBegin;
23832546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23932546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
24032546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
24132546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
24232546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2449a42bb27SBarry Smith }
2459a42bb27SBarry Smith 
2465d83a8b1SBarry Smith /*@
247bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
248c0dedaeaSBarry Smith 
24920f4b53cSBarry Smith   Logically Collective
250c0dedaeaSBarry Smith 
251c0dedaeaSBarry Smith   Input Parameter:
252c0dedaeaSBarry Smith . da - initial distributed array
253c0dedaeaSBarry Smith 
254c0dedaeaSBarry Smith   Output Parameter:
255c0dedaeaSBarry Smith . ctype - the vector type
256c0dedaeaSBarry Smith 
257c0dedaeaSBarry Smith   Level: intermediate
258c0dedaeaSBarry Smith 
25960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
260c0dedaeaSBarry Smith @*/
261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
262d71ae5a4SJacob Faibussowitsch {
263c0dedaeaSBarry Smith   PetscFunctionBegin;
264c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
265c0dedaeaSBarry Smith   *ctype = da->vectype;
2663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
267c0dedaeaSBarry Smith }
268c0dedaeaSBarry Smith 
2695f1ad066SMatthew G Knepley /*@
270bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2715f1ad066SMatthew G Knepley 
27220f4b53cSBarry Smith   Not Collective
2735f1ad066SMatthew G Knepley 
2745f1ad066SMatthew G Knepley   Input Parameter:
275bb7acecfSBarry Smith . v - The `Vec`
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley   Output Parameter:
278bb7acecfSBarry Smith . dm - The `DM`
2795f1ad066SMatthew G Knepley 
2805f1ad066SMatthew G Knepley   Level: intermediate
2815f1ad066SMatthew G Knepley 
282bb7acecfSBarry Smith   Note:
283bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
284bb7acecfSBarry Smith 
2851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2865f1ad066SMatthew G Knepley @*/
287d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
288d71ae5a4SJacob Faibussowitsch {
2895f1ad066SMatthew G Knepley   PetscFunctionBegin;
2905f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2914f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2929566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2945f1ad066SMatthew G Knepley }
2955f1ad066SMatthew G Knepley 
2965f1ad066SMatthew G Knepley /*@
297bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2985f1ad066SMatthew G Knepley 
29920f4b53cSBarry Smith   Not Collective
3005f1ad066SMatthew G Knepley 
3015f1ad066SMatthew G Knepley   Input Parameters:
302bb7acecfSBarry Smith + v  - The `Vec`
303bb7acecfSBarry Smith - dm - The `DM`
3045f1ad066SMatthew G Knepley 
30520f4b53cSBarry Smith   Level: developer
30620f4b53cSBarry Smith 
30773ff1848SBarry Smith   Notes:
308bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
309d9805387SMatthew G. Knepley 
310bb7acecfSBarry 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.
311bb7acecfSBarry Smith 
3121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3135f1ad066SMatthew G Knepley @*/
314d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
315d71ae5a4SJacob Faibussowitsch {
3165f1ad066SMatthew G Knepley   PetscFunctionBegin;
3175f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
318d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3199566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3215f1ad066SMatthew G Knepley }
3225f1ad066SMatthew G Knepley 
323cc4c1da9SBarry Smith /*@
324bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3258f1509bcSBarry Smith 
32620f4b53cSBarry Smith   Logically Collective
3278f1509bcSBarry Smith 
3288f1509bcSBarry Smith   Input Parameters:
329bb7acecfSBarry Smith + dm    - the `DM` context
3308f1509bcSBarry Smith - ctype - the matrix type
3318f1509bcSBarry Smith 
33220f4b53cSBarry Smith   Options Database Key:
3338f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3348f1509bcSBarry Smith 
3358f1509bcSBarry Smith   Level: intermediate
3368f1509bcSBarry Smith 
3371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
338bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3398f1509bcSBarry Smith @*/
340d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
341d71ae5a4SJacob Faibussowitsch {
3428f1509bcSBarry Smith   PetscFunctionBegin;
3438f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3448f1509bcSBarry Smith   dm->coloringtype = ctype;
3453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3468f1509bcSBarry Smith }
3478f1509bcSBarry Smith 
348cc4c1da9SBarry Smith /*@
349bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
350521d9a4cSLisandro Dalcin 
35120f4b53cSBarry Smith   Logically Collective
352521d9a4cSLisandro Dalcin 
353521d9a4cSLisandro Dalcin   Input Parameter:
354bb7acecfSBarry Smith . dm - the `DM` context
3558f1509bcSBarry Smith 
3568f1509bcSBarry Smith   Output Parameter:
3578f1509bcSBarry Smith . ctype - the matrix type
3588f1509bcSBarry Smith 
35920f4b53cSBarry Smith   Options Database Key:
3608f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3618f1509bcSBarry Smith 
3628f1509bcSBarry Smith   Level: intermediate
3638f1509bcSBarry Smith 
3641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
36542747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3668f1509bcSBarry Smith @*/
367d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
368d71ae5a4SJacob Faibussowitsch {
3698f1509bcSBarry Smith   PetscFunctionBegin;
3708f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3718f1509bcSBarry Smith   *ctype = dm->coloringtype;
3723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3738f1509bcSBarry Smith }
3748f1509bcSBarry Smith 
375cc4c1da9SBarry Smith /*@
376bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3778f1509bcSBarry Smith 
37820f4b53cSBarry Smith   Logically Collective
3798f1509bcSBarry Smith 
3808f1509bcSBarry Smith   Input Parameters:
381bb7acecfSBarry Smith + dm    - the `DM` context
382bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
383521d9a4cSLisandro Dalcin 
38420f4b53cSBarry Smith   Options Database Key:
385bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
386521d9a4cSLisandro Dalcin 
387521d9a4cSLisandro Dalcin   Level: intermediate
388521d9a4cSLisandro Dalcin 
38942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
390521d9a4cSLisandro Dalcin @*/
391d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
392d71ae5a4SJacob Faibussowitsch {
39332546409SMatthew G. Knepley   char *tmp;
39432546409SMatthew G. Knepley 
395521d9a4cSLisandro Dalcin   PetscFunctionBegin;
396521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39732546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
39832546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
3999566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
40032546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
4013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
402521d9a4cSLisandro Dalcin }
403521d9a4cSLisandro Dalcin 
404cc4c1da9SBarry Smith /*@
40520f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
406c0dedaeaSBarry Smith 
40720f4b53cSBarry Smith   Logically Collective
408c0dedaeaSBarry Smith 
409c0dedaeaSBarry Smith   Input Parameter:
410bb7acecfSBarry Smith . dm - the `DM` context
411c0dedaeaSBarry Smith 
412c0dedaeaSBarry Smith   Output Parameter:
413c0dedaeaSBarry Smith . ctype - the matrix type
414c0dedaeaSBarry Smith 
415c0dedaeaSBarry Smith   Level: intermediate
416c0dedaeaSBarry Smith 
41742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
418c0dedaeaSBarry Smith @*/
419d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
420d71ae5a4SJacob Faibussowitsch {
421c0dedaeaSBarry Smith   PetscFunctionBegin;
422c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
423c0dedaeaSBarry Smith   *ctype = dm->mattype;
4243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
425c0dedaeaSBarry Smith }
426c0dedaeaSBarry Smith 
427c688c046SMatthew G Knepley /*@
428bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
429c688c046SMatthew G Knepley 
43020f4b53cSBarry Smith   Not Collective
431c688c046SMatthew G Knepley 
432c688c046SMatthew G Knepley   Input Parameter:
433bb7acecfSBarry Smith . A - The `Mat`
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   Output Parameter:
436bb7acecfSBarry Smith . dm - The `DM`
437c688c046SMatthew G Knepley 
438c688c046SMatthew G Knepley   Level: intermediate
439c688c046SMatthew G Knepley 
440bb7acecfSBarry Smith   Note:
441bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
442bb7acecfSBarry Smith 
44373ff1848SBarry Smith   Developer Note:
444bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4458f1509bcSBarry Smith 
4461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
447c688c046SMatthew G Knepley @*/
448d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
449d71ae5a4SJacob Faibussowitsch {
450c688c046SMatthew G Knepley   PetscFunctionBegin;
451c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4524f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4539566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
455c688c046SMatthew G Knepley }
456c688c046SMatthew G Knepley 
457c688c046SMatthew G Knepley /*@
458bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
459c688c046SMatthew G Knepley 
46020f4b53cSBarry Smith   Not Collective
461c688c046SMatthew G Knepley 
462c688c046SMatthew G Knepley   Input Parameters:
46320f4b53cSBarry Smith + A  - The `Mat`
46420f4b53cSBarry Smith - dm - The `DM`
465c688c046SMatthew G Knepley 
466bb7acecfSBarry Smith   Level: developer
467c688c046SMatthew G Knepley 
468bb7acecfSBarry Smith   Note:
469bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
470bb7acecfSBarry Smith 
47173ff1848SBarry Smith   Developer Note:
472bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
473bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4748f1509bcSBarry Smith 
4751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
476c688c046SMatthew G Knepley @*/
477d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
478d71ae5a4SJacob Faibussowitsch {
479c688c046SMatthew G Knepley   PetscFunctionBegin;
480c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4818865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4829566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
484c688c046SMatthew G Knepley }
485c688c046SMatthew G Knepley 
486cc4c1da9SBarry Smith /*@
487bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4889a42bb27SBarry Smith 
48920f4b53cSBarry Smith   Logically Collective
4909a42bb27SBarry Smith 
491d8d19677SJose E. Roman   Input Parameters:
49260225df5SJacob Faibussowitsch + dm     - the `DM` context
493bb7acecfSBarry Smith - prefix - the prefix to prepend
4949a42bb27SBarry Smith 
49520f4b53cSBarry Smith   Level: advanced
49620f4b53cSBarry Smith 
49720f4b53cSBarry Smith   Note:
4989a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
4999a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
5009a42bb27SBarry Smith 
5011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
5029a42bb27SBarry Smith @*/
503d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
504d71ae5a4SJacob Faibussowitsch {
5059a42bb27SBarry Smith   PetscFunctionBegin;
5069a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5079566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5081baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5091baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5119a42bb27SBarry Smith }
5129a42bb27SBarry Smith 
513cc4c1da9SBarry Smith /*@
514da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
515bb7acecfSBarry Smith   `DM` options in the options database.
51631697293SDave May 
51720f4b53cSBarry Smith   Logically Collective
51831697293SDave May 
51931697293SDave May   Input Parameters:
520bb7acecfSBarry Smith + dm     - the `DM` context
521bb7acecfSBarry Smith - prefix - the string to append to the current prefix
52231697293SDave May 
52320f4b53cSBarry Smith   Level: advanced
52420f4b53cSBarry Smith 
52520f4b53cSBarry Smith   Note:
526bb7acecfSBarry 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.
52731697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
52831697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
52931697293SDave May 
5301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
53131697293SDave May @*/
532d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
533d71ae5a4SJacob Faibussowitsch {
53431697293SDave May   PetscFunctionBegin;
53531697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5369566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
53831697293SDave May }
53931697293SDave May 
540cc4c1da9SBarry Smith /*@
54131697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
542bb7acecfSBarry Smith   DM options in the options database.
54331697293SDave May 
54431697293SDave May   Not Collective
54531697293SDave May 
5462fe279fdSBarry Smith   Input Parameter:
547bb7acecfSBarry Smith . dm - the `DM` context
54831697293SDave May 
5492fe279fdSBarry Smith   Output Parameter:
55031697293SDave May . prefix - pointer to the prefix string used is returned
55131697293SDave May 
55231697293SDave May   Level: advanced
55331697293SDave May 
55473ff1848SBarry Smith   Fortran Note:
55520f4b53cSBarry Smith   Pass in a string 'prefix' of
55620f4b53cSBarry Smith   sufficient length to hold the prefix.
55720f4b53cSBarry Smith 
5581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
55931697293SDave May @*/
560d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
561d71ae5a4SJacob Faibussowitsch {
56231697293SDave May   PetscFunctionBegin;
56331697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5649566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56631697293SDave May }
56731697293SDave May 
56862e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
569d71ae5a4SJacob Faibussowitsch {
5706eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
57188bdff64SToby Isaac 
57288bdff64SToby Isaac   PetscFunctionBegin;
573aab5bcd8SJed Brown   *ncrefct = 0;
57488bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
57588bdff64SToby Isaac     refct--;
57688bdff64SToby Isaac     if (recurseCoarse) {
57788bdff64SToby Isaac       PetscInt coarseCount;
57888bdff64SToby Isaac 
57962e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
58088bdff64SToby Isaac       refct += coarseCount;
58188bdff64SToby Isaac     }
58288bdff64SToby Isaac   }
58388bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
58488bdff64SToby Isaac     refct--;
58588bdff64SToby Isaac     if (recurseFine) {
58688bdff64SToby Isaac       PetscInt fineCount;
58788bdff64SToby Isaac 
58862e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
58988bdff64SToby Isaac       refct += fineCount;
59088bdff64SToby Isaac     }
59188bdff64SToby Isaac   }
59288bdff64SToby Isaac   *ncrefct = refct;
5933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59488bdff64SToby Isaac }
59588bdff64SToby Isaac 
59662e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
59762e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
59862e5d2d2SJDBetteridge {
59962e5d2d2SJDBetteridge   PetscFunctionBegin;
60062e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
6013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60262e5d2d2SJDBetteridge }
60362e5d2d2SJDBetteridge 
604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
605d71ae5a4SJacob Faibussowitsch {
6065d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
607354557abSToby Isaac 
608354557abSToby Isaac   PetscFunctionBegin;
609354557abSToby Isaac   /* destroy the labels */
610354557abSToby Isaac   while (next) {
611354557abSToby Isaac     DMLabelLink tmp = next->next;
612354557abSToby Isaac 
6135d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
614ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6159566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6169566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
617354557abSToby Isaac     next = tmp;
618354557abSToby Isaac   }
6195d80c0bfSVaclav Hapla   dm->labels = NULL;
6203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
621354557abSToby Isaac }
622354557abSToby Isaac 
62366976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
624d71ae5a4SJacob Faibussowitsch {
6256858538eSMatthew G. Knepley   PetscFunctionBegin;
6266858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6276858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6286858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6296858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6306858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6326858538eSMatthew G. Knepley }
6336858538eSMatthew G. Knepley 
6340764c050SBarry Smith /*@
635bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
63647c6ae99SBarry Smith 
63720f4b53cSBarry Smith   Collective
63847c6ae99SBarry Smith 
63947c6ae99SBarry Smith   Input Parameter:
640bb7acecfSBarry Smith . dm - the `DM` object to destroy
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   Level: developer
64347c6ae99SBarry Smith 
6441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
64547c6ae99SBarry Smith @*/
646d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
647d71ae5a4SJacob Faibussowitsch {
6486eb26441SStefano Zampini   PetscInt cnt;
64947c6ae99SBarry Smith 
65047c6ae99SBarry Smith   PetscFunctionBegin;
6513ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
652f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*dm, DM_CLASSID, 1);
65387e657c6SBarry Smith 
65488bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
65562e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
656f4f49eeaSPierre Jolivet   --((PetscObject)*dm)->refct;
6579371c9d4SSatish Balay   if (--cnt > 0) {
6589371c9d4SSatish Balay     *dm = NULL;
6593ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6609371c9d4SSatish Balay   }
661f4f49eeaSPierre Jolivet   if (((PetscObject)*dm)->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
662f4f49eeaSPierre Jolivet   ((PetscObject)*dm)->refct = 0;
6636eb26441SStefano Zampini 
6649566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6659566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
666974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
667974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6682348bcf4SPeter Brune 
669b17ce1afSJed Brown   /* Destroy the list of hooks */
670c833c3b5SJed Brown   {
671c833c3b5SJed Brown     DMCoarsenHookLink link, next;
672b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
673b17ce1afSJed Brown       next = link->next;
6749566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
675b17ce1afSJed Brown     }
6760298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
677c833c3b5SJed Brown   }
678c833c3b5SJed Brown   {
679c833c3b5SJed Brown     DMRefineHookLink link, next;
680c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
681c833c3b5SJed Brown       next = link->next;
6829566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
683c833c3b5SJed Brown     }
6840298fd71SBarry Smith     (*dm)->refinehook = NULL;
685c833c3b5SJed Brown   }
686be081cd6SPeter Brune   {
687be081cd6SPeter Brune     DMSubDomainHookLink link, next;
688be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
689be081cd6SPeter Brune       next = link->next;
6909566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
691be081cd6SPeter Brune     }
6920298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
693be081cd6SPeter Brune   }
694baf369e7SPeter Brune   {
695baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
696baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
697baf369e7SPeter Brune       next = link->next;
6989566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
699baf369e7SPeter Brune     }
7000298fd71SBarry Smith     (*dm)->gtolhook = NULL;
701baf369e7SPeter Brune   }
702d4d07f1eSToby Isaac   {
703d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
704d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
705d4d07f1eSToby Isaac       next = link->next;
7069566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
707d4d07f1eSToby Isaac     }
708d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
709d4d07f1eSToby Isaac   }
710aa1993deSMatthew G Knepley   /* Destroy the work arrays */
711aa1993deSMatthew G Knepley   {
712aa1993deSMatthew G Knepley     DMWorkLink link, next;
713835f2295SStefano Zampini     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (*dm)->workout->mem);
714aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
715aa1993deSMatthew G Knepley       next = link->next;
7169566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7179566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
718aa1993deSMatthew G Knepley     }
7190298fd71SBarry Smith     (*dm)->workin = NULL;
720aa1993deSMatthew G Knepley   }
721c58f1c22SToby Isaac   /* destroy the labels */
7229566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
723f4cdcedcSVaclav Hapla   /* destroy the fields */
7249566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
725f4cdcedcSVaclav Hapla   /* destroy the boundaries */
726e6f8dbb6SToby Isaac   {
727e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
728e6f8dbb6SToby Isaac     while (next) {
729e6f8dbb6SToby Isaac       DMBoundary b = next;
730e6f8dbb6SToby Isaac 
731e6f8dbb6SToby Isaac       next = b->next;
7329566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
733e6f8dbb6SToby Isaac     }
734e6f8dbb6SToby Isaac   }
735b17ce1afSJed Brown 
7369566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7379566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7389566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
73952536dc3SBarry Smith 
74048a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7419566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7429566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7439566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7449566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74588ed4aceSMatthew G Knepley 
7469566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7479566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
748adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7499566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7509566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7519566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7529566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7539566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
75448a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7559566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
756e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7579566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
75848a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7596eb26441SStefano Zampini 
7609566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76148a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7629566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7634fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7649566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7659566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7666858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7676858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7689566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7699566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
771ddedc8f6SJames Wright   for (PetscInt i = 0; i < (*dm)->periodic.num_affines; i++) {
772ddedc8f6SJames Wright     PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local[i]));
773ddedc8f6SJames Wright     PetscCall(VecDestroy(&(*dm)->periodic.affine[i]));
774ddedc8f6SJames Wright   }
775ddedc8f6SJames Wright   if ((*dm)->periodic.num_affines > 0) PetscCall(PetscFree2((*dm)->periodic.affine_to_local, (*dm)->periodic.affine));
7766636e97aSMatthew G Knepley 
7779566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7789566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
779e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7809566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
781732e2eb9SMatthew G Knepley 
782213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7839566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
784d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
785f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7869566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7879566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
788f918ec44SMatthew G. Knepley #endif
789435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7909566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79247c6ae99SBarry Smith }
79347c6ae99SBarry Smith 
794d7bf68aeSBarry Smith /*@
795bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
796d7bf68aeSBarry Smith 
79720f4b53cSBarry Smith   Collective
798d7bf68aeSBarry Smith 
799d7bf68aeSBarry Smith   Input Parameter:
800bb7acecfSBarry Smith . dm - the `DM` object to setup
801d7bf68aeSBarry Smith 
802bb7acecfSBarry Smith   Level: intermediate
803d7bf68aeSBarry Smith 
804bb7acecfSBarry Smith   Note:
805bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
806bb7acecfSBarry Smith 
8071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
808d7bf68aeSBarry Smith @*/
809d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
810d71ae5a4SJacob Faibussowitsch {
811d7bf68aeSBarry Smith   PetscFunctionBegin;
812171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8133ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
814dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8158387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
817d7bf68aeSBarry Smith }
818d7bf68aeSBarry Smith 
819d7bf68aeSBarry Smith /*@
820bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
821d7bf68aeSBarry Smith 
82220f4b53cSBarry Smith   Collective
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith   Input Parameter:
825bb7acecfSBarry Smith . dm - the `DM` object to set options for
826d7bf68aeSBarry Smith 
82720f4b53cSBarry Smith   Options Database Keys:
828bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
829bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
830bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
831a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83220f4b53cSBarry 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`
83322d6dc08SStefano Zampini . -dm_plex_option_phases <ph0_, ph1_, ...>           - List of prefixes for option processing phases
83420f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8359318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
836cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8375dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8389318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8399318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8409318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
841bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
842bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
843b9da1bb3SMatthew G. Knepley . -dm_plex_orient <bool>                             - `PETSC_TRUE` turns on topological orientation (flipping edges and faces)
8449318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
845be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
846be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
847be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8489318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8499318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8509318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
851bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8529318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8539318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8549318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8559318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
856bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8579318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8589318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8599318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8609318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
861bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
862d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
863d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
864d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
865d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
866d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
867909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
868909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8699318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8709318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8719318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
87220f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
873d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
87420f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
875bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
876bb7acecfSBarry 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()`
877bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
878bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
879bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
880384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
881d7bf68aeSBarry Smith 
88295eb5ee5SVaclav Hapla   Level: intermediate
88395eb5ee5SVaclav Hapla 
8848e704042SBarry Smith   Note:
8858e704042SBarry Smith   For some `DMType` such as `DMDA` this cannot be called after `DMSetUp()` has been called.
8868e704042SBarry Smith 
8871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
888bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
8898e704042SBarry Smith          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`, `DMSetUp()`
890d7bf68aeSBarry Smith @*/
891d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
892d71ae5a4SJacob Faibussowitsch {
8937781c08eSBarry Smith   char      typeName[256];
894ca266f36SBarry Smith   PetscBool flg;
895d7bf68aeSBarry Smith 
896d7bf68aeSBarry Smith   PetscFunctionBegin;
897171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89849be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8999566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9009566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
901dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
902d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9051baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9071baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
908863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9109566063dSJacob 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));
911eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
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 
919ffeef943SBarry Smith /*@
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)
92760225df5SJacob Faibussowitsch - name - 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 
93460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
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 
944ffeef943SBarry Smith /*@
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 
95649c89c76SBlaise Bourdin   Notes:
95749c89c76SBlaise Bourdin 
95849c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWERHDF5` i.e. HDF5 format can be used with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` to save multiple `DMPLEX`
959bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
960bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
961cd7e8a5eSksagiyam 
96249c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWEREXODUSII` i.e. ExodusII format assumes that element blocks (mapped to "Cell sets" labels)
96349c89c76SBlaise Bourdin   consists of sequentially numbered cells.
96449c89c76SBlaise Bourdin 
96549c89c76SBlaise Bourdin   If `dm` has been distributed, only the part of the `DM` on MPI rank 0 (including "ghost" cells and vertices) will be written.
96649c89c76SBlaise Bourdin 
96749c89c76SBlaise Bourdin   Only TRI, TET, QUAD, and HEX cells are supported.
96849c89c76SBlaise Bourdin 
96949c89c76SBlaise Bourdin   `DMPLEX` only represents geometry while most post-processing software expect that a mesh also provides information on the discretization space. This function assumes that the file represents Lagrange finite elements of order 1 or 2.
97049c89c76SBlaise Bourdin   The order of the mesh shall be set using `PetscViewerExodusIISetOrder()`
97149c89c76SBlaise Bourdin 
972d7c1f440SPierre Jolivet   Variable names can be set and queried using `PetscViewerExodusII[Set/Get][Nodal/Zonal]VariableNames[s]`.
97349c89c76SBlaise Bourdin 
9741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
97547c6ae99SBarry Smith @*/
976d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
977d71ae5a4SJacob Faibussowitsch {
97832c0f0efSBarry Smith   PetscBool         isbinary;
97976a8abe0SBarry Smith   PetscMPIInt       size;
98076a8abe0SBarry Smith   PetscViewerFormat format;
98147c6ae99SBarry Smith 
98247c6ae99SBarry Smith   PetscFunctionBegin;
983171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98448a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
985b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
98674903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
98774903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
98874903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
98974903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
99074903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
99174903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
99274903a4fSStefano Zampini      in an error here */
99374903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9949566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
995b1b135c8SBarry Smith 
99642d0c57aSJames Wright   PetscCall(PetscLogEventBegin(DM_View, v, 0, 0, 0));
9979566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9989566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9993ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
10009566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
10019566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
100232c0f0efSBarry Smith   if (isbinary) {
100355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
100432c0f0efSBarry Smith     char     type[256];
100532c0f0efSBarry Smith 
10069566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
1007c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
10089566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
100932c0f0efSBarry Smith   }
1010dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
101142d0c57aSJames Wright   PetscCall(PetscLogEventEnd(DM_View, v, 0, 0, 0));
10123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
101347c6ae99SBarry Smith }
101447c6ae99SBarry Smith 
101547c6ae99SBarry Smith /*@
1016bb7acecfSBarry 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,
1017bb7acecfSBarry Smith   that is it has no ghost locations.
101847c6ae99SBarry Smith 
101920f4b53cSBarry Smith   Collective
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith   Input Parameter:
1022bb7acecfSBarry Smith . dm - the `DM` object
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith   Output Parameter:
102547c6ae99SBarry Smith . vec - the global vector
102647c6ae99SBarry Smith 
1027073dac72SJed Brown   Level: beginner
102847c6ae99SBarry Smith 
10291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1030bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
103147c6ae99SBarry Smith @*/
1032d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1033d71ae5a4SJacob Faibussowitsch {
103447c6ae99SBarry Smith   PetscFunctionBegin;
1035171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10364f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1037dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
103876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1039c6b011d8SStefano Zampini     DM vdm;
1040c6b011d8SStefano Zampini 
10419566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10427a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1043c6b011d8SStefano Zampini   }
10443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104547c6ae99SBarry Smith }
104647c6ae99SBarry Smith 
104747c6ae99SBarry Smith /*@
1048bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
104947c6ae99SBarry Smith 
105047c6ae99SBarry Smith   Not Collective
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith   Input Parameter:
1053bb7acecfSBarry Smith . dm - the `DM` object
105447c6ae99SBarry Smith 
105547c6ae99SBarry Smith   Output Parameter:
105647c6ae99SBarry Smith . vec - the local vector
105747c6ae99SBarry Smith 
1058073dac72SJed Brown   Level: beginner
105947c6ae99SBarry Smith 
106020f4b53cSBarry Smith   Note:
1061bb7acecfSBarry 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.
1062bb7acecfSBarry Smith 
10631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1064bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
106547c6ae99SBarry Smith @*/
1066d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1067d71ae5a4SJacob Faibussowitsch {
106847c6ae99SBarry Smith   PetscFunctionBegin;
1069171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10704f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1071dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
107276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1073c6b011d8SStefano Zampini     DM vdm;
1074c6b011d8SStefano Zampini 
10759566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10767a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1077c6b011d8SStefano Zampini   }
10783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
107947c6ae99SBarry Smith }
108047c6ae99SBarry Smith 
10811411c6eeSJed Brown /*@
1082bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10831411c6eeSJed Brown 
108420f4b53cSBarry Smith   Collective
10851411c6eeSJed Brown 
10861411c6eeSJed Brown   Input Parameter:
1087bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10881411c6eeSJed Brown 
10891411c6eeSJed Brown   Output Parameter:
10901411c6eeSJed Brown . ltog - the mapping
10911411c6eeSJed Brown 
1092bb7acecfSBarry Smith   Level: advanced
10931411c6eeSJed Brown 
10941411c6eeSJed Brown   Notes:
1095bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10961411c6eeSJed Brown 
1097bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1098bb7acecfSBarry Smith   need to use this function with those objects.
1099bb7acecfSBarry Smith 
1100bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1101bb7acecfSBarry Smith 
110260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1103bb7acecfSBarry Smith           `DMCreateMatrix()`
11041411c6eeSJed Brown @*/
1105d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1106d71ae5a4SJacob Faibussowitsch {
11070be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
11081411c6eeSJed Brown 
11091411c6eeSJed Brown   PetscFunctionBegin;
11101411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11114f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
11121411c6eeSJed Brown   if (!dm->ltogmap) {
111337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
111437d0c07bSMatthew G Knepley 
11159566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
111637d0c07bSMatthew G Knepley     if (section) {
1117a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
111837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1119ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
112037d0c07bSMatthew G Knepley 
11219566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11229566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11249566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
112537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1126e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
112737d0c07bSMatthew G Knepley 
112837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11329566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11331a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11341a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1135ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11365227eafbSStefano Zampini 
1137e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11385227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1139e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1140e6befd46SJed Brown             cind++;
1141e6befd46SJed Brown           } else {
11425227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1143e6befd46SJed Brown           }
114437d0c07bSMatthew G Knepley         }
114537d0c07bSMatthew G Knepley       }
1146bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11471690c2aeSBarry Smith       bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
11489371c9d4SSatish Balay       bsLocal[1] = bs;
11499566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11509371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11519371c9d4SSatish Balay         bs = 1;
11529371c9d4SSatish Balay       } else {
11539371c9d4SSatish Balay         bs = bsMinMax[0];
11549371c9d4SSatish Balay       }
11557591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11567591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1157ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1158ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1159ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1160ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1161ca469d19SJed Brown         }
1162ccf3bd66SMatthew G. Knepley         n /= bs;
1163ccf3bd66SMatthew G. Knepley       }
11649566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1165dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
116637d0c07bSMatthew G Knepley   }
11671411c6eeSJed Brown   *ltog = dm->ltogmap;
11683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11691411c6eeSJed Brown }
11701411c6eeSJed Brown 
11711411c6eeSJed Brown /*@
1172bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11731411c6eeSJed Brown 
11741411c6eeSJed Brown   Not Collective
11751411c6eeSJed Brown 
11761411c6eeSJed Brown   Input Parameter:
1177bb7acecfSBarry Smith . dm - the `DM` with block structure
11781411c6eeSJed Brown 
11791411c6eeSJed Brown   Output Parameter:
11801411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11811411c6eeSJed Brown 
11821411c6eeSJed Brown   Level: intermediate
11831411c6eeSJed Brown 
118473ff1848SBarry Smith   Notes:
1185bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1186bb7acecfSBarry Smith 
1187bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1188bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1189bb7acecfSBarry Smith 
11901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11911411c6eeSJed Brown @*/
1192d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1193d71ae5a4SJacob Faibussowitsch {
11941411c6eeSJed Brown   PetscFunctionBegin;
11951411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11964f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11977a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11981411c6eeSJed Brown   *bs = dm->bs;
11993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12001411c6eeSJed Brown }
12011411c6eeSJed Brown 
1202ffeef943SBarry Smith /*@
1203bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1204bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
120547c6ae99SBarry Smith 
120620f4b53cSBarry Smith   Collective
120747c6ae99SBarry Smith 
1208d8d19677SJose E. Roman   Input Parameters:
1209bb7acecfSBarry Smith + dmc - the `DM` object
1210bb7acecfSBarry Smith - dmf - the second, finer `DM` object
121147c6ae99SBarry Smith 
1212d8d19677SJose E. Roman   Output Parameters:
121347c6ae99SBarry Smith + mat - the interpolation
1214b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
121547c6ae99SBarry Smith 
121647c6ae99SBarry Smith   Level: developer
121747c6ae99SBarry Smith 
121895452b02SPatrick Sanan   Notes:
1219bb7acecfSBarry 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
1220bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1221d52bd9f3SBarry Smith 
1222bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1223bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
122485afcc9aSBarry Smith 
12251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
122647c6ae99SBarry Smith @*/
1227d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1228d71ae5a4SJacob Faibussowitsch {
122947c6ae99SBarry Smith   PetscFunctionBegin;
1230a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1231a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12324f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12339566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1234dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
123747c6ae99SBarry Smith }
123847c6ae99SBarry Smith 
12393ad4599aSBarry Smith /*@
1240a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1241a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12422ed6491fSPatrick Sanan 
12432ed6491fSPatrick Sanan   Input Parameters:
1244bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1245bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12462ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12472ed6491fSPatrick Sanan 
12482ed6491fSPatrick Sanan   Output Parameter:
12492ed6491fSPatrick Sanan . scale - the scaled vector
12502ed6491fSPatrick Sanan 
1251bb7acecfSBarry Smith   Level: advanced
12522ed6491fSPatrick Sanan 
125373ff1848SBarry Smith   Note:
1254a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1255a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1256a4e35b19SJacob Faibussowitsch 
125773ff1848SBarry Smith   Developer Note:
1258bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1259e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1260e9c74fd6SRichard Tran Mills 
126160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12622ed6491fSPatrick Sanan @*/
1263d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1264d71ae5a4SJacob Faibussowitsch {
12652ed6491fSPatrick Sanan   Vec         fine;
12662ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12679704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1268e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12699704db99SRichard Tran Mills #endif
12702ed6491fSPatrick Sanan 
12712ed6491fSPatrick Sanan   PetscFunctionBegin;
12729566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12739566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12749566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12759704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12769704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12779704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12789704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12799566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1280e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12819566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12829566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12839566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
128483aa49f4SRichard Tran Mills   }
12859704db99SRichard Tran Mills #endif
12869566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12879566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12889566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12902ed6491fSPatrick Sanan }
12912ed6491fSPatrick Sanan 
12922ed6491fSPatrick Sanan /*@
1293bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1294bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12953ad4599aSBarry Smith 
129620f4b53cSBarry Smith   Collective
12973ad4599aSBarry Smith 
1298d8d19677SJose E. Roman   Input Parameters:
1299bb7acecfSBarry Smith + dmc - the `DM` object
1300bb7acecfSBarry Smith - dmf - the second, finer `DM` object
13013ad4599aSBarry Smith 
13023ad4599aSBarry Smith   Output Parameter:
13033ad4599aSBarry Smith . mat - the restriction
13043ad4599aSBarry Smith 
13053ad4599aSBarry Smith   Level: developer
13063ad4599aSBarry Smith 
1307bb7acecfSBarry Smith   Note:
1308bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1309bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
13103ad4599aSBarry Smith 
13111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
13123ad4599aSBarry Smith @*/
1313d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1314d71ae5a4SJacob Faibussowitsch {
13153ad4599aSBarry Smith   PetscFunctionBegin;
1316a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1317a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13184f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1320dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13233ad4599aSBarry Smith }
13243ad4599aSBarry Smith 
132547c6ae99SBarry Smith /*@
1326a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
132747c6ae99SBarry Smith 
132820f4b53cSBarry Smith   Collective
132947c6ae99SBarry Smith 
1330d8d19677SJose E. Roman   Input Parameters:
1331bb7acecfSBarry Smith + dac - the `DM` object
1332bb7acecfSBarry Smith - daf - the second, finer `DM` object
133347c6ae99SBarry Smith 
133447c6ae99SBarry Smith   Output Parameter:
13356dbf9973SLawrence Mitchell . mat - the injection
133647c6ae99SBarry Smith 
133747c6ae99SBarry Smith   Level: developer
133847c6ae99SBarry Smith 
1339a4e35b19SJacob Faibussowitsch   Notes:
1340a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1341a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1342a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1343a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1344a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1345a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1346a4e35b19SJacob Faibussowitsch 
1347bb7acecfSBarry 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
1348bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
134985afcc9aSBarry Smith 
13501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1351bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
135247c6ae99SBarry Smith @*/
1353d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1354d71ae5a4SJacob Faibussowitsch {
135547c6ae99SBarry Smith   PetscFunctionBegin;
1356a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1357a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13584f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1360dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13619566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
136347c6ae99SBarry Smith }
136447c6ae99SBarry Smith 
1365b412c318SBarry Smith /*@
1366bb7acecfSBarry 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
1367bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1368bd041c0cSMatthew G. Knepley 
136920f4b53cSBarry Smith   Collective
1370bd041c0cSMatthew G. Knepley 
1371d8d19677SJose E. Roman   Input Parameters:
1372bb7acecfSBarry Smith + dmc - the target `DM` object
13738c1c0954SStefano Zampini - dmf - the source `DM` object, can be `NULL`
1374bd041c0cSMatthew G. Knepley 
1375bd041c0cSMatthew G. Knepley   Output Parameter:
1376b4937a87SMatthew G. Knepley . mat - the mass matrix
1377bd041c0cSMatthew G. Knepley 
1378bd041c0cSMatthew G. Knepley   Level: developer
1379bd041c0cSMatthew G. Knepley 
1380bb7acecfSBarry Smith   Notes:
1381bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1382bb7acecfSBarry Smith 
13838c1c0954SStefano Zampini   if `dmc` is `dmf` or `NULL`, then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1384bb7acecfSBarry Smith 
138542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1386bd041c0cSMatthew G. Knepley @*/
1387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1388d71ae5a4SJacob Faibussowitsch {
1389bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1390b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
13918c1c0954SStefano Zampini   if (!dmf) dmf = dmc;
1392b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13934f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13948c1c0954SStefano Zampini   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, dmc, dmf, 0, 0));
1395dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13968c1c0954SStefano Zampini   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, dmc, dmf, 0, 0));
13973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1398b4937a87SMatthew G. Knepley }
1399b4937a87SMatthew G. Knepley 
1400b4937a87SMatthew G. Knepley /*@
1401bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1402b4937a87SMatthew G. Knepley 
140320f4b53cSBarry Smith   Collective
1404b4937a87SMatthew G. Knepley 
1405b4937a87SMatthew G. Knepley   Input Parameter:
1406bb7acecfSBarry Smith . dm - the `DM` object
1407b4937a87SMatthew G. Knepley 
14087dcfde4dSJose E. Roman   Output Parameters:
14098e9849d2SStefano Zampini + llm - the local lumped mass matrix, which is a diagonal matrix, represented as a vector
14108e9849d2SStefano Zampini - lm  - the global lumped mass matrix, which is a diagonal matrix, represented as a vector
1411b4937a87SMatthew G. Knepley 
1412b4937a87SMatthew G. Knepley   Level: developer
1413b4937a87SMatthew G. Knepley 
1414bb7acecfSBarry Smith   Note:
1415bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1416bb7acecfSBarry Smith 
141760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1418b4937a87SMatthew G. Knepley @*/
14198e9849d2SStefano Zampini PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *llm, Vec *lm)
1420d71ae5a4SJacob Faibussowitsch {
1421b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1422b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14238e9849d2SStefano Zampini   if (llm) PetscAssertPointer(llm, 2);
14248e9849d2SStefano Zampini   if (lm) PetscAssertPointer(lm, 3);
14258e9849d2SStefano Zampini   if (llm || lm) PetscUseTypeMethod(dm, createmassmatrixlumped, llm, lm);
14263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1427bd041c0cSMatthew G. Knepley }
1428bd041c0cSMatthew G. Knepley 
1429bd041c0cSMatthew G. Knepley /*@
1430bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1431bb7acecfSBarry Smith   of a PDE on the `DM`.
143247c6ae99SBarry Smith 
143320f4b53cSBarry Smith   Collective
143447c6ae99SBarry Smith 
1435d8d19677SJose E. Roman   Input Parameters:
1436bb7acecfSBarry Smith + dm    - the `DM` object
1437bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
143847c6ae99SBarry Smith 
143947c6ae99SBarry Smith   Output Parameter:
144047c6ae99SBarry Smith . coloring - the coloring
144147c6ae99SBarry Smith 
14421bf8429eSBarry Smith   Level: developer
14431bf8429eSBarry Smith 
1444ec5066bdSBarry Smith   Notes:
1445bb7acecfSBarry 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
1446bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1447ec5066bdSBarry Smith 
1448bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14491bf8429eSBarry 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,
14501bf8429eSBarry Smith   otherwise an error will be generated.
1451ec5066bdSBarry Smith 
14521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1453aab9d709SJed Brown @*/
1454d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1455d71ae5a4SJacob Faibussowitsch {
145647c6ae99SBarry Smith   PetscFunctionBegin;
1457171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14584f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1459dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
146147c6ae99SBarry Smith }
146247c6ae99SBarry Smith 
1463b412c318SBarry Smith /*@
1464bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
146547c6ae99SBarry Smith 
146620f4b53cSBarry Smith   Collective
146747c6ae99SBarry Smith 
146847c6ae99SBarry Smith   Input Parameter:
1469bb7acecfSBarry Smith . dm - the `DM` object
147047c6ae99SBarry Smith 
147147c6ae99SBarry Smith   Output Parameter:
147247c6ae99SBarry Smith . mat - the empty Jacobian
147347c6ae99SBarry Smith 
147420f4b53cSBarry Smith   Options Database Key:
1475bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1476f27dd7c6SMatthew G. Knepley 
147720f4b53cSBarry Smith   Level: beginner
147820f4b53cSBarry Smith 
147995452b02SPatrick Sanan   Notes:
148095452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
148194013140SBarry Smith   do not need to do it yourself.
148294013140SBarry Smith 
148394013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1484bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
148594013140SBarry Smith 
1486bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
148794013140SBarry Smith   internally by PETSc.
148894013140SBarry Smith 
1489bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1490bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
149194013140SBarry Smith 
14921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1493aab9d709SJed Brown @*/
1494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1495d71ae5a4SJacob Faibussowitsch {
149647c6ae99SBarry Smith   PetscFunctionBegin;
1497171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14984f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14999566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
15009566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1501dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
150276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1503c6b011d8SStefano Zampini     DM mdm;
1504c6b011d8SStefano Zampini 
15059566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
15067a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1507c6b011d8SStefano Zampini   }
1508e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1509e5e52638SMatthew G. Knepley   if (dm->Nf) {
1510e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1511649ef022SMatthew Knepley     PetscInt     Nf, f;
1512e571a35bSMatthew G. Knepley 
15139566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1514649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1515649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15169566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15179566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15189566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1519649ef022SMatthew Knepley         break;
1520e571a35bSMatthew G. Knepley       }
1521649ef022SMatthew Knepley     }
1522649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1523649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15249566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15259566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15269566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1527e571a35bSMatthew G. Knepley       }
1528e571a35bSMatthew G. Knepley     }
1529e571a35bSMatthew G. Knepley   }
15309566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
153247c6ae99SBarry Smith }
153347c6ae99SBarry Smith 
1534732e2eb9SMatthew G Knepley /*@
1535a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1536a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1537a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1538aa0f6e3cSJed Brown 
153920f4b53cSBarry Smith   Logically Collective
1540aa0f6e3cSJed Brown 
1541aa0f6e3cSJed Brown   Input Parameters:
1542bb7acecfSBarry Smith + dm   - the `DM`
1543bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1544aa0f6e3cSJed Brown 
1545aa0f6e3cSJed Brown   Level: developer
1546aa0f6e3cSJed Brown 
154773ff1848SBarry Smith   Note:
1548a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1549a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1550a4e35b19SJacob Faibussowitsch 
15511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1552aa0f6e3cSJed Brown @*/
1553d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1554d71ae5a4SJacob Faibussowitsch {
1555aa0f6e3cSJed Brown   PetscFunctionBegin;
1556aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1557aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1559aa0f6e3cSJed Brown }
1560aa0f6e3cSJed Brown 
1561aa0f6e3cSJed Brown /*@
1562bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1563732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1564732e2eb9SMatthew G Knepley 
156520f4b53cSBarry Smith   Logically Collective
1566732e2eb9SMatthew G Knepley 
1567d8d19677SJose E. Roman   Input Parameters:
1568bb7acecfSBarry Smith + dm   - the `DM`
1569bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1570732e2eb9SMatthew G Knepley 
157120f4b53cSBarry Smith   Options Database Key:
1572bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1573f27dd7c6SMatthew G. Knepley 
157420f4b53cSBarry Smith   Level: developer
157520f4b53cSBarry Smith 
15761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1577732e2eb9SMatthew G Knepley @*/
1578d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1579d71ae5a4SJacob Faibussowitsch {
1580732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1581732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1582732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1584732e2eb9SMatthew G Knepley }
1585732e2eb9SMatthew G Knepley 
1586b06ff27eSHong Zhang /*@
1587bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1588bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1589b06ff27eSHong Zhang 
159020f4b53cSBarry Smith   Logically Collective
1591b06ff27eSHong Zhang 
1592d8d19677SJose E. Roman   Input Parameters:
1593bb7acecfSBarry Smith + dm   - the `DM`
1594da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1595b06ff27eSHong Zhang 
1596b06ff27eSHong Zhang   Level: developer
1597bb7acecfSBarry Smith 
15981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1599b06ff27eSHong Zhang @*/
1600d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1601d71ae5a4SJacob Faibussowitsch {
1602b06ff27eSHong Zhang   PetscFunctionBegin;
1603b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1604b06ff27eSHong Zhang   dm->structure_only = only;
16053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1606b06ff27eSHong Zhang }
1607b06ff27eSHong Zhang 
1608863027abSJed Brown /*@
1609863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1610863027abSJed Brown 
161120f4b53cSBarry Smith   Logically Collective
1612863027abSJed Brown 
1613863027abSJed Brown   Input Parameters:
1614863027abSJed Brown + dm    - the `DM`
1615863027abSJed Brown - btype - block by topological point or field node
1616863027abSJed Brown 
161720f4b53cSBarry Smith   Options Database Key:
16180e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1619863027abSJed Brown 
162020f4b53cSBarry Smith   Level: advanced
162120f4b53cSBarry Smith 
16221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1623863027abSJed Brown @*/
1624863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1625863027abSJed Brown {
1626863027abSJed Brown   PetscFunctionBegin;
1627863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1628863027abSJed Brown   dm->blocking_type = btype;
16293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1630863027abSJed Brown }
1631863027abSJed Brown 
1632863027abSJed Brown /*@
1633863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1634863027abSJed Brown 
1635863027abSJed Brown   Not Collective
1636863027abSJed Brown 
16372fe279fdSBarry Smith   Input Parameter:
1638863027abSJed Brown . dm - the `DM`
1639863027abSJed Brown 
16402fe279fdSBarry Smith   Output Parameter:
1641863027abSJed Brown . btype - block by topological point or field node
1642863027abSJed Brown 
1643863027abSJed Brown   Level: advanced
1644863027abSJed Brown 
16451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1646863027abSJed Brown @*/
1647863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1648863027abSJed Brown {
1649863027abSJed Brown   PetscFunctionBegin;
1650863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16514f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1652863027abSJed Brown   *btype = dm->blocking_type;
16533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1654863027abSJed Brown }
1655863027abSJed Brown 
1656a89ea682SMatthew G Knepley /*@C
1657bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1658a89ea682SMatthew G Knepley 
1659a89ea682SMatthew G Knepley   Not Collective
1660a89ea682SMatthew G Knepley 
1661a89ea682SMatthew G Knepley   Input Parameters:
1662bb7acecfSBarry Smith + dm    - the `DM` object
1663a5b23f4aSJose E. Roman . count - The minimum size
166420f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1665a89ea682SMatthew G Knepley 
1666a89ea682SMatthew G Knepley   Output Parameter:
166760225df5SJacob Faibussowitsch . mem - the work array
1668a89ea682SMatthew G Knepley 
1669a89ea682SMatthew G Knepley   Level: developer
1670a89ea682SMatthew G Knepley 
167173ff1848SBarry Smith   Notes:
1672da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1673bb7acecfSBarry Smith 
1674bb7acecfSBarry Smith   The array may contain nonzero values
1675bb7acecfSBarry Smith 
16761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1677a89ea682SMatthew G Knepley @*/
1678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1679d71ae5a4SJacob Faibussowitsch {
1680aa1993deSMatthew G Knepley   DMWorkLink  link;
168169291d52SBarry Smith   PetscMPIInt dsize;
1682a89ea682SMatthew G Knepley 
1683a89ea682SMatthew G Knepley   PetscFunctionBegin;
1684a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16854f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1686442f3b32SStefano Zampini   if (!count) {
1687442f3b32SStefano Zampini     *(void **)mem = NULL;
1688442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1689442f3b32SStefano Zampini   }
1690aa1993deSMatthew G Knepley   if (dm->workin) {
1691aa1993deSMatthew G Knepley     link       = dm->workin;
1692aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1693aa1993deSMatthew G Knepley   } else {
16944dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1695a89ea682SMatthew G Knepley   }
1696b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1697b77fa653SStefano Zampini      Get size directly */
1698b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1699b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1700b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1701b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1702b77fa653SStefano Zampini #endif
1703b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1704b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1705b77fa653SStefano Zampini #endif
1706b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1707b77fa653SStefano Zampini 
17085056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
17099566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
17109566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1711854ce69bSBarry Smith     link->bytes = dsize * count;
1712aa1993deSMatthew G Knepley   }
1713aa1993deSMatthew G Knepley   link->next    = dm->workout;
1714aa1993deSMatthew G Knepley   dm->workout   = link;
1715aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1717a89ea682SMatthew G Knepley }
1718a89ea682SMatthew G Knepley 
1719aa1993deSMatthew G Knepley /*@C
1720bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1721aa1993deSMatthew G Knepley 
1722aa1993deSMatthew G Knepley   Not Collective
1723aa1993deSMatthew G Knepley 
1724aa1993deSMatthew G Knepley   Input Parameters:
1725bb7acecfSBarry Smith + dm    - the `DM` object
1726a5b23f4aSJose E. Roman . count - The minimum size
172720f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1728aa1993deSMatthew G Knepley 
1729aa1993deSMatthew G Knepley   Output Parameter:
173060225df5SJacob Faibussowitsch . mem - the work array
1731aa1993deSMatthew G Knepley 
1732aa1993deSMatthew G Knepley   Level: developer
1733aa1993deSMatthew G Knepley 
173473ff1848SBarry Smith   Developer Note:
1735bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1736147403d9SBarry Smith 
17371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1738aa1993deSMatthew G Knepley @*/
1739d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1740d71ae5a4SJacob Faibussowitsch {
1741aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1742aa1993deSMatthew G Knepley 
1743aa1993deSMatthew G Knepley   PetscFunctionBegin;
1744aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17454f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1746a4e35b19SJacob Faibussowitsch   (void)count;
1747a4e35b19SJacob Faibussowitsch   (void)dtype;
1748442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1749aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1750aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1751aa1993deSMatthew G Knepley       *p            = link->next;
1752aa1993deSMatthew G Knepley       link->next    = dm->workin;
1753aa1993deSMatthew G Knepley       dm->workin    = link;
17540298fd71SBarry Smith       *(void **)mem = NULL;
17553ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1756aa1993deSMatthew G Knepley     }
1757aa1993deSMatthew G Knepley   }
1758aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1759aa1993deSMatthew G Knepley }
1760e7c4fc90SDmitry Karpeev 
17618cda7954SMatthew G. Knepley /*@C
1762bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1763bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17648cda7954SMatthew G. Knepley 
176520f4b53cSBarry Smith   Logically Collective; No Fortran Support
17668cda7954SMatthew G. Knepley 
17678cda7954SMatthew G. Knepley   Input Parameters:
1768bb7acecfSBarry Smith + dm     - The `DM`
17698cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17708cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17718cda7954SMatthew G. Knepley 
177220f4b53cSBarry Smith   Calling sequence of `nullsp`:
1773bb7acecfSBarry Smith + dm        - The present `DM`
1774bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1775147403d9SBarry Smith . field     - The field number in dm
1776147403d9SBarry Smith - nullSpace - The nullspace for the given field
17778cda7954SMatthew G. Knepley 
177849762cbcSSatish Balay   Level: intermediate
177949762cbcSSatish Balay 
17801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1781147403d9SBarry Smith @*/
1782a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1783d71ae5a4SJacob Faibussowitsch {
1784435a35e8SMatthew G Knepley   PetscFunctionBegin;
1785435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17867a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1787435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1789435a35e8SMatthew G Knepley }
1790435a35e8SMatthew G Knepley 
17918cda7954SMatthew G. Knepley /*@C
1792bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17938cda7954SMatthew G. Knepley 
179420f4b53cSBarry Smith   Not Collective; No Fortran Support
17958cda7954SMatthew G. Knepley 
17968cda7954SMatthew G. Knepley   Input Parameters:
1797bb7acecfSBarry Smith + dm    - The `DM`
17988cda7954SMatthew G. Knepley - field - The field number for the nullspace
17998cda7954SMatthew G. Knepley 
18008cda7954SMatthew G. Knepley   Output Parameter:
18018cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
18028cda7954SMatthew G. Knepley 
180320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1804147403d9SBarry Smith + dm        - The present DM
1805147403d9SBarry Smith . origField - The field number given above, in the original DM
1806147403d9SBarry Smith . field     - The field number in dm
1807147403d9SBarry Smith - nullSpace - The nullspace for the given field
18088cda7954SMatthew G. Knepley 
180949762cbcSSatish Balay   Level: intermediate
181049762cbcSSatish Balay 
18111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1812147403d9SBarry Smith @*/
1813a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1814d71ae5a4SJacob Faibussowitsch {
18150a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18160a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18174f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18187a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18190a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18210a50eb56SMatthew G. Knepley }
18220a50eb56SMatthew G. Knepley 
18238cda7954SMatthew G. Knepley /*@C
1824bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18258cda7954SMatthew G. Knepley 
182620f4b53cSBarry Smith   Logically Collective; No Fortran Support
18278cda7954SMatthew G. Knepley 
18288cda7954SMatthew G. Knepley   Input Parameters:
1829bb7acecfSBarry Smith + dm     - The `DM`
18308cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18318cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18328cda7954SMatthew G. Knepley 
183320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1834bb7acecfSBarry Smith + dm        - The present `DM`
1835bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1836147403d9SBarry Smith . field     - The field number in dm
1837147403d9SBarry Smith - nullSpace - The nullspace for the given field
18388cda7954SMatthew G. Knepley 
183949762cbcSSatish Balay   Level: intermediate
184049762cbcSSatish Balay 
18411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1842bb7acecfSBarry Smith           `MatNullSpace`
1843147403d9SBarry Smith @*/
1844a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1845d71ae5a4SJacob Faibussowitsch {
1846f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1847f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18487a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1849f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1851f9d4088aSMatthew G. Knepley }
1852f9d4088aSMatthew G. Knepley 
18538cda7954SMatthew G. Knepley /*@C
1854bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18558cda7954SMatthew G. Knepley 
185620f4b53cSBarry Smith   Not Collective; No Fortran Support
18578cda7954SMatthew G. Knepley 
18588cda7954SMatthew G. Knepley   Input Parameters:
1859bb7acecfSBarry Smith + dm    - The `DM`
18608cda7954SMatthew G. Knepley - field - The field number for the nullspace
18618cda7954SMatthew G. Knepley 
18628cda7954SMatthew G. Knepley   Output Parameter:
18638cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18648cda7954SMatthew G. Knepley 
186520f4b53cSBarry Smith   Calling sequence of `nullsp`:
1866bb7acecfSBarry Smith + dm        - The present `DM`
1867bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1868147403d9SBarry Smith . field     - The field number in dm
1869147403d9SBarry Smith - nullSpace - The nullspace for the given field
18708cda7954SMatthew G. Knepley 
187149762cbcSSatish Balay   Level: intermediate
187249762cbcSSatish Balay 
18731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1874bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1875147403d9SBarry Smith @*/
1876a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1877d71ae5a4SJacob Faibussowitsch {
1878f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1879f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18804f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18817a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1882f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1884f9d4088aSMatthew G. Knepley }
1885f9d4088aSMatthew G. Knepley 
18864f3b5142SJed Brown /*@C
1887bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18884d343eeaSMatthew G Knepley 
188920f4b53cSBarry Smith   Not Collective; No Fortran Support
18904d343eeaSMatthew G Knepley 
18914d343eeaSMatthew G Knepley   Input Parameter:
1892bb7acecfSBarry Smith . dm - the `DM` object
18934d343eeaSMatthew G Knepley 
18944d343eeaSMatthew G Knepley   Output Parameters:
189520f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
189620f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
189720f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18984d343eeaSMatthew G Knepley 
18994d343eeaSMatthew G Knepley   Level: intermediate
19004d343eeaSMatthew G Knepley 
1901bb7acecfSBarry Smith   Note:
190273ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
190373ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1904bb7acecfSBarry Smith   `PetscFree()`.
190521c9b008SJed Brown 
190673ff1848SBarry Smith   Developer Note:
1907bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1908bb7acecfSBarry Smith   likely be removed.
1909bb7acecfSBarry Smith 
19101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1911bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19124d343eeaSMatthew G Knepley @*/
1913d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1914d71ae5a4SJacob Faibussowitsch {
191537d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19164d343eeaSMatthew G Knepley 
19174d343eeaSMatthew G Knepley   PetscFunctionBegin;
19184d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
191969ca1f37SDmitry Karpeev   if (numFields) {
19204f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
192169ca1f37SDmitry Karpeev     *numFields = 0;
192269ca1f37SDmitry Karpeev   }
192337d0c07bSMatthew G Knepley   if (fieldNames) {
19244f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19250298fd71SBarry Smith     *fieldNames = NULL;
192669ca1f37SDmitry Karpeev   }
192769ca1f37SDmitry Karpeev   if (fields) {
19284f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19290298fd71SBarry Smith     *fields = NULL;
193069ca1f37SDmitry Karpeev   }
19319566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
193237d0c07bSMatthew G Knepley   if (section) {
19333a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
193437d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
193537d0c07bSMatthew G Knepley 
19369566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19389566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
194037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
194137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19429566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
194337d0c07bSMatthew G Knepley     }
194437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
194537d0c07bSMatthew G Knepley       PetscInt gdof;
194637d0c07bSMatthew G Knepley 
19479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
194837d0c07bSMatthew G Knepley       if (gdof > 0) {
194937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19503a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
195137d0c07bSMatthew G Knepley 
19529566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19539566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19543a544194SStefano Zampini           fpdof = fdof - fcdof;
19553a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19563a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19573a544194SStefano Zampini             fieldNc[f] = 1;
19583a544194SStefano Zampini           }
19593a544194SStefano Zampini           fieldSizes[f] += fpdof;
196037d0c07bSMatthew G Knepley         }
196137d0c07bSMatthew G Knepley       }
196237d0c07bSMatthew G Knepley     }
196337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19649566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
196537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
196637d0c07bSMatthew G Knepley     }
196737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
196837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
196937d0c07bSMatthew G Knepley 
19709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
197137d0c07bSMatthew G Knepley       if (gdof > 0) {
19729566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
197337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
197437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
197537d0c07bSMatthew G Knepley 
19769566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19779566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1978ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
197937d0c07bSMatthew G Knepley         }
198037d0c07bSMatthew G Knepley       }
198137d0c07bSMatthew G Knepley     }
19828865f1eaSKarl Rupp     if (numFields) *numFields = nF;
198337d0c07bSMatthew G Knepley     if (fieldNames) {
19849566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
198537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
198637d0c07bSMatthew G Knepley         const char *fieldName;
198737d0c07bSMatthew G Knepley 
19889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
1989835f2295SStefano Zampini         PetscCall(PetscStrallocpy(fieldName, &(*fieldNames)[f]));
199037d0c07bSMatthew G Knepley       }
199137d0c07bSMatthew G Knepley     }
199237d0c07bSMatthew G Knepley     if (fields) {
19939566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
199437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19953a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19963a544194SStefano Zampini 
19979566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19983a544194SStefano Zampini         in[0] = -fieldNc[f];
19993a544194SStefano Zampini         in[1] = fieldNc[f];
2000462c564dSBarry Smith         PetscCallMPI(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
20013a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
20029566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
200337d0c07bSMatthew G Knepley       }
200437d0c07bSMatthew G Knepley     }
20059566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
2006dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
20073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20084d343eeaSMatthew G Knepley }
20094d343eeaSMatthew G Knepley 
201016621825SDmitry Karpeev /*@C
2011bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2012a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2013e7c4fc90SDmitry Karpeev 
201420f4b53cSBarry Smith   Not Collective; No Fortran Support
2015e7c4fc90SDmitry Karpeev 
2016e7c4fc90SDmitry Karpeev   Input Parameter:
2017bb7acecfSBarry Smith . dm - the `DM` object
2018e7c4fc90SDmitry Karpeev 
2019e7c4fc90SDmitry Karpeev   Output Parameters:
202020f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
202120f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
202220f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
202320f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2024e7c4fc90SDmitry Karpeev 
2025e7c4fc90SDmitry Karpeev   Level: intermediate
2026e7c4fc90SDmitry Karpeev 
2027a4e35b19SJacob Faibussowitsch   Notes:
2028a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2029a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2030a4e35b19SJacob Faibussowitsch 
2031a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2032a4e35b19SJacob Faibussowitsch 
203373ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
203473ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2035bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2036e7c4fc90SDmitry Karpeev 
203760225df5SJacob Faibussowitsch   Developer Notes:
2038bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2039bb7acecfSBarry Smith 
204073ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
204173ff1848SBarry Smith   decomposition is computed.
204273ff1848SBarry Smith 
204373ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2044e7c4fc90SDmitry Karpeev @*/
2045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2046d71ae5a4SJacob Faibussowitsch {
2047e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2048e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20498865f1eaSKarl Rupp   if (len) {
20504f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20518865f1eaSKarl Rupp     *len = 0;
20528865f1eaSKarl Rupp   }
20538865f1eaSKarl Rupp   if (namelist) {
20544f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2055ea78f98cSLisandro Dalcin     *namelist = NULL;
20568865f1eaSKarl Rupp   }
20578865f1eaSKarl Rupp   if (islist) {
20584f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2059ea78f98cSLisandro Dalcin     *islist = NULL;
20608865f1eaSKarl Rupp   }
20618865f1eaSKarl Rupp   if (dmlist) {
20624f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2063ea78f98cSLisandro Dalcin     *dmlist = NULL;
20648865f1eaSKarl Rupp   }
2065f3f0edfdSDmitry Karpeev   /*
2066f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2067f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2068f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2069f3f0edfdSDmitry Karpeev    */
20707a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
207116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2072435a35e8SMatthew G Knepley     PetscSection section;
2073435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2074435a35e8SMatthew G Knepley 
20759566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20769566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2077435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2078f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20799566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20809566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20819566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2082435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2083435a35e8SMatthew G Knepley         const char *fieldName;
2084435a35e8SMatthew G Knepley 
20859566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
208603dc3394SMatthew G. Knepley         if (namelist) {
20879566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
2088835f2295SStefano Zampini           PetscCall(PetscStrallocpy(fieldName, &(*namelist)[f]));
2089435a35e8SMatthew G Knepley         }
209003dc3394SMatthew G. Knepley       }
2091435a35e8SMatthew G Knepley     } else {
20929566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2093e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20940298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2095e7c4fc90SDmitry Karpeev     }
2096dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
209816621825SDmitry Karpeev }
209916621825SDmitry Karpeev 
21005d83a8b1SBarry Smith /*@
210120f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
210220f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2103435a35e8SMatthew G Knepley 
2104435a35e8SMatthew G Knepley   Not collective
2105435a35e8SMatthew G Knepley 
2106435a35e8SMatthew G Knepley   Input Parameters:
2107bb7acecfSBarry Smith + dm        - The `DM` object
2108bb7acecfSBarry Smith . numFields - The number of fields to select
21092adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2110435a35e8SMatthew G Knepley 
2111435a35e8SMatthew G Knepley   Output Parameters:
2112b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2113b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2114435a35e8SMatthew G Knepley 
211520f4b53cSBarry Smith   Level: intermediate
211620f4b53cSBarry Smith 
2117bb7acecfSBarry Smith   Note:
2118bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21195d3b26e6SMatthew G. Knepley 
212060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2121435a35e8SMatthew G Knepley @*/
2122d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2123d71ae5a4SJacob Faibussowitsch {
2124435a35e8SMatthew G Knepley   PetscFunctionBegin;
2125435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21264f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21274f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21284f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2129dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2131435a35e8SMatthew G Knepley }
2132435a35e8SMatthew G Knepley 
21332adcc780SMatthew G. Knepley /*@C
2134bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21352adcc780SMatthew G. Knepley 
21362adcc780SMatthew G. Knepley   Not collective
21372adcc780SMatthew G. Knepley 
2138d8d19677SJose E. Roman   Input Parameters:
2139bb7acecfSBarry Smith + dms - The `DM` objects
2140bb7acecfSBarry Smith - n   - The number of `DM`s
21412adcc780SMatthew G. Knepley 
21422adcc780SMatthew G. Knepley   Output Parameters:
2143bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2144bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21452adcc780SMatthew G. Knepley 
214620f4b53cSBarry Smith   Level: intermediate
214720f4b53cSBarry Smith 
2148bb7acecfSBarry Smith   Note:
2149bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21505d3b26e6SMatthew G. Knepley 
215173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21522adcc780SMatthew G. Knepley @*/
21535d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2154d71ae5a4SJacob Faibussowitsch {
21552adcc780SMatthew G. Knepley   PetscInt i;
21562adcc780SMatthew G. Knepley 
21572adcc780SMatthew G. Knepley   PetscFunctionBegin;
21584f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2159ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21604f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21614f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2162bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2163bb7acecfSBarry Smith   if (n) {
2164b9d85ea2SLisandro Dalcin     DM dm = dms[0];
216500045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2166dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21672adcc780SMatthew G. Knepley   }
21683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21692adcc780SMatthew G. Knepley }
21702adcc780SMatthew G. Knepley 
217116621825SDmitry Karpeev /*@C
2172a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2173a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
217416621825SDmitry Karpeev 
217520f4b53cSBarry Smith   Not Collective
217616621825SDmitry Karpeev 
217716621825SDmitry Karpeev   Input Parameter:
2178bb7acecfSBarry Smith . dm - the `DM` object
217916621825SDmitry Karpeev 
218016621825SDmitry Karpeev   Output Parameters:
218120f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
218220f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
218373ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
218473ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
218573ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
218616621825SDmitry Karpeev 
218716621825SDmitry Karpeev   Level: intermediate
218816621825SDmitry Karpeev 
218973ff1848SBarry Smith   Notes:
2190a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2191a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2192a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2193a4e35b19SJacob Faibussowitsch 
2194a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2195a4e35b19SJacob Faibussowitsch 
219673ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
219773ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2198bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
219916621825SDmitry Karpeev 
2200a4e35b19SJacob Faibussowitsch   Developer Notes:
220120f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2202bb7acecfSBarry Smith 
220373ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
220473ff1848SBarry Smith 
220573ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
220673ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
220716621825SDmitry Karpeev @*/
2208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2209d71ae5a4SJacob Faibussowitsch {
2210be081cd6SPeter Brune   DMSubDomainHookLink link;
2211be081cd6SPeter Brune   PetscInt            i, l;
221216621825SDmitry Karpeev 
221316621825SDmitry Karpeev   PetscFunctionBegin;
221416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22159371c9d4SSatish Balay   if (n) {
22164f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22179371c9d4SSatish Balay     *n = 0;
22189371c9d4SSatish Balay   }
22199371c9d4SSatish Balay   if (namelist) {
22204f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22219371c9d4SSatish Balay     *namelist = NULL;
22229371c9d4SSatish Balay   }
22239371c9d4SSatish Balay   if (innerislist) {
22244f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22259371c9d4SSatish Balay     *innerislist = NULL;
22269371c9d4SSatish Balay   }
22279371c9d4SSatish Balay   if (outerislist) {
22284f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22299371c9d4SSatish Balay     *outerislist = NULL;
22309371c9d4SSatish Balay   }
22319371c9d4SSatish Balay   if (dmlist) {
22324f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22339371c9d4SSatish Balay     *dmlist = NULL;
22349371c9d4SSatish Balay   }
2235f3f0edfdSDmitry Karpeev   /*
2236f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2237f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2238f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2239f3f0edfdSDmitry Karpeev    */
22407a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
224116621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2242dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
224314a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2244f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2245be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2246be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22479566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2248be081cd6SPeter Brune         }
2249648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2250e7c4fc90SDmitry Karpeev       }
225114a18fd3SPeter Brune     }
2252bb7acecfSBarry Smith     if (n) *n = l;
225314a18fd3SPeter Brune   }
22543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2255e30e807fSPeter Brune }
2256e30e807fSPeter Brune 
2257e30e807fSPeter Brune /*@C
225873ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
225973ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2260e30e807fSPeter Brune 
226120f4b53cSBarry Smith   Not Collective
2262e30e807fSPeter Brune 
2263e30e807fSPeter Brune   Input Parameters:
2264bb7acecfSBarry Smith + dm     - the `DM` object
226573ff1848SBarry Smith . n      - the number of subdomains
2266e30e807fSPeter Brune - subdms - the local subdomains
2267e30e807fSPeter Brune 
2268e30e807fSPeter Brune   Output Parameters:
22696b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2270e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2271e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2272e30e807fSPeter Brune 
227320f4b53cSBarry Smith   Level: developer
227420f4b53cSBarry Smith 
2275bb7acecfSBarry Smith   Note:
2276bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2277e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2278e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2279e30e807fSPeter Brune   solution and residual data.
2280e30e807fSPeter Brune 
228173ff1848SBarry Smith   Developer Note:
2282a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2283a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2284bb7acecfSBarry Smith 
22851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2286e30e807fSPeter Brune @*/
22875d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2288d71ae5a4SJacob Faibussowitsch {
2289e30e807fSPeter Brune   PetscFunctionBegin;
2290e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22914f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2292dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2294e7c4fc90SDmitry Karpeev }
2295e7c4fc90SDmitry Karpeev 
229647c6ae99SBarry Smith /*@
2297bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
229847c6ae99SBarry Smith 
229920f4b53cSBarry Smith   Collective
230047c6ae99SBarry Smith 
2301d8d19677SJose E. Roman   Input Parameters:
2302bb7acecfSBarry Smith + dm   - the `DM` object
2303bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
230447c6ae99SBarry Smith 
230547c6ae99SBarry Smith   Output Parameter:
230620f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2307ae0a1c52SMatthew G Knepley 
230820f4b53cSBarry Smith   Options Database Key:
2309412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2310412e9a14SMatthew G. Knepley 
231147c6ae99SBarry Smith   Level: developer
231247c6ae99SBarry Smith 
231320f4b53cSBarry Smith   Note:
231420f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
231520f4b53cSBarry Smith 
231673ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
231773ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
231847c6ae99SBarry Smith @*/
2319d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2320d71ae5a4SJacob Faibussowitsch {
2321c833c3b5SJed Brown   DMRefineHookLink link;
232247c6ae99SBarry Smith 
232347c6ae99SBarry Smith   PetscFunctionBegin;
2324732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23259566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2326dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23274057135bSMatthew G Knepley   if (*dmf) {
232843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23298865f1eaSKarl Rupp 
23309566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23318865f1eaSKarl Rupp 
2332644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23330598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2334656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23358865f1eaSKarl Rupp 
23369566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2337c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23381baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2339c833c3b5SJed Brown     }
2340c833c3b5SJed Brown   }
23419566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2343c833c3b5SJed Brown }
2344c833c3b5SJed Brown 
2345bb9467b5SJed Brown /*@C
2346c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2347c833c3b5SJed Brown 
234820f4b53cSBarry Smith   Logically Collective; No Fortran Support
2349c833c3b5SJed Brown 
23504165533cSJose E. Roman   Input Parameters:
2351bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2352bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2353f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
235420f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2355c833c3b5SJed Brown 
235620f4b53cSBarry Smith   Calling sequence of `refinehook`:
2357bb7acecfSBarry Smith + coarse - coarse level `DM`
2358bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2359c833c3b5SJed Brown - ctx    - optional user-defined function context
2360c833c3b5SJed Brown 
236120f4b53cSBarry Smith   Calling sequence of `interphook`:
2362bb7acecfSBarry Smith + coarse - coarse level `DM`
2363c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2364bb7acecfSBarry Smith . fine   - fine level `DM` to update
2365c833c3b5SJed Brown - ctx    - optional user-defined function context
2366c833c3b5SJed Brown 
2367c833c3b5SJed Brown   Level: advanced
2368c833c3b5SJed Brown 
2369c833c3b5SJed Brown   Notes:
2370bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2371bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2372bb7acecfSBarry Smith 
2373bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2374c833c3b5SJed Brown 
2375c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2376c833c3b5SJed Brown 
23771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2378c833c3b5SJed Brown @*/
2379a4e35b19SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM coarse, DM fine, void *ctx), PetscErrorCode (*interphook)(DM coarse, Mat interp, DM fine, void *ctx), void *ctx)
2380d71ae5a4SJacob Faibussowitsch {
2381c833c3b5SJed Brown   DMRefineHookLink link, *p;
2382c833c3b5SJed Brown 
2383c833c3b5SJed Brown   PetscFunctionBegin;
2384c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23853d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23863ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23873d8e3701SJed Brown   }
23889566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2389c833c3b5SJed Brown   link->refinehook = refinehook;
2390c833c3b5SJed Brown   link->interphook = interphook;
2391c833c3b5SJed Brown   link->ctx        = ctx;
23920298fd71SBarry Smith   link->next       = NULL;
2393c833c3b5SJed Brown   *p               = link;
23943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2395c833c3b5SJed Brown }
2396c833c3b5SJed Brown 
23973d8e3701SJed Brown /*@C
2398bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2399bb7acecfSBarry Smith   a nonlinear problem to a finer grid
24003d8e3701SJed Brown 
240120f4b53cSBarry Smith   Logically Collective; No Fortran Support
24023d8e3701SJed Brown 
24034165533cSJose E. Roman   Input Parameters:
2404bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2405bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2406bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
240720f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
24083d8e3701SJed Brown 
24093d8e3701SJed Brown   Level: advanced
24103d8e3701SJed Brown 
2411bb7acecfSBarry Smith   Note:
24123d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24133d8e3701SJed Brown 
24141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24153d8e3701SJed Brown @*/
2416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2417d71ae5a4SJacob Faibussowitsch {
24183d8e3701SJed Brown   DMRefineHookLink link, *p;
24193d8e3701SJed Brown 
24203d8e3701SJed Brown   PetscFunctionBegin;
24213d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24223d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24233d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24243d8e3701SJed Brown       link = *p;
24253d8e3701SJed Brown       *p   = link->next;
24269566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24273d8e3701SJed Brown       break;
24283d8e3701SJed Brown     }
24293d8e3701SJed Brown   }
24303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24313d8e3701SJed Brown }
24323d8e3701SJed Brown 
2433c833c3b5SJed Brown /*@
2434bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2435c833c3b5SJed Brown 
2436c833c3b5SJed Brown   Collective if any hooks are
2437c833c3b5SJed Brown 
24384165533cSJose E. Roman   Input Parameters:
2439bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2440bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2441bb7acecfSBarry Smith - fine   - finer `DM` to update
2442c833c3b5SJed Brown 
2443c833c3b5SJed Brown   Level: developer
2444c833c3b5SJed Brown 
244573ff1848SBarry Smith   Developer Note:
2446bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2447bb7acecfSBarry Smith   an API with consistent terminology.
2448bb7acecfSBarry Smith 
24491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2450c833c3b5SJed Brown @*/
2451d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2452d71ae5a4SJacob Faibussowitsch {
2453c833c3b5SJed Brown   DMRefineHookLink link;
2454c833c3b5SJed Brown 
2455c833c3b5SJed Brown   PetscFunctionBegin;
2456c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24571baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24584057135bSMatthew G Knepley   }
24593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
246047c6ae99SBarry Smith }
246147c6ae99SBarry Smith 
2462eb3f98d2SBarry Smith /*@
24631f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24641f3379b2SToby Isaac 
246520f4b53cSBarry Smith   Collective
24661f3379b2SToby Isaac 
24674165533cSJose E. Roman   Input Parameters:
2468bb7acecfSBarry Smith + coarse    - coarse `DM`
2469bb7acecfSBarry Smith . fine      - fine `DM`
2470bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2471bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2472bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24731f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24741f3379b2SToby Isaac 
24754165533cSJose E. Roman   Output Parameter:
24761f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24771f3379b2SToby Isaac 
24781f3379b2SToby Isaac   Level: developer
24791f3379b2SToby Isaac 
2480bb7acecfSBarry Smith   Note:
2481bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24821f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24831f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24841f3379b2SToby Isaac   slope-limiting reconstruction.
24851f3379b2SToby Isaac 
248673ff1848SBarry Smith   Developer Note:
2487bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2488bb7acecfSBarry Smith 
24891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24901f3379b2SToby Isaac @*/
2491d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2492d71ae5a4SJacob Faibussowitsch {
24931f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24941f3379b2SToby Isaac 
24951f3379b2SToby Isaac   PetscFunctionBegin;
24961f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24971f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24981f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24991f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
25001f3379b2SToby Isaac 
25019566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
25021f3379b2SToby Isaac   if (interpsol) {
25039566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
25041f3379b2SToby Isaac   } else if (interp) {
25059566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
250698921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
25073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25081f3379b2SToby Isaac }
25091f3379b2SToby Isaac 
25101f3379b2SToby Isaac /*@
2511bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2512eb3f98d2SBarry Smith 
2513eb3f98d2SBarry Smith   Not Collective
2514eb3f98d2SBarry Smith 
2515eb3f98d2SBarry Smith   Input Parameter:
2516bb7acecfSBarry Smith . dm - the `DM` object
2517eb3f98d2SBarry Smith 
2518eb3f98d2SBarry Smith   Output Parameter:
2519eb3f98d2SBarry Smith . level - number of refinements
2520eb3f98d2SBarry Smith 
2521eb3f98d2SBarry Smith   Level: developer
2522eb3f98d2SBarry Smith 
2523bb7acecfSBarry Smith   Note:
2524bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2525bb7acecfSBarry Smith 
25261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2527eb3f98d2SBarry Smith @*/
2528d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2529d71ae5a4SJacob Faibussowitsch {
2530eb3f98d2SBarry Smith   PetscFunctionBegin;
2531eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2532eb3f98d2SBarry Smith   *level = dm->levelup;
25333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2534eb3f98d2SBarry Smith }
2535eb3f98d2SBarry Smith 
2536fef3a512SBarry Smith /*@
2537bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2538fef3a512SBarry Smith 
2539fef3a512SBarry Smith   Not Collective
2540fef3a512SBarry Smith 
2541d8d19677SJose E. Roman   Input Parameters:
2542bb7acecfSBarry Smith + dm    - the `DM` object
2543fef3a512SBarry Smith - level - number of refinements
2544fef3a512SBarry Smith 
2545fef3a512SBarry Smith   Level: advanced
2546fef3a512SBarry Smith 
254795452b02SPatrick Sanan   Notes:
2548bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2549fef3a512SBarry Smith 
2550bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2551bb7acecfSBarry Smith 
25521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2553fef3a512SBarry Smith @*/
2554d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2555d71ae5a4SJacob Faibussowitsch {
2556fef3a512SBarry Smith   PetscFunctionBegin;
2557fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2558fef3a512SBarry Smith   dm->levelup = level;
25593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2560fef3a512SBarry Smith }
2561fef3a512SBarry Smith 
2562d410b0cfSMatthew G. Knepley /*@
2563bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2564d410b0cfSMatthew G. Knepley 
256520f4b53cSBarry Smith   Collective
2566d410b0cfSMatthew G. Knepley 
2567f1a722f8SMatthew G. Knepley   Input Parameters:
2568bb7acecfSBarry Smith + dm     - the `DM` object
2569d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2570d410b0cfSMatthew G. Knepley 
2571d410b0cfSMatthew G. Knepley   Output Parameter:
257220f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2573d410b0cfSMatthew G. Knepley 
2574d410b0cfSMatthew G. Knepley   Level: developer
2575d410b0cfSMatthew G. Knepley 
257620f4b53cSBarry Smith   Note:
257720f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
257820f4b53cSBarry Smith 
25791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2580d410b0cfSMatthew G. Knepley @*/
2581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2582d71ae5a4SJacob Faibussowitsch {
2583d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2584d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2585dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2586d410b0cfSMatthew G. Knepley   if (*dme) {
2587d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25889566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2589d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25909566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2591d410b0cfSMatthew G. Knepley   }
25923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2593d410b0cfSMatthew G. Knepley }
2594d410b0cfSMatthew G. Knepley 
2595d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2596d71ae5a4SJacob Faibussowitsch {
2597ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2598ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25994f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2600ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
26013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2602ca3d3a14SMatthew G. Knepley }
2603ca3d3a14SMatthew G. Knepley 
2604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2605d71ae5a4SJacob Faibussowitsch {
2606ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2607ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26084f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2609ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
26103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2611ca3d3a14SMatthew G. Knepley }
2612ca3d3a14SMatthew G. Knepley 
2613ca3d3a14SMatthew G. Knepley /*@
2614bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2615ca3d3a14SMatthew G. Knepley 
2616ca3d3a14SMatthew G. Knepley   Input Parameter:
261720f4b53cSBarry Smith . dm - The `DM`
2618ca3d3a14SMatthew G. Knepley 
2619ca3d3a14SMatthew G. Knepley   Output Parameter:
262020f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2621ca3d3a14SMatthew G. Knepley 
2622ca3d3a14SMatthew G. Knepley   Level: developer
2623ca3d3a14SMatthew G. Knepley 
26241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2625ca3d3a14SMatthew G. Knepley @*/
2626d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2627d71ae5a4SJacob Faibussowitsch {
2628ca3d3a14SMatthew G. Knepley   Vec tv;
2629ca3d3a14SMatthew G. Knepley 
2630ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2631ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26324f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26339566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2634ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2636ca3d3a14SMatthew G. Knepley }
2637ca3d3a14SMatthew G. Knepley 
2638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2639d71ae5a4SJacob Faibussowitsch {
2640ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2641ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2642ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2643ca3d3a14SMatthew G. Knepley 
2644ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26459566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26469566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26479566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26499566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26509566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2653ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2655ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2656ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2657ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2659ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26609566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26619566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2662ca3d3a14SMatthew G. Knepley     }
2663ca3d3a14SMatthew G. Knepley   }
26649566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26659566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26669566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2667ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2668ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26699566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2670ca3d3a14SMatthew G. Knepley       if (dof) {
2671ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2672ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2673ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2674ca3d3a14SMatthew G. Knepley 
2675ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26769566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26779566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26789566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2679ca3d3a14SMatthew G. Knepley       }
2680ca3d3a14SMatthew G. Knepley     }
2681ca3d3a14SMatthew G. Knepley   }
26829566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2684ca3d3a14SMatthew G. Knepley }
2685ca3d3a14SMatthew G. Knepley 
2686d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2687d71ae5a4SJacob Faibussowitsch {
2688ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2689ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2690ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2691ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2692ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2693ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2694ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26959566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2697ca3d3a14SMatthew G. Knepley }
2698ca3d3a14SMatthew G. Knepley 
2699bb9467b5SJed Brown /*@C
2700bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2701baf369e7SPeter Brune 
270220f4b53cSBarry Smith   Logically Collective
2703baf369e7SPeter Brune 
27044165533cSJose E. Roman   Input Parameters:
2705bb7acecfSBarry Smith + dm        - the `DM`
2706bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2707bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
270820f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2709baf369e7SPeter Brune 
271020f4b53cSBarry Smith   Calling sequence of `beginhook`:
2711a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2712baf369e7SPeter Brune . g    - global vector
2713baf369e7SPeter Brune . mode - mode
2714baf369e7SPeter Brune . l    - local vector
2715baf369e7SPeter Brune - ctx  - optional user-defined function context
2716baf369e7SPeter Brune 
271720f4b53cSBarry Smith   Calling sequence of `endhook`:
2718a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2719a4e35b19SJacob Faibussowitsch . g    - global vector
2720a4e35b19SJacob Faibussowitsch . mode - mode
2721a4e35b19SJacob Faibussowitsch . l    - local vector
2722baf369e7SPeter Brune - ctx  - optional user-defined function context
2723baf369e7SPeter Brune 
2724baf369e7SPeter Brune   Level: advanced
2725baf369e7SPeter Brune 
2726bb7acecfSBarry Smith   Note:
2727bb7acecfSBarry 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.
2728bb7acecfSBarry Smith 
27291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2730baf369e7SPeter Brune @*/
2731a4e35b19SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), PetscErrorCode (*endhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), void *ctx)
2732d71ae5a4SJacob Faibussowitsch {
2733baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2734baf369e7SPeter Brune 
2735baf369e7SPeter Brune   PetscFunctionBegin;
2736baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2737baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27389566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2739baf369e7SPeter Brune   link->beginhook = beginhook;
2740baf369e7SPeter Brune   link->endhook   = endhook;
2741baf369e7SPeter Brune   link->ctx       = ctx;
27420298fd71SBarry Smith   link->next      = NULL;
2743baf369e7SPeter Brune   *p              = link;
27443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2745baf369e7SPeter Brune }
2746baf369e7SPeter Brune 
2747d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2748d71ae5a4SJacob Faibussowitsch {
27494c274da1SToby Isaac   Mat          cMat;
275079769bd5SJed Brown   Vec          cVec, cBias;
27514c274da1SToby Isaac   PetscSection section, cSec;
27524c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27534c274da1SToby Isaac 
27544c274da1SToby Isaac   PetscFunctionBegin;
2755a4e35b19SJacob Faibussowitsch   (void)g;
2756a4e35b19SJacob Faibussowitsch   (void)ctx;
27574c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27589566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27594c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27605db9a05bSToby Isaac     PetscInt nRows;
27615db9a05bSToby Isaac 
27629566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27633ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27649566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27659566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27669566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27679566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27694c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27714c274da1SToby Isaac       if (dof) {
27724c274da1SToby Isaac         PetscScalar *vals;
27739566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27749566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27754c274da1SToby Isaac       }
27764c274da1SToby Isaac     }
27779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27784c274da1SToby Isaac   }
27793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27804c274da1SToby Isaac }
27814c274da1SToby Isaac 
278247c6ae99SBarry Smith /*@
278301729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
278401729b5cSPatrick Sanan 
278520f4b53cSBarry Smith   Neighbor-wise Collective
278601729b5cSPatrick Sanan 
278701729b5cSPatrick Sanan   Input Parameters:
2788bb7acecfSBarry Smith + dm   - the `DM` object
278901729b5cSPatrick Sanan . g    - the global vector
2790bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
279101729b5cSPatrick Sanan - l    - the local vector
279201729b5cSPatrick Sanan 
279320f4b53cSBarry Smith   Level: beginner
279420f4b53cSBarry Smith 
279501729b5cSPatrick Sanan   Notes:
2796bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2797bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2798bb7acecfSBarry Smith 
2799bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
280001729b5cSPatrick Sanan 
28011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
280260225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2803bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
280401729b5cSPatrick Sanan @*/
2805d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2806d71ae5a4SJacob Faibussowitsch {
280701729b5cSPatrick Sanan   PetscFunctionBegin;
28089566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
28099566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
28103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
281101729b5cSPatrick Sanan }
281201729b5cSPatrick Sanan 
281301729b5cSPatrick Sanan /*@
281447c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
281547c6ae99SBarry Smith 
281620f4b53cSBarry Smith   Neighbor-wise Collective
281747c6ae99SBarry Smith 
281847c6ae99SBarry Smith   Input Parameters:
2819bb7acecfSBarry Smith + dm   - the `DM` object
282047c6ae99SBarry Smith . g    - the global vector
2821bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
282247c6ae99SBarry Smith - l    - the local vector
282347c6ae99SBarry Smith 
282401729b5cSPatrick Sanan   Level: intermediate
282547c6ae99SBarry Smith 
2826bb7acecfSBarry Smith   Notes:
2827bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2828bb7acecfSBarry Smith 
2829bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2830bb7acecfSBarry Smith 
2831bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2832bb7acecfSBarry Smith 
2833bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2834bb7acecfSBarry Smith 
283560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
283647c6ae99SBarry Smith @*/
2837d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2838d71ae5a4SJacob Faibussowitsch {
28397128ae9fSMatthew G Knepley   PetscSF                 sf;
2840baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
284147c6ae99SBarry Smith 
284247c6ae99SBarry Smith   PetscFunctionBegin;
2843171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2844baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28451baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2846baf369e7SPeter Brune   }
28479566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28487128ae9fSMatthew G Knepley   if (sf) {
2849ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2850ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2851d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28527128ae9fSMatthew G Knepley 
28537a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28549566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28559566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28569566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28579566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28589566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28597128ae9fSMatthew G Knepley   } else {
2860fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28617128ae9fSMatthew G Knepley   }
28623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
286347c6ae99SBarry Smith }
286447c6ae99SBarry Smith 
286547c6ae99SBarry Smith /*@
286647c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
286747c6ae99SBarry Smith 
286820f4b53cSBarry Smith   Neighbor-wise Collective
286947c6ae99SBarry Smith 
287047c6ae99SBarry Smith   Input Parameters:
2871bb7acecfSBarry Smith + dm   - the `DM` object
287247c6ae99SBarry Smith . g    - the global vector
2873bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
287447c6ae99SBarry Smith - l    - the local vector
287547c6ae99SBarry Smith 
287601729b5cSPatrick Sanan   Level: intermediate
287747c6ae99SBarry Smith 
2878bb7acecfSBarry Smith   Note:
2879bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2880bb7acecfSBarry Smith 
288160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
288247c6ae99SBarry Smith @*/
2883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2884d71ae5a4SJacob Faibussowitsch {
28857128ae9fSMatthew G Knepley   PetscSF                 sf;
2886ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2887ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2888ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2889baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2890d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
289147c6ae99SBarry Smith 
289247c6ae99SBarry Smith   PetscFunctionBegin;
2893171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28949566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28959566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28967128ae9fSMatthew G Knepley   if (sf) {
28977a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28987128ae9fSMatthew G Knepley 
28999566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
29009566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
29019566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
29029566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
29039566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
29049566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
29057128ae9fSMatthew G Knepley   } else {
2906fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
29077128ae9fSMatthew G Knepley   }
29089566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2909baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
29109566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2911baf369e7SPeter Brune   }
29123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
291347c6ae99SBarry Smith }
291447c6ae99SBarry Smith 
2915d4d07f1eSToby Isaac /*@C
2916d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2917d4d07f1eSToby Isaac 
291820f4b53cSBarry Smith   Logically Collective
2919d4d07f1eSToby Isaac 
29204165533cSJose E. Roman   Input Parameters:
2921bb7acecfSBarry Smith + dm        - the `DM`
2922bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2923bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
292420f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2925d4d07f1eSToby Isaac 
292620f4b53cSBarry Smith   Calling sequence of `beginhook`:
2927a4e35b19SJacob Faibussowitsch + global - global `DM`
2928d4d07f1eSToby Isaac . l      - local vector
2929d4d07f1eSToby Isaac . mode   - mode
2930d4d07f1eSToby Isaac . g      - global vector
2931d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2932d4d07f1eSToby Isaac 
293320f4b53cSBarry Smith   Calling sequence of `endhook`:
2934bb7acecfSBarry Smith + global - global `DM`
2935d4d07f1eSToby Isaac . l      - local vector
2936d4d07f1eSToby Isaac . mode   - mode
2937d4d07f1eSToby Isaac . g      - global vector
2938d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2939d4d07f1eSToby Isaac 
2940d4d07f1eSToby Isaac   Level: advanced
2941d4d07f1eSToby Isaac 
29421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2943d4d07f1eSToby Isaac @*/
2944a4e35b19SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), PetscErrorCode (*endhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), void *ctx)
2945d71ae5a4SJacob Faibussowitsch {
2946d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2947d4d07f1eSToby Isaac 
2948d4d07f1eSToby Isaac   PetscFunctionBegin;
2949d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2950d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29519566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2952d4d07f1eSToby Isaac   link->beginhook = beginhook;
2953d4d07f1eSToby Isaac   link->endhook   = endhook;
2954d4d07f1eSToby Isaac   link->ctx       = ctx;
2955d4d07f1eSToby Isaac   link->next      = NULL;
2956d4d07f1eSToby Isaac   *p              = link;
29573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2958d4d07f1eSToby Isaac }
2959d4d07f1eSToby Isaac 
2960d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2961d71ae5a4SJacob Faibussowitsch {
29624c274da1SToby Isaac   PetscFunctionBegin;
2963a4e35b19SJacob Faibussowitsch   (void)g;
2964a4e35b19SJacob Faibussowitsch   (void)ctx;
29654c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29662b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29672b6b7d09SToby Isaac     Mat          cMat;
29682b6b7d09SToby Isaac     Vec          cVec;
29695db9a05bSToby Isaac     PetscInt     nRows;
29702b6b7d09SToby Isaac     PetscSection section, cSec;
29712b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29722b6b7d09SToby Isaac 
29732b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29742b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29755db9a05bSToby Isaac 
29769566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29773ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29789566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29799566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29814c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29834c274da1SToby Isaac       if (dof) {
29844c274da1SToby Isaac         PetscInt     d;
29854c274da1SToby Isaac         PetscScalar *vals;
29869566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29879566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29884c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29894c274da1SToby Isaac          * we just extracted */
2990ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29914c274da1SToby Isaac       }
29924c274da1SToby Isaac     }
29939566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29949566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29954c274da1SToby Isaac   }
29963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29974c274da1SToby Isaac }
299801729b5cSPatrick Sanan /*@
299901729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
300001729b5cSPatrick Sanan 
300120f4b53cSBarry Smith   Neighbor-wise Collective
300201729b5cSPatrick Sanan 
300301729b5cSPatrick Sanan   Input Parameters:
3004bb7acecfSBarry Smith + dm   - the `DM` object
300501729b5cSPatrick Sanan . l    - the local vector
3006bb7acecfSBarry 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.
300701729b5cSPatrick Sanan - g    - the global vector
300801729b5cSPatrick Sanan 
300920f4b53cSBarry Smith   Level: beginner
301020f4b53cSBarry Smith 
301101729b5cSPatrick Sanan   Notes:
301201729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3013bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
301401729b5cSPatrick Sanan 
3015bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3016bb7acecfSBarry Smith 
3017bb7acecfSBarry 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.
3018bb7acecfSBarry Smith 
3019bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
302001729b5cSPatrick Sanan 
30211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
302201729b5cSPatrick Sanan @*/
3023d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3024d71ae5a4SJacob Faibussowitsch {
302501729b5cSPatrick Sanan   PetscFunctionBegin;
30269566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30279566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
302901729b5cSPatrick Sanan }
30304c274da1SToby Isaac 
303147c6ae99SBarry Smith /*@
303201729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30339a42bb27SBarry Smith 
303420f4b53cSBarry Smith   Neighbor-wise Collective
30359a42bb27SBarry Smith 
30369a42bb27SBarry Smith   Input Parameters:
3037bb7acecfSBarry Smith + dm   - the `DM` object
3038f6813fd5SJed Brown . l    - the local vector
3039aa624791SPierre 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.
30401eb28f2eSBarry Smith - g    - the global vector
30419a42bb27SBarry Smith 
304220f4b53cSBarry Smith   Level: intermediate
304320f4b53cSBarry Smith 
304495452b02SPatrick Sanan   Notes:
3045bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3046bb7acecfSBarry Smith 
3047bb7acecfSBarry 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.
3048bb7acecfSBarry Smith 
3049bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3050bb7acecfSBarry Smith 
3051bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3052bb7acecfSBarry Smith 
3053bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30549a42bb27SBarry Smith 
30551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30569a42bb27SBarry Smith @*/
3057d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3058d71ae5a4SJacob Faibussowitsch {
30597128ae9fSMatthew G Knepley   PetscSF                 sf;
306084330215SMatthew G. Knepley   PetscSection            s, gs;
3061d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3062ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3063ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3064ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3065fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3066d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30679a42bb27SBarry Smith 
30689a42bb27SBarry Smith   PetscFunctionBegin;
3069171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3070d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30711baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3072d4d07f1eSToby Isaac   }
30739566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30749566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30759566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30767128ae9fSMatthew G Knepley   switch (mode) {
30777128ae9fSMatthew G Knepley   case INSERT_VALUES:
30787128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3079d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3080d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3081d71ae5a4SJacob Faibussowitsch     break;
30827128ae9fSMatthew G Knepley   case ADD_VALUES:
30837128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3084d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3085d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3086d71ae5a4SJacob Faibussowitsch     break;
3087d71ae5a4SJacob Faibussowitsch   default:
3088d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30897128ae9fSMatthew G Knepley   }
3090ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30919566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3092ca3d3a14SMatthew G. Knepley     if (transform) {
30939566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30949566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30959566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30969566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3097fa88e482SJed Brown     } else if (isInsert) {
30989566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3099fa88e482SJed Brown     } else {
31009566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3101fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3102ca3d3a14SMatthew G. Knepley     }
3103fa88e482SJed Brown     if (s && isInsert) {
31049566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3105fa88e482SJed Brown     } else {
31069566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3107fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3108fa88e482SJed Brown     }
3109ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
31109566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
311184330215SMatthew G. Knepley     } else if (s && isInsert) {
311284330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
311384330215SMatthew G. Knepley 
31149566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31169566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
311784330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3118b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
311984330215SMatthew G. Knepley 
31209566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31219566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31229566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31239566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31249566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31259566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3126b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
312703442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31287a8be351SBarry 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);
3129b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3130b3b16f48SMatthew G. Knepley         if (!gcdof) {
313184330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3132b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3133b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
313484330215SMatthew G. Knepley           const PetscInt *cdofs;
313584330215SMatthew G. Knepley           PetscInt        cind = 0;
313684330215SMatthew G. Knepley 
31379566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3138b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31399371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31409371c9d4SSatish Balay               ++cind;
31419371c9d4SSatish Balay               continue;
31429371c9d4SSatish Balay             }
3143b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
314484330215SMatthew G. Knepley           }
31457a8be351SBarry 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);
314684330215SMatthew G. Knepley       }
3147ca3d3a14SMatthew G. Knepley     }
3148fa88e482SJed Brown     if (g_inplace) {
31499566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3150fa88e482SJed Brown     } else {
31519566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3152fa88e482SJed Brown     }
3153ca3d3a14SMatthew G. Knepley     if (transform) {
31549566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31559566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3156fa88e482SJed Brown     } else if (l_inplace) {
31579566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3158ca3d3a14SMatthew G. Knepley     } else {
31599566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3160ca3d3a14SMatthew G. Knepley     }
31617128ae9fSMatthew G Knepley   } else {
3162fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31637128ae9fSMatthew G Knepley   }
31643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31659a42bb27SBarry Smith }
31669a42bb27SBarry Smith 
31679a42bb27SBarry Smith /*@
31689a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
316947c6ae99SBarry Smith 
317020f4b53cSBarry Smith   Neighbor-wise Collective
317147c6ae99SBarry Smith 
317247c6ae99SBarry Smith   Input Parameters:
3173bb7acecfSBarry Smith + dm   - the `DM` object
3174f6813fd5SJed Brown . l    - the local vector
3175bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3176f6813fd5SJed Brown - g    - the global vector
317747c6ae99SBarry Smith 
317801729b5cSPatrick Sanan   Level: intermediate
317947c6ae99SBarry Smith 
3180bb7acecfSBarry Smith   Note:
3181bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3182bb7acecfSBarry Smith 
318360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
318447c6ae99SBarry Smith @*/
3185d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3186d71ae5a4SJacob Faibussowitsch {
31877128ae9fSMatthew G Knepley   PetscSF                 sf;
318884330215SMatthew G. Knepley   PetscSection            s;
3189d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3190ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
319147c6ae99SBarry Smith 
319247c6ae99SBarry Smith   PetscFunctionBegin;
3193171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31949566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31959566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31967128ae9fSMatthew G Knepley   switch (mode) {
31977128ae9fSMatthew G Knepley   case INSERT_VALUES:
3198d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3199d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3200d71ae5a4SJacob Faibussowitsch     break;
32017128ae9fSMatthew G Knepley   case ADD_VALUES:
3202d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3203d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3204d71ae5a4SJacob Faibussowitsch     break;
3205d71ae5a4SJacob Faibussowitsch   default:
3206d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
32077128ae9fSMatthew G Knepley   }
320884330215SMatthew G. Knepley   if (sf && !isInsert) {
3209ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3210ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3211ca3d3a14SMatthew G. Knepley     Vec                tmpl;
321284330215SMatthew G. Knepley 
32139566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3214ca3d3a14SMatthew G. Knepley     if (transform) {
32159566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32169566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3217ca3d3a14SMatthew G. Knepley     } else {
32189566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3219ca3d3a14SMatthew G. Knepley     }
32209566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32219566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3222ca3d3a14SMatthew G. Knepley     if (transform) {
32239566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32249566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3225ca3d3a14SMatthew G. Knepley     } else {
32269566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3227ca3d3a14SMatthew G. Knepley     }
32289566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
322984330215SMatthew G. Knepley   } else if (s && isInsert) {
32307128ae9fSMatthew G Knepley   } else {
3231fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32327128ae9fSMatthew G Knepley   }
3233d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32349566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3235d4d07f1eSToby Isaac   }
32363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
323747c6ae99SBarry Smith }
323847c6ae99SBarry Smith 
3239f089877aSRichard Tran Mills /*@
3240a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3241a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3242a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3243f089877aSRichard Tran Mills 
324420f4b53cSBarry Smith   Neighbor-wise Collective
3245f089877aSRichard Tran Mills 
3246f089877aSRichard Tran Mills   Input Parameters:
3247bb7acecfSBarry Smith + dm   - the `DM` object
3248bc0a1609SRichard Tran Mills . g    - the original local vector
3249bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3250f089877aSRichard Tran Mills 
3251bc0a1609SRichard Tran Mills   Output Parameter:
3252bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3253f089877aSRichard Tran Mills 
3254f089877aSRichard Tran Mills   Level: intermediate
3255f089877aSRichard Tran Mills 
325673ff1848SBarry Smith   Note:
3257a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3258a4e35b19SJacob Faibussowitsch 
325960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3260f089877aSRichard Tran Mills @*/
3261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3262d71ae5a4SJacob Faibussowitsch {
3263f089877aSRichard Tran Mills   PetscFunctionBegin;
3264f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32659f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32669f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32679f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3269f089877aSRichard Tran Mills }
3270f089877aSRichard Tran Mills 
3271f089877aSRichard Tran Mills /*@
3272bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3273bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3274f089877aSRichard Tran Mills 
327520f4b53cSBarry Smith   Neighbor-wise Collective
3276f089877aSRichard Tran Mills 
3277f089877aSRichard Tran Mills   Input Parameters:
327860225df5SJacob Faibussowitsch + dm   - the `DM` object
3279bc0a1609SRichard Tran Mills . g    - the original local vector
3280bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3281f089877aSRichard Tran Mills 
3282bc0a1609SRichard Tran Mills   Output Parameter:
3283bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3284f089877aSRichard Tran Mills 
3285f089877aSRichard Tran Mills   Level: intermediate
3286f089877aSRichard Tran Mills 
328760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3288f089877aSRichard Tran Mills @*/
3289d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3290d71ae5a4SJacob Faibussowitsch {
3291f089877aSRichard Tran Mills   PetscFunctionBegin;
3292f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32939f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32949f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32959f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3297f089877aSRichard Tran Mills }
3298f089877aSRichard Tran Mills 
329947c6ae99SBarry Smith /*@
3300bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
330147c6ae99SBarry Smith 
330220f4b53cSBarry Smith   Collective
330347c6ae99SBarry Smith 
3304d8d19677SJose E. Roman   Input Parameters:
3305bb7acecfSBarry Smith + dm   - the `DM` object
330620f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
330747c6ae99SBarry Smith 
330847c6ae99SBarry Smith   Output Parameter:
3309bb7acecfSBarry Smith . dmc - the coarsened `DM`
331047c6ae99SBarry Smith 
331147c6ae99SBarry Smith   Level: developer
331247c6ae99SBarry Smith 
331373ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
331473ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
331547c6ae99SBarry Smith @*/
3316d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3317d71ae5a4SJacob Faibussowitsch {
3318b17ce1afSJed Brown   DMCoarsenHookLink link;
331947c6ae99SBarry Smith 
332047c6ae99SBarry Smith   PetscFunctionBegin;
3321171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3323dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3324b9d85ea2SLisandro Dalcin   if (*dmc) {
3325a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33269566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
332743842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33289566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3329644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33300598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3331656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33329566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3333b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33349566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3335b17ce1afSJed Brown     }
3336b9d85ea2SLisandro Dalcin   }
33379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33387a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3340b17ce1afSJed Brown }
3341b17ce1afSJed Brown 
3342bb9467b5SJed Brown /*@C
3343b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3344b17ce1afSJed Brown 
334520f4b53cSBarry Smith   Logically Collective; No Fortran Support
3346b17ce1afSJed Brown 
33474165533cSJose E. Roman   Input Parameters:
3348bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3349b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3350bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
335120f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3352b17ce1afSJed Brown 
335320f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3354bb7acecfSBarry Smith + fine   - fine level `DM`
3355bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3356b17ce1afSJed Brown - ctx    - optional user-defined function context
3357b17ce1afSJed Brown 
335820f4b53cSBarry Smith   Calling sequence of `restricthook`:
3359bb7acecfSBarry Smith + fine      - fine level `DM`
3360bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3361c833c3b5SJed Brown . rscale    - scaling vector for restriction
3362c833c3b5SJed Brown . inject    - matrix restricting by injection
3363b17ce1afSJed Brown . coarse    - coarse level DM to update
3364b17ce1afSJed Brown - ctx       - optional user-defined function context
3365b17ce1afSJed Brown 
3366b17ce1afSJed Brown   Level: advanced
3367b17ce1afSJed Brown 
3368b17ce1afSJed Brown   Notes:
3369bb7acecfSBarry 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`.
3370b17ce1afSJed Brown 
3371b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3372b17ce1afSJed Brown 
3373b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3374bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3375b17ce1afSJed Brown 
3376bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3377bb7acecfSBarry Smith 
33781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3379b17ce1afSJed Brown @*/
3380a4e35b19SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM fine, DM coarse, void *ctx), PetscErrorCode (*restricthook)(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx), void *ctx)
3381d71ae5a4SJacob Faibussowitsch {
3382b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3383b17ce1afSJed Brown 
3384b17ce1afSJed Brown   PetscFunctionBegin;
3385b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33861e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33873ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33881e3d8eccSJed Brown   }
33899566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3390b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3391b17ce1afSJed Brown   link->restricthook = restricthook;
3392b17ce1afSJed Brown   link->ctx          = ctx;
33930298fd71SBarry Smith   link->next         = NULL;
3394b17ce1afSJed Brown   *p                 = link;
33953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3396b17ce1afSJed Brown }
3397b17ce1afSJed Brown 
3398dc822a44SJed Brown /*@C
3399bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3400dc822a44SJed Brown 
340120f4b53cSBarry Smith   Logically Collective; No Fortran Support
3402dc822a44SJed Brown 
34034165533cSJose E. Roman   Input Parameters:
3404bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3405dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3406bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
340720f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3408dc822a44SJed Brown 
3409dc822a44SJed Brown   Level: advanced
3410dc822a44SJed Brown 
341173ff1848SBarry Smith   Notes:
341273ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
341373ff1848SBarry Smith 
341473ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3415dc822a44SJed Brown 
34161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3417dc822a44SJed Brown @*/
3418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3419d71ae5a4SJacob Faibussowitsch {
3420dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3421dc822a44SJed Brown 
3422dc822a44SJed Brown   PetscFunctionBegin;
3423dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3424dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3425dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3426dc822a44SJed Brown       link = *p;
3427dc822a44SJed Brown       *p   = link->next;
34289566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3429dc822a44SJed Brown       break;
3430dc822a44SJed Brown     }
3431dc822a44SJed Brown   }
34323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3433dc822a44SJed Brown }
3434dc822a44SJed Brown 
3435b17ce1afSJed Brown /*@
3436bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3437b17ce1afSJed Brown 
3438b17ce1afSJed Brown   Collective if any hooks are
3439b17ce1afSJed Brown 
34404165533cSJose E. Roman   Input Parameters:
3441bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3442bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3443e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3444bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
344520f4b53cSBarry Smith - coarse  - coarser `DM` to update
3446b17ce1afSJed Brown 
3447b17ce1afSJed Brown   Level: developer
3448b17ce1afSJed Brown 
344973ff1848SBarry Smith   Developer Note:
3450bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3451bb7acecfSBarry Smith 
34521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3453b17ce1afSJed Brown @*/
3454d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3455d71ae5a4SJacob Faibussowitsch {
3456b17ce1afSJed Brown   DMCoarsenHookLink link;
3457b17ce1afSJed Brown 
3458b17ce1afSJed Brown   PetscFunctionBegin;
3459b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34601baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3461b17ce1afSJed Brown   }
34623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
346347c6ae99SBarry Smith }
346447c6ae99SBarry Smith 
3465bb9467b5SJed Brown /*@C
346673ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34675dbd56e3SPeter Brune 
346820f4b53cSBarry Smith   Logically Collective; No Fortran Support
34695dbd56e3SPeter Brune 
34704165533cSJose E. Roman   Input Parameters:
3471bb7acecfSBarry Smith + global       - global `DM`
3472bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34735dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
347420f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34755dbd56e3SPeter Brune 
347620f4b53cSBarry Smith   Calling sequence of `ddhook`:
3477bb7acecfSBarry Smith + global - global `DM`
347873ff1848SBarry Smith . block  - subdomain `DM`
3479ec4806b8SPeter Brune - ctx    - optional user-defined function context
3480ec4806b8SPeter Brune 
348120f4b53cSBarry Smith   Calling sequence of `restricthook`:
3482bb7acecfSBarry Smith + global - global `DM`
348373ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
348473ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
348573ff1848SBarry Smith . block  - subdomain `DM`
34865dbd56e3SPeter Brune - ctx    - optional user-defined function context
34875dbd56e3SPeter Brune 
34885dbd56e3SPeter Brune   Level: advanced
34895dbd56e3SPeter Brune 
34905dbd56e3SPeter Brune   Notes:
349173ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34925dbd56e3SPeter Brune 
34935dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34945dbd56e3SPeter Brune 
34955dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3496bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34975dbd56e3SPeter Brune 
349873ff1848SBarry Smith   Developer Note:
349973ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
350073ff1848SBarry Smith 
350173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
35025dbd56e3SPeter Brune @*/
3503a4e35b19SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM global, DM block, void *ctx), PetscErrorCode (*restricthook)(DM global, VecScatter out, VecScatter in, DM block, void *ctx), void *ctx)
3504d71ae5a4SJacob Faibussowitsch {
3505be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
35065dbd56e3SPeter Brune 
35075dbd56e3SPeter Brune   PetscFunctionBegin;
35085dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3509b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35103ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3511b3a6b972SJed Brown   }
35129566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35135dbd56e3SPeter Brune   link->restricthook = restricthook;
3514be081cd6SPeter Brune   link->ddhook       = ddhook;
35155dbd56e3SPeter Brune   link->ctx          = ctx;
35160298fd71SBarry Smith   link->next         = NULL;
35175dbd56e3SPeter Brune   *p                 = link;
35183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35195dbd56e3SPeter Brune }
35205dbd56e3SPeter Brune 
3521b3a6b972SJed Brown /*@C
352273ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3523b3a6b972SJed Brown 
352420f4b53cSBarry Smith   Logically Collective; No Fortran Support
3525b3a6b972SJed Brown 
35264165533cSJose E. Roman   Input Parameters:
3527bb7acecfSBarry Smith + global       - global `DM`
3528bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3529b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
353020f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3531b3a6b972SJed Brown 
3532b3a6b972SJed Brown   Level: advanced
3533b3a6b972SJed Brown 
353473ff1848SBarry Smith   Note:
353573ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
353673ff1848SBarry Smith 
353773ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
353873ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3539b3a6b972SJed Brown @*/
3540d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3541d71ae5a4SJacob Faibussowitsch {
3542b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3543b3a6b972SJed Brown 
3544b3a6b972SJed Brown   PetscFunctionBegin;
3545b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3546b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3547b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3548b3a6b972SJed Brown       link = *p;
3549b3a6b972SJed Brown       *p   = link->next;
35509566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3551b3a6b972SJed Brown       break;
3552b3a6b972SJed Brown     }
3553b3a6b972SJed Brown   }
35543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3555b3a6b972SJed Brown }
3556b3a6b972SJed Brown 
35575dbd56e3SPeter Brune /*@
355873ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35595dbd56e3SPeter Brune 
35605dbd56e3SPeter Brune   Collective if any hooks are
35615dbd56e3SPeter Brune 
35624165533cSJose E. Roman   Input Parameters:
3563a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3564a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3565a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3566a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35675dbd56e3SPeter Brune 
35685dbd56e3SPeter Brune   Level: developer
35695dbd56e3SPeter Brune 
357073ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35715dbd56e3SPeter Brune @*/
3572d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3573d71ae5a4SJacob Faibussowitsch {
3574be081cd6SPeter Brune   DMSubDomainHookLink link;
35755dbd56e3SPeter Brune 
35765dbd56e3SPeter Brune   PetscFunctionBegin;
3577be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35781baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35795dbd56e3SPeter Brune   }
35803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35815dbd56e3SPeter Brune }
35825dbd56e3SPeter Brune 
35835fe1f584SPeter Brune /*@
3584bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35855fe1f584SPeter Brune 
35865fe1f584SPeter Brune   Not Collective
35875fe1f584SPeter Brune 
35885fe1f584SPeter Brune   Input Parameter:
3589bb7acecfSBarry Smith . dm - the `DM` object
35905fe1f584SPeter Brune 
35915fe1f584SPeter Brune   Output Parameter:
35926a7d9d85SPeter Brune . level - number of coarsenings
35935fe1f584SPeter Brune 
35945fe1f584SPeter Brune   Level: developer
35955fe1f584SPeter Brune 
35961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35975fe1f584SPeter Brune @*/
3598d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3599d71ae5a4SJacob Faibussowitsch {
36005fe1f584SPeter Brune   PetscFunctionBegin;
36015fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36024f572ea9SToby Isaac   PetscAssertPointer(level, 2);
36035fe1f584SPeter Brune   *level = dm->leveldown;
36043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36055fe1f584SPeter Brune }
36065fe1f584SPeter Brune 
36079a64c4a8SMatthew G. Knepley /*@
3608bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36099a64c4a8SMatthew G. Knepley 
361020f4b53cSBarry Smith   Collective
36119a64c4a8SMatthew G. Knepley 
36129a64c4a8SMatthew G. Knepley   Input Parameters:
3613bb7acecfSBarry Smith + dm    - the `DM` object
36149a64c4a8SMatthew G. Knepley - level - number of coarsenings
36159a64c4a8SMatthew G. Knepley 
36169a64c4a8SMatthew G. Knepley   Level: developer
36179a64c4a8SMatthew G. Knepley 
3618bb7acecfSBarry Smith   Note:
3619bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3620bb7acecfSBarry Smith 
362142747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36229a64c4a8SMatthew G. Knepley @*/
3623d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3624d71ae5a4SJacob Faibussowitsch {
36259a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36269a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36279a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36299a64c4a8SMatthew G. Knepley }
36309a64c4a8SMatthew G. Knepley 
3631cc4c1da9SBarry Smith /*@
3632bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
363347c6ae99SBarry Smith 
363420f4b53cSBarry Smith   Collective
363547c6ae99SBarry Smith 
3636d8d19677SJose E. Roman   Input Parameters:
3637bb7acecfSBarry Smith + dm      - the `DM` object
363847c6ae99SBarry Smith - nlevels - the number of levels of refinement
363947c6ae99SBarry Smith 
364047c6ae99SBarry Smith   Output Parameter:
3641bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
364247c6ae99SBarry Smith 
364347c6ae99SBarry Smith   Level: developer
364447c6ae99SBarry Smith 
36451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
364647c6ae99SBarry Smith @*/
3647d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3648d71ae5a4SJacob Faibussowitsch {
364947c6ae99SBarry Smith   PetscFunctionBegin;
3650171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36517a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36523ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36534f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3654213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
365547c6ae99SBarry Smith     PetscInt i;
365647c6ae99SBarry Smith 
36579566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
365848a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3659213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
366147c6ae99SBarry Smith }
366247c6ae99SBarry Smith 
3663cc4c1da9SBarry Smith /*@
3664bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
366547c6ae99SBarry Smith 
366620f4b53cSBarry Smith   Collective
366747c6ae99SBarry Smith 
3668d8d19677SJose E. Roman   Input Parameters:
3669bb7acecfSBarry Smith + dm      - the `DM` object
367047c6ae99SBarry Smith - nlevels - the number of levels of coarsening
367147c6ae99SBarry Smith 
367247c6ae99SBarry Smith   Output Parameter:
3673bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
367447c6ae99SBarry Smith 
367547c6ae99SBarry Smith   Level: developer
367647c6ae99SBarry Smith 
36771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
367847c6ae99SBarry Smith @*/
3679d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3680d71ae5a4SJacob Faibussowitsch {
368147c6ae99SBarry Smith   PetscFunctionBegin;
3682171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36837a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36843ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36854f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3686213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
368747c6ae99SBarry Smith     PetscInt i;
368847c6ae99SBarry Smith 
36899566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
369048a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3691213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
369347c6ae99SBarry Smith }
369447c6ae99SBarry Smith 
36951a266240SBarry Smith /*@C
3696bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36971a266240SBarry Smith 
3698bb7acecfSBarry Smith   Logically Collective if the function is collective
36991a266240SBarry Smith 
37001a266240SBarry Smith   Input Parameters:
3701bb7acecfSBarry Smith + dm      - the `DM` object
370249abdd8aSBarry Smith - destroy - the destroy function, see `PetscCtxDestroyFn` for the calling sequence
37031a266240SBarry Smith 
37041a266240SBarry Smith   Level: intermediate
37051a266240SBarry Smith 
370649abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`,
370749abdd8aSBarry Smith           `DMGetApplicationContext()`, `PetscCtxDestroyFn`
3708f07f9ceaSJed Brown @*/
370949abdd8aSBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscCtxDestroyFn *destroy)
3710d71ae5a4SJacob Faibussowitsch {
37111a266240SBarry Smith   PetscFunctionBegin;
3712171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37131a266240SBarry Smith   dm->ctxdestroy = destroy;
37143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37151a266240SBarry Smith }
37161a266240SBarry Smith 
3717b07ff414SBarry Smith /*@
3718bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
371947c6ae99SBarry Smith 
372047c6ae99SBarry Smith   Not Collective
372147c6ae99SBarry Smith 
372247c6ae99SBarry Smith   Input Parameters:
3723bb7acecfSBarry Smith + dm  - the `DM` object
372447c6ae99SBarry Smith - ctx - the user context
372547c6ae99SBarry Smith 
372647c6ae99SBarry Smith   Level: intermediate
372747c6ae99SBarry Smith 
3728e33b3f0fSStefano Zampini   Notes:
372935cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3730e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3731e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3732bb7acecfSBarry Smith 
373360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
373447c6ae99SBarry Smith @*/
3735d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3736d71ae5a4SJacob Faibussowitsch {
373747c6ae99SBarry Smith   PetscFunctionBegin;
3738171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373947c6ae99SBarry Smith   dm->ctx = ctx;
37403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374147c6ae99SBarry Smith }
374247c6ae99SBarry Smith 
374347c6ae99SBarry Smith /*@
3744bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
374547c6ae99SBarry Smith 
374647c6ae99SBarry Smith   Not Collective
374747c6ae99SBarry Smith 
374847c6ae99SBarry Smith   Input Parameter:
3749bb7acecfSBarry Smith . dm - the `DM` object
375047c6ae99SBarry Smith 
375147c6ae99SBarry Smith   Output Parameter:
375247c6ae99SBarry Smith . ctx - the user context
375347c6ae99SBarry Smith 
375447c6ae99SBarry Smith   Level: intermediate
375547c6ae99SBarry Smith 
3756bb7acecfSBarry Smith   Note:
375735cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3758bb7acecfSBarry Smith 
375942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
376047c6ae99SBarry Smith @*/
3761d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3762d71ae5a4SJacob Faibussowitsch {
376347c6ae99SBarry Smith   PetscFunctionBegin;
3764171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37651b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
376747c6ae99SBarry Smith }
376847c6ae99SBarry Smith 
376908da532bSDmitry Karpeev /*@C
3770bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
377108da532bSDmitry Karpeev 
377220f4b53cSBarry Smith   Logically Collective
377308da532bSDmitry Karpeev 
3774d8d19677SJose E. Roman   Input Parameters:
377508da532bSDmitry Karpeev + dm - the DM object
377620f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
377708da532bSDmitry Karpeev 
377808da532bSDmitry Karpeev   Level: intermediate
377908da532bSDmitry Karpeev 
37801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3781db781477SPatrick Sanan          `DMSetJacobian()`
378208da532bSDmitry Karpeev @*/
3783d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3784d71ae5a4SJacob Faibussowitsch {
378508da532bSDmitry Karpeev   PetscFunctionBegin;
37865a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
378908da532bSDmitry Karpeev }
379008da532bSDmitry Karpeev 
379108da532bSDmitry Karpeev /*@
3792bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
379308da532bSDmitry Karpeev 
379408da532bSDmitry Karpeev   Not Collective
379508da532bSDmitry Karpeev 
379608da532bSDmitry Karpeev   Input Parameter:
3797bb7acecfSBarry Smith . dm - the `DM` object to destroy
379808da532bSDmitry Karpeev 
379908da532bSDmitry Karpeev   Output Parameter:
3800bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
380108da532bSDmitry Karpeev 
380208da532bSDmitry Karpeev   Level: developer
380308da532bSDmitry Karpeev 
38041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
380508da532bSDmitry Karpeev @*/
3806d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3807d71ae5a4SJacob Faibussowitsch {
380808da532bSDmitry Karpeev   PetscFunctionBegin;
38095a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38104f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
381108da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
381308da532bSDmitry Karpeev }
381408da532bSDmitry Karpeev 
3815cc4c1da9SBarry Smith /*@
3816bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
381708da532bSDmitry Karpeev 
381820f4b53cSBarry Smith   Logically Collective
381908da532bSDmitry Karpeev 
3820f899ff85SJose E. Roman   Input Parameter:
3821bb7acecfSBarry Smith . dm - the `DM` object
382208da532bSDmitry Karpeev 
382360225df5SJacob Faibussowitsch   Output Parameters:
382408da532bSDmitry Karpeev + xl - lower bound
382508da532bSDmitry Karpeev - xu - upper bound
382608da532bSDmitry Karpeev 
3827907376e6SBarry Smith   Level: advanced
3828907376e6SBarry Smith 
382920f4b53cSBarry Smith   Note:
383095452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
383108da532bSDmitry Karpeev 
38321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
383308da532bSDmitry Karpeev @*/
3834d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3835d71ae5a4SJacob Faibussowitsch {
383608da532bSDmitry Karpeev   PetscFunctionBegin;
38375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
383808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38395a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3840dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
384208da532bSDmitry Karpeev }
384308da532bSDmitry Karpeev 
3844b0ae01b7SPeter Brune /*@
3845bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3846b0ae01b7SPeter Brune 
3847b0ae01b7SPeter Brune   Not Collective
3848b0ae01b7SPeter Brune 
3849b0ae01b7SPeter Brune   Input Parameter:
3850b0ae01b7SPeter Brune . dm - the DM object
3851b0ae01b7SPeter Brune 
3852b0ae01b7SPeter Brune   Output Parameter:
3853bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3854b0ae01b7SPeter Brune 
3855b0ae01b7SPeter Brune   Level: developer
3856b0ae01b7SPeter Brune 
38571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3858b0ae01b7SPeter Brune @*/
3859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3860d71ae5a4SJacob Faibussowitsch {
3861b0ae01b7SPeter Brune   PetscFunctionBegin;
38625a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38634f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3864b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3866b0ae01b7SPeter Brune }
3867b0ae01b7SPeter Brune 
38683ad4599aSBarry Smith /*@
3869bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38703ad4599aSBarry Smith 
38713ad4599aSBarry Smith   Not Collective
38723ad4599aSBarry Smith 
38733ad4599aSBarry Smith   Input Parameter:
3874bb7acecfSBarry Smith . dm - the `DM` object
38753ad4599aSBarry Smith 
38763ad4599aSBarry Smith   Output Parameter:
3877bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38783ad4599aSBarry Smith 
38793ad4599aSBarry Smith   Level: developer
38803ad4599aSBarry Smith 
38811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38823ad4599aSBarry Smith @*/
3883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3884d71ae5a4SJacob Faibussowitsch {
38853ad4599aSBarry Smith   PetscFunctionBegin;
38865a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38874f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38883ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38903ad4599aSBarry Smith }
38913ad4599aSBarry Smith 
3892a7058e45SLawrence Mitchell /*@
3893bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3894a7058e45SLawrence Mitchell 
3895a7058e45SLawrence Mitchell   Not Collective
3896a7058e45SLawrence Mitchell 
3897a7058e45SLawrence Mitchell   Input Parameter:
3898bb7acecfSBarry Smith . dm - the `DM` object
3899a7058e45SLawrence Mitchell 
3900a7058e45SLawrence Mitchell   Output Parameter:
3901bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3902a7058e45SLawrence Mitchell 
3903a7058e45SLawrence Mitchell   Level: developer
3904a7058e45SLawrence Mitchell 
39051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3906a7058e45SLawrence Mitchell @*/
3907d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3908d71ae5a4SJacob Faibussowitsch {
3909a7058e45SLawrence Mitchell   PetscFunctionBegin;
39105a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39114f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3912dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3913ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3915a7058e45SLawrence Mitchell }
3916a7058e45SLawrence Mitchell 
39170298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3918264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3919264ace61SBarry Smith 
3920cc4c1da9SBarry Smith /*@
3921bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3922264ace61SBarry Smith 
392320f4b53cSBarry Smith   Collective
3924264ace61SBarry Smith 
3925264ace61SBarry Smith   Input Parameters:
3926bb7acecfSBarry Smith + dm     - The `DM` object
3927bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3928264ace61SBarry Smith 
3929264ace61SBarry Smith   Options Database Key:
3930bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3931264ace61SBarry Smith 
3932264ace61SBarry Smith   Level: intermediate
3933264ace61SBarry Smith 
3934bb7acecfSBarry Smith   Note:
39350462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3936bb7acecfSBarry Smith 
39371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3938264ace61SBarry Smith @*/
3939d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3940d71ae5a4SJacob Faibussowitsch {
3941264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3942264ace61SBarry Smith   PetscBool match;
3943264ace61SBarry Smith 
3944264ace61SBarry Smith   PetscFunctionBegin;
3945264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39469566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39473ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3948264ace61SBarry Smith 
39499566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39509566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39517a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3952264ace61SBarry Smith 
3953dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39549566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39559566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39569566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3958264ace61SBarry Smith }
3959264ace61SBarry Smith 
3960cc4c1da9SBarry Smith /*@
3961bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3962264ace61SBarry Smith 
3963264ace61SBarry Smith   Not Collective
3964264ace61SBarry Smith 
3965264ace61SBarry Smith   Input Parameter:
3966bb7acecfSBarry Smith . dm - The `DM`
3967264ace61SBarry Smith 
3968264ace61SBarry Smith   Output Parameter:
3969bb7acecfSBarry Smith . type - The `DMType` name
3970264ace61SBarry Smith 
3971264ace61SBarry Smith   Level: intermediate
3972264ace61SBarry Smith 
39731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3974264ace61SBarry Smith @*/
3975d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3976d71ae5a4SJacob Faibussowitsch {
3977264ace61SBarry Smith   PetscFunctionBegin;
3978264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39794f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39809566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3981264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3983264ace61SBarry Smith }
3984264ace61SBarry Smith 
39855d83a8b1SBarry Smith /*@
3986bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
398767a56275SMatthew G Knepley 
398820f4b53cSBarry Smith   Collective
398967a56275SMatthew G Knepley 
399067a56275SMatthew G Knepley   Input Parameters:
3991bb7acecfSBarry Smith + dm      - the `DM`
3992bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
399367a56275SMatthew G Knepley 
399467a56275SMatthew G Knepley   Output Parameter:
3995bb7acecfSBarry Smith . M - pointer to new `DM`
399667a56275SMatthew G Knepley 
399720f4b53cSBarry Smith   Level: intermediate
399820f4b53cSBarry Smith 
399973ff1848SBarry Smith   Note:
4000bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
4001bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
4002bb7acecfSBarry Smith   of the input `DM`.
400367a56275SMatthew G Knepley 
40041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
400567a56275SMatthew G Knepley @*/
4006d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
4007d71ae5a4SJacob Faibussowitsch {
400867a56275SMatthew G Knepley   DM        B;
400967a56275SMatthew G Knepley   char      convname[256];
4010c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
401167a56275SMatthew G Knepley 
401267a56275SMatthew G Knepley   PetscFunctionBegin;
401367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
401467a56275SMatthew G Knepley   PetscValidType(dm, 1);
40154f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40169566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40179566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4018c067b6caSMatthew G. Knepley   if (sametype) {
4019c067b6caSMatthew G. Knepley     *M = dm;
40209566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40213ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4022c067b6caSMatthew G. Knepley   } else {
40230298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
402467a56275SMatthew G Knepley 
402567a56275SMatthew G Knepley     /*
402667a56275SMatthew G Knepley        Order of precedence:
402767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
402867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
402967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
403067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
403167a56275SMatthew G Knepley        5) Use a really basic converter.
403267a56275SMatthew G Knepley     */
403367a56275SMatthew G Knepley 
403467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40359566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40369566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40379566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40389566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40399566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40409566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
404167a56275SMatthew G Knepley     if (conv) goto foundconv;
404267a56275SMatthew G Knepley 
404367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40449566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40459566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40469566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40479566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40489566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40499566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40509566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40519566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
405267a56275SMatthew G Knepley     if (conv) {
40539566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
405467a56275SMatthew G Knepley       goto foundconv;
405567a56275SMatthew G Knepley     }
405667a56275SMatthew G Knepley 
405767a56275SMatthew G Knepley #if 0
405867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
405967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40609566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
406167a56275SMatthew G Knepley     if (conv) goto foundconv;
406267a56275SMatthew G Knepley 
406367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
406467a56275SMatthew G Knepley     if (dm->ops->convert) {
406567a56275SMatthew G Knepley       conv = dm->ops->convert;
406667a56275SMatthew G Knepley     }
406767a56275SMatthew G Knepley     if (conv) goto foundconv;
406867a56275SMatthew G Knepley #endif
406967a56275SMatthew G Knepley 
407067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
407198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
407267a56275SMatthew G Knepley 
407367a56275SMatthew G Knepley   foundconv:
40749566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40759566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
407612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
407790b157c4SStefano Zampini     {
40784fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40796858538eSMatthew G. Knepley 
40804fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40814fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4082c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40839566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40849566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40859566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40869566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
408712fa691eSMatthew G. Knepley     }
40889566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
408967a56275SMatthew G Knepley   }
40909566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
409267a56275SMatthew G Knepley }
4093264ace61SBarry Smith 
4094264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4095264ace61SBarry Smith 
4096264ace61SBarry Smith /*@C
4097bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40981c84c290SBarry Smith 
4099cc4c1da9SBarry Smith   Not Collective, No Fortran Support
41001c84c290SBarry Smith 
41011c84c290SBarry Smith   Input Parameters:
410220f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
410320f4b53cSBarry Smith - function - The creation routine itself
410420f4b53cSBarry Smith 
410520f4b53cSBarry Smith   Level: advanced
41061c84c290SBarry Smith 
410773ff1848SBarry Smith   Note:
410820f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
41091c84c290SBarry Smith 
411060225df5SJacob Faibussowitsch   Example Usage:
41111c84c290SBarry Smith .vb
4112bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41131c84c290SBarry Smith .ve
41141c84c290SBarry Smith 
411520f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41161c84c290SBarry Smith .vb
41171c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41181c84c290SBarry Smith     DMSetType(DM,"my_da");
41191c84c290SBarry Smith .ve
41201c84c290SBarry Smith   or at runtime via the option
41211c84c290SBarry Smith .vb
41221c84c290SBarry Smith     -da_type my_da
41231c84c290SBarry Smith .ve
4124264ace61SBarry Smith 
41251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4126264ace61SBarry Smith @*/
4127d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4128d71ae5a4SJacob Faibussowitsch {
4129264ace61SBarry Smith   PetscFunctionBegin;
41309566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41319566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4133264ace61SBarry Smith }
4134264ace61SBarry Smith 
4135ffeef943SBarry Smith /*@
4136bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4137b859378eSBarry Smith 
413820f4b53cSBarry Smith   Collective
4139b859378eSBarry Smith 
4140b859378eSBarry Smith   Input Parameters:
4141bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4142bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4143bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4144bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4145b859378eSBarry Smith 
4146b859378eSBarry Smith   Level: intermediate
4147b859378eSBarry Smith 
4148b859378eSBarry Smith   Notes:
414955849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4150b859378eSBarry Smith 
4151bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4152bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4153bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4154cd7e8a5eSksagiyam 
41551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4156b859378eSBarry Smith @*/
4157d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4158d71ae5a4SJacob Faibussowitsch {
41599331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4160b859378eSBarry Smith 
4161b859378eSBarry Smith   PetscFunctionBegin;
4162b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4163b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41649566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41659566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41669566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41689331c7a4SMatthew G. Knepley   if (isbinary) {
41699331c7a4SMatthew G. Knepley     PetscInt classid;
41709331c7a4SMatthew G. Knepley     char     type[256];
4171b859378eSBarry Smith 
41729566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
4173835f2295SStefano Zampini     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %" PetscInt_FMT, classid);
41749566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41759566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4176dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41779331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4178dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41799331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41809566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4182b859378eSBarry Smith }
4183b859378eSBarry Smith 
41844ee01570SBarry Smith /* FEM Support */
41857da65231SMatthew G Knepley 
41862ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41872ecf6ec3SMatthew G. Knepley {
41882ecf6ec3SMatthew G. Knepley   PetscInt f;
41892ecf6ec3SMatthew G. Knepley 
41902ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41912ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41922ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41932ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41942ecf6ec3SMatthew G. Knepley }
41952ecf6ec3SMatthew G. Knepley 
4196d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4197d71ae5a4SJacob Faibussowitsch {
41981d47ebbbSSatish Balay   PetscInt f;
41991b30c384SMatthew G Knepley 
42007da65231SMatthew G Knepley   PetscFunctionBegin;
420163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
420248a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
42033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42047da65231SMatthew G Knepley }
42057da65231SMatthew G Knepley 
42065962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
42075962854dSMatthew G. Knepley {
42085962854dSMatthew G. Knepley   PetscInt f;
42095962854dSMatthew G. Knepley 
42105962854dSMatthew G. Knepley   PetscFunctionBegin;
42115962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42125962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42135962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42145962854dSMatthew G. Knepley }
42155962854dSMatthew G. Knepley 
4216d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4217d71ae5a4SJacob Faibussowitsch {
42181b30c384SMatthew G Knepley   PetscInt f, g;
42197da65231SMatthew G Knepley 
42207da65231SMatthew G Knepley   PetscFunctionBegin;
422163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42221d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42239566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
422448a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42259566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42267da65231SMatthew G Knepley   }
42273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42287da65231SMatthew G Knepley }
4229e7c4fc90SDmitry Karpeev 
4230d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4231d71ae5a4SJacob Faibussowitsch {
42320c5b8624SToby Isaac   PetscInt           localSize, bs;
42330c5b8624SToby Isaac   PetscMPIInt        size;
42340c5b8624SToby Isaac   Vec                x, xglob;
42350c5b8624SToby Isaac   const PetscScalar *xarray;
4236e759306cSMatthew G. Knepley 
4237e759306cSMatthew G. Knepley   PetscFunctionBegin;
42389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42399566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42409566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
424193ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42429566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42430c5b8624SToby Isaac   if (size > 1) {
42449566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42459566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42469566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42479566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42480c5b8624SToby Isaac   } else {
42490c5b8624SToby Isaac     xglob = x;
42500c5b8624SToby Isaac   }
42519566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42520c5b8624SToby Isaac   if (size > 1) {
42539566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42549566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42550c5b8624SToby Isaac   }
42569566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4258e759306cSMatthew G. Knepley }
4259e759306cSMatthew G. Knepley 
426088ed4aceSMatthew G Knepley /*@
4261bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
426288ed4aceSMatthew G Knepley 
426388ed4aceSMatthew G Knepley   Input Parameter:
4264bb7acecfSBarry Smith . dm - The `DM`
426588ed4aceSMatthew G Knepley 
426688ed4aceSMatthew G Knepley   Output Parameter:
4267bb7acecfSBarry Smith . section - The `PetscSection`
426888ed4aceSMatthew G Knepley 
426920f4b53cSBarry Smith   Options Database Key:
4270bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4271e5893cccSMatthew G. Knepley 
427288ed4aceSMatthew G Knepley   Level: intermediate
427388ed4aceSMatthew G Knepley 
4274bb7acecfSBarry Smith   Note:
4275bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
427688ed4aceSMatthew G Knepley 
42771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
427888ed4aceSMatthew G Knepley @*/
4279d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4280d71ae5a4SJacob Faibussowitsch {
428188ed4aceSMatthew G Knepley   PetscFunctionBegin;
428288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42834f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42841bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4285e5e52638SMatthew G. Knepley     PetscInt d;
4286e5e52638SMatthew G. Knepley 
428745480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
428845480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
428945480ffeSMatthew G. Knepley       PetscViewer       viewer;
429045480ffeSMatthew G. Knepley       PetscViewerFormat format;
429145480ffeSMatthew G. Knepley       PetscBool         flg;
429245480ffeSMatthew G. Knepley 
4293648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42949566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
429545480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42969566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42979566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
429845480ffeSMatthew G. Knepley       }
429945480ffeSMatthew G. Knepley       if (flg) {
43009566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43019566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4302648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
430345480ffeSMatthew G. Knepley       }
430445480ffeSMatthew G. Knepley     }
4305dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43069566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43072f0f8703SMatthew G. Knepley   }
43081bb6d2a8SBarry Smith   *section = dm->localSection;
43093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
431088ed4aceSMatthew G Knepley }
431188ed4aceSMatthew G Knepley 
431288ed4aceSMatthew G Knepley /*@
4313bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
431488ed4aceSMatthew G Knepley 
431588ed4aceSMatthew G Knepley   Input Parameters:
4316bb7acecfSBarry Smith + dm      - The `DM`
4317bb7acecfSBarry Smith - section - The `PetscSection`
431888ed4aceSMatthew G Knepley 
431988ed4aceSMatthew G Knepley   Level: intermediate
432088ed4aceSMatthew G Knepley 
4321bb7acecfSBarry Smith   Note:
4322bb7acecfSBarry Smith   Any existing Section will be destroyed
432388ed4aceSMatthew G Knepley 
43241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
432588ed4aceSMatthew G Knepley @*/
4326d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4327d71ae5a4SJacob Faibussowitsch {
4328c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4329af122d2aSMatthew G Knepley   PetscInt f;
433088ed4aceSMatthew G Knepley 
433188ed4aceSMatthew G Knepley   PetscFunctionBegin;
433288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4333b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43359566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43361bb6d2a8SBarry Smith   dm->localSection = section;
43379566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4338af122d2aSMatthew G Knepley   if (numFields) {
43399566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4340af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43410f21e855SMatthew G. Knepley       PetscObject disc;
4342af122d2aSMatthew G Knepley       const char *name;
4343af122d2aSMatthew G Knepley 
43449566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43459566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43469566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4347af122d2aSMatthew G Knepley     }
4348af122d2aSMatthew G Knepley   }
434950350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
435050350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43519566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
435250350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
435352947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
435450350c8eSStefano Zampini 
435550350c8eSStefano Zampini   /* Clear scratch vectors */
435650350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
435750350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
435850350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
435950350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
43603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
436188ed4aceSMatthew G Knepley }
436288ed4aceSMatthew G Knepley 
4363adc21957SMatthew G. Knepley /*@C
4364d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4365adc21957SMatthew G. Knepley 
4366adc21957SMatthew G. Knepley   Input Parameter:
4367adc21957SMatthew G. Knepley . dm - The `DM`
4368adc21957SMatthew G. Knepley 
43699cde84edSJose E. Roman   Output Parameters:
4370adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4371b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4372adc21957SMatthew G. Knepley 
4373adc21957SMatthew G. Knepley   Level: developer
4374adc21957SMatthew G. Knepley 
4375adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4376adc21957SMatthew G. Knepley @*/
4377adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4378adc21957SMatthew G. Knepley {
4379adc21957SMatthew G. Knepley   PetscFunctionBegin;
4380adc21957SMatthew G. Knepley   *perm        = NULL;
4381adc21957SMatthew G. Knepley   *blockStarts = NULL;
4382adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4383adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4384adc21957SMatthew G. Knepley }
4385adc21957SMatthew G. Knepley 
43869435951eSToby Isaac /*@
4387bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43889435951eSToby Isaac 
438920f4b53cSBarry Smith   not Collective
4390e228b242SToby Isaac 
43919435951eSToby Isaac   Input Parameter:
4392bb7acecfSBarry Smith . dm - The `DM`
43939435951eSToby Isaac 
4394d8d19677SJose E. Roman   Output Parameters:
439520f4b53cSBarry 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.
439620f4b53cSBarry 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.
439779769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
43989435951eSToby Isaac 
43999435951eSToby Isaac   Level: advanced
44009435951eSToby Isaac 
4401bb7acecfSBarry Smith   Note:
4402bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44039435951eSToby Isaac 
44041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44059435951eSToby Isaac @*/
4406d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4407d71ae5a4SJacob Faibussowitsch {
44089435951eSToby Isaac   PetscFunctionBegin;
44099435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4410dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44113b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44123b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
441379769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44159435951eSToby Isaac }
44169435951eSToby Isaac 
44179435951eSToby Isaac /*@
4418bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44199435951eSToby Isaac 
442020f4b53cSBarry Smith   Collective
4421e228b242SToby Isaac 
44229435951eSToby Isaac   Input Parameters:
4423bb7acecfSBarry Smith + dm      - The `DM`
4424bb7acecfSBarry 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).
442520f4b53cSBarry 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).
442620f4b53cSBarry 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).
44279435951eSToby Isaac 
44289435951eSToby Isaac   Level: advanced
44299435951eSToby Isaac 
443020f4b53cSBarry Smith   Notes:
443120f4b53cSBarry 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()`.
443220f4b53cSBarry Smith 
443320f4b53cSBarry 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.
443420f4b53cSBarry Smith 
4435bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44369435951eSToby Isaac 
44371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44389435951eSToby Isaac @*/
4439d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4440d71ae5a4SJacob Faibussowitsch {
4441e228b242SToby Isaac   PetscMPIInt result;
44429435951eSToby Isaac 
44439435951eSToby Isaac   PetscFunctionBegin;
44449435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4445e228b242SToby Isaac   if (section) {
4446e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44479566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44487a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4449e228b242SToby Isaac   }
4450e228b242SToby Isaac   if (mat) {
4451e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44529566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44537a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4454e228b242SToby Isaac   }
445579769bd5SJed Brown   if (bias) {
445679769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44579566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
445879769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
445979769bd5SJed Brown   }
44609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44619566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44623b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44649566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44653b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
446879769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44709435951eSToby Isaac }
44719435951eSToby Isaac 
4472497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4473507e4973SMatthew G. Knepley /*
4474bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4475507e4973SMatthew G. Knepley 
4476507e4973SMatthew G. Knepley   Input Parameters:
4477bb7acecfSBarry Smith + dm - The `DM`
4478bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4479bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4480507e4973SMatthew G. Knepley 
4481507e4973SMatthew G. Knepley   Level: intermediate
4482507e4973SMatthew G. Knepley 
44831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4484507e4973SMatthew G. Knepley */
4485d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4486d71ae5a4SJacob Faibussowitsch {
4487507e4973SMatthew G. Knepley   MPI_Comm        comm;
4488507e4973SMatthew G. Knepley   PetscLayout     layout;
4489507e4973SMatthew G. Knepley   const PetscInt *ranges;
4490507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4491507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4492507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4493507e4973SMatthew G. Knepley 
4494507e4973SMatthew G. Knepley   PetscFunctionBegin;
44959566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4496507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44989566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44999566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45009566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45019566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45029566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45039566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45049566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45059566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4506507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4507f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4508507e4973SMatthew G. Knepley 
45099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4515507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45169371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45179371c9d4SSatish 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));
45189371c9d4SSatish Balay       valid = PETSC_FALSE;
45199371c9d4SSatish Balay     }
45209371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45219371c9d4SSatish 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));
45229371c9d4SSatish Balay       valid = PETSC_FALSE;
45239371c9d4SSatish Balay     }
4524507e4973SMatthew G. Knepley     if (gdof < 0) {
4525507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4526507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4527507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4528507e4973SMatthew G. Knepley 
45299566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4530507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45319371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45329371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45339371c9d4SSatish Balay           valid = PETSC_FALSE;
45349371c9d4SSatish Balay           break;
45359371c9d4SSatish Balay         }
4536507e4973SMatthew G. Knepley       }
4537507e4973SMatthew G. Knepley     }
4538507e4973SMatthew G. Knepley   }
45399566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45409566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
4541462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4542507e4973SMatthew G. Knepley   if (!gvalid) {
45439566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4544507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4545507e4973SMatthew G. Knepley   }
45463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4547507e4973SMatthew G. Knepley }
4548f741bcd2SMatthew G. Knepley #endif
4549507e4973SMatthew G. Knepley 
455090df3356SJames Wright PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45516725e60dSJed Brown {
45526725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45534d86920dSPierre Jolivet 
45546725e60dSJed Brown   PetscFunctionBegin;
45556725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45564f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45575f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45586725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45596725e60dSJed Brown   else *sf = dm->sf;
45603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45616725e60dSJed Brown }
45626725e60dSJed Brown 
456388ed4aceSMatthew G Knepley /*@
4564bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
456588ed4aceSMatthew G Knepley 
456620f4b53cSBarry Smith   Collective
45678b1ab98fSJed Brown 
456888ed4aceSMatthew G Knepley   Input Parameter:
4569bb7acecfSBarry Smith . dm - The `DM`
457088ed4aceSMatthew G Knepley 
457188ed4aceSMatthew G Knepley   Output Parameter:
4572bb7acecfSBarry Smith . section - The `PetscSection`
457388ed4aceSMatthew G Knepley 
457488ed4aceSMatthew G Knepley   Level: intermediate
457588ed4aceSMatthew G Knepley 
4576bb7acecfSBarry Smith   Note:
4577bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
457888ed4aceSMatthew G Knepley 
45791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
458088ed4aceSMatthew G Knepley @*/
4581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4582d71ae5a4SJacob Faibussowitsch {
458388ed4aceSMatthew G Knepley   PetscFunctionBegin;
458488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45854f572ea9SToby Isaac   PetscAssertPointer(section, 2);
45861bb6d2a8SBarry Smith   if (!dm->globalSection) {
4587fd59a867SMatthew G. Knepley     PetscSection s;
45886725e60dSJed Brown     PetscSF      sf;
4589fd59a867SMatthew G. Knepley 
45909566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45917a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45927a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45935f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4594eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45959566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45969566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45979566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
459888ed4aceSMatthew G Knepley   }
45991bb6d2a8SBarry Smith   *section = dm->globalSection;
46003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
460188ed4aceSMatthew G Knepley }
460288ed4aceSMatthew G Knepley 
4603b21d0597SMatthew G Knepley /*@
4604bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4605b21d0597SMatthew G Knepley 
4606b21d0597SMatthew G Knepley   Input Parameters:
4607bb7acecfSBarry Smith + dm      - The `DM`
46082fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4609b21d0597SMatthew G Knepley 
4610b21d0597SMatthew G Knepley   Level: intermediate
4611b21d0597SMatthew G Knepley 
4612bb7acecfSBarry Smith   Note:
4613bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4614b21d0597SMatthew G Knepley 
46151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4616b21d0597SMatthew G Knepley @*/
4617d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4618d71ae5a4SJacob Faibussowitsch {
4619b21d0597SMatthew G Knepley   PetscFunctionBegin;
4620b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46215080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46229566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46239566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46241bb6d2a8SBarry Smith   dm->globalSection = section;
4625497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46269566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4627507e4973SMatthew G. Knepley #endif
462850350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
462950350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
463052947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
463150350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
463250350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4634b21d0597SMatthew G Knepley }
4635b21d0597SMatthew G Knepley 
463688ed4aceSMatthew G Knepley /*@
4637bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4638bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
463988ed4aceSMatthew G Knepley 
464088ed4aceSMatthew G Knepley   Input Parameter:
4641bb7acecfSBarry Smith . dm - The `DM`
464288ed4aceSMatthew G Knepley 
464388ed4aceSMatthew G Knepley   Output Parameter:
4644bb7acecfSBarry Smith . sf - The `PetscSF`
464588ed4aceSMatthew G Knepley 
464688ed4aceSMatthew G Knepley   Level: intermediate
464788ed4aceSMatthew G Knepley 
4648bb7acecfSBarry Smith   Note:
4649bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
465088ed4aceSMatthew G Knepley 
46511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
465288ed4aceSMatthew G Knepley @*/
4653d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4654d71ae5a4SJacob Faibussowitsch {
465588ed4aceSMatthew G Knepley   PetscInt nroots;
465688ed4aceSMatthew G Knepley 
465788ed4aceSMatthew G Knepley   PetscFunctionBegin;
465888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46594f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
466048a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46619566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
466288ed4aceSMatthew G Knepley   if (nroots < 0) {
466388ed4aceSMatthew G Knepley     PetscSection section, gSection;
466488ed4aceSMatthew G Knepley 
46659566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
466631ea6d37SMatthew G Knepley     if (section) {
46679566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46689566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
466931ea6d37SMatthew G Knepley     } else {
46700298fd71SBarry Smith       *sf = NULL;
46713ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
467231ea6d37SMatthew G Knepley     }
467388ed4aceSMatthew G Knepley   }
46741bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
467688ed4aceSMatthew G Knepley }
467788ed4aceSMatthew G Knepley 
467888ed4aceSMatthew G Knepley /*@
4679bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
468088ed4aceSMatthew G Knepley 
468188ed4aceSMatthew G Knepley   Input Parameters:
4682bb7acecfSBarry Smith + dm - The `DM`
4683bb7acecfSBarry Smith - sf - The `PetscSF`
468488ed4aceSMatthew G Knepley 
468588ed4aceSMatthew G Knepley   Level: intermediate
468688ed4aceSMatthew G Knepley 
4687bb7acecfSBarry Smith   Note:
4688bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
468988ed4aceSMatthew G Knepley 
46901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
469188ed4aceSMatthew G Knepley @*/
4692d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4693d71ae5a4SJacob Faibussowitsch {
469488ed4aceSMatthew G Knepley   PetscFunctionBegin;
469588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4696b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46979566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46989566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46991bb6d2a8SBarry Smith   dm->sectionSF = sf;
47003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
470188ed4aceSMatthew G Knepley }
470288ed4aceSMatthew G Knepley 
4703cc4c1da9SBarry Smith /*@
4704bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
470588ed4aceSMatthew G Knepley   describing the data layout.
470688ed4aceSMatthew G Knepley 
470788ed4aceSMatthew G Knepley   Input Parameters:
4708bb7acecfSBarry Smith + dm            - The `DM`
4709bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4710bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
471188ed4aceSMatthew G Knepley 
47121bb6d2a8SBarry Smith   Level: developer
47131bb6d2a8SBarry Smith 
4714bb7acecfSBarry Smith   Note:
4715bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4716bb7acecfSBarry Smith 
471773ff1848SBarry Smith   Developer Note:
4718bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4719bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4720b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4721b6971eaeSBarry Smith   they create, this returns nothing
47221bb6d2a8SBarry Smith 
47231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
472488ed4aceSMatthew G Knepley @*/
4725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4726d71ae5a4SJacob Faibussowitsch {
472788ed4aceSMatthew G Knepley   PetscFunctionBegin;
472888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47299566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
473188ed4aceSMatthew G Knepley }
4732af122d2aSMatthew G Knepley 
4733b21d0597SMatthew G Knepley /*@
4734bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4735bb7acecfSBarry Smith 
4736bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4737b21d0597SMatthew G Knepley 
4738b21d0597SMatthew G Knepley   Input Parameter:
4739bb7acecfSBarry Smith . dm - The `DM`
4740b21d0597SMatthew G Knepley 
4741b21d0597SMatthew G Knepley   Output Parameter:
4742bb7acecfSBarry Smith . sf - The `PetscSF`
4743b21d0597SMatthew G Knepley 
4744b21d0597SMatthew G Knepley   Level: intermediate
4745b21d0597SMatthew G Knepley 
4746bb7acecfSBarry Smith   Note:
4747bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4748b21d0597SMatthew G Knepley 
47491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4750b21d0597SMatthew G Knepley @*/
4751d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4752d71ae5a4SJacob Faibussowitsch {
4753b21d0597SMatthew G Knepley   PetscFunctionBegin;
4754b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47554f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4756b21d0597SMatthew G Knepley   *sf = dm->sf;
47573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4758b21d0597SMatthew G Knepley }
4759b21d0597SMatthew G Knepley 
4760057b4bcdSMatthew G Knepley /*@
4761bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4762bb7acecfSBarry Smith 
476320f4b53cSBarry Smith   Collective
4764057b4bcdSMatthew G Knepley 
4765057b4bcdSMatthew G Knepley   Input Parameters:
4766bb7acecfSBarry Smith + dm - The `DM`
4767bb7acecfSBarry Smith - sf - The `PetscSF`
4768057b4bcdSMatthew G Knepley 
4769057b4bcdSMatthew G Knepley   Level: intermediate
4770057b4bcdSMatthew G Knepley 
47711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4772057b4bcdSMatthew G Knepley @*/
4773d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4774d71ae5a4SJacob Faibussowitsch {
4775057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4776057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4777b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47789566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47799566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4780057b4bcdSMatthew G Knepley   dm->sf = sf;
47813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4782057b4bcdSMatthew G Knepley }
4783057b4bcdSMatthew G Knepley 
47844f37162bSMatthew G. Knepley /*@
4785bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47864f37162bSMatthew G. Knepley 
47874f37162bSMatthew G. Knepley   Input Parameter:
4788bb7acecfSBarry Smith . dm - The `DM`
47894f37162bSMatthew G. Knepley 
47904f37162bSMatthew G. Knepley   Output Parameter:
4791bb7acecfSBarry Smith . sf - The `PetscSF`
47924f37162bSMatthew G. Knepley 
47934f37162bSMatthew G. Knepley   Level: intermediate
47944f37162bSMatthew G. Knepley 
4795bb7acecfSBarry Smith   Note:
4796bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47974f37162bSMatthew G. Knepley 
47981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47994f37162bSMatthew G. Knepley @*/
4800d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4801d71ae5a4SJacob Faibussowitsch {
48024f37162bSMatthew G. Knepley   PetscFunctionBegin;
48034f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48044f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48054f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48074f37162bSMatthew G. Knepley }
48084f37162bSMatthew G. Knepley 
48094f37162bSMatthew G. Knepley /*@
48104f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48114f37162bSMatthew G. Knepley 
48124f37162bSMatthew G. Knepley   Input Parameters:
48134f37162bSMatthew G. Knepley + dm - The DM
48144f37162bSMatthew G. Knepley - sf - The PetscSF
48154f37162bSMatthew G. Knepley 
48164f37162bSMatthew G. Knepley   Level: intermediate
48174f37162bSMatthew G. Knepley 
48181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48194f37162bSMatthew G. Knepley @*/
4820d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4821d71ae5a4SJacob Faibussowitsch {
48224f37162bSMatthew G. Knepley   PetscFunctionBegin;
48234f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48244f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48269566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48274f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48294f37162bSMatthew G. Knepley }
48304f37162bSMatthew G. Knepley 
4831d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4832d71ae5a4SJacob Faibussowitsch {
483334aa8a36SMatthew G. Knepley   PetscClassId id;
483434aa8a36SMatthew G. Knepley 
483534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48369566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
483734aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48389566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
483934aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48409566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
484117c1d62eSMatthew G. Knepley   } else {
48429566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
484334aa8a36SMatthew G. Knepley   }
48443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
484534aa8a36SMatthew G. Knepley }
484634aa8a36SMatthew G. Knepley 
4847d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4848d71ae5a4SJacob Faibussowitsch {
484944a7f3ddSMatthew G. Knepley   RegionField *tmpr;
485044a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
485144a7f3ddSMatthew G. Knepley 
485244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48533ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
485544a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48569371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48579371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48589371c9d4SSatish Balay     tmpr[f].label       = NULL;
48599371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48609371c9d4SSatish Balay   }
48619566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
486244a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
486344a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
486544a7f3ddSMatthew G. Knepley }
486644a7f3ddSMatthew G. Knepley 
486744a7f3ddSMatthew G. Knepley /*@
486820f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
486944a7f3ddSMatthew G. Knepley 
487020f4b53cSBarry Smith   Logically Collective
487144a7f3ddSMatthew G. Knepley 
487244a7f3ddSMatthew G. Knepley   Input Parameter:
487320f4b53cSBarry Smith . dm - The `DM`
487444a7f3ddSMatthew G. Knepley 
487544a7f3ddSMatthew G. Knepley   Level: intermediate
487644a7f3ddSMatthew G. Knepley 
48771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
487844a7f3ddSMatthew G. Knepley @*/
4879d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4880d71ae5a4SJacob Faibussowitsch {
488144a7f3ddSMatthew G. Knepley   PetscInt f;
488244a7f3ddSMatthew G. Knepley 
488344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
488444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
488544a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48869566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48879566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
488844a7f3ddSMatthew G. Knepley   }
48899566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
489044a7f3ddSMatthew G. Knepley   dm->fields = NULL;
489144a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
489344a7f3ddSMatthew G. Knepley }
489444a7f3ddSMatthew G. Knepley 
4895689b5837SMatthew G. Knepley /*@
489620f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4897689b5837SMatthew G. Knepley 
489820f4b53cSBarry Smith   Not Collective
4899689b5837SMatthew G. Knepley 
4900689b5837SMatthew G. Knepley   Input Parameter:
490120f4b53cSBarry Smith . dm - The `DM`
4902689b5837SMatthew G. Knepley 
4903689b5837SMatthew G. Knepley   Output Parameter:
490460225df5SJacob Faibussowitsch . numFields - The number of fields
4905689b5837SMatthew G. Knepley 
4906689b5837SMatthew G. Knepley   Level: intermediate
4907689b5837SMatthew G. Knepley 
49081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4909689b5837SMatthew G. Knepley @*/
4910d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4911d71ae5a4SJacob Faibussowitsch {
49120f21e855SMatthew G. Knepley   PetscFunctionBegin;
49130f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49144f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
491544a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4917af122d2aSMatthew G Knepley }
4918af122d2aSMatthew G Knepley 
4919689b5837SMatthew G. Knepley /*@
492020f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4921689b5837SMatthew G. Knepley 
492220f4b53cSBarry Smith   Logically Collective
4923689b5837SMatthew G. Knepley 
4924689b5837SMatthew G. Knepley   Input Parameters:
492520f4b53cSBarry Smith + dm        - The `DM`
492660225df5SJacob Faibussowitsch - numFields - The number of fields
4927689b5837SMatthew G. Knepley 
4928689b5837SMatthew G. Knepley   Level: intermediate
4929689b5837SMatthew G. Knepley 
49301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4931689b5837SMatthew G. Knepley @*/
4932d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4933d71ae5a4SJacob Faibussowitsch {
49340f21e855SMatthew G. Knepley   PetscInt Nf, f;
4935af122d2aSMatthew G Knepley 
4936af122d2aSMatthew G Knepley   PetscFunctionBegin;
4937af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49389566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49390f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49400f21e855SMatthew G. Knepley     PetscContainer obj;
49410f21e855SMatthew G. Knepley 
49429566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49439566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49449566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4945af122d2aSMatthew G Knepley   }
49463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4947af122d2aSMatthew G Knepley }
4948af122d2aSMatthew G Knepley 
4949c1929be8SMatthew G. Knepley /*@
4950bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4951c1929be8SMatthew G. Knepley 
495220f4b53cSBarry Smith   Not Collective
4953c1929be8SMatthew G. Knepley 
4954c1929be8SMatthew G. Knepley   Input Parameters:
4955bb7acecfSBarry Smith + dm - The `DM`
4956c1929be8SMatthew G. Knepley - f  - The field number
4957c1929be8SMatthew G. Knepley 
495844a7f3ddSMatthew G. Knepley   Output Parameters:
495920f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
496020f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4961c1929be8SMatthew G. Knepley 
496244a7f3ddSMatthew G. Knepley   Level: intermediate
4963c1929be8SMatthew G. Knepley 
49641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4965c1929be8SMatthew G. Knepley @*/
4966d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4967d71ae5a4SJacob Faibussowitsch {
4968af122d2aSMatthew G Knepley   PetscFunctionBegin;
4969af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49704f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
49717a8be351SBarry 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);
497244a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4973bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4975decb47aaSMatthew G. Knepley }
4976decb47aaSMatthew G. Knepley 
4977083401c6SMatthew G. Knepley /* Does not clear the DS */
4978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4979d71ae5a4SJacob Faibussowitsch {
4980083401c6SMatthew G. Knepley   PetscFunctionBegin;
49819566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49829566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49839566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4984083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4985bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49869566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4987835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
49883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4989083401c6SMatthew G. Knepley }
4990083401c6SMatthew G. Knepley 
4991ffeef943SBarry Smith /*@
4992bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4993bb7acecfSBarry Smith   the field numbering.
4994c1929be8SMatthew G. Knepley 
499520f4b53cSBarry Smith   Logically Collective
4996c1929be8SMatthew G. Knepley 
4997c1929be8SMatthew G. Knepley   Input Parameters:
4998bb7acecfSBarry Smith + dm    - The `DM`
4999c1929be8SMatthew G. Knepley . f     - The field number
500020f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5001bb7acecfSBarry Smith - disc  - The discretization object
5002c1929be8SMatthew G. Knepley 
500344a7f3ddSMatthew G. Knepley   Level: intermediate
5004c1929be8SMatthew G. Knepley 
50051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5006c1929be8SMatthew G. Knepley @*/
5007d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5008d71ae5a4SJacob Faibussowitsch {
5009decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5010decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5011e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5012bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50137a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5014bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5015bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50169566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
501844a7f3ddSMatthew G. Knepley }
501944a7f3ddSMatthew G. Knepley 
5020ffeef943SBarry Smith /*@
5021bb7acecfSBarry 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)
5022bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
502344a7f3ddSMatthew G. Knepley 
502420f4b53cSBarry Smith   Logically Collective
502544a7f3ddSMatthew G. Knepley 
502644a7f3ddSMatthew G. Knepley   Input Parameters:
5027bb7acecfSBarry Smith + dm    - The `DM`
502820f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5029bb7acecfSBarry Smith - disc  - The discretization object
503044a7f3ddSMatthew G. Knepley 
503144a7f3ddSMatthew G. Knepley   Level: intermediate
503244a7f3ddSMatthew G. Knepley 
5033bb7acecfSBarry Smith   Notes:
5034bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5035bb7acecfSBarry Smith 
5036da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5037bb7acecfSBarry 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
5038bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5039bb7acecfSBarry Smith 
50401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
504144a7f3ddSMatthew G. Knepley @*/
5042d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5043d71ae5a4SJacob Faibussowitsch {
504444a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
504544a7f3ddSMatthew G. Knepley 
504644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
504744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5048064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5049bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50509566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
505144a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5052bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5054835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
5055bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50569566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5058af122d2aSMatthew G Knepley }
50596636e97aSMatthew G Knepley 
5060e5e52638SMatthew G. Knepley /*@
5061e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5062e0b68406SMatthew Knepley 
506320f4b53cSBarry Smith   Logically Collective
5064e0b68406SMatthew Knepley 
5065e0b68406SMatthew Knepley   Input Parameters:
5066bb7acecfSBarry Smith + dm          - The `DM`
5067e0b68406SMatthew Knepley . f           - The field index
5068bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5069e0b68406SMatthew Knepley 
5070e0b68406SMatthew Knepley   Level: intermediate
5071e0b68406SMatthew Knepley 
50721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5073e0b68406SMatthew Knepley @*/
5074d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5075d71ae5a4SJacob Faibussowitsch {
5076e0b68406SMatthew Knepley   PetscFunctionBegin;
507763a3b9bcSJacob 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);
5078e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5080e0b68406SMatthew Knepley }
5081e0b68406SMatthew Knepley 
5082e0b68406SMatthew Knepley /*@
5083e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5084e0b68406SMatthew Knepley 
508520f4b53cSBarry Smith   Not Collective
5086e0b68406SMatthew Knepley 
5087e0b68406SMatthew Knepley   Input Parameters:
5088bb7acecfSBarry Smith + dm - The `DM`
5089e0b68406SMatthew Knepley - f  - The field index
5090e0b68406SMatthew Knepley 
5091e0b68406SMatthew Knepley   Output Parameter:
5092e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5093e0b68406SMatthew Knepley 
5094e0b68406SMatthew Knepley   Level: intermediate
5095e0b68406SMatthew Knepley 
509660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5097e0b68406SMatthew Knepley @*/
5098d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5099d71ae5a4SJacob Faibussowitsch {
5100e0b68406SMatthew Knepley   PetscFunctionBegin;
510163a3b9bcSJacob 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);
5102e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5104e0b68406SMatthew Knepley }
5105e0b68406SMatthew Knepley 
5106e0b68406SMatthew Knepley /*@
5107bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5108e5e52638SMatthew G. Knepley 
510920f4b53cSBarry Smith   Collective
5110e5e52638SMatthew G. Knepley 
5111bb4b53efSMatthew G. Knepley   Input Parameters:
5112bb4b53efSMatthew G. Knepley + dm        - The `DM`
5113bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
5114bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
5115e5e52638SMatthew G. Knepley 
5116e5e52638SMatthew G. Knepley   Output Parameter:
5117bb7acecfSBarry Smith . newdm - The `DM`
5118e5e52638SMatthew G. Knepley 
5119e5e52638SMatthew G. Knepley   Level: advanced
5120e5e52638SMatthew G. Knepley 
51211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5122e5e52638SMatthew G. Knepley @*/
5123bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
5124d71ae5a4SJacob Faibussowitsch {
5125e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5126e5e52638SMatthew G. Knepley 
5127e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51283ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51299566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51309566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5131e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5132e5e52638SMatthew G. Knepley     DMLabel      label;
5133e5e52638SMatthew G. Knepley     PetscObject  field;
5134bb4b53efSMatthew G. Knepley     PetscClassId id;
513534aa8a36SMatthew G. Knepley     PetscBool    useCone, useClosure;
5136e5e52638SMatthew G. Knepley 
51379566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
5138bb4b53efSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(field, &id));
5139bb4b53efSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
5140bb4b53efSMatthew G. Knepley       PetscFE newfe;
5141bb4b53efSMatthew G. Knepley 
5142bb4b53efSMatthew G. Knepley       PetscCall(PetscFELimitDegree((PetscFE)field, minDegree, maxDegree, &newfe));
5143bb4b53efSMatthew G. Knepley       PetscCall(DMSetField(newdm, f, label, (PetscObject)newfe));
5144bb4b53efSMatthew G. Knepley       PetscCall(PetscFEDestroy(&newfe));
5145bb4b53efSMatthew G. Knepley     } else {
51469566063dSJacob Faibussowitsch       PetscCall(DMSetField(newdm, f, label, field));
5147bb4b53efSMatthew G. Knepley     }
51489566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51499566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
515034aa8a36SMatthew G. Knepley   }
51513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
515234aa8a36SMatthew G. Knepley }
515334aa8a36SMatthew G. Knepley 
515434aa8a36SMatthew G. Knepley /*@
515534aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
515634aa8a36SMatthew G. Knepley 
515720f4b53cSBarry Smith   Not Collective
515834aa8a36SMatthew G. Knepley 
515934aa8a36SMatthew G. Knepley   Input Parameters:
516020f4b53cSBarry Smith + dm - The `DM` object
516120f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
516234aa8a36SMatthew G. Knepley 
5163d8d19677SJose E. Roman   Output Parameters:
516434aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
516534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
516634aa8a36SMatthew G. Knepley 
516734aa8a36SMatthew G. Knepley   Level: developer
516834aa8a36SMatthew G. Knepley 
516920f4b53cSBarry Smith   Notes:
517020f4b53cSBarry Smith .vb
517120f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
517220f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
517320f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
517420f4b53cSBarry Smith .ve
517520f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
517620f4b53cSBarry Smith 
51771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
517834aa8a36SMatthew G. Knepley @*/
5179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5180d71ae5a4SJacob Faibussowitsch {
518134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
518234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51834f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
51844f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
518534aa8a36SMatthew G. Knepley   if (f < 0) {
518634aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
518734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
518834aa8a36SMatthew G. Knepley   } else {
518934aa8a36SMatthew G. Knepley     PetscInt Nf;
519034aa8a36SMatthew G. Knepley 
51919566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51927a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
519334aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
519434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
519534aa8a36SMatthew G. Knepley   }
51963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
519734aa8a36SMatthew G. Knepley }
519834aa8a36SMatthew G. Knepley 
519934aa8a36SMatthew G. Knepley /*@
520034aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
520134aa8a36SMatthew G. Knepley 
520220f4b53cSBarry Smith   Not Collective
520334aa8a36SMatthew G. Knepley 
520434aa8a36SMatthew G. Knepley   Input Parameters:
520520f4b53cSBarry Smith + dm         - The `DM` object
520634aa8a36SMatthew G. Knepley . f          - The field number
520734aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
520834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
520934aa8a36SMatthew G. Knepley 
521034aa8a36SMatthew G. Knepley   Level: developer
521134aa8a36SMatthew G. Knepley 
521220f4b53cSBarry Smith   Notes:
521320f4b53cSBarry Smith .vb
521420f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
521520f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
521620f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
521720f4b53cSBarry Smith .ve
521820f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
521920f4b53cSBarry Smith 
52201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
522134aa8a36SMatthew G. Knepley @*/
5222d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5223d71ae5a4SJacob Faibussowitsch {
522434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
522534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
522634aa8a36SMatthew G. Knepley   if (f < 0) {
522734aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
522834aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
522934aa8a36SMatthew G. Knepley   } else {
523034aa8a36SMatthew G. Knepley     PetscInt Nf;
523134aa8a36SMatthew G. Knepley 
52329566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52337a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
523434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
523534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5236e5e52638SMatthew G. Knepley   }
52373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5238e5e52638SMatthew G. Knepley }
5239e5e52638SMatthew G. Knepley 
5240b0441da4SMatthew G. Knepley /*@
5241b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5242b0441da4SMatthew G. Knepley 
5243b0441da4SMatthew G. Knepley   Not collective
5244b0441da4SMatthew G. Knepley 
5245f899ff85SJose E. Roman   Input Parameter:
524620f4b53cSBarry Smith . dm - The `DM` object
5247b0441da4SMatthew G. Knepley 
5248d8d19677SJose E. Roman   Output Parameters:
5249b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5250b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5251b0441da4SMatthew G. Knepley 
5252b0441da4SMatthew G. Knepley   Level: developer
5253b0441da4SMatthew G. Knepley 
525420f4b53cSBarry Smith   Notes:
525520f4b53cSBarry Smith .vb
525620f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
525720f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
525820f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
525920f4b53cSBarry Smith .ve
526020f4b53cSBarry Smith 
52611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5262b0441da4SMatthew G. Knepley @*/
5263d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5264d71ae5a4SJacob Faibussowitsch {
5265b0441da4SMatthew G. Knepley   PetscInt Nf;
5266b0441da4SMatthew G. Knepley 
5267b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5268b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52694f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52704f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52719566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5272b0441da4SMatthew G. Knepley   if (!Nf) {
52739566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5274b0441da4SMatthew G. Knepley   } else {
52759566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5276b0441da4SMatthew G. Knepley   }
52773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5278b0441da4SMatthew G. Knepley }
5279b0441da4SMatthew G. Knepley 
5280b0441da4SMatthew G. Knepley /*@
5281b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5282b0441da4SMatthew G. Knepley 
528320f4b53cSBarry Smith   Not Collective
5284b0441da4SMatthew G. Knepley 
5285b0441da4SMatthew G. Knepley   Input Parameters:
528620f4b53cSBarry Smith + dm         - The `DM` object
5287b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5288b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5289b0441da4SMatthew G. Knepley 
5290b0441da4SMatthew G. Knepley   Level: developer
5291b0441da4SMatthew G. Knepley 
529220f4b53cSBarry Smith   Notes:
529320f4b53cSBarry Smith .vb
529420f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
529520f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
529620f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
529720f4b53cSBarry Smith .ve
529820f4b53cSBarry Smith 
52991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5300b0441da4SMatthew G. Knepley @*/
5301d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5302d71ae5a4SJacob Faibussowitsch {
5303b0441da4SMatthew G. Knepley   PetscInt Nf;
5304b0441da4SMatthew G. Knepley 
5305b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5306b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53079566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5308b0441da4SMatthew G. Knepley   if (!Nf) {
53099566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5310b0441da4SMatthew G. Knepley   } else {
53119566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5312e5e52638SMatthew G. Knepley   }
53133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5314e5e52638SMatthew G. Knepley }
5315e5e52638SMatthew G. Knepley 
5316d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5317d71ae5a4SJacob Faibussowitsch {
5318799db056SMatthew G. Knepley   DM           plex;
5319799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5320799db056SMatthew G. Knepley   const char **names;
5321799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5322799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5323799db056SMatthew G. Knepley   size_t       len;
5324799db056SMatthew G. Knepley   MPI_Comm     comm;
5325799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5326783e2ec8SMatthew G. Knepley 
5327783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5328799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5329799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5330799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5331799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5332799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5333799db056SMatthew G. Knepley     PetscDS  dsBC;
5334799db056SMatthew G. Knepley     PetscInt numBd;
5335799db056SMatthew G. Knepley 
533607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5337799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5338799db056SMatthew G. Knepley     maxLabels += numBd;
5339799db056SMatthew G. Knepley   }
5340799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5341799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5342799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5343799db056SMatthew G. Knepley     PetscDS  dsBC;
5344799db056SMatthew G. Knepley     PetscInt numBd, bd;
5345799db056SMatthew G. Knepley 
534607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5347799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5348799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5349799db056SMatthew G. Knepley       DMLabel      label;
5350799db056SMatthew G. Knepley       PetscInt     field;
5351799db056SMatthew G. Knepley       PetscObject  obj;
5352799db056SMatthew G. Knepley       PetscClassId id;
5353799db056SMatthew G. Knepley 
5354799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53559566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53569566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5357799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53589371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53599371c9d4SSatish Balay         if (labels[l] == label) break;
5360799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5361783e2ec8SMatthew G. Knepley     }
5362799db056SMatthew G. Knepley   }
5363799db056SMatthew G. Knepley   /* Get label names */
5364799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5365799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53669371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53679371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53689371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53699371c9d4SSatish Balay   }
5370799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5371462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5372799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5373c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5374799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5375799db056SMatthew G. Knepley   /* Put all names on all processes */
5376799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5377799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5378799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5379799db056SMatthew G. Knepley   gNl = displs[size];
53809371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53819371c9d4SSatish Balay     counts[p] *= gmaxLen;
53829371c9d4SSatish Balay     displs[p] *= gmaxLen;
53839371c9d4SSatish Balay   }
5384799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5385799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5386799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5387799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5388799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5389799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5390799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53919371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53925d6b2bfcSJames Wright       if (glabels[m] == glabels[gl]) goto next_label;
53939566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5394799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53959566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5396799db056SMatthew G. Knepley     ++gl;
53975d6b2bfcSJames Wright   next_label:
53985d6b2bfcSJames Wright     continue;
5399783e2ec8SMatthew G. Knepley   }
5400799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5402783e2ec8SMatthew G. Knepley }
5403783e2ec8SMatthew G. Knepley 
5404d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5405d71ae5a4SJacob Faibussowitsch {
5406e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5407e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5408e5e52638SMatthew G. Knepley 
5409e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54103ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54119566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5412e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54139371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54149371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54159371c9d4SSatish Balay     tmpd[s].label  = NULL;
54169371c9d4SSatish Balay     tmpd[s].fields = NULL;
54179371c9d4SSatish Balay   }
54189566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5419e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5420e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5422e5e52638SMatthew G. Knepley }
5423e5e52638SMatthew G. Knepley 
5424e5e52638SMatthew G. Knepley /*@
542520f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5426e5e52638SMatthew G. Knepley 
542720f4b53cSBarry Smith   Not Collective
5428e5e52638SMatthew G. Knepley 
5429e5e52638SMatthew G. Knepley   Input Parameter:
543020f4b53cSBarry Smith . dm - The `DM`
5431e5e52638SMatthew G. Knepley 
5432e5e52638SMatthew G. Knepley   Output Parameter:
543320f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   Level: intermediate
5436e5e52638SMatthew G. Knepley 
54371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5438e5e52638SMatthew G. Knepley @*/
5439d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5440d71ae5a4SJacob Faibussowitsch {
5441e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5442e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54434f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5444e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5446e5e52638SMatthew G. Knepley }
5447e5e52638SMatthew G. Knepley 
5448e5e52638SMatthew G. Knepley /*@
544920f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5450e5e52638SMatthew G. Knepley 
545120f4b53cSBarry Smith   Logically Collective
5452e5e52638SMatthew G. Knepley 
5453e5e52638SMatthew G. Knepley   Input Parameter:
545420f4b53cSBarry Smith . dm - The `DM`
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley   Level: intermediate
5457e5e52638SMatthew G. Knepley 
54581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5459e5e52638SMatthew G. Knepley @*/
5460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5461d71ae5a4SJacob Faibussowitsch {
5462e5e52638SMatthew G. Knepley   PetscInt s;
5463e5e52638SMatthew G. Knepley 
5464e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5465e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5466e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54679566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
546807218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54699566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54709566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5471e5e52638SMatthew G. Knepley   }
54729566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5473e5e52638SMatthew G. Knepley   dm->probs = NULL;
5474e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5476e5e52638SMatthew G. Knepley }
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley /*@
547920f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5480e5e52638SMatthew G. Knepley 
548120f4b53cSBarry Smith   Not Collective
5482e5e52638SMatthew G. Knepley 
5483e5e52638SMatthew G. Knepley   Input Parameter:
548420f4b53cSBarry Smith . dm - The `DM`
5485e5e52638SMatthew G. Knepley 
5486e5e52638SMatthew G. Knepley   Output Parameter:
548707218a29SMatthew G. Knepley . ds - The default `PetscDS`
5488e5e52638SMatthew G. Knepley 
5489e5e52638SMatthew G. Knepley   Level: intermediate
5490e5e52638SMatthew G. Knepley 
54911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5492e5e52638SMatthew G. Knepley @*/
549307218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5494d71ae5a4SJacob Faibussowitsch {
5495e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5496e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54974f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
549807218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
549907218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5501e5e52638SMatthew G. Knepley }
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley /*@
550420f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5505e5e52638SMatthew G. Knepley 
550620f4b53cSBarry Smith   Not Collective
5507e5e52638SMatthew G. Knepley 
5508e5e52638SMatthew G. Knepley   Input Parameters:
550920f4b53cSBarry Smith + dm    - The `DM`
551020f4b53cSBarry Smith - point - Cell for the `PetscDS`
5511e5e52638SMatthew G. Knepley 
551207218a29SMatthew G. Knepley   Output Parameters:
551307218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
551407218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5515e5e52638SMatthew G. Knepley 
5516e5e52638SMatthew G. Knepley   Level: developer
5517e5e52638SMatthew G. Knepley 
55181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5519e5e52638SMatthew G. Knepley @*/
552007218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5521d71ae5a4SJacob Faibussowitsch {
552207218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5523e5e52638SMatthew G. Knepley   PetscInt s;
5524e5e52638SMatthew G. Knepley 
5525e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5526e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55274f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55284f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
552963a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
553007218a29SMatthew G. Knepley   if (ds) *ds = NULL;
553107218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5532e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5533e5e52638SMatthew G. Knepley     PetscInt val;
5534e5e52638SMatthew G. Knepley 
55359371c9d4SSatish Balay     if (!dm->probs[s].label) {
553607218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55379371c9d4SSatish Balay     } else {
55389566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55399371c9d4SSatish Balay       if (val >= 0) {
554007218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
554107218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55429371c9d4SSatish Balay         break;
55439371c9d4SSatish Balay       }
5544e5e52638SMatthew G. Knepley     }
5545e5e52638SMatthew G. Knepley   }
554607218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5548e5e52638SMatthew G. Knepley }
5549e5e52638SMatthew G. Knepley 
5550e5e52638SMatthew G. Knepley /*@
555120f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5552e5e52638SMatthew G. Knepley 
555320f4b53cSBarry Smith   Not Collective
5554e5e52638SMatthew G. Knepley 
5555e5e52638SMatthew G. Knepley   Input Parameters:
555620f4b53cSBarry Smith + dm    - The `DM`
555720f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5558e5e52638SMatthew G. Knepley 
5559b3cf3223SMatthew G. Knepley   Output Parameters:
556020f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
556107218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
556207218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5563e5e52638SMatthew G. Knepley 
5564e5e52638SMatthew G. Knepley   Level: advanced
5565e5e52638SMatthew G. Knepley 
556620f4b53cSBarry Smith   Note:
556720f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
556820f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
556907218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
557020f4b53cSBarry Smith 
55711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5572e5e52638SMatthew G. Knepley @*/
557307218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5574d71ae5a4SJacob Faibussowitsch {
5575e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5576e5e52638SMatthew G. Knepley 
5577e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5578e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5579e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55809371c9d4SSatish Balay   if (fields) {
55814f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
55829371c9d4SSatish Balay     *fields = NULL;
55839371c9d4SSatish Balay   }
55849371c9d4SSatish Balay   if (ds) {
55854f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
55869371c9d4SSatish Balay     *ds = NULL;
55879371c9d4SSatish Balay   }
558807218a29SMatthew G. Knepley   if (dsIn) {
55894f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
559007218a29SMatthew G. Knepley     *dsIn = NULL;
559107218a29SMatthew G. Knepley   }
5592e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5593154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5594b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5595b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
559607218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55973ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5598b3cf3223SMatthew G. Knepley     }
5599e5e52638SMatthew G. Knepley   }
56003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5601e5e52638SMatthew G. Knepley }
5602e5e52638SMatthew G. Knepley 
5603e5e52638SMatthew G. Knepley /*@
5604bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5605083401c6SMatthew G. Knepley 
560620f4b53cSBarry Smith   Collective
5607083401c6SMatthew G. Knepley 
5608083401c6SMatthew G. Knepley   Input Parameters:
5609bb7acecfSBarry Smith + dm     - The `DM`
561020f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
561107218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
561207218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
561307218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5614083401c6SMatthew G. Knepley 
561520f4b53cSBarry Smith   Level: advanced
561620f4b53cSBarry Smith 
5617bb7acecfSBarry Smith   Note:
5618bb7acecfSBarry 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,
5619083401c6SMatthew G. Knepley   the fields argument is ignored.
5620083401c6SMatthew G. Knepley 
56211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5622083401c6SMatthew G. Knepley @*/
562307218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5624d71ae5a4SJacob Faibussowitsch {
5625083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5626083401c6SMatthew G. Knepley 
5627083401c6SMatthew G. Knepley   PetscFunctionBegin;
5628083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5629083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
563007218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5631064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
563207218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5633083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5634083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56359566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
563607218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5637083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
563807218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56393ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5640083401c6SMatthew G. Knepley     }
5641083401c6SMatthew G. Knepley   }
56429566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56439566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56459566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
564607218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5647083401c6SMatthew G. Knepley   if (!label) {
5648083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5649083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5650083401c6SMatthew G. Knepley     Nds = 0;
5651083401c6SMatthew G. Knepley   }
5652083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5653083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5654083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
565507218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5657083401c6SMatthew G. Knepley }
5658083401c6SMatthew G. Knepley 
5659083401c6SMatthew G. Knepley /*@
566020f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5661e5e52638SMatthew G. Knepley 
566220f4b53cSBarry Smith   Not Collective
5663e5e52638SMatthew G. Knepley 
5664e5e52638SMatthew G. Knepley   Input Parameters:
566520f4b53cSBarry Smith + dm  - The `DM`
5666e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5667e5e52638SMatthew G. Knepley 
5668e5e52638SMatthew G. Knepley   Output Parameters:
566920f4b53cSBarry Smith + label  - The region label, or `NULL`
567020f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
567107218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
567207218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5673e5e52638SMatthew G. Knepley 
5674e5e52638SMatthew G. Knepley   Level: advanced
5675e5e52638SMatthew G. Knepley 
56761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5677e5e52638SMatthew G. Knepley @*/
567807218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5679d71ae5a4SJacob Faibussowitsch {
5680e5e52638SMatthew G. Knepley   PetscInt Nds;
5681e5e52638SMatthew G. Knepley 
5682e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5683e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56849566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
568563a3b9bcSJacob Faibussowitsch   PetscCheck((num >= 0) && (num < Nds), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %" PetscInt_FMT " is not in [0, %" PetscInt_FMT ")", num, Nds);
5686e5e52638SMatthew G. Knepley   if (label) {
56874f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5688e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5689e5e52638SMatthew G. Knepley   }
5690b3cf3223SMatthew G. Knepley   if (fields) {
56914f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5692b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5693b3cf3223SMatthew G. Knepley   }
5694e5e52638SMatthew G. Knepley   if (ds) {
56954f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5696e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5697e5e52638SMatthew G. Knepley   }
569807218a29SMatthew G. Knepley   if (dsIn) {
56994f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
570007218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
570107218a29SMatthew G. Knepley   }
57023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5703e5e52638SMatthew G. Knepley }
5704e5e52638SMatthew G. Knepley 
5705e5e52638SMatthew G. Knepley /*@
570620f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5707e5e52638SMatthew G. Knepley 
570820f4b53cSBarry Smith   Not Collective
5709e5e52638SMatthew G. Knepley 
5710e5e52638SMatthew G. Knepley   Input Parameters:
571120f4b53cSBarry Smith + dm     - The `DM`
5712083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
571320f4b53cSBarry Smith . label  - The region label, or `NULL`
571407218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
571507218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
571607218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5717e5e52638SMatthew G. Knepley 
5718e5e52638SMatthew G. Knepley   Level: advanced
5719e5e52638SMatthew G. Knepley 
57201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5721e5e52638SMatthew G. Knepley @*/
572207218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5723d71ae5a4SJacob Faibussowitsch {
5724083401c6SMatthew G. Knepley   PetscInt Nds;
5725e5e52638SMatthew G. Knepley 
5726e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5727e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5728ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57299566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
573063a3b9bcSJacob 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);
57319566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57329566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5733083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5734083401c6SMatthew G. Knepley   if (fields) {
5735083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57369566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57379566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5738083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5739e5e52638SMatthew G. Knepley   }
5740083401c6SMatthew G. Knepley   if (ds) {
5741083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57429566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57439566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5744083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5745083401c6SMatthew G. Knepley   }
574607218a29SMatthew G. Knepley   if (dsIn) {
574707218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
574807218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
574907218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
575007218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
575107218a29SMatthew G. Knepley   }
57523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5753e5e52638SMatthew G. Knepley }
5754e5e52638SMatthew G. Knepley 
5755e5e52638SMatthew G. Knepley /*@
575620f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57571d3af9e0SMatthew G. Knepley 
575820f4b53cSBarry Smith   Not Collective
57591d3af9e0SMatthew G. Knepley 
57601d3af9e0SMatthew G. Knepley   Input Parameters:
576120f4b53cSBarry Smith + dm - The `DM`
576220f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57631d3af9e0SMatthew G. Knepley 
57641d3af9e0SMatthew G. Knepley   Output Parameter:
57651d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57661d3af9e0SMatthew G. Knepley 
57671d3af9e0SMatthew G. Knepley   Level: advanced
57681d3af9e0SMatthew G. Knepley 
57691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57701d3af9e0SMatthew G. Knepley @*/
5771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5772d71ae5a4SJacob Faibussowitsch {
57731d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57741d3af9e0SMatthew G. Knepley 
57751d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57761d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57771d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57784f572ea9SToby Isaac   PetscAssertPointer(num, 3);
57799566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57809371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57819371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57821d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57831d3af9e0SMatthew G. Knepley   else *num = n;
57843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57851d3af9e0SMatthew G. Knepley }
57861d3af9e0SMatthew G. Knepley 
5787cc4c1da9SBarry Smith /*@
5788bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57892df84da0SMatthew G. Knepley 
579020f4b53cSBarry Smith   Not Collective
57912df84da0SMatthew G. Knepley 
5792f1a722f8SMatthew G. Knepley   Input Parameters:
5793bb7acecfSBarry Smith + dm     - The `DM`
57942df84da0SMatthew G. Knepley . Nc     - The number of components for the field
579520f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5796bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57972df84da0SMatthew G. Knepley 
57982df84da0SMatthew G. Knepley   Output Parameter:
5799bb7acecfSBarry Smith . fem - The `PetscFE`
58002df84da0SMatthew G. Knepley 
580120f4b53cSBarry Smith   Level: intermediate
580220f4b53cSBarry Smith 
5803bb7acecfSBarry Smith   Note:
5804bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58052df84da0SMatthew G. Knepley 
58061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58072df84da0SMatthew G. Knepley @*/
5808d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5809d71ae5a4SJacob Faibussowitsch {
58102df84da0SMatthew G. Knepley   DMPolytopeType ct;
58112df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58122df84da0SMatthew G. Knepley 
58132df84da0SMatthew G. Knepley   PetscFunctionBegin;
58142df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58152df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58164f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58172df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58184f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58199566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58229566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58242df84da0SMatthew G. Knepley }
58252df84da0SMatthew G. Knepley 
58261d3af9e0SMatthew G. Knepley /*@
5827bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5828e5e52638SMatthew G. Knepley 
582920f4b53cSBarry Smith   Collective
5830e5e52638SMatthew G. Knepley 
5831e5e52638SMatthew G. Knepley   Input Parameter:
5832bb7acecfSBarry Smith . dm - The `DM`
5833e5e52638SMatthew G. Knepley 
583420f4b53cSBarry Smith   Options Database Key:
5835bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
583645480ffeSMatthew G. Knepley 
583720f4b53cSBarry Smith   Level: intermediate
583820f4b53cSBarry Smith 
58391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5840e5e52638SMatthew G. Knepley @*/
5841d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5842d71ae5a4SJacob Faibussowitsch {
5843e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5844083401c6SMatthew G. Knepley   PetscDS   dsDef;
5845083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5846f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5847f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5848e5e52638SMatthew G. Knepley 
5849e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5850e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58513ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58529566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58539566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5854083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5856083401c6SMatthew G. Knepley   Nl   = 0;
5857083401c6SMatthew G. Knepley   Ndef = 0;
5858083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5859083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5860083401c6SMatthew G. Knepley     PetscInt l;
5861083401c6SMatthew G. Knepley 
5862f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5863f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5864f918ec44SMatthew G. Knepley     {
5865f918ec44SMatthew G. Knepley       PetscClassId id;
5866f918ec44SMatthew G. Knepley 
58679566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5868f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5869f918ec44SMatthew G. Knepley         Ceed ceed;
5870f918ec44SMatthew G. Knepley 
58719566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58729566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5873f918ec44SMatthew G. Knepley       }
5874f918ec44SMatthew G. Knepley     }
5875f918ec44SMatthew G. Knepley #endif
58769371c9d4SSatish Balay     if (!label) {
58779371c9d4SSatish Balay       ++Ndef;
58789371c9d4SSatish Balay       continue;
58799371c9d4SSatish Balay     }
58809371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58819371c9d4SSatish Balay       if (label == labelSet[l]) break;
5882083401c6SMatthew G. Knepley     if (l < Nl) continue;
5883083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5884083401c6SMatthew G. Knepley   }
5885083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
588607218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5887083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5888b3cf3223SMatthew G. Knepley     IS        fields;
5889b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5890b3cf3223SMatthew G. Knepley 
58919371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58929371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58937a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58949566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58959371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58969371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58979566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58989566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58999566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59009566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
590188f0c812SMatthew G. Knepley 
59029566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
590307218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59049566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59059566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59062df9ee95SMatthew G. Knepley   }
590707218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59089566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5909083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5910083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59110122748bSMatthew G. Knepley     DM              plex;
5912083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5913083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5914083401c6SMatthew G. Knepley     PetscInt       *fields;
5915083401c6SMatthew G. Knepley     const PetscInt *cells;
5916083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59170122748bSMatthew G. Knepley 
59189566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59209566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59219566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59225fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5923083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5924083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5925083401c6SMatthew G. Knepley       IS      pointIS;
5926083401c6SMatthew G. Knepley 
59279566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59289566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59299566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59309566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5931083401c6SMatthew G. Knepley     }
59329566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5933083401c6SMatthew G. Knepley 
59349566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59359566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59369566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59379566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59389566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59399566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59409566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5941083401c6SMatthew G. Knepley 
59429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59439371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59449371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59459566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59469566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59479566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59489566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5949083401c6SMatthew G. Knepley 
59509566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
595107218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59529566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59539566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59549566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59559566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59569566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5957083401c6SMatthew G. Knepley   }
5958083401c6SMatthew G. Knepley   /* Create label DSes
5959083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5960083401c6SMatthew G. Knepley   */
5961083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5962083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5963083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
596407218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5965083401c6SMatthew G. Knepley     IS        fields;
5966083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5967083401c6SMatthew G. Knepley 
59689566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59699371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59709371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59719566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59729371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59739371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59749566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59759566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59769566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59779566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59789566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5979083401c6SMatthew G. Knepley     {
5980083401c6SMatthew G. Knepley       DMPolytopeType ct;
5981083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59825fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59830122748bSMatthew G. Knepley 
59849566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5985665f567fSMatthew G. Knepley       if (lStart >= 0) {
59869566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5987412e9a14SMatthew G. Knepley         switch (ct) {
5988412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5989412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5990412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5991d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5992d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5993d71ae5a4SJacob Faibussowitsch           break;
5994d71ae5a4SJacob Faibussowitsch         default:
5995d71ae5a4SJacob Faibussowitsch           break;
5996412e9a14SMatthew G. Knepley         }
5997665f567fSMatthew G. Knepley       }
5998462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
599907218a29SMatthew G. Knepley       if (isCohesive) {
600007218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
600107218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
600207218a29SMatthew G. Knepley       }
60035fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60045fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60055fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60069566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60079566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
600807218a29SMatthew G. Knepley             if (dsIn) {
600907218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
601007218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
601107218a29SMatthew G. Knepley             }
60125fedec97SMatthew G. Knepley           }
60135fedec97SMatthew G. Knepley           ++nf;
60145fedec97SMatthew G. Knepley         }
60155fedec97SMatthew G. Knepley       }
6016e5e52638SMatthew G. Knepley     }
601707218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
601807218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60199566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
602007218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6021e5e52638SMatthew G. Knepley   }
60229566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6023e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6024083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6025083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
602607218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6027083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6028083401c6SMatthew G. Knepley     const PetscInt *fld;
60295fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60305fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6031e5e52638SMatthew G. Knepley 
60329566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60339566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60349566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60359566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6036083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6037083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60385fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6039e5e52638SMatthew G. Knepley       PetscClassId id;
6040e5e52638SMatthew G. Knepley 
60415fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60429566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60435fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
604407218a29SMatthew G. Knepley       if (isCohesive) {
604507218a29SMatthew G. Knepley         if (!isCohesiveField) {
604607218a29SMatthew G. Knepley           PetscObject bdDisc;
604707218a29SMatthew G. Knepley 
604807218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
604907218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
605007218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
605107218a29SMatthew G. Knepley         } else {
60529566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
605307218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
605407218a29SMatthew G. Knepley         }
605507218a29SMatthew G. Knepley       } else {
605607218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
605707218a29SMatthew G. Knepley       }
6058083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60599566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6060e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6061e5e52638SMatthew G. Knepley     }
60629566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6063e5e52638SMatthew G. Knepley   }
6064f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60659566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6066f9244615SMatthew G. Knepley   if (flg) {
60673b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60683b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
606907218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60703b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60713b4aee56SMatthew G. Knepley 
60729566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
607307218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
607407218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
607507218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
607607218a29SMatthew G. Knepley       }
60773b4aee56SMatthew G. Knepley     }
6078f9244615SMatthew G. Knepley   }
6079e5e52638SMatthew G. Knepley   /* Setup DSes */
6080e5e52638SMatthew G. Knepley   if (doSetup) {
608107218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6082cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6083cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6084cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6085cf1363e4SMatthew G. Knepley       }
608607218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
608707218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
608807218a29SMatthew G. Knepley     }
6089e5e52638SMatthew G. Knepley   }
60903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6091e5e52638SMatthew G. Knepley }
6092e5e52638SMatthew G. Knepley 
6093e5e52638SMatthew G. Knepley /*@
6094d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6095d2b2dc1eSMatthew G. Knepley 
6096d2b2dc1eSMatthew G. Knepley   Input Parameters:
6097d2b2dc1eSMatthew G. Knepley + dm     - The DM
6098d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6099d2b2dc1eSMatthew G. Knepley 
6100d2b2dc1eSMatthew G. Knepley   Level: developer
6101d2b2dc1eSMatthew G. Knepley 
6102d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6103d2b2dc1eSMatthew G. Knepley @*/
6104d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6105d2b2dc1eSMatthew G. Knepley {
6106d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6107d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6108d2b2dc1eSMatthew G. Knepley 
6109d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6110d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6111d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6112d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6113d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6114d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6115d2b2dc1eSMatthew G. Knepley 
6116d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6117d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6118d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6119d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6120d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6121d2b2dc1eSMatthew G. Knepley 
6122d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6123d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6124d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6125d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6126d2b2dc1eSMatthew G. Knepley   }
6127d2b2dc1eSMatthew G. Knepley   if (tensor) {
6128d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6129d2b2dc1eSMatthew G. Knepley   } else {
6130d2b2dc1eSMatthew G. Knepley     PetscSection s;
6131d2b2dc1eSMatthew G. Knepley 
6132d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6133d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6134d2b2dc1eSMatthew G. Knepley   }
6135d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6136d2b2dc1eSMatthew G. Knepley }
6137d2b2dc1eSMatthew G. Knepley 
6138d2b2dc1eSMatthew G. Knepley /*@
6139bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61407f96f943SMatthew G. Knepley 
614120f4b53cSBarry Smith   Collective
6142f2cacb80SMatthew G. Knepley 
61437f96f943SMatthew G. Knepley   Input Parameters:
6144bb7acecfSBarry Smith + dm   - The `DM`
61457f96f943SMatthew G. Knepley - time - The time
61467f96f943SMatthew G. Knepley 
61477f96f943SMatthew G. Knepley   Output Parameters:
614820f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
614920f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
615020f4b53cSBarry Smith 
615120f4b53cSBarry Smith   Level: developer
61527f96f943SMatthew G. Knepley 
6153bb7acecfSBarry Smith   Note:
6154bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61557f96f943SMatthew G. Knepley 
61561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61577f96f943SMatthew G. Knepley @*/
6158d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6159d71ae5a4SJacob Faibussowitsch {
61607f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61617f96f943SMatthew G. Knepley   void   **ectxs;
6162f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61637f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61647f96f943SMatthew G. Knepley 
61657f96f943SMatthew G. Knepley   PetscFunctionBegin;
6166f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6167f60fa741SMatthew G. Knepley   if (u) {
6168f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6169f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6170f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6171f60fa741SMatthew G. Knepley   }
6172f60fa741SMatthew G. Knepley   if (u_t) {
6173f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6174f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6175f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6176f60fa741SMatthew G. Knepley   }
61779566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
61789566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
61799566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61807f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
61817f96f943SMatthew G. Knepley     PetscDS         ds;
61827f96f943SMatthew G. Knepley     DMLabel         label;
61837f96f943SMatthew G. Knepley     IS              fieldIS;
61847f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
61857f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
61867f96f943SMatthew G. Knepley 
618707218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
61889566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
61899566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
61909566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
61919566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6192f2cacb80SMatthew G. Knepley     if (u) {
6193f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6194f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6195f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
61967f96f943SMatthew G. Knepley     }
6197f2cacb80SMatthew G. Knepley     if (u_t) {
61989566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
61999566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6200f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6201f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6202f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6203f2cacb80SMatthew G. Knepley     }
62049566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6205f2cacb80SMatthew G. Knepley   }
6206f2cacb80SMatthew G. Knepley   if (u) {
62079566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62089566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6209f2cacb80SMatthew G. Knepley   }
6210f2cacb80SMatthew G. Knepley   if (u_t) {
62119566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62129566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6213f2cacb80SMatthew G. Knepley   }
62149566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6215f60fa741SMatthew G. Knepley   if (u) {
6216f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6217f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6218f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6219f60fa741SMatthew G. Knepley   }
6220f60fa741SMatthew G. Knepley   if (u_t) {
6221f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6222f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6223f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6224f60fa741SMatthew G. Knepley   }
62253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62267f96f943SMatthew G. Knepley }
62277f96f943SMatthew G. Knepley 
6228bb4b53efSMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscInt minDegree, PetscInt maxDegree, PetscDS ds, PetscDS dsIn)
6229d71ae5a4SJacob Faibussowitsch {
623007218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
623145480ffeSMatthew G. Knepley 
623245480ffeSMatthew G. Knepley   PetscFunctionBegin;
62339566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
6234bb4b53efSMatthew G. Knepley   PetscCall(PetscDSCopy(ds, minDegree, maxDegree, dm, dsNew));
623507218a29SMatthew G. Knepley   if (dsIn) {
623607218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
6237bb4b53efSMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, minDegree, maxDegree, dm, dsInNew));
623845480ffeSMatthew G. Knepley   }
623907218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62409566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
624107218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
624345480ffeSMatthew G. Knepley }
624445480ffeSMatthew G. Knepley 
62457f96f943SMatthew G. Knepley /*@
6246bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6247e5e52638SMatthew G. Knepley 
624820f4b53cSBarry Smith   Collective
6249e5e52638SMatthew G. Knepley 
6250bb4b53efSMatthew G. Knepley   Input Parameters:
6251bb4b53efSMatthew G. Knepley + dm        - The `DM`
6252bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
6253bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
6254e5e52638SMatthew G. Knepley 
6255e5e52638SMatthew G. Knepley   Output Parameter:
6256bb7acecfSBarry Smith . newdm - The `DM`
6257e5e52638SMatthew G. Knepley 
6258e5e52638SMatthew G. Knepley   Level: advanced
6259e5e52638SMatthew G. Knepley 
62601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6261e5e52638SMatthew G. Knepley @*/
6262bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
6263d71ae5a4SJacob Faibussowitsch {
6264e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6265e5e52638SMatthew G. Knepley 
6266e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62673ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62689566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62699566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6270e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6271e5e52638SMatthew G. Knepley     DMLabel  label;
6272b3cf3223SMatthew G. Knepley     IS       fields;
627307218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6274783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6275e5e52638SMatthew G. Knepley 
627607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6277b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
6278bb4b53efSMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, minDegree, maxDegree, ds, dsIn));
6279d5b43468SJose E. Roman     /* Complete new labels in the new DS */
628007218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
62819566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6282783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6283b8025e53SMatthew G. Knepley       PetscWeakForm wf;
628445480ffeSMatthew G. Knepley       DMLabel       label;
6285783e2ec8SMatthew G. Knepley       PetscInt      field;
6286783e2ec8SMatthew G. Knepley 
62879566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
62889566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6289783e2ec8SMatthew G. Knepley     }
6290e5e52638SMatthew G. Knepley   }
6291799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
62923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6293e5e52638SMatthew G. Knepley }
6294e5e52638SMatthew G. Knepley 
6295e5e52638SMatthew G. Knepley /*@
6296bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6297e5e52638SMatthew G. Knepley 
629820f4b53cSBarry Smith   Collective
6299e5e52638SMatthew G. Knepley 
6300e5e52638SMatthew G. Knepley   Input Parameter:
6301bb7acecfSBarry Smith . dm - The `DM`
6302e5e52638SMatthew G. Knepley 
6303e5e52638SMatthew G. Knepley   Output Parameter:
6304bb7acecfSBarry Smith . newdm - The `DM`
6305e5e52638SMatthew G. Knepley 
6306e5e52638SMatthew G. Knepley   Level: advanced
6307e5e52638SMatthew G. Knepley 
630873ff1848SBarry Smith   Developer Note:
6309bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6310bb7acecfSBarry Smith 
63111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6312e5e52638SMatthew G. Knepley @*/
6313d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6314d71ae5a4SJacob Faibussowitsch {
6315e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6316bb4b53efSMatthew G. Knepley   PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
6317bb4b53efSMatthew G. Knepley   PetscCall(DMCopyDS(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
63183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6319e5e52638SMatthew G. Knepley }
6320e5e52638SMatthew G. Knepley 
6321c73cfb54SMatthew G. Knepley /*@
6322bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6323c73cfb54SMatthew G. Knepley 
632420f4b53cSBarry Smith   Not Collective
6325c73cfb54SMatthew G. Knepley 
6326c73cfb54SMatthew G. Knepley   Input Parameter:
6327bb7acecfSBarry Smith . dm - The `DM`
6328c73cfb54SMatthew G. Knepley 
6329c73cfb54SMatthew G. Knepley   Output Parameter:
6330c73cfb54SMatthew G. Knepley . dim - The topological dimension
6331c73cfb54SMatthew G. Knepley 
6332c73cfb54SMatthew G. Knepley   Level: beginner
6333c73cfb54SMatthew G. Knepley 
63341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6335c73cfb54SMatthew G. Knepley @*/
6336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6337d71ae5a4SJacob Faibussowitsch {
6338c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6339c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63404f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6341c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6343c73cfb54SMatthew G. Knepley }
6344c73cfb54SMatthew G. Knepley 
6345c73cfb54SMatthew G. Knepley /*@
6346bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6347c73cfb54SMatthew G. Knepley 
634820f4b53cSBarry Smith   Collective
6349c73cfb54SMatthew G. Knepley 
6350c73cfb54SMatthew G. Knepley   Input Parameters:
6351bb7acecfSBarry Smith + dm  - The `DM`
6352c73cfb54SMatthew G. Knepley - dim - The topological dimension
6353c73cfb54SMatthew G. Knepley 
6354c73cfb54SMatthew G. Knepley   Level: beginner
6355c73cfb54SMatthew G. Knepley 
63561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6357c73cfb54SMatthew G. Knepley @*/
6358d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6359d71ae5a4SJacob Faibussowitsch {
6360e5e52638SMatthew G. Knepley   PetscDS  ds;
636145480ffeSMatthew G. Knepley   PetscInt Nds, n;
6362f17e8794SMatthew G. Knepley 
6363c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6364c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6365c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6366c73cfb54SMatthew G. Knepley   dm->dim = dim;
6367d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63689566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
636945480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
637007218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63719566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
637245480ffeSMatthew G. Knepley     }
6373d17bd122SMatthew G. Knepley   }
63743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6375c73cfb54SMatthew G. Knepley }
6376c73cfb54SMatthew G. Knepley 
6377793f3fe5SMatthew G. Knepley /*@
6378793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6379793f3fe5SMatthew G. Knepley 
638020f4b53cSBarry Smith   Collective
6381793f3fe5SMatthew G. Knepley 
6382793f3fe5SMatthew G. Knepley   Input Parameters:
6383bb7acecfSBarry Smith + dm  - the `DM`
6384793f3fe5SMatthew G. Knepley - dim - the dimension
6385793f3fe5SMatthew G. Knepley 
6386793f3fe5SMatthew G. Knepley   Output Parameters:
6387793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6388aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6389793f3fe5SMatthew G. Knepley 
639020f4b53cSBarry Smith   Level: intermediate
639120f4b53cSBarry Smith 
6392793f3fe5SMatthew G. Knepley   Note:
6393793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6394a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6395793f3fe5SMatthew G. Knepley   then the interval is empty.
6396793f3fe5SMatthew G. Knepley 
63971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6398793f3fe5SMatthew G. Knepley @*/
6399d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6400d71ae5a4SJacob Faibussowitsch {
6401793f3fe5SMatthew G. Knepley   PetscInt d;
6402793f3fe5SMatthew G. Knepley 
6403793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6404793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64059566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64067a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6407dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6409793f3fe5SMatthew G. Knepley }
6410793f3fe5SMatthew G. Knepley 
64116636e97aSMatthew G Knepley /*@
6412bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6413f4d763aaSMatthew G. Knepley 
641420f4b53cSBarry Smith   Collective
64158f700142SStefano Zampini 
6416f4d763aaSMatthew G. Knepley   Input Parameter:
6417bb7acecfSBarry Smith . dm - The original `DM`
6418f4d763aaSMatthew G. Knepley 
6419f4d763aaSMatthew G. Knepley   Output Parameter:
6420bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6421f4d763aaSMatthew G. Knepley 
6422f4d763aaSMatthew G. Knepley   Level: intermediate
6423f4d763aaSMatthew G. Knepley 
6424bb7acecfSBarry Smith   Note:
6425bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6426bb7acecfSBarry 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
6427bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6428bb7acecfSBarry Smith 
64291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6430f4d763aaSMatthew G. Knepley @*/
6431d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6432d71ae5a4SJacob Faibussowitsch {
6433c26acbdeSMatthew G. Knepley   PetscSection section;
6434eb9d3e4dSMatthew G. Knepley   IS           perm;
6435eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
6436*92a26154SJames Wright   PetscInt     num_face_sfs = 0;
643714f150ffSMatthew G. Knepley 
643814f150ffSMatthew G. Knepley   PetscFunctionBegin;
643914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64404f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64419566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64429566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6443eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6444*92a26154SJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, NULL));
6445*92a26154SJames Wright   newDM = hasConstraints || perm || (num_face_sfs > 0) ? PETSC_TRUE : PETSC_FALSE;
6446462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6447eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6448c26acbdeSMatthew G. Knepley     *odm = dm;
64493ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6450c26acbdeSMatthew G. Knepley   }
645114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6452c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
6453*92a26154SJames Wright     PetscSF      sf, sfNatural;
6454eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
645514f150ffSMatthew G. Knepley 
64569566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64579566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64589566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64599566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64609566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
6461*92a26154SJames Wright     PetscCall(DMGetNaturalSF(dm, &sfNatural));
6462*92a26154SJames Wright     PetscCall(DMSetNaturalSF(dm->dmBC, sfNatural));
64639566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6464eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
64659566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64669566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
646714f150ffSMatthew G. Knepley   }
646814f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
647014f150ffSMatthew G. Knepley }
6471f4d763aaSMatthew G. Knepley 
6472f4d763aaSMatthew G. Knepley /*@
6473cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6474f4d763aaSMatthew G. Knepley 
6475f4d763aaSMatthew G. Knepley   Input Parameter:
6476bb7acecfSBarry Smith . dm - The original `DM`
6477f4d763aaSMatthew G. Knepley 
6478cdb7a50dSMatthew G. Knepley   Output Parameters:
6479cdb7a50dSMatthew G. Knepley + num - The output sequence number
6480cdb7a50dSMatthew G. Knepley - val - The output sequence value
6481f4d763aaSMatthew G. Knepley 
6482f4d763aaSMatthew G. Knepley   Level: intermediate
6483f4d763aaSMatthew G. Knepley 
6484bb7acecfSBarry Smith   Note:
6485bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6486bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6487bb7acecfSBarry Smith 
648873ff1848SBarry Smith   Developer Note:
6489bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6490bb7acecfSBarry Smith   not directly related to the `DM`.
6491f4d763aaSMatthew G. Knepley 
64921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6493f4d763aaSMatthew G. Knepley @*/
6494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6495d71ae5a4SJacob Faibussowitsch {
6496f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6497f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64989371c9d4SSatish Balay   if (num) {
64994f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65009371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65019371c9d4SSatish Balay   }
65029371c9d4SSatish Balay   if (val) {
65034f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65049371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65059371c9d4SSatish Balay   }
65063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6507f4d763aaSMatthew G. Knepley }
6508f4d763aaSMatthew G. Knepley 
6509f4d763aaSMatthew G. Knepley /*@
6510cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6511f4d763aaSMatthew G. Knepley 
6512f4d763aaSMatthew G. Knepley   Input Parameters:
6513bb7acecfSBarry Smith + dm  - The original `DM`
6514cdb7a50dSMatthew G. Knepley . num - The output sequence number
6515cdb7a50dSMatthew G. Knepley - val - The output sequence value
6516f4d763aaSMatthew G. Knepley 
6517f4d763aaSMatthew G. Knepley   Level: intermediate
6518f4d763aaSMatthew G. Knepley 
6519bb7acecfSBarry Smith   Note:
6520bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6521bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6522f4d763aaSMatthew G. Knepley 
65231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6524f4d763aaSMatthew G. Knepley @*/
6525d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6526d71ae5a4SJacob Faibussowitsch {
6527f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6528f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6529f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6530cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6532cdb7a50dSMatthew G. Knepley }
6533cdb7a50dSMatthew G. Knepley 
65345d83a8b1SBarry Smith /*@
6535bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6536cdb7a50dSMatthew G. Knepley 
6537cdb7a50dSMatthew G. Knepley   Input Parameters:
6538bb7acecfSBarry Smith + dm     - The original `DM`
6539b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6540cdb7a50dSMatthew G. Knepley . name   - The sequence name
6541cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6542cdb7a50dSMatthew G. Knepley 
6543cdb7a50dSMatthew G. Knepley   Output Parameter:
6544cdb7a50dSMatthew G. Knepley . val - The output sequence value
6545cdb7a50dSMatthew G. Knepley 
6546cdb7a50dSMatthew G. Knepley   Level: intermediate
6547cdb7a50dSMatthew G. Knepley 
6548bb7acecfSBarry Smith   Note:
6549bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6550bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6551bb7acecfSBarry Smith 
655273ff1848SBarry Smith   Developer Note:
6553bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6554cdb7a50dSMatthew G. Knepley 
65551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6556cdb7a50dSMatthew G. Knepley @*/
6557b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6558d71ae5a4SJacob Faibussowitsch {
6559cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6560cdb7a50dSMatthew G. Knepley 
6561cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6562cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6563cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6564b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
65654f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65669566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6567cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6568cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6569cdb7a50dSMatthew G. Knepley     PetscScalar value;
6570cdb7a50dSMatthew G. Knepley 
65719566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65724aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6573cdb7a50dSMatthew G. Knepley #endif
6574cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6576f4d763aaSMatthew G. Knepley }
65778e4ac7eaSMatthew G. Knepley 
65788e4ac7eaSMatthew G. Knepley /*@
6579b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6580b2033f5dSMatthew G. Knepley 
6581b2033f5dSMatthew G. Knepley   Input Parameters:
6582b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6583b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6584b2033f5dSMatthew G. Knepley - name   - The sequence name
6585b2033f5dSMatthew G. Knepley 
6586b2033f5dSMatthew G. Knepley   Output Parameter:
6587b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6588b2033f5dSMatthew G. Knepley 
6589b2033f5dSMatthew G. Knepley   Level: intermediate
6590b2033f5dSMatthew G. Knepley 
6591b2033f5dSMatthew G. Knepley   Note:
6592b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6593b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6594b2033f5dSMatthew G. Knepley 
6595b2033f5dSMatthew G. Knepley   Developer Note:
6596b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6597b2033f5dSMatthew G. Knepley 
6598b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6599b2033f5dSMatthew G. Knepley @*/
6600b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6601b2033f5dSMatthew G. Knepley {
6602b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6603b2033f5dSMatthew G. Knepley 
6604b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6605b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6606b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6607b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6608b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6609b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6610b2033f5dSMatthew G. Knepley   if (ishdf5) {
6611b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6612b2033f5dSMatthew G. Knepley     PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6613b2033f5dSMatthew G. Knepley #endif
6614b2033f5dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6615b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6616b2033f5dSMatthew G. Knepley }
6617b2033f5dSMatthew G. Knepley 
6618b2033f5dSMatthew G. Knepley /*@
6619bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66208e4ac7eaSMatthew G. Knepley 
662120f4b53cSBarry Smith   Not Collective
66228e4ac7eaSMatthew G. Knepley 
66238e4ac7eaSMatthew G. Knepley   Input Parameter:
6624bb7acecfSBarry Smith . dm - The `DM`
66258e4ac7eaSMatthew G. Knepley 
66268e4ac7eaSMatthew G. Knepley   Output Parameter:
6627bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66288e4ac7eaSMatthew G. Knepley 
66298e4ac7eaSMatthew G. Knepley   Level: beginner
66308e4ac7eaSMatthew G. Knepley 
66311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66328e4ac7eaSMatthew G. Knepley @*/
6633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6634d71ae5a4SJacob Faibussowitsch {
66358e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66368e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66374f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66388e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66408e4ac7eaSMatthew G. Knepley }
66418e4ac7eaSMatthew G. Knepley 
66428e4ac7eaSMatthew G. Knepley /*@
6643bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66448e4ac7eaSMatthew G. Knepley 
664520f4b53cSBarry Smith   Collective
66468e4ac7eaSMatthew G. Knepley 
66478e4ac7eaSMatthew G. Knepley   Input Parameters:
6648bb7acecfSBarry Smith + dm         - The `DM`
6649bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66508e4ac7eaSMatthew G. Knepley 
665173ff1848SBarry Smith   Level: beginner
665273ff1848SBarry Smith 
6653bb7acecfSBarry Smith   Note:
6654bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
66555d3b26e6SMatthew G. Knepley 
66561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
66578e4ac7eaSMatthew G. Knepley @*/
6658d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6659d71ae5a4SJacob Faibussowitsch {
66608e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66618e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66628833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66638e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66658e4ac7eaSMatthew G. Knepley }
6666c58f1c22SToby Isaac 
6667cc4c1da9SBarry Smith /*@
6668bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6669c58f1c22SToby Isaac 
6670c58f1c22SToby Isaac   Not Collective
6671c58f1c22SToby Isaac 
6672c58f1c22SToby Isaac   Input Parameters:
6673bb7acecfSBarry Smith + dm   - The `DM` object
6674c58f1c22SToby Isaac - name - The label name
6675c58f1c22SToby Isaac 
6676c58f1c22SToby Isaac   Level: intermediate
6677c58f1c22SToby Isaac 
66781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6679c58f1c22SToby Isaac @*/
6680d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6681d71ae5a4SJacob Faibussowitsch {
66825d80c0bfSVaclav Hapla   PetscBool flg;
66835d80c0bfSVaclav Hapla   DMLabel   label;
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   PetscFunctionBegin;
6686c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66874f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66889566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6689c58f1c22SToby Isaac   if (!flg) {
66909566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66919566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66929566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6693c58f1c22SToby Isaac   }
66943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6695c58f1c22SToby Isaac }
6696c58f1c22SToby Isaac 
6697cc4c1da9SBarry Smith /*@
6698bb7acecfSBarry 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.
66990fdc7489SMatthew Knepley 
67000fdc7489SMatthew Knepley   Not Collective
67010fdc7489SMatthew Knepley 
67020fdc7489SMatthew Knepley   Input Parameters:
6703bb7acecfSBarry Smith + dm   - The `DM` object
67040fdc7489SMatthew Knepley . l    - The index for the label
67050fdc7489SMatthew Knepley - name - The label name
67060fdc7489SMatthew Knepley 
67070fdc7489SMatthew Knepley   Level: intermediate
67080fdc7489SMatthew Knepley 
67091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
67100fdc7489SMatthew Knepley @*/
6711d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6712d71ae5a4SJacob Faibussowitsch {
67130fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
67140fdc7489SMatthew Knepley   DMLabel     label;
67150fdc7489SMatthew Knepley   PetscInt    Nl, m;
67160fdc7489SMatthew Knepley   PetscBool   flg, match;
67170fdc7489SMatthew Knepley   const char *lname;
67180fdc7489SMatthew Knepley 
67190fdc7489SMatthew Knepley   PetscFunctionBegin;
67200fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67214f572ea9SToby Isaac   PetscAssertPointer(name, 3);
67229566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67230fdc7489SMatthew Knepley   if (!flg) {
67249566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67259566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67269566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67270fdc7489SMatthew Knepley   }
67289566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
672963a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67300fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67319566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67329566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67330fdc7489SMatthew Knepley     if (match) break;
67340fdc7489SMatthew Knepley   }
67353ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67360fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67370fdc7489SMatthew Knepley   else prev->next = orig->next;
67380fdc7489SMatthew Knepley   if (!l) {
67390fdc7489SMatthew Knepley     orig->next = dm->labels;
67400fdc7489SMatthew Knepley     dm->labels = orig;
67410fdc7489SMatthew Knepley   } else {
6742fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67430fdc7489SMatthew Knepley     orig->next = prev->next;
67440fdc7489SMatthew Knepley     prev->next = orig;
67450fdc7489SMatthew Knepley   }
67463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67470fdc7489SMatthew Knepley }
67480fdc7489SMatthew Knepley 
6749cc4c1da9SBarry Smith /*@
6750bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6751c58f1c22SToby Isaac 
6752c58f1c22SToby Isaac   Not Collective
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac   Input Parameters:
6755bb7acecfSBarry Smith + dm    - The `DM` object
6756c58f1c22SToby Isaac . name  - The label name
6757c58f1c22SToby Isaac - point - The mesh point
6758c58f1c22SToby Isaac 
6759c58f1c22SToby Isaac   Output Parameter:
6760c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6761c58f1c22SToby Isaac 
6762c58f1c22SToby Isaac   Level: beginner
6763c58f1c22SToby Isaac 
67641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6765c58f1c22SToby Isaac @*/
6766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6767d71ae5a4SJacob Faibussowitsch {
6768c58f1c22SToby Isaac   DMLabel label;
6769c58f1c22SToby Isaac 
6770c58f1c22SToby Isaac   PetscFunctionBegin;
6771c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67724f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67747a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
67759566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
67763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6777c58f1c22SToby Isaac }
6778c58f1c22SToby Isaac 
6779cc4c1da9SBarry Smith /*@
6780bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6781c58f1c22SToby Isaac 
6782c58f1c22SToby Isaac   Not Collective
6783c58f1c22SToby Isaac 
6784c58f1c22SToby Isaac   Input Parameters:
6785bb7acecfSBarry Smith + dm    - The `DM` object
6786c58f1c22SToby Isaac . name  - The label name
6787c58f1c22SToby Isaac . point - The mesh point
6788c58f1c22SToby Isaac - value - The label value for this point
6789c58f1c22SToby Isaac 
6790c58f1c22SToby Isaac   Output Parameter:
6791c58f1c22SToby Isaac 
6792c58f1c22SToby Isaac   Level: beginner
6793c58f1c22SToby Isaac 
67941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6795c58f1c22SToby Isaac @*/
6796d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6797d71ae5a4SJacob Faibussowitsch {
6798c58f1c22SToby Isaac   DMLabel label;
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   PetscFunctionBegin;
6801c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68024f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68039566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6804c58f1c22SToby Isaac   if (!label) {
68059566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68069566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6807c58f1c22SToby Isaac   }
68089566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
68093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6810c58f1c22SToby Isaac }
6811c58f1c22SToby Isaac 
6812cc4c1da9SBarry Smith /*@
6813bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   Not Collective
6816c58f1c22SToby Isaac 
6817c58f1c22SToby Isaac   Input Parameters:
6818bb7acecfSBarry Smith + dm    - The `DM` object
6819c58f1c22SToby Isaac . name  - The label name
6820c58f1c22SToby Isaac . point - The mesh point
6821c58f1c22SToby Isaac - value - The label value for this point
6822c58f1c22SToby Isaac 
6823c58f1c22SToby Isaac   Level: beginner
6824c58f1c22SToby Isaac 
68251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6826c58f1c22SToby Isaac @*/
6827d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6828d71ae5a4SJacob Faibussowitsch {
6829c58f1c22SToby Isaac   DMLabel label;
6830c58f1c22SToby Isaac 
6831c58f1c22SToby Isaac   PetscFunctionBegin;
6832c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68334f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68349566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68353ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68369566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6838c58f1c22SToby Isaac }
6839c58f1c22SToby Isaac 
6840cc4c1da9SBarry Smith /*@
6841bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   Not Collective
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   Input Parameters:
6846bb7acecfSBarry Smith + dm   - The `DM` object
6847c58f1c22SToby Isaac - name - The label name
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac   Output Parameter:
6850c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   Level: beginner
6853c58f1c22SToby Isaac 
685473ff1848SBarry Smith   Developer Note:
6855bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6856bb7acecfSBarry Smith 
68571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6858c58f1c22SToby Isaac @*/
6859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6860d71ae5a4SJacob Faibussowitsch {
6861c58f1c22SToby Isaac   DMLabel label;
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   PetscFunctionBegin;
6864c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68654f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68664f572ea9SToby Isaac   PetscAssertPointer(size, 3);
68679566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6868c58f1c22SToby Isaac   *size = 0;
68693ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68709566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
68713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6872c58f1c22SToby Isaac }
6873c58f1c22SToby Isaac 
6874cc4c1da9SBarry Smith /*@
6875bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Not Collective
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac   Input Parameters:
688060225df5SJacob Faibussowitsch + dm   - The `DM` object
6881c58f1c22SToby Isaac - name - The label name
6882c58f1c22SToby Isaac 
6883c58f1c22SToby Isaac   Output Parameter:
688420f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6885c58f1c22SToby Isaac 
6886c58f1c22SToby Isaac   Level: beginner
6887c58f1c22SToby Isaac 
68881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6889c58f1c22SToby Isaac @*/
6890d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6891d71ae5a4SJacob Faibussowitsch {
6892c58f1c22SToby Isaac   DMLabel label;
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   PetscFunctionBegin;
6895c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68964f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68974f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
68989566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6899c58f1c22SToby Isaac   *ids = NULL;
6900dab2e251SBlaise Bourdin   if (label) {
69019566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6902dab2e251SBlaise Bourdin   } else {
6903dab2e251SBlaise Bourdin     /* returning an empty IS */
69049566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6905dab2e251SBlaise Bourdin   }
69063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6907c58f1c22SToby Isaac }
6908c58f1c22SToby Isaac 
6909cc4c1da9SBarry Smith /*@
6910c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6911c58f1c22SToby Isaac 
6912c58f1c22SToby Isaac   Not Collective
6913c58f1c22SToby Isaac 
6914c58f1c22SToby Isaac   Input Parameters:
6915bb7acecfSBarry Smith + dm    - The `DM` object
6916b6971eaeSBarry Smith . name  - The label name of the stratum
6917c58f1c22SToby Isaac - value - The stratum value
6918c58f1c22SToby Isaac 
6919c58f1c22SToby Isaac   Output Parameter:
6920bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6921c58f1c22SToby Isaac 
6922c58f1c22SToby Isaac   Level: beginner
6923c58f1c22SToby Isaac 
69241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6925c58f1c22SToby Isaac @*/
6926d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6927d71ae5a4SJacob Faibussowitsch {
6928c58f1c22SToby Isaac   DMLabel label;
6929c58f1c22SToby Isaac 
6930c58f1c22SToby Isaac   PetscFunctionBegin;
6931c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69324f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69334f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69349566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6935c58f1c22SToby Isaac   *size = 0;
69363ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69379566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6939c58f1c22SToby Isaac }
6940c58f1c22SToby Isaac 
6941cc4c1da9SBarry Smith /*@
6942c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6943c58f1c22SToby Isaac 
6944c58f1c22SToby Isaac   Not Collective
6945c58f1c22SToby Isaac 
6946c58f1c22SToby Isaac   Input Parameters:
6947bb7acecfSBarry Smith + dm    - The `DM` object
6948c58f1c22SToby Isaac . name  - The label name
6949c58f1c22SToby Isaac - value - The stratum value
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac   Output Parameter:
695220f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6953c58f1c22SToby Isaac 
6954c58f1c22SToby Isaac   Level: beginner
6955c58f1c22SToby Isaac 
69561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6957c58f1c22SToby Isaac @*/
6958d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6959d71ae5a4SJacob Faibussowitsch {
6960c58f1c22SToby Isaac   DMLabel label;
6961c58f1c22SToby Isaac 
6962c58f1c22SToby Isaac   PetscFunctionBegin;
6963c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69644f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69654f572ea9SToby Isaac   PetscAssertPointer(points, 4);
69669566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6967c58f1c22SToby Isaac   *points = NULL;
69683ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69699566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
69703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6971c58f1c22SToby Isaac }
6972c58f1c22SToby Isaac 
6973cc4c1da9SBarry Smith /*@
69749044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69754de306b1SToby Isaac 
69764de306b1SToby Isaac   Not Collective
69774de306b1SToby Isaac 
69784de306b1SToby Isaac   Input Parameters:
6979bb7acecfSBarry Smith + dm     - The `DM` object
69804de306b1SToby Isaac . name   - The label name
69814de306b1SToby Isaac . value  - The stratum value
69824de306b1SToby Isaac - points - The stratum points
69834de306b1SToby Isaac 
69844de306b1SToby Isaac   Level: beginner
69854de306b1SToby Isaac 
69861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
69874de306b1SToby Isaac @*/
6988d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6989d71ae5a4SJacob Faibussowitsch {
69904de306b1SToby Isaac   DMLabel label;
69914de306b1SToby Isaac 
69924de306b1SToby Isaac   PetscFunctionBegin;
69934de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69944f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6995292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
69969566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69973ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69989566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
69993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70004de306b1SToby Isaac }
70014de306b1SToby Isaac 
7002cc4c1da9SBarry Smith /*@
7003bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7004c58f1c22SToby Isaac 
7005c58f1c22SToby Isaac   Not Collective
7006c58f1c22SToby Isaac 
7007c58f1c22SToby Isaac   Input Parameters:
7008bb7acecfSBarry Smith + dm    - The `DM` object
7009c58f1c22SToby Isaac . name  - The label name
7010c58f1c22SToby Isaac - value - The label value for this point
7011c58f1c22SToby Isaac 
7012c58f1c22SToby Isaac   Output Parameter:
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   Level: beginner
7015c58f1c22SToby Isaac 
70161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7017c58f1c22SToby Isaac @*/
7018d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7019d71ae5a4SJacob Faibussowitsch {
7020c58f1c22SToby Isaac   DMLabel label;
7021c58f1c22SToby Isaac 
7022c58f1c22SToby Isaac   PetscFunctionBegin;
7023c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70244f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70263ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70279566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7029c58f1c22SToby Isaac }
7030c58f1c22SToby Isaac 
7031c58f1c22SToby Isaac /*@
7032bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7033c58f1c22SToby Isaac 
7034c58f1c22SToby Isaac   Not Collective
7035c58f1c22SToby Isaac 
7036c58f1c22SToby Isaac   Input Parameter:
7037bb7acecfSBarry Smith . dm - The `DM` object
7038c58f1c22SToby Isaac 
7039c58f1c22SToby Isaac   Output Parameter:
7040c58f1c22SToby Isaac . numLabels - the number of Labels
7041c58f1c22SToby Isaac 
7042c58f1c22SToby Isaac   Level: intermediate
7043c58f1c22SToby Isaac 
70441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7045c58f1c22SToby Isaac @*/
7046d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7047d71ae5a4SJacob Faibussowitsch {
70485d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7049c58f1c22SToby Isaac   PetscInt    n    = 0;
7050c58f1c22SToby Isaac 
7051c58f1c22SToby Isaac   PetscFunctionBegin;
7052c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70534f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70549371c9d4SSatish Balay   while (next) {
70559371c9d4SSatish Balay     ++n;
70569371c9d4SSatish Balay     next = next->next;
70579371c9d4SSatish Balay   }
7058c58f1c22SToby Isaac   *numLabels = n;
70593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7060c58f1c22SToby Isaac }
7061c58f1c22SToby Isaac 
7062cc4c1da9SBarry Smith /*@
7063c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Not Collective
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac   Input Parameters:
7068bb7acecfSBarry Smith + dm - The `DM` object
7069c58f1c22SToby Isaac - n  - the label number
7070c58f1c22SToby Isaac 
7071c58f1c22SToby Isaac   Output Parameter:
7072c58f1c22SToby Isaac . name - the label name
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac   Level: intermediate
7075c58f1c22SToby Isaac 
707673ff1848SBarry Smith   Developer Note:
7077bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7078bb7acecfSBarry Smith 
70791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7080c58f1c22SToby Isaac @*/
7081cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7082d71ae5a4SJacob Faibussowitsch {
70835d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7084c58f1c22SToby Isaac   PetscInt    l    = 0;
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   PetscFunctionBegin;
7087c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70884f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7089c58f1c22SToby Isaac   while (next) {
7090c58f1c22SToby Isaac     if (l == n) {
70919566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
70923ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7093c58f1c22SToby Isaac     }
7094c58f1c22SToby Isaac     ++l;
7095c58f1c22SToby Isaac     next = next->next;
7096c58f1c22SToby Isaac   }
709763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7098c58f1c22SToby Isaac }
7099c58f1c22SToby Isaac 
7100cc4c1da9SBarry Smith /*@
7101bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7102c58f1c22SToby Isaac 
7103c58f1c22SToby Isaac   Not Collective
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac   Input Parameters:
7106bb7acecfSBarry Smith + dm   - The `DM` object
7107c58f1c22SToby Isaac - name - The label name
7108c58f1c22SToby Isaac 
7109c58f1c22SToby Isaac   Output Parameter:
7110bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7111c58f1c22SToby Isaac 
7112c58f1c22SToby Isaac   Level: intermediate
7113c58f1c22SToby Isaac 
71141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7115c58f1c22SToby Isaac @*/
7116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7117d71ae5a4SJacob Faibussowitsch {
71185d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7119d67d17b1SMatthew G. Knepley   const char *lname;
7120c58f1c22SToby Isaac 
7121c58f1c22SToby Isaac   PetscFunctionBegin;
7122c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71234f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71244f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7125c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7126c58f1c22SToby Isaac   while (next) {
71279566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71289566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7129c58f1c22SToby Isaac     if (*hasLabel) break;
7130c58f1c22SToby Isaac     next = next->next;
7131c58f1c22SToby Isaac   }
71323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7133c58f1c22SToby Isaac }
7134c58f1c22SToby Isaac 
7135a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7136cc4c1da9SBarry Smith /*@
713720f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7138c58f1c22SToby Isaac 
7139c58f1c22SToby Isaac   Not Collective
7140c58f1c22SToby Isaac 
7141c58f1c22SToby Isaac   Input Parameters:
7142bb7acecfSBarry Smith + dm   - The `DM` object
7143c58f1c22SToby Isaac - name - The label name
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac   Output Parameter:
714620f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7147c58f1c22SToby Isaac 
7148bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7149bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7150bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7151bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7152bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7153bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7154bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71556d7c9049SMatthew G. Knepley 
7156c58f1c22SToby Isaac   Level: intermediate
7157c58f1c22SToby Isaac 
715860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7159c58f1c22SToby Isaac @*/
7160d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7161d71ae5a4SJacob Faibussowitsch {
71625d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7163c58f1c22SToby Isaac   PetscBool   hasLabel;
7164d67d17b1SMatthew G. Knepley   const char *lname;
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac   PetscFunctionBegin;
7167c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71684f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71694f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7170c58f1c22SToby Isaac   *label = NULL;
7171c58f1c22SToby Isaac   while (next) {
71729566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71739566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7174c58f1c22SToby Isaac     if (hasLabel) {
7175c58f1c22SToby Isaac       *label = next->label;
7176c58f1c22SToby Isaac       break;
7177c58f1c22SToby Isaac     }
7178c58f1c22SToby Isaac     next = next->next;
7179c58f1c22SToby Isaac   }
71803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7181c58f1c22SToby Isaac }
7182c58f1c22SToby Isaac 
7183cc4c1da9SBarry Smith /*@
7184bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7185c58f1c22SToby Isaac 
7186c58f1c22SToby Isaac   Not Collective
7187c58f1c22SToby Isaac 
7188c58f1c22SToby Isaac   Input Parameters:
7189bb7acecfSBarry Smith + dm - The `DM` object
7190c58f1c22SToby Isaac - n  - the label number
7191c58f1c22SToby Isaac 
7192c58f1c22SToby Isaac   Output Parameter:
7193c58f1c22SToby Isaac . label - the label
7194c58f1c22SToby Isaac 
7195c58f1c22SToby Isaac   Level: intermediate
7196c58f1c22SToby Isaac 
71971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7198c58f1c22SToby Isaac @*/
7199d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7200d71ae5a4SJacob Faibussowitsch {
72015d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7202c58f1c22SToby Isaac   PetscInt    l    = 0;
7203c58f1c22SToby Isaac 
7204c58f1c22SToby Isaac   PetscFunctionBegin;
7205c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72064f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7207c58f1c22SToby Isaac   while (next) {
7208c58f1c22SToby Isaac     if (l == n) {
7209c58f1c22SToby Isaac       *label = next->label;
72103ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7211c58f1c22SToby Isaac     }
7212c58f1c22SToby Isaac     ++l;
7213c58f1c22SToby Isaac     next = next->next;
7214c58f1c22SToby Isaac   }
721563a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7216c58f1c22SToby Isaac }
7217c58f1c22SToby Isaac 
7218cc4c1da9SBarry Smith /*@
7219bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7220c58f1c22SToby Isaac 
7221c58f1c22SToby Isaac   Not Collective
7222c58f1c22SToby Isaac 
7223c58f1c22SToby Isaac   Input Parameters:
7224bb7acecfSBarry Smith + dm    - The `DM` object
7225bb7acecfSBarry Smith - label - The `DMLabel`
7226c58f1c22SToby Isaac 
7227c58f1c22SToby Isaac   Level: developer
7228c58f1c22SToby Isaac 
722960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7230c58f1c22SToby Isaac @*/
7231d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7232d71ae5a4SJacob Faibussowitsch {
72335d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7234c58f1c22SToby Isaac   PetscBool   hasLabel;
7235d67d17b1SMatthew G. Knepley   const char *lname;
72365d80c0bfSVaclav Hapla   PetscBool   flg;
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   PetscFunctionBegin;
7239c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72409566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72419566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72427a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72439566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7244c58f1c22SToby Isaac   tmpLabel->label  = label;
7245c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72465d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72475d80c0bfSVaclav Hapla   *p = tmpLabel;
72489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
72499566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
72505d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
72519566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7252ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7254c58f1c22SToby Isaac }
7255c58f1c22SToby Isaac 
7256a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7257cc4c1da9SBarry Smith /*@
72584a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
72594a7ee7d0SMatthew G. Knepley 
72604a7ee7d0SMatthew G. Knepley   Not Collective
72614a7ee7d0SMatthew G. Knepley 
72624a7ee7d0SMatthew G. Knepley   Input Parameters:
7263bb7acecfSBarry Smith + dm    - The `DM` object
7264bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
72654a7ee7d0SMatthew G. Knepley 
7266bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7267bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7268bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7269bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7270bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7271bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7272bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72734a7ee7d0SMatthew G. Knepley 
72744a7ee7d0SMatthew G. Knepley   Level: intermediate
72754a7ee7d0SMatthew G. Knepley 
72761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72774a7ee7d0SMatthew G. Knepley @*/
7278d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7279d71ae5a4SJacob Faibussowitsch {
72804a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
72814a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
72824a7ee7d0SMatthew G. Knepley   const char *name, *lname;
72834a7ee7d0SMatthew G. Knepley 
72844a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
72854a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72864a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
72879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
72884a7ee7d0SMatthew G. Knepley   while (next) {
72899566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72909566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
72914a7ee7d0SMatthew G. Knepley     if (hasLabel) {
72929566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
72939566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
72944a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
72959566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
72964a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
72979566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
72984a7ee7d0SMatthew G. Knepley       next->label = label;
72994a7ee7d0SMatthew G. Knepley       break;
73004a7ee7d0SMatthew G. Knepley     }
73014a7ee7d0SMatthew G. Knepley     next = next->next;
73024a7ee7d0SMatthew G. Knepley   }
73033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73044a7ee7d0SMatthew G. Knepley }
73054a7ee7d0SMatthew G. Knepley 
7306cc4c1da9SBarry Smith /*@
7307bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7308c58f1c22SToby Isaac 
7309c58f1c22SToby Isaac   Not Collective
7310c58f1c22SToby Isaac 
7311c58f1c22SToby Isaac   Input Parameters:
7312bb7acecfSBarry Smith + dm   - The `DM` object
7313c58f1c22SToby Isaac - name - The label name
7314c58f1c22SToby Isaac 
7315c58f1c22SToby Isaac   Output Parameter:
731620f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7317bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7318c58f1c22SToby Isaac 
7319c58f1c22SToby Isaac   Level: developer
7320c58f1c22SToby Isaac 
73211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7322c58f1c22SToby Isaac @*/
7323d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7324d71ae5a4SJacob Faibussowitsch {
732595d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7326c58f1c22SToby Isaac   PetscBool   hasLabel;
7327d67d17b1SMatthew G. Knepley   const char *lname;
7328c58f1c22SToby Isaac 
7329c58f1c22SToby Isaac   PetscFunctionBegin;
7330c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73314f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7332e5472504SVaclav Hapla   if (label) {
73334f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7334c58f1c22SToby Isaac     *label = NULL;
7335e5472504SVaclav Hapla   }
73365d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73379566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73389566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7339c58f1c22SToby Isaac     if (hasLabel) {
734095d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73419566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
734295d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73439566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7344ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
734595d578d6SVaclav Hapla       if (label) *label = link->label;
73469566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73479566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7348c58f1c22SToby Isaac       break;
7349c58f1c22SToby Isaac     }
7350c58f1c22SToby Isaac   }
73513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7352c58f1c22SToby Isaac }
7353c58f1c22SToby Isaac 
7354306894acSVaclav Hapla /*@
7355bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7356306894acSVaclav Hapla 
7357306894acSVaclav Hapla   Not Collective
7358306894acSVaclav Hapla 
7359306894acSVaclav Hapla   Input Parameters:
7360bb7acecfSBarry Smith + dm           - The `DM` object
7361bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
736220f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7363306894acSVaclav Hapla 
7364306894acSVaclav Hapla   Level: developer
7365306894acSVaclav Hapla 
7366bb7acecfSBarry Smith   Note:
7367306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7368bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7369306894acSVaclav Hapla   *label nullified.
7370306894acSVaclav Hapla 
73711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7372306894acSVaclav Hapla @*/
7373d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7374d71ae5a4SJacob Faibussowitsch {
737543e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7376306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7377306894acSVaclav Hapla 
7378306894acSVaclav Hapla   PetscFunctionBegin;
7379306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73804f572ea9SToby Isaac   PetscAssertPointer(label, 2);
73813ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7382306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7383306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
73845d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
738543e45a93SVaclav Hapla     if (*label == link->label) {
7386306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
738743e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7388306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7389ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
739043e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
73919566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
73929566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7393306894acSVaclav Hapla       break;
7394306894acSVaclav Hapla     }
7395306894acSVaclav Hapla   }
73967a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
73973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7398306894acSVaclav Hapla }
7399306894acSVaclav Hapla 
7400cc4c1da9SBarry Smith /*@
7401c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7402c58f1c22SToby Isaac 
7403c58f1c22SToby Isaac   Not Collective
7404c58f1c22SToby Isaac 
7405c58f1c22SToby Isaac   Input Parameters:
7406bb7acecfSBarry Smith + dm   - The `DM` object
7407c58f1c22SToby Isaac - name - The label name
7408c58f1c22SToby Isaac 
7409c58f1c22SToby Isaac   Output Parameter:
7410c58f1c22SToby Isaac . output - The flag for output
7411c58f1c22SToby Isaac 
7412c58f1c22SToby Isaac   Level: developer
7413c58f1c22SToby Isaac 
74141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7415c58f1c22SToby Isaac @*/
7416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7417d71ae5a4SJacob Faibussowitsch {
74185d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7419d67d17b1SMatthew G. Knepley   const char *lname;
7420c58f1c22SToby Isaac 
7421c58f1c22SToby Isaac   PetscFunctionBegin;
7422c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74234f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74244f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7425c58f1c22SToby Isaac   while (next) {
7426c58f1c22SToby Isaac     PetscBool flg;
7427c58f1c22SToby Isaac 
74289566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74299566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74309371c9d4SSatish Balay     if (flg) {
74319371c9d4SSatish Balay       *output = next->output;
74323ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74339371c9d4SSatish Balay     }
7434c58f1c22SToby Isaac     next = next->next;
7435c58f1c22SToby Isaac   }
743698921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7437c58f1c22SToby Isaac }
7438c58f1c22SToby Isaac 
7439cc4c1da9SBarry Smith /*@
7440bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7441c58f1c22SToby Isaac 
7442c58f1c22SToby Isaac   Not Collective
7443c58f1c22SToby Isaac 
7444c58f1c22SToby Isaac   Input Parameters:
7445bb7acecfSBarry Smith + dm     - The `DM` object
7446c58f1c22SToby Isaac . name   - The label name
7447bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7448c58f1c22SToby Isaac 
7449c58f1c22SToby Isaac   Level: developer
7450c58f1c22SToby Isaac 
74511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7452c58f1c22SToby Isaac @*/
7453d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7454d71ae5a4SJacob Faibussowitsch {
74555d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7456d67d17b1SMatthew G. Knepley   const char *lname;
7457c58f1c22SToby Isaac 
7458c58f1c22SToby Isaac   PetscFunctionBegin;
7459c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74604f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7461c58f1c22SToby Isaac   while (next) {
7462c58f1c22SToby Isaac     PetscBool flg;
7463c58f1c22SToby Isaac 
74649566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74659566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74669371c9d4SSatish Balay     if (flg) {
74679371c9d4SSatish Balay       next->output = output;
74683ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74699371c9d4SSatish Balay     }
7470c58f1c22SToby Isaac     next = next->next;
7471c58f1c22SToby Isaac   }
747298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7473c58f1c22SToby Isaac }
7474c58f1c22SToby Isaac 
7475c58f1c22SToby Isaac /*@
7476bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7477c58f1c22SToby Isaac 
747820f4b53cSBarry Smith   Collective
7479c58f1c22SToby Isaac 
7480d8d19677SJose E. Roman   Input Parameters:
7481bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7482bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7483bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7484bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7485bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7486c58f1c22SToby Isaac 
7487c58f1c22SToby Isaac   Level: intermediate
7488c58f1c22SToby Isaac 
7489bb7acecfSBarry Smith   Note:
74902cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7491c58f1c22SToby Isaac 
74921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7493c58f1c22SToby Isaac @*/
7494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7495d71ae5a4SJacob Faibussowitsch {
74962cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7497c58f1c22SToby Isaac   const char *name;
7498c58f1c22SToby Isaac   PetscBool   flg;
74995d80c0bfSVaclav Hapla   DMLabelLink link;
7500c58f1c22SToby Isaac 
75015d80c0bfSVaclav Hapla   PetscFunctionBegin;
75025d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75035d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75045d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75055d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75067a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
75073ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
75085d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
75095d80c0bfSVaclav Hapla     label = link->label;
75109566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
75115d80c0bfSVaclav Hapla     if (!all) {
75129566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7513c58f1c22SToby Isaac       if (flg) continue;
75149566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
75157d5acc75SStefano Zampini       if (flg) continue;
75169566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7517ba2698f1SMatthew G. Knepley       if (flg) continue;
75185d80c0bfSVaclav Hapla     }
75199566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
75202cbb9b06SVaclav Hapla     if (labelOld) {
75212cbb9b06SVaclav Hapla       switch (emode) {
7522d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7523d71ae5a4SJacob Faibussowitsch         continue;
7524d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7525d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7526d71ae5a4SJacob Faibussowitsch         break;
7527d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7528d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7529d71ae5a4SJacob Faibussowitsch       default:
7530d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75312cbb9b06SVaclav Hapla       }
75322cbb9b06SVaclav Hapla     }
75335d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75349566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75355d80c0bfSVaclav Hapla     } else {
75365d80c0bfSVaclav Hapla       labelNew = label;
75375d80c0bfSVaclav Hapla     }
75389566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75399566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7540c58f1c22SToby Isaac   }
75413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7542c58f1c22SToby Isaac }
7543461a15a0SLisandro Dalcin 
7544609dae6eSVaclav Hapla /*@C
7545b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7546609dae6eSVaclav Hapla 
754720f4b53cSBarry Smith   Collective; No Fortran Support
7548609dae6eSVaclav Hapla 
7549609dae6eSVaclav Hapla   Input Parameters:
7550bb7acecfSBarry Smith + dm0 - First `DM` object
7551bb7acecfSBarry Smith - dm1 - Second `DM` object
7552609dae6eSVaclav Hapla 
7553a4e35b19SJacob Faibussowitsch   Output Parameters:
75545efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
755520f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7556609dae6eSVaclav Hapla 
7557609dae6eSVaclav Hapla   Level: intermediate
7558609dae6eSVaclav Hapla 
7559609dae6eSVaclav Hapla   Notes:
7560bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7561bb7acecfSBarry Smith 
756220f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7563bb7acecfSBarry Smith 
756420f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7565609dae6eSVaclav Hapla 
75665efe38ccSVaclav Hapla   The output message is set independently on each rank.
7567bb7acecfSBarry Smith 
7568bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7569bb7acecfSBarry Smith 
757020f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7571bb7acecfSBarry Smith 
757220f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7573609dae6eSVaclav Hapla 
7574609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7575bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7576609dae6eSVaclav Hapla 
7577cc4c1da9SBarry Smith   Developer Note:
7578cc4c1da9SBarry Smith   Can automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7579cc4c1da9SBarry Smith 
75801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7581609dae6eSVaclav Hapla @*/
7582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7583d71ae5a4SJacob Faibussowitsch {
75845efe38ccSVaclav Hapla   PetscInt    n, i;
7585609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
75865efe38ccSVaclav Hapla   PetscBool   eq;
7587609dae6eSVaclav Hapla   MPI_Comm    comm;
75885efe38ccSVaclav Hapla   PetscMPIInt rank;
7589609dae6eSVaclav Hapla 
7590609dae6eSVaclav Hapla   PetscFunctionBegin;
7591609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7592609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7593609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
75944f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
75954f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
75969566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
75979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
75985efe38ccSVaclav Hapla   {
75995efe38ccSVaclav Hapla     PetscInt n1;
76005efe38ccSVaclav Hapla 
76019566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76029566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76035efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
760448a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7605462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
76065efe38ccSVaclav Hapla     if (!eq) goto finish;
76075efe38ccSVaclav Hapla   }
76085efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7609609dae6eSVaclav Hapla     DMLabel     l0, l1;
7610609dae6eSVaclav Hapla     const char *name;
7611609dae6eSVaclav Hapla     char       *msgInner;
7612609dae6eSVaclav Hapla 
7613609dae6eSVaclav Hapla     /* Ignore label order */
76149566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
76159566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
76169566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7617609dae6eSVaclav Hapla     if (!l1) {
761863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
76195efe38ccSVaclav Hapla       eq = PETSC_FALSE;
76205efe38ccSVaclav Hapla       break;
7621609dae6eSVaclav Hapla     }
76229566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
76239566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
76249566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
76255efe38ccSVaclav Hapla     if (!eq) break;
7626609dae6eSVaclav Hapla   }
7627462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7628609dae6eSVaclav Hapla finish:
76295efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7630609dae6eSVaclav Hapla   if (message) {
7631609dae6eSVaclav Hapla     *message = NULL;
763248a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76335efe38ccSVaclav Hapla   } else {
763448a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76359566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76365efe38ccSVaclav Hapla   }
76375efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76385efe38ccSVaclav Hapla   if (equal) *equal = eq;
76397a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7641609dae6eSVaclav Hapla }
7642609dae6eSVaclav Hapla 
7643d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7644d71ae5a4SJacob Faibussowitsch {
7645461a15a0SLisandro Dalcin   PetscFunctionBegin;
76464f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7647461a15a0SLisandro Dalcin   if (!*label) {
76489566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
76499566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7650461a15a0SLisandro Dalcin   }
76519566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
76523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7653461a15a0SLisandro Dalcin }
7654461a15a0SLisandro Dalcin 
76550fdc7489SMatthew Knepley /*
76560fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
76570fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
76580fdc7489SMatthew Knepley   (label, id) pair in the DM.
76590fdc7489SMatthew Knepley 
76600fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
76610fdc7489SMatthew Knepley   each label.
76620fdc7489SMatthew Knepley */
7663d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7664d71ae5a4SJacob Faibussowitsch {
76650fdc7489SMatthew Knepley   DMUniversalLabel ul;
76660fdc7489SMatthew Knepley   PetscBool       *active;
76670fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
76680fdc7489SMatthew Knepley 
76690fdc7489SMatthew Knepley   PetscFunctionBegin;
76709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
76719566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
76729566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
76739566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
76740fdc7489SMatthew Knepley   ul->Nl = 0;
76750fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76760fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
76770fdc7489SMatthew Knepley     const char *name;
76780fdc7489SMatthew Knepley 
76799566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76809566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
76819566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
76820fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
76830fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
76840fdc7489SMatthew Knepley   }
76859566063dSJacob 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));
76860fdc7489SMatthew Knepley   ul->Nv = 0;
76870fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76880fdc7489SMatthew Knepley     DMLabel     label;
76890fdc7489SMatthew Knepley     PetscInt    nv;
76900fdc7489SMatthew Knepley     const char *name;
76910fdc7489SMatthew Knepley 
76920fdc7489SMatthew Knepley     if (!active[l]) continue;
76939566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76949566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76959566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76969566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
76970fdc7489SMatthew Knepley     ul->indices[m] = l;
76980fdc7489SMatthew Knepley     ul->Nv += nv;
76990fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77000fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77010fdc7489SMatthew Knepley     ++m;
77020fdc7489SMatthew Knepley   }
77030fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77040fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77050fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77060fdc7489SMatthew Knepley   }
77070fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77080fdc7489SMatthew Knepley     PetscInt b;
77090fdc7489SMatthew Knepley 
77100fdc7489SMatthew Knepley     ul->masks[l] = 0;
77110fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
77120fdc7489SMatthew Knepley   }
77139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
77140fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77150fdc7489SMatthew Knepley     DMLabel         label;
77160fdc7489SMatthew Knepley     IS              valueIS;
77170fdc7489SMatthew Knepley     const PetscInt *varr;
77180fdc7489SMatthew Knepley     PetscInt        nv, v;
77190fdc7489SMatthew Knepley 
77200fdc7489SMatthew Knepley     if (!active[l]) continue;
77219566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77239566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
77249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7725ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77269566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77279566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77289566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77290fdc7489SMatthew Knepley     ++m;
77300fdc7489SMatthew Knepley   }
77319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77320fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77330fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77340fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77350fdc7489SMatthew Knepley 
77360fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77370fdc7489SMatthew Knepley       DMLabel  label;
77380649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77390fdc7489SMatthew Knepley 
77400fdc7489SMatthew Knepley       if (!active[l]) continue;
77419566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77429566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77449371c9d4SSatish Balay       if (val == defval) {
77459371c9d4SSatish Balay         ++m;
77469371c9d4SSatish Balay         continue;
77479371c9d4SSatish Balay       }
77480649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
77490fdc7489SMatthew Knepley       marked = PETSC_TRUE;
77509566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
775163a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
77520fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
77530fdc7489SMatthew Knepley       ++m;
77540fdc7489SMatthew Knepley     }
77559566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
77560fdc7489SMatthew Knepley   }
77579566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
77580fdc7489SMatthew Knepley   *universal = ul;
77593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77600fdc7489SMatthew Knepley }
77610fdc7489SMatthew Knepley 
7762d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7763d71ae5a4SJacob Faibussowitsch {
77640fdc7489SMatthew Knepley   PetscInt l;
77650fdc7489SMatthew Knepley 
77660fdc7489SMatthew Knepley   PetscFunctionBegin;
77679566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
77689566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
77699566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
77709566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
77719566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
77720fdc7489SMatthew Knepley   *universal = NULL;
77733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77740fdc7489SMatthew Knepley }
77750fdc7489SMatthew Knepley 
7776d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7777d71ae5a4SJacob Faibussowitsch {
77780fdc7489SMatthew Knepley   PetscFunctionBegin;
77794f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
77800fdc7489SMatthew Knepley   *ulabel = ul->label;
77813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77820fdc7489SMatthew Knepley }
77830fdc7489SMatthew Knepley 
7784d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7785d71ae5a4SJacob Faibussowitsch {
77860fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
77870fdc7489SMatthew Knepley 
77880fdc7489SMatthew Knepley   PetscFunctionBegin;
7789064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
77900fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77919566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
77929566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
77930fdc7489SMatthew Knepley   }
77940fdc7489SMatthew Knepley   if (preserveOrder) {
77950fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
77960fdc7489SMatthew Knepley       const char *name;
77970fdc7489SMatthew Knepley       PetscBool   match;
77980fdc7489SMatthew Knepley 
77999566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78009566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
780163a3b9bcSJacob 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]);
78020fdc7489SMatthew Knepley     }
78030fdc7489SMatthew Knepley   }
78043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78050fdc7489SMatthew Knepley }
78060fdc7489SMatthew Knepley 
7807d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7808d71ae5a4SJacob Faibussowitsch {
78090fdc7489SMatthew Knepley   PetscInt l;
78100fdc7489SMatthew Knepley 
78110fdc7489SMatthew Knepley   PetscFunctionBegin;
78120fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78130fdc7489SMatthew Knepley     DMLabel  label;
78140fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
78150fdc7489SMatthew Knepley 
78160fdc7489SMatthew Knepley     if (lval) {
78179566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
78189566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
78199566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
78200fdc7489SMatthew Knepley     }
78210fdc7489SMatthew Knepley   }
78223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78230fdc7489SMatthew Knepley }
7824a8fb8f29SToby Isaac 
7825a8fb8f29SToby Isaac /*@
7826bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7827bb7acecfSBarry Smith 
782820f4b53cSBarry Smith   Not Collective
7829a8fb8f29SToby Isaac 
7830a8fb8f29SToby Isaac   Input Parameter:
7831bb7acecfSBarry Smith . dm - The `DM` object
7832a8fb8f29SToby Isaac 
7833a8fb8f29SToby Isaac   Output Parameter:
7834bb7acecfSBarry Smith . cdm - The coarse `DM`
7835a8fb8f29SToby Isaac 
7836a8fb8f29SToby Isaac   Level: intermediate
7837a8fb8f29SToby Isaac 
78381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7839a8fb8f29SToby Isaac @*/
7840d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7841d71ae5a4SJacob Faibussowitsch {
7842a8fb8f29SToby Isaac   PetscFunctionBegin;
7843a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78444f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7845a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7847a8fb8f29SToby Isaac }
7848a8fb8f29SToby Isaac 
7849a8fb8f29SToby Isaac /*@
7850bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7851a8fb8f29SToby Isaac 
7852a8fb8f29SToby Isaac   Input Parameters:
7853bb7acecfSBarry Smith + dm  - The `DM` object
7854bb7acecfSBarry Smith - cdm - The coarse `DM`
7855a8fb8f29SToby Isaac 
7856a8fb8f29SToby Isaac   Level: intermediate
7857a8fb8f29SToby Isaac 
7858bb7acecfSBarry Smith   Note:
7859bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7860bb7acecfSBarry Smith 
78611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7862a8fb8f29SToby Isaac @*/
7863d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7864d71ae5a4SJacob Faibussowitsch {
7865a8fb8f29SToby Isaac   PetscFunctionBegin;
7866a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7867a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
786889d734beSBarry Smith   if (dm == cdm) cdm = NULL;
78699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
78709566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7871a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
78723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7873a8fb8f29SToby Isaac }
7874a8fb8f29SToby Isaac 
787588bdff64SToby Isaac /*@
7876bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
787788bdff64SToby Isaac 
787888bdff64SToby Isaac   Input Parameter:
7879bb7acecfSBarry Smith . dm - The `DM` object
788088bdff64SToby Isaac 
788188bdff64SToby Isaac   Output Parameter:
7882bb7acecfSBarry Smith . fdm - The fine `DM`
788388bdff64SToby Isaac 
788488bdff64SToby Isaac   Level: intermediate
788588bdff64SToby Isaac 
78861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
788788bdff64SToby Isaac @*/
7888d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7889d71ae5a4SJacob Faibussowitsch {
789088bdff64SToby Isaac   PetscFunctionBegin;
789188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78924f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
789388bdff64SToby Isaac   *fdm = dm->fineMesh;
78943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
789588bdff64SToby Isaac }
789688bdff64SToby Isaac 
789788bdff64SToby Isaac /*@
7898bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
789988bdff64SToby Isaac 
790088bdff64SToby Isaac   Input Parameters:
7901bb7acecfSBarry Smith + dm  - The `DM` object
7902bb7acecfSBarry Smith - fdm - The fine `DM`
790388bdff64SToby Isaac 
7904bb7acecfSBarry Smith   Level: developer
790588bdff64SToby Isaac 
7906bb7acecfSBarry Smith   Note:
7907bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7908bb7acecfSBarry Smith 
79091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
791088bdff64SToby Isaac @*/
7911d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7912d71ae5a4SJacob Faibussowitsch {
791388bdff64SToby Isaac   PetscFunctionBegin;
791488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
791588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
791689d734beSBarry Smith   if (dm == fdm) fdm = NULL;
79179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
79189566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
791988bdff64SToby Isaac   dm->fineMesh = fdm;
79203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
792188bdff64SToby Isaac }
792288bdff64SToby Isaac 
7923a6ba4734SToby Isaac /*@C
7924bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7925a6ba4734SToby Isaac 
792620f4b53cSBarry Smith   Collective
7927783e2ec8SMatthew G. Knepley 
7928a6ba4734SToby Isaac   Input Parameters:
7929bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7930bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7931a6ba4734SToby Isaac . name     - The BC name
793245480ffeSMatthew G. Knepley . label    - The label defining constrained points
7933bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
793445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7935a6ba4734SToby Isaac . field    - The field to constrain
793645480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7937a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7938a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
793956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7940a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7941a6ba4734SToby Isaac 
794245480ffeSMatthew G. Knepley   Output Parameter:
794345480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
794445480ffeSMatthew G. Knepley 
7945a6ba4734SToby Isaac   Options Database Keys:
7946a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7947a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7948a6ba4734SToby Isaac 
794920f4b53cSBarry Smith   Level: intermediate
795020f4b53cSBarry Smith 
7951bb7acecfSBarry Smith   Notes:
7952aeebefc2SPierre Jolivet   Both bcFunc and bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is\:
795373ff1848SBarry Smith .vb
795473ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
795573ff1848SBarry Smith .ve
795656cf3b9cSMatthew G. Knepley 
7957a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
795856cf3b9cSMatthew G. Knepley 
795920f4b53cSBarry Smith .vb
796020f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
796120f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
796220f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
796320f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
796420f4b53cSBarry Smith .ve
796556cf3b9cSMatthew G. Knepley + dim - the spatial dimension
796656cf3b9cSMatthew G. Knepley . Nf - the number of fields
796756cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
796856cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
796956cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
797056cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
797156cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
797256cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
797356cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
797456cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
797556cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
797656cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
797756cf3b9cSMatthew G. Knepley . t - current time
797856cf3b9cSMatthew G. Knepley . x - coordinates of the current point
797956cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
798056cf3b9cSMatthew G. Knepley . constants - constant parameters
798156cf3b9cSMatthew G. Knepley - bcval - output values at the current point
798256cf3b9cSMatthew G. Knepley 
79831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7984a6ba4734SToby Isaac @*/
7985d71ae5a4SJacob 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)
7986d71ae5a4SJacob Faibussowitsch {
7987e5e52638SMatthew G. Knepley   PetscDS ds;
7988a6ba4734SToby Isaac 
7989a6ba4734SToby Isaac   PetscFunctionBegin;
7990a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7991783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
799245480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
799345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
799445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
799545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
799601a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
79979566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7998799db056SMatthew G. Knepley   /* Complete label */
7999799db056SMatthew G. Knepley   if (label) {
8000799db056SMatthew G. Knepley     PetscObject  obj;
8001799db056SMatthew G. Knepley     PetscClassId id;
8002799db056SMatthew G. Knepley 
8003799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8004799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8005799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8006799db056SMatthew G. Knepley       DM plex;
8007799db056SMatthew G. Knepley 
8008799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8009799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8010799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8011799db056SMatthew G. Knepley     }
8012799db056SMatthew G. Knepley   }
80139566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
80143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8015a6ba4734SToby Isaac }
8016a6ba4734SToby Isaac 
801745480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8018d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8019d71ae5a4SJacob Faibussowitsch {
8020e5e52638SMatthew G. Knepley   PetscDS     ds;
8021dff059c6SToby Isaac   DMBoundary *lastnext;
8022e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8023e6f8dbb6SToby Isaac 
8024e6f8dbb6SToby Isaac   PetscFunctionBegin;
80259566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8026e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
802747a1f5adSToby Isaac   if (dm->boundary) {
802847a1f5adSToby Isaac     DMBoundary next = dm->boundary;
802947a1f5adSToby Isaac 
803047a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80313ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
803247a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
803347a1f5adSToby Isaac     while (next) {
803447a1f5adSToby Isaac       DMBoundary b = next;
803547a1f5adSToby Isaac 
803647a1f5adSToby Isaac       next = b->next;
80379566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8038a6ba4734SToby Isaac     }
803947a1f5adSToby Isaac     dm->boundary = NULL;
8040a6ba4734SToby Isaac   }
804147a1f5adSToby Isaac 
8042f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8043e6f8dbb6SToby Isaac   while (dsbound) {
8044e6f8dbb6SToby Isaac     DMBoundary dmbound;
8045e6f8dbb6SToby Isaac 
80469566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8047e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
804845480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
804947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8050dff059c6SToby Isaac     *lastnext = dmbound;
8051f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8052dff059c6SToby Isaac     dsbound   = dsbound->next;
8053a6ba4734SToby Isaac   }
80543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8055a6ba4734SToby Isaac }
8056a6ba4734SToby Isaac 
8057bb7acecfSBarry Smith /* TODO: missing manual page */
8058d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8059d71ae5a4SJacob Faibussowitsch {
8060b95f2879SToby Isaac   DMBoundary b;
8061a6ba4734SToby Isaac 
8062a6ba4734SToby Isaac   PetscFunctionBegin;
8063a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80644f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8065a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
80669566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8067b95f2879SToby Isaac   b = dm->boundary;
806857508eceSPierre Jolivet   while (b && !*isBd) {
8069e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8070e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8071a6ba4734SToby Isaac     PetscInt   i;
8072a6ba4734SToby Isaac 
807345480ffeSMatthew G. Knepley     if (label) {
807457508eceSPierre Jolivet       for (i = 0; i < dsb->Nv && !*isBd; ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8075a6ba4734SToby Isaac     }
8076a6ba4734SToby Isaac     b = b->next;
8077a6ba4734SToby Isaac   }
80783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8079a6ba4734SToby Isaac }
80804d6f44ffSToby Isaac 
80814d6f44ffSToby Isaac /*@C
8082bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8083a6e0b375SMatthew G. Knepley 
808420f4b53cSBarry Smith   Collective
80854d6f44ffSToby Isaac 
80864d6f44ffSToby Isaac   Input Parameters:
8087bb7acecfSBarry Smith + dm    - The `DM`
80880709b2feSToby Isaac . time  - The time
80894d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
80904d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80914d6f44ffSToby Isaac - mode  - The insertion mode for values
80924d6f44ffSToby Isaac 
80934d6f44ffSToby Isaac   Output Parameter:
80944d6f44ffSToby Isaac . X - vector
80954d6f44ffSToby Isaac 
809620f4b53cSBarry Smith   Calling sequence of `funcs`:
80974d6f44ffSToby Isaac + dim  - The spatial dimension
80988ec8862eSJed Brown . time - The time at which to sample
80994d6f44ffSToby Isaac . x    - The coordinates
810077b739a6SMatthew Knepley . Nc   - The number of components
81014d6f44ffSToby Isaac . u    - The output field values
81024d6f44ffSToby Isaac - ctx  - optional user-defined function context
81034d6f44ffSToby Isaac 
81044d6f44ffSToby Isaac   Level: developer
81054d6f44ffSToby Isaac 
8106bb7acecfSBarry Smith   Developer Notes:
8107bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8108bb7acecfSBarry Smith 
8109bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8110bb7acecfSBarry Smith 
81111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
81124d6f44ffSToby Isaac @*/
8113a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
8114d71ae5a4SJacob Faibussowitsch {
81154d6f44ffSToby Isaac   Vec localX;
81164d6f44ffSToby Isaac 
81174d6f44ffSToby Isaac   PetscFunctionBegin;
81184d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8119708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
81209566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8121f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81229566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
81239566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81249566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81259566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8126708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
81273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81284d6f44ffSToby Isaac }
81294d6f44ffSToby Isaac 
8130a6e0b375SMatthew G. Knepley /*@C
8131bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8132a6e0b375SMatthew G. Knepley 
813320f4b53cSBarry Smith   Not Collective
8134a6e0b375SMatthew G. Knepley 
8135a6e0b375SMatthew G. Knepley   Input Parameters:
8136bb7acecfSBarry Smith + dm    - The `DM`
8137a6e0b375SMatthew G. Knepley . time  - The time
8138a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8139a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8140a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8141a6e0b375SMatthew G. Knepley 
8142a6e0b375SMatthew G. Knepley   Output Parameter:
8143a6e0b375SMatthew G. Knepley . localX - vector
8144a6e0b375SMatthew G. Knepley 
814520f4b53cSBarry Smith   Calling sequence of `funcs`:
8146a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8147a4e35b19SJacob Faibussowitsch . time - The current timestep
8148a6e0b375SMatthew G. Knepley . x    - The coordinates
814977b739a6SMatthew Knepley . Nc   - The number of components
8150a6e0b375SMatthew G. Knepley . u    - The output field values
8151a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8152a6e0b375SMatthew G. Knepley 
8153a6e0b375SMatthew G. Knepley   Level: developer
8154a6e0b375SMatthew G. Knepley 
8155bb7acecfSBarry Smith   Developer Notes:
8156bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8157bb7acecfSBarry Smith 
8158bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8159bb7acecfSBarry Smith 
81601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8161a6e0b375SMatthew G. Knepley @*/
8162a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
8163d71ae5a4SJacob Faibussowitsch {
81644d6f44ffSToby Isaac   PetscFunctionBegin;
81654d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8166064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8167fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
81683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81694d6f44ffSToby Isaac }
81704d6f44ffSToby Isaac 
8171a6e0b375SMatthew G. Knepley /*@C
8172bb7acecfSBarry 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.
8173a6e0b375SMatthew G. Knepley 
817420f4b53cSBarry Smith   Collective
8175a6e0b375SMatthew G. Knepley 
8176a6e0b375SMatthew G. Knepley   Input Parameters:
8177bb7acecfSBarry Smith + dm     - The `DM`
8178a6e0b375SMatthew G. Knepley . time   - The time
8179a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8180a4e35b19SJacob Faibussowitsch . ids    - The ids
8181a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8182a4e35b19SJacob Faibussowitsch . comps  - The components
8183bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8184a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8185bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8186a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8187a6e0b375SMatthew G. Knepley 
8188a6e0b375SMatthew G. Knepley   Output Parameter:
8189a6e0b375SMatthew G. Knepley . X - vector
8190a6e0b375SMatthew G. Knepley 
819120f4b53cSBarry Smith   Calling sequence of `funcs`:
8192a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8193a4e35b19SJacob Faibussowitsch . time - The current timestep
8194a6e0b375SMatthew G. Knepley . x    - The coordinates
819577b739a6SMatthew Knepley . Nc   - The number of components
8196a6e0b375SMatthew G. Knepley . u    - The output field values
8197a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8198a6e0b375SMatthew G. Knepley 
8199a6e0b375SMatthew G. Knepley   Level: developer
8200a6e0b375SMatthew G. Knepley 
8201bb7acecfSBarry Smith   Developer Notes:
8202bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8203bb7acecfSBarry Smith 
8204bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8205bb7acecfSBarry Smith 
82061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8207a6e0b375SMatthew G. Knepley @*/
8208a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X)
8209d71ae5a4SJacob Faibussowitsch {
82102c53366bSMatthew G. Knepley   Vec localX;
82112c53366bSMatthew G. Knepley 
82122c53366bSMatthew G. Knepley   PetscFunctionBegin;
82132c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82149566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8215f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82169566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
82179566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82189566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82199566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
82203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82212c53366bSMatthew G. Knepley }
82222c53366bSMatthew G. Knepley 
8223a6e0b375SMatthew G. Knepley /*@C
8224bb7acecfSBarry 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.
8225a6e0b375SMatthew G. Knepley 
822620f4b53cSBarry Smith   Not Collective
8227a6e0b375SMatthew G. Knepley 
8228a6e0b375SMatthew G. Knepley   Input Parameters:
8229bb7acecfSBarry Smith + dm     - The `DM`
8230a6e0b375SMatthew G. Knepley . time   - The time
8231bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8232a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8233a4e35b19SJacob Faibussowitsch . ids    - The ids
8234a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8235a4e35b19SJacob Faibussowitsch . comps  - The components
8236a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8237a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8238a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8239a6e0b375SMatthew G. Knepley 
8240a6e0b375SMatthew G. Knepley   Output Parameter:
8241a6e0b375SMatthew G. Knepley . localX - vector
8242a6e0b375SMatthew G. Knepley 
824320f4b53cSBarry Smith   Calling sequence of `funcs`:
8244a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8245a4e35b19SJacob Faibussowitsch . time - The current time
8246a6e0b375SMatthew G. Knepley . x    - The coordinates
824777b739a6SMatthew Knepley . Nc   - The number of components
8248a6e0b375SMatthew G. Knepley . u    - The output field values
8249a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8250a6e0b375SMatthew G. Knepley 
8251a6e0b375SMatthew G. Knepley   Level: developer
8252a6e0b375SMatthew G. Knepley 
8253bb7acecfSBarry Smith   Developer Notes:
8254bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8255bb7acecfSBarry Smith 
8256bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8257bb7acecfSBarry Smith 
82581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8259a6e0b375SMatthew G. Knepley @*/
8260a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX)
8261d71ae5a4SJacob Faibussowitsch {
82624d6f44ffSToby Isaac   PetscFunctionBegin;
82634d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8264064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8265fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
82663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82674d6f44ffSToby Isaac }
82682716604bSToby Isaac 
8269a6e0b375SMatthew G. Knepley /*@C
8270bb7acecfSBarry 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.
8271a6e0b375SMatthew G. Knepley 
827220f4b53cSBarry Smith   Not Collective
8273a6e0b375SMatthew G. Knepley 
8274a6e0b375SMatthew G. Knepley   Input Parameters:
8275bb7acecfSBarry Smith + dm     - The `DM`
8276a6e0b375SMatthew G. Knepley . time   - The time
827720f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8278a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8279a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8280a6e0b375SMatthew G. Knepley 
8281a6e0b375SMatthew G. Knepley   Output Parameter:
8282a6e0b375SMatthew G. Knepley . localX - The output vector
8283a6e0b375SMatthew G. Knepley 
828420f4b53cSBarry Smith   Calling sequence of `funcs`:
8285a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8286a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8287a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8288a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8289a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8290a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8291a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8292a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8293a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8294a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8295a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8296a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8297a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8298a6e0b375SMatthew G. Knepley . t            - The current time
8299a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8300a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8301a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8302a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8303a6e0b375SMatthew G. Knepley 
830473ff1848SBarry Smith   Level: intermediate
830573ff1848SBarry Smith 
8306bb7acecfSBarry Smith   Note:
8307bb7acecfSBarry 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.
8308bb7acecfSBarry 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
8309bb7acecfSBarry 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
8310a6e0b375SMatthew 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.
8311a6e0b375SMatthew G. Knepley 
8312bb7acecfSBarry Smith   Developer Notes:
8313bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8314bb7acecfSBarry Smith 
8315bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8316bb7acecfSBarry Smith 
8317a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8318a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8319a6e0b375SMatthew G. Knepley @*/
8320a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8321d71ae5a4SJacob Faibussowitsch {
83228c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83238c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8324eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
83258c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8326fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
83273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83288c6c5593SMatthew G. Knepley }
83298c6c5593SMatthew G. Knepley 
8330a6e0b375SMatthew G. Knepley /*@C
8331a6e0b375SMatthew 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.
8332a6e0b375SMatthew G. Knepley 
833320f4b53cSBarry Smith   Not Collective
8334a6e0b375SMatthew G. Knepley 
8335a6e0b375SMatthew G. Knepley   Input Parameters:
8336bb7acecfSBarry Smith + dm     - The `DM`
8337a6e0b375SMatthew G. Knepley . time   - The time
8338bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8339a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8340a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8341bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
834220f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8343a6e0b375SMatthew G. Knepley . localU - The input field vector
8344a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8345a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8346a6e0b375SMatthew G. Knepley 
8347a6e0b375SMatthew G. Knepley   Output Parameter:
8348a6e0b375SMatthew G. Knepley . localX - The output vector
8349a6e0b375SMatthew G. Knepley 
835020f4b53cSBarry Smith   Calling sequence of `funcs`:
8351a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8352a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8353a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8354a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8355a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8356a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8357a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8358a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8359a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8360a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8361a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8362a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8363a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8364a6e0b375SMatthew G. Knepley . t            - The current time
8365a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8366a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8367a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8368a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8369a6e0b375SMatthew G. Knepley 
837073ff1848SBarry Smith   Level: intermediate
837173ff1848SBarry Smith 
8372bb7acecfSBarry Smith   Note:
8373bb7acecfSBarry 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.
8374bb7acecfSBarry 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
8375bb7acecfSBarry 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
8376a6e0b375SMatthew 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.
8377a6e0b375SMatthew G. Knepley 
8378bb7acecfSBarry Smith   Developer Notes:
8379bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8380bb7acecfSBarry Smith 
8381bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8382bb7acecfSBarry Smith 
83831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8384a6e0b375SMatthew G. Knepley @*/
8385a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8386d71ae5a4SJacob Faibussowitsch {
83878c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83888c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8389064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8390064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8391fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
83923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83938c6c5593SMatthew G. Knepley }
83948c6c5593SMatthew G. Knepley 
83952716604bSToby Isaac /*@C
8396d29d7c6eSMatthew 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.
8397d29d7c6eSMatthew G. Knepley 
839820f4b53cSBarry Smith   Not Collective
8399d29d7c6eSMatthew G. Knepley 
8400d29d7c6eSMatthew G. Knepley   Input Parameters:
8401bb7acecfSBarry Smith + dm     - The `DM`
8402d29d7c6eSMatthew G. Knepley . time   - The time
8403bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8404d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8405d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8406bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
840720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8408d29d7c6eSMatthew G. Knepley . U      - The input field vector
8409d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8410d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8411d29d7c6eSMatthew G. Knepley 
8412d29d7c6eSMatthew G. Knepley   Output Parameter:
8413d29d7c6eSMatthew G. Knepley . X - The output vector
8414d29d7c6eSMatthew G. Knepley 
841520f4b53cSBarry Smith   Calling sequence of `funcs`:
8416d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8417d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8418d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8419d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8420d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8421d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8422d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8423d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8424d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8425d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8426d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8427d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8428d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8429d29d7c6eSMatthew G. Knepley . t            - The current time
8430d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8431d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8432d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8433d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8434d29d7c6eSMatthew G. Knepley 
843573ff1848SBarry Smith   Level: intermediate
843673ff1848SBarry Smith 
8437bb7acecfSBarry Smith   Note:
8438bb7acecfSBarry 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.
8439bb7acecfSBarry 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
8440bb7acecfSBarry 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
8441d29d7c6eSMatthew 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.
8442d29d7c6eSMatthew G. Knepley 
8443bb7acecfSBarry Smith   Developer Notes:
8444bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8445bb7acecfSBarry Smith 
8446bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8447bb7acecfSBarry Smith 
84481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8449d29d7c6eSMatthew G. Knepley @*/
8450a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec X)
8451d71ae5a4SJacob Faibussowitsch {
8452d29d7c6eSMatthew G. Knepley   DM  dmIn;
8453d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8454d29d7c6eSMatthew G. Knepley 
8455d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8456d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8457d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8458d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8459d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8460f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
846172fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
846272fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8463d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8464d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8465d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8466d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8467d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
84683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8469d29d7c6eSMatthew G. Knepley }
8470d29d7c6eSMatthew G. Knepley 
8471d29d7c6eSMatthew G. Knepley /*@C
8472ece3a9fcSMatthew 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.
8473ece3a9fcSMatthew G. Knepley 
847420f4b53cSBarry Smith   Not Collective
8475ece3a9fcSMatthew G. Knepley 
8476ece3a9fcSMatthew G. Knepley   Input Parameters:
8477bb7acecfSBarry Smith + dm     - The `DM`
8478ece3a9fcSMatthew G. Knepley . time   - The time
8479bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8480ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8481ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8482bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
848320f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8484ece3a9fcSMatthew G. Knepley . localU - The input field vector
8485ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8486ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8487ece3a9fcSMatthew G. Knepley 
8488ece3a9fcSMatthew G. Knepley   Output Parameter:
8489ece3a9fcSMatthew G. Knepley . localX - The output vector
8490ece3a9fcSMatthew G. Knepley 
849120f4b53cSBarry Smith   Calling sequence of `funcs`:
8492ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8493ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8494ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8495ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8496ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8497ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8498ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8499ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8500ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8501ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8502ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8503ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8504ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8505ece3a9fcSMatthew G. Knepley . t            - The current time
8506ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8507ece3a9fcSMatthew G. Knepley . n            - The face normal
8508ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8509ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8510ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8511ece3a9fcSMatthew G. Knepley 
851273ff1848SBarry Smith   Level: intermediate
851373ff1848SBarry Smith 
8514ece3a9fcSMatthew G. Knepley   Note:
8515bb7acecfSBarry 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.
8516bb7acecfSBarry 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
8517bb7acecfSBarry 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
8518ece3a9fcSMatthew 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.
8519ece3a9fcSMatthew G. Knepley 
8520bb7acecfSBarry Smith   Developer Notes:
8521bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8522bb7acecfSBarry Smith 
8523bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8524bb7acecfSBarry Smith 
85251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8526ece3a9fcSMatthew G. Knepley @*/
8527a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX)
8528d71ae5a4SJacob Faibussowitsch {
8529ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8530ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8531064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8532064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8533fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8535ece3a9fcSMatthew G. Knepley }
8536ece3a9fcSMatthew G. Knepley 
8537ece3a9fcSMatthew G. Knepley /*@C
85382716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85392716604bSToby Isaac 
854020f4b53cSBarry Smith   Collective
8541bb7acecfSBarry Smith 
85422716604bSToby Isaac   Input Parameters:
8543bb7acecfSBarry Smith + dm    - The `DM`
85440709b2feSToby Isaac . time  - The time
85452716604bSToby Isaac . funcs - The functions to evaluate for each field component
85462716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8547574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85482716604bSToby Isaac 
85492716604bSToby Isaac   Output Parameter:
85502716604bSToby Isaac . diff - The diff ||u - u_h||_2
85512716604bSToby Isaac 
85522716604bSToby Isaac   Level: developer
85532716604bSToby Isaac 
8554bb7acecfSBarry Smith   Developer Notes:
8555bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8556bb7acecfSBarry Smith 
8557bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8558bb7acecfSBarry Smith 
85591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
85602716604bSToby Isaac @*/
8561d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8562d71ae5a4SJacob Faibussowitsch {
85632716604bSToby Isaac   PetscFunctionBegin;
85642716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8565b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8566fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
85673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85682716604bSToby Isaac }
8569b698f381SToby Isaac 
8570b698f381SToby Isaac /*@C
8571b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8572b698f381SToby Isaac 
857320f4b53cSBarry Smith   Collective
8574d083f849SBarry Smith 
8575b698f381SToby Isaac   Input Parameters:
8576bb7acecfSBarry Smith + dm    - The `DM`
8577a4e35b19SJacob Faibussowitsch . time  - The time
8578b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8579b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8580574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8581b698f381SToby Isaac - n     - The vector to project along
8582b698f381SToby Isaac 
8583b698f381SToby Isaac   Output Parameter:
8584b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8585b698f381SToby Isaac 
8586b698f381SToby Isaac   Level: developer
8587b698f381SToby Isaac 
8588bb7acecfSBarry Smith   Developer Notes:
8589bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8590bb7acecfSBarry Smith 
8591bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8592bb7acecfSBarry Smith 
85931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8594b698f381SToby Isaac @*/
8595d71ae5a4SJacob 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)
8596d71ae5a4SJacob Faibussowitsch {
8597b698f381SToby Isaac   PetscFunctionBegin;
8598b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8599b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8600fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
86013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8602b698f381SToby Isaac }
8603b698f381SToby Isaac 
86042a16baeaSToby Isaac /*@C
86052a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86062a16baeaSToby Isaac 
860720f4b53cSBarry Smith   Collective
8608d083f849SBarry Smith 
86092a16baeaSToby Isaac   Input Parameters:
8610bb7acecfSBarry Smith + dm    - The `DM`
86112a16baeaSToby Isaac . time  - The time
86122a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86132a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8614574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86152a16baeaSToby Isaac 
86162a16baeaSToby Isaac   Output Parameter:
86172a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86182a16baeaSToby Isaac 
86192a16baeaSToby Isaac   Level: developer
86202a16baeaSToby Isaac 
8621bb7acecfSBarry Smith   Developer Notes:
8622bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8623bb7acecfSBarry Smith 
8624bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8625bb7acecfSBarry Smith 
862642747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
86272a16baeaSToby Isaac @*/
8628d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8629d71ae5a4SJacob Faibussowitsch {
86302a16baeaSToby Isaac   PetscFunctionBegin;
86312a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86322a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8633fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
86343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86352a16baeaSToby Isaac }
86362a16baeaSToby Isaac 
8637df0b854cSToby Isaac /*@C
8638bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8639502a2867SDave May 
8640502a2867SDave May   Not Collective
8641502a2867SDave May 
8642502a2867SDave May   Input Parameter:
8643bb7acecfSBarry Smith . dm - The `DM`
8644502a2867SDave May 
86450a19bb7dSprj-   Output Parameters:
86460a19bb7dSprj- + nranks - the number of neighbours
86470a19bb7dSprj- - ranks  - the neighbors ranks
8648502a2867SDave May 
86499bdbcad8SBarry Smith   Level: beginner
86509bdbcad8SBarry Smith 
8651bb7acecfSBarry Smith   Note:
8652bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8653502a2867SDave May 
86541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8655502a2867SDave May @*/
8656d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8657d71ae5a4SJacob Faibussowitsch {
8658502a2867SDave May   PetscFunctionBegin;
8659502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8660fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
86613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8662502a2867SDave May }
8663502a2867SDave May 
8664531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8665531c7667SBarry Smith 
8666531c7667SBarry Smith /*
8667531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
86682b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8669531c7667SBarry Smith */
867066976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8671d71ae5a4SJacob Faibussowitsch {
8672531c7667SBarry Smith   PetscFunctionBegin;
8673531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8674531c7667SBarry Smith     Vec x1local;
8675531c7667SBarry Smith     DM  dm;
86769566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86777a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
86789566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
86799566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
86809566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8681531c7667SBarry Smith     x1 = x1local;
8682531c7667SBarry Smith   }
86839566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8684531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8685531c7667SBarry Smith     DM dm;
86869566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86879566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8688531c7667SBarry Smith   }
86893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8690531c7667SBarry Smith }
8691531c7667SBarry Smith 
8692531c7667SBarry Smith /*@
8693bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8694531c7667SBarry Smith 
8695a4e35b19SJacob Faibussowitsch   Input Parameters:
8696a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8697a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8698531c7667SBarry Smith 
86999bdbcad8SBarry Smith   Level: advanced
87009bdbcad8SBarry Smith 
870173ff1848SBarry Smith   Developer Note:
870273ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8703531c7667SBarry Smith 
87041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8705531c7667SBarry Smith @*/
8706d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8707d71ae5a4SJacob Faibussowitsch {
8708531c7667SBarry Smith   PetscFunctionBegin;
8709531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
87103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8711531c7667SBarry Smith }
87128320bc6fSPatrick Sanan 
87138320bc6fSPatrick Sanan /*@
8714bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
87158320bc6fSPatrick Sanan 
87168320bc6fSPatrick Sanan   Collective
87178320bc6fSPatrick Sanan 
87188320bc6fSPatrick Sanan   Input Parameters:
8719bb7acecfSBarry Smith + dm1 - the first `DM`
8720bb7acecfSBarry Smith - dm2 - the second `DM`
87218320bc6fSPatrick Sanan 
87228320bc6fSPatrick Sanan   Output Parameters:
8723bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8724bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
87258320bc6fSPatrick Sanan 
872620f4b53cSBarry Smith   Level: advanced
872720f4b53cSBarry Smith 
87288320bc6fSPatrick Sanan   Notes:
8729bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
87303d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
87318320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8732bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
87333d862458SPatrick Sanan   decomposition, but hold different data.
87348320bc6fSPatrick Sanan 
87358320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8736bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87378320bc6fSPatrick Sanan 
8738bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87398320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87408320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8741bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87428320bc6fSPatrick Sanan 
8743bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8744bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87458320bc6fSPatrick Sanan .vb
87468320bc6fSPatrick Sanan   ...
87479566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
87488320bc6fSPatrick Sanan   if (set && compatible)  {
87499566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
87509566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
87519566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
87528320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
87538320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
87548320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
87558320bc6fSPatrick Sanan       }
87568320bc6fSPatrick Sanan     }
87579566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
87589566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
87598320bc6fSPatrick Sanan   } else {
87608320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
87618320bc6fSPatrick Sanan   }
87628320bc6fSPatrick Sanan   ...
87638320bc6fSPatrick Sanan .ve
87648320bc6fSPatrick Sanan 
8765bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
87668320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
87678320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
87688320bc6fSPatrick Sanan   always check the "set" output parameter.
87698320bc6fSPatrick Sanan 
8770bb7acecfSBarry Smith   A `DM` is always compatible with itself.
87718320bc6fSPatrick Sanan 
8772bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
87738320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87748320bc6fSPatrick Sanan   incompatible.
87758320bc6fSPatrick Sanan 
87768320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8777bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
87788320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
87798320bc6fSPatrick Sanan 
878073ff1848SBarry Smith   Developer Note:
8781bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
87823d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8783a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8784bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
87858320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
87863d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8787bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
87888320bc6fSPatrick Sanan 
87891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
87908320bc6fSPatrick Sanan @*/
8791d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8792d71ae5a4SJacob Faibussowitsch {
87938320bc6fSPatrick Sanan   PetscMPIInt compareResult;
87948320bc6fSPatrick Sanan   DMType      type, type2;
87958320bc6fSPatrick Sanan   PetscBool   sameType;
87968320bc6fSPatrick Sanan 
87978320bc6fSPatrick Sanan   PetscFunctionBegin;
8798a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
87998320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
88008320bc6fSPatrick Sanan 
88018320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8802a5bc1bf3SBarry Smith   if (dm1 == dm2) {
88038320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88048320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
88053ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88068320bc6fSPatrick Sanan   }
88078320bc6fSPatrick Sanan 
88088320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
88098320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
88108320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
88118320bc6fSPatrick Sanan      determined by the implementation-specific logic */
88129566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
88138320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
88148320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88158320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
88163ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88178320bc6fSPatrick Sanan   }
88188320bc6fSPatrick Sanan 
88198320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8820a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8821dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
88223ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
88238320bc6fSPatrick Sanan   }
88248320bc6fSPatrick Sanan 
8825a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
88268320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
88279566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
88289566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
88299566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
88308320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8831dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
88328320bc6fSPatrick Sanan   } else {
88338320bc6fSPatrick Sanan     *set = PETSC_FALSE;
88348320bc6fSPatrick Sanan   }
88353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88368320bc6fSPatrick Sanan }
8837c0f0dcc3SMatthew G. Knepley 
8838c0f0dcc3SMatthew G. Knepley /*@C
8839bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8840c0f0dcc3SMatthew G. Knepley 
884120f4b53cSBarry Smith   Logically Collective
8842c0f0dcc3SMatthew G. Knepley 
8843c0f0dcc3SMatthew G. Knepley   Input Parameters:
884460225df5SJacob Faibussowitsch + dm             - the `DM`
8845c0f0dcc3SMatthew G. Knepley . f              - the monitor function
884620f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
884749abdd8aSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`), see `PetscCtxDestroyFn` for the calling sequence
8848c0f0dcc3SMatthew G. Knepley 
884920f4b53cSBarry Smith   Options Database Key:
885060225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8851c0f0dcc3SMatthew G. Knepley                        does not cancel those set via the options database.
8852c0f0dcc3SMatthew G. Knepley 
88539bdbcad8SBarry Smith   Level: intermediate
88549bdbcad8SBarry Smith 
8855bb7acecfSBarry Smith   Note:
8856c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8857bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8858c0f0dcc3SMatthew G. Knepley   order in which they were set.
8859c0f0dcc3SMatthew G. Knepley 
886073ff1848SBarry Smith   Fortran Note:
8861bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8862bb7acecfSBarry Smith 
886373ff1848SBarry Smith   Developer Note:
8864bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8865c0f0dcc3SMatthew G. Knepley 
886649abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`, `PetscCtxDestroyFn`
8867c0f0dcc3SMatthew G. Knepley @*/
886849abdd8aSBarry Smith PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscCtxDestroyFn *monitordestroy)
8869d71ae5a4SJacob Faibussowitsch {
8870c0f0dcc3SMatthew G. Knepley   PetscInt m;
8871c0f0dcc3SMatthew G. Knepley 
8872c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8873c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8874c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8875c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8876c0f0dcc3SMatthew G. Knepley 
88779566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f, mctx, monitordestroy, (PetscErrorCode (*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
88783ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8879c0f0dcc3SMatthew G. Knepley   }
88807a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8881c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8882c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8883835f2295SStefano Zampini   dm->monitorcontext[dm->numbermonitors++] = mctx;
88843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8885c0f0dcc3SMatthew G. Knepley }
8886c0f0dcc3SMatthew G. Knepley 
8887c0f0dcc3SMatthew G. Knepley /*@
8888bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8889c0f0dcc3SMatthew G. Knepley 
889020f4b53cSBarry Smith   Logically Collective
8891c0f0dcc3SMatthew G. Knepley 
8892c0f0dcc3SMatthew G. Knepley   Input Parameter:
8893c0f0dcc3SMatthew G. Knepley . dm - the DM
8894c0f0dcc3SMatthew G. Knepley 
8895c0f0dcc3SMatthew G. Knepley   Options Database Key:
8896c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8897bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8898c0f0dcc3SMatthew G. Knepley   set via the options database
8899c0f0dcc3SMatthew G. Knepley 
89009bdbcad8SBarry Smith   Level: intermediate
89019bdbcad8SBarry Smith 
8902bb7acecfSBarry Smith   Note:
8903bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8904c0f0dcc3SMatthew G. Knepley 
89051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8906c0f0dcc3SMatthew G. Knepley @*/
8907d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8908d71ae5a4SJacob Faibussowitsch {
8909c0f0dcc3SMatthew G. Knepley   PetscInt m;
8910c0f0dcc3SMatthew G. Knepley 
8911c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8912c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8913c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
89149566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8915c0f0dcc3SMatthew G. Knepley   }
8916c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
89173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8918c0f0dcc3SMatthew G. Knepley }
8919c0f0dcc3SMatthew G. Knepley 
8920c0f0dcc3SMatthew G. Knepley /*@C
8921c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8922c0f0dcc3SMatthew G. Knepley 
892320f4b53cSBarry Smith   Collective
8924c0f0dcc3SMatthew G. Knepley 
8925c0f0dcc3SMatthew G. Knepley   Input Parameters:
8926bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8927c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8928c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8929c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
893049abdd8aSBarry Smith . monitor      - the monitor function, this must use a `PetscViewerFormat` as its context
8931bb7acecfSBarry 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
8932c0f0dcc3SMatthew G. Knepley 
8933c0f0dcc3SMatthew G. Knepley   Output Parameter:
8934c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8935c0f0dcc3SMatthew G. Knepley 
8936c0f0dcc3SMatthew G. Knepley   Level: developer
8937c0f0dcc3SMatthew G. Knepley 
8938648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8939db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
894060225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8941db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8942c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8943db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8944bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8945c0f0dcc3SMatthew G. Knepley @*/
8946d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8947d71ae5a4SJacob Faibussowitsch {
8948c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8949c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8950c0f0dcc3SMatthew G. Knepley 
8951c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8952c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8953648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8954c0f0dcc3SMatthew G. Knepley   if (*flg) {
8955c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8956c0f0dcc3SMatthew G. Knepley 
89579566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8958648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
89599566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
8960835f2295SStefano Zampini     PetscCall(DMMonitorSet(dm, monitor, vf, (PetscCtxDestroyFn *)PetscViewerAndFormatDestroy));
8961c0f0dcc3SMatthew G. Knepley   }
89623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8963c0f0dcc3SMatthew G. Knepley }
8964c0f0dcc3SMatthew G. Knepley 
8965c0f0dcc3SMatthew G. Knepley /*@
8966c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8967c0f0dcc3SMatthew G. Knepley 
896820f4b53cSBarry Smith   Collective
8969c0f0dcc3SMatthew G. Knepley 
89702fe279fdSBarry Smith   Input Parameter:
8971bb7acecfSBarry Smith . dm - The `DM`
8972c0f0dcc3SMatthew G. Knepley 
8973c0f0dcc3SMatthew G. Knepley   Level: developer
8974c0f0dcc3SMatthew G. Knepley 
897573ff1848SBarry Smith   Developer Note:
8976a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8977a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8978a4e35b19SJacob Faibussowitsch   concept of discretization.
8979bb7acecfSBarry Smith 
89801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8981c0f0dcc3SMatthew G. Knepley @*/
8982d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8983d71ae5a4SJacob Faibussowitsch {
8984c0f0dcc3SMatthew G. Knepley   PetscInt m;
8985c0f0dcc3SMatthew G. Knepley 
8986c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
89873ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8988c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
898948a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
89903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8991c0f0dcc3SMatthew G. Knepley }
89922e4af2aeSMatthew G. Knepley 
89932e4af2aeSMatthew G. Knepley /*@
8994bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
89952e4af2aeSMatthew G. Knepley 
899620f4b53cSBarry Smith   Collective
89972e4af2aeSMatthew G. Knepley 
89982e4af2aeSMatthew G. Knepley   Input Parameters:
8999bb7acecfSBarry Smith + dm  - The `DM`
90006b867d5aSJose E. Roman - sol - The solution vector
90012e4af2aeSMatthew G. Knepley 
90026b867d5aSJose E. Roman   Input/Output Parameter:
900320f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
90046b867d5aSJose E. Roman            contains the error in each field
90056b867d5aSJose E. Roman 
90066b867d5aSJose E. Roman   Output Parameter:
900720f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
900820f4b53cSBarry Smith 
900920f4b53cSBarry Smith   Level: developer
90102e4af2aeSMatthew G. Knepley 
9011bb7acecfSBarry Smith   Note:
9012bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
90132e4af2aeSMatthew G. Knepley 
90141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
90152e4af2aeSMatthew G. Knepley @*/
9016d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9017d71ae5a4SJacob Faibussowitsch {
90182e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
90192e4af2aeSMatthew G. Knepley   void    **ctxs;
90202e4af2aeSMatthew G. Knepley   PetscReal time;
90212e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
90222e4af2aeSMatthew G. Knepley 
90232e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
90249566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
90259566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
90269566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
90272e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
90282e4af2aeSMatthew G. Knepley     PetscDS         ds;
90292e4af2aeSMatthew G. Knepley     DMLabel         label;
90302e4af2aeSMatthew G. Knepley     IS              fieldIS;
90312e4af2aeSMatthew G. Knepley     const PetscInt *fields;
90322e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
90332e4af2aeSMatthew G. Knepley 
903407218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
90359566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90369566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90372e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90382e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90399566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90402e4af2aeSMatthew G. Knepley     }
90419566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90422e4af2aeSMatthew G. Knepley   }
9043ad540459SPierre 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);
90449566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90459566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90462e4af2aeSMatthew G. Knepley   if (errorVec) {
90472e4af2aeSMatthew G. Knepley     DM             edm;
90482e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
90492e4af2aeSMatthew G. Knepley     PetscBool      simplex;
90502e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
90512e4af2aeSMatthew G. Knepley 
90529566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
90539566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
90549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
90559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90562e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
90579566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
90582e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
90592e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
90602e4af2aeSMatthew G. Knepley       PetscQuadrature q;
90612e4af2aeSMatthew G. Knepley       const char     *name;
90622e4af2aeSMatthew G. Knepley 
90639566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
90649566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
90659566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
90669566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
90679566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
90689566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
90699566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
90709566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
90712e4af2aeSMatthew G. Knepley     }
90729566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
90732e4af2aeSMatthew G. Knepley 
90749566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
90759566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
90769566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
90779566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
90782e4af2aeSMatthew G. Knepley   }
90799566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
90803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90812e4af2aeSMatthew G. Knepley }
90829a2a23afSMatthew G. Knepley 
90839a2a23afSMatthew G. Knepley /*@
9084bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
90859a2a23afSMatthew G. Knepley 
908620f4b53cSBarry Smith   Not Collective
90879a2a23afSMatthew G. Knepley 
90889a2a23afSMatthew G. Knepley   Input Parameter:
9089bb7acecfSBarry Smith . dm - The `DM`
90909a2a23afSMatthew G. Knepley 
90919a2a23afSMatthew G. Knepley   Output Parameter:
9092a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
90939a2a23afSMatthew G. Knepley 
90949a2a23afSMatthew G. Knepley   Level: advanced
90959a2a23afSMatthew G. Knepley 
9096e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
90979a2a23afSMatthew G. Knepley @*/
9098d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9099d71ae5a4SJacob Faibussowitsch {
91009a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91019a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91029566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
91033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91049a2a23afSMatthew G. Knepley }
91059a2a23afSMatthew G. Knepley 
91069a2a23afSMatthew G. Knepley /*@
9107ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
91089a2a23afSMatthew G. Knepley 
910920f4b53cSBarry Smith   Not Collective
91109a2a23afSMatthew G. Knepley 
91119a2a23afSMatthew G. Knepley   Input Parameters:
9112bb7acecfSBarry Smith + dm    - The `DM`
9113bb7acecfSBarry Smith . label - The `DMLabel`
9114ac17215fSMatthew G. Knepley . value - The label value indicating the region
9115ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
91169a2a23afSMatthew G. Knepley 
91179a2a23afSMatthew G. Knepley   Output Parameter:
9118bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
91199a2a23afSMatthew G. Knepley 
91209bdbcad8SBarry Smith   Level: advanced
91219bdbcad8SBarry Smith 
9122bb7acecfSBarry Smith   Note:
9123bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
912404c51a94SMatthew G. Knepley 
9125e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
91269a2a23afSMatthew G. Knepley @*/
9127d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9128d71ae5a4SJacob Faibussowitsch {
9129ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
913004c51a94SMatthew G. Knepley   PetscBool       has;
91319a2a23afSMatthew G. Knepley 
91329a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91339a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91349a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91359a2a23afSMatthew G. Knepley   key.label = label;
91369a2a23afSMatthew G. Knepley   key.value = value;
9137ac17215fSMatthew G. Knepley   key.part  = part;
91389566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91399566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91409566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91429a2a23afSMatthew G. Knepley }
91439a2a23afSMatthew G. Knepley 
91449a2a23afSMatthew G. Knepley /*@
9145bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91469a2a23afSMatthew G. Knepley 
914720f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
91489a2a23afSMatthew G. Knepley 
91499a2a23afSMatthew G. Knepley   Input Parameters:
9150bb7acecfSBarry Smith + dm    - The `DM`
9151bb7acecfSBarry Smith . label - The `DMLabel`
91529a2a23afSMatthew G. Knepley . value - The label value indicating the region
9153ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9154bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
91559a2a23afSMatthew G. Knepley 
91569a2a23afSMatthew G. Knepley   Level: advanced
91579a2a23afSMatthew G. Knepley 
9158e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
91599a2a23afSMatthew G. Knepley @*/
9160d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9161d71ae5a4SJacob Faibussowitsch {
91629a2a23afSMatthew G. Knepley   Vec             old;
91639a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
91649a2a23afSMatthew G. Knepley 
91659a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91669a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91679a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91689a2a23afSMatthew G. Knepley   key.label = label;
91699a2a23afSMatthew G. Knepley   key.value = value;
9170ac17215fSMatthew G. Knepley   key.part  = part;
91719566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
91729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
91739566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
91749566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9175d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
91763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91779a2a23afSMatthew G. Knepley }
91789a2a23afSMatthew G. Knepley 
9179cc4c1da9SBarry Smith /*@
9180bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
91819a2a23afSMatthew G. Knepley 
918220f4b53cSBarry Smith   Not Collective
91839a2a23afSMatthew G. Knepley 
91849a2a23afSMatthew G. Knepley   Input Parameter:
9185bb7acecfSBarry Smith . dm - The `DM`
91869a2a23afSMatthew G. Knepley 
91879a2a23afSMatthew G. Knepley   Output Parameters:
9188bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9189bb7acecfSBarry Smith . values - The label values for each `Vec`
9190bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
91919a2a23afSMatthew G. Knepley 
91929bdbcad8SBarry Smith   Level: advanced
91939bdbcad8SBarry Smith 
9194bb7acecfSBarry Smith   Note:
9195bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
91969a2a23afSMatthew G. Knepley 
9197e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
91989a2a23afSMatthew G. Knepley @*/
9199d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9200d71ae5a4SJacob Faibussowitsch {
92019a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
92029a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
92039a2a23afSMatthew G. Knepley 
92049a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92059a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92064f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
92074f572ea9SToby Isaac   PetscAssertPointer(values, 3);
92084f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
92099566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
92109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
92119566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
92129371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
92139371c9d4SSatish Balay     labels[i] = keys[i].label;
92149371c9d4SSatish Balay     values[i] = keys[i].value;
92159371c9d4SSatish Balay     parts[i]  = keys[i].part;
92169371c9d4SSatish Balay   }
92179566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
92183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92199a2a23afSMatthew G. Knepley }
92209a2a23afSMatthew G. Knepley 
92219a2a23afSMatthew G. Knepley /*@
9222bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
92239a2a23afSMatthew G. Knepley 
922420f4b53cSBarry Smith   Not Collective
92259a2a23afSMatthew G. Knepley 
92269a2a23afSMatthew G. Knepley   Input Parameter:
9227bb7acecfSBarry Smith . dm - The `DM`
92289a2a23afSMatthew G. Knepley 
92299a2a23afSMatthew G. Knepley   Output Parameter:
9230bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
92319a2a23afSMatthew G. Knepley 
92329a2a23afSMatthew G. Knepley   Level: advanced
92339a2a23afSMatthew G. Knepley 
9234bb7acecfSBarry Smith   Note:
9235bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9236bb7acecfSBarry Smith 
9237e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92389a2a23afSMatthew G. Knepley @*/
9239d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9240d71ae5a4SJacob Faibussowitsch {
92419a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92429a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9243d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9244d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9245e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9246e4d5475eSStefano Zampini 
9247e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
92489566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9249d705d0eaSStefano Zampini   {
9250d705d0eaSStefano Zampini     Vec     *auxData;
9251d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9252d705d0eaSStefano Zampini 
9253d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9254d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9255d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9256d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9257d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9258e4d5475eSStefano Zampini   }
9259e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9260e4d5475eSStefano Zampini }
9261e4d5475eSStefano Zampini 
9262e4d5475eSStefano Zampini /*@
9263e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9264e4d5475eSStefano Zampini 
9265e4d5475eSStefano Zampini   Not Collective
9266e4d5475eSStefano Zampini 
9267e4d5475eSStefano Zampini   Input Parameter:
9268e4d5475eSStefano Zampini . dm - The `DM`
9269e4d5475eSStefano Zampini 
9270e4d5475eSStefano Zampini   Level: advanced
9271e4d5475eSStefano Zampini 
9272e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9273e4d5475eSStefano Zampini @*/
9274e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9275e4d5475eSStefano Zampini {
9276e4d5475eSStefano Zampini   Vec     *auxData;
9277e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9278e4d5475eSStefano Zampini 
9279e4d5475eSStefano Zampini   PetscFunctionBegin;
9280e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9281d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9282e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9283d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9284d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9285e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9286e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
92873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92889a2a23afSMatthew G. Knepley }
9289b5a892a1SMatthew G. Knepley 
9290cc4c1da9SBarry Smith /*@
9291bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9292b5a892a1SMatthew G. Knepley 
929320f4b53cSBarry Smith   Not Collective
9294b5a892a1SMatthew G. Knepley 
9295b5a892a1SMatthew G. Knepley   Input Parameters:
9296bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9297b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9298b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9299b5a892a1SMatthew G. Knepley 
9300b5a892a1SMatthew G. Knepley   Output Parameters:
9301bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9302b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9303b5a892a1SMatthew G. Knepley 
9304b5a892a1SMatthew G. Knepley   Level: advanced
9305b5a892a1SMatthew G. Knepley 
9306bb7acecfSBarry Smith   Note:
9307bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9308bb7acecfSBarry Smith 
930985036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9310bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9311bb7acecfSBarry Smith 
9312bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9313bb7acecfSBarry Smith 
93141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9315b5a892a1SMatthew G. Knepley @*/
9316d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9317d71ae5a4SJacob Faibussowitsch {
9318b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
931985036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9320b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9321b5a892a1SMatthew G. Knepley 
9322b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93239371c9d4SSatish Balay   if (!nO) {
93249371c9d4SSatish Balay     *ornt  = 0;
93259371c9d4SSatish Balay     *found = PETSC_TRUE;
93263ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93279371c9d4SSatish Balay   }
9328b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
932985036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9330b5a892a1SMatthew G. Knepley 
93319371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93329371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
93339371c9d4SSatish Balay     if (c == cS) {
93349371c9d4SSatish Balay       *ornt = o;
93359371c9d4SSatish Balay       break;
93369371c9d4SSatish Balay     }
9337b5a892a1SMatthew G. Knepley   }
9338b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9340b5a892a1SMatthew G. Knepley }
9341b5a892a1SMatthew G. Knepley 
9342cc4c1da9SBarry Smith /*@
9343bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9344b5a892a1SMatthew G. Knepley 
934520f4b53cSBarry Smith   Not Collective
9346b5a892a1SMatthew G. Knepley 
9347b5a892a1SMatthew G. Knepley   Input Parameters:
9348bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9349b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9350b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9351b5a892a1SMatthew G. Knepley 
93522fe279fdSBarry Smith   Output Parameter:
9353bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9354b5a892a1SMatthew G. Knepley 
9355b5a892a1SMatthew G. Knepley   Level: advanced
9356b5a892a1SMatthew G. Knepley 
9357bb7acecfSBarry Smith   Note:
9358bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9359bb7acecfSBarry Smith 
936073ff1848SBarry Smith   Developer Note:
9361bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9362bb7acecfSBarry Smith 
93631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9364b5a892a1SMatthew G. Knepley @*/
9365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9366d71ae5a4SJacob Faibussowitsch {
9367b5a892a1SMatthew G. Knepley   PetscBool found;
9368b5a892a1SMatthew G. Knepley 
9369b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93709566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
93717a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9373b5a892a1SMatthew G. Knepley }
9374b5a892a1SMatthew G. Knepley 
9375cc4c1da9SBarry Smith /*@
9376bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9377b5a892a1SMatthew G. Knepley 
937820f4b53cSBarry Smith   Not Collective
9379b5a892a1SMatthew G. Knepley 
9380b5a892a1SMatthew G. Knepley   Input Parameters:
9381bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9382b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9383b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9384b5a892a1SMatthew G. Knepley 
9385b5a892a1SMatthew G. Knepley   Output Parameters:
9386bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9387b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9388b5a892a1SMatthew G. Knepley 
9389b5a892a1SMatthew G. Knepley   Level: advanced
9390b5a892a1SMatthew G. Knepley 
939173ff1848SBarry Smith   Notes:
9392bb7acecfSBarry Smith   An arrangement is a vertex order
9393bb7acecfSBarry Smith 
939485036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9395bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9396bb7acecfSBarry Smith 
9397bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9398bb7acecfSBarry Smith 
939985036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9400b5a892a1SMatthew G. Knepley @*/
9401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9402d71ae5a4SJacob Faibussowitsch {
9403b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
940485036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9405b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9406b5a892a1SMatthew G. Knepley 
9407b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94089371c9d4SSatish Balay   if (!nO) {
94099371c9d4SSatish Balay     *ornt  = 0;
94109371c9d4SSatish Balay     *found = PETSC_TRUE;
94113ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94129371c9d4SSatish Balay   }
9413b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
941485036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9415b5a892a1SMatthew G. Knepley 
94169371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94179371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
94189371c9d4SSatish Balay     if (c == cS) {
94199371c9d4SSatish Balay       *ornt = o;
94209371c9d4SSatish Balay       break;
94219371c9d4SSatish Balay     }
9422b5a892a1SMatthew G. Knepley   }
9423b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9425b5a892a1SMatthew G. Knepley }
9426b5a892a1SMatthew G. Knepley 
9427cc4c1da9SBarry Smith /*@
9428bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9429b5a892a1SMatthew G. Knepley 
943020f4b53cSBarry Smith   Not Collective
9431b5a892a1SMatthew G. Knepley 
9432b5a892a1SMatthew G. Knepley   Input Parameters:
9433bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9434b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9435b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9436b5a892a1SMatthew G. Knepley 
94372fe279fdSBarry Smith   Output Parameter:
9438bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9439b5a892a1SMatthew G. Knepley 
9440b5a892a1SMatthew G. Knepley   Level: advanced
9441b5a892a1SMatthew G. Knepley 
9442bb7acecfSBarry Smith   Note:
9443bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9444bb7acecfSBarry Smith 
944573ff1848SBarry Smith   Developer Note:
9446bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9447bb7acecfSBarry Smith 
94481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9449b5a892a1SMatthew G. Knepley @*/
9450d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9451d71ae5a4SJacob Faibussowitsch {
9452b5a892a1SMatthew G. Knepley   PetscBool found;
9453b5a892a1SMatthew G. Knepley 
9454b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94559566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
94567a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9458b5a892a1SMatthew G. Knepley }
9459012bc364SMatthew G. Knepley 
9460cc4c1da9SBarry Smith /*@
9461012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9462012bc364SMatthew G. Knepley 
946320f4b53cSBarry Smith   Not Collective
9464012bc364SMatthew G. Knepley 
9465012bc364SMatthew G. Knepley   Input Parameters:
9466bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9467012bc364SMatthew G. Knepley - point - Coordinates of the point
9468012bc364SMatthew G. Knepley 
94692fe279fdSBarry Smith   Output Parameter:
9470012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9471012bc364SMatthew G. Knepley 
9472012bc364SMatthew G. Knepley   Level: advanced
9473012bc364SMatthew G. Knepley 
94741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9475012bc364SMatthew G. Knepley @*/
9476d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9477d71ae5a4SJacob Faibussowitsch {
9478012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9479012bc364SMatthew G. Knepley   PetscInt  d;
9480012bc364SMatthew G. Knepley 
9481012bc364SMatthew G. Knepley   PetscFunctionBegin;
9482012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9483012bc364SMatthew G. Knepley   switch (ct) {
9484012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9485012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9486012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
94879371c9d4SSatish Balay       if (point[d] < -1.0) {
94889371c9d4SSatish Balay         *inside = PETSC_FALSE;
94899371c9d4SSatish Balay         break;
94909371c9d4SSatish Balay       }
9491012bc364SMatthew G. Knepley       sum += point[d];
9492012bc364SMatthew G. Knepley     }
94939371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
94949371c9d4SSatish Balay       *inside = PETSC_FALSE;
94959371c9d4SSatish Balay       break;
94969371c9d4SSatish Balay     }
9497012bc364SMatthew G. Knepley     break;
9498012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9499012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9500012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
95019371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
95029371c9d4SSatish Balay         *inside = PETSC_FALSE;
9503012bc364SMatthew G. Knepley         break;
95049371c9d4SSatish Balay       }
95059371c9d4SSatish Balay     break;
9506d71ae5a4SJacob Faibussowitsch   default:
9507d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9508012bc364SMatthew G. Knepley   }
95093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9510012bc364SMatthew G. Knepley }
9511adc21957SMatthew G. Knepley 
9512adc21957SMatthew G. Knepley /*@
9513adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9514adc21957SMatthew G. Knepley 
9515adc21957SMatthew G. Knepley   Logically collective
9516adc21957SMatthew G. Knepley 
9517adc21957SMatthew G. Knepley   Input Parameters:
9518adc21957SMatthew G. Knepley + dm      - The DM
9519adc21957SMatthew G. Knepley - reorder - Flag for reordering
9520adc21957SMatthew G. Knepley 
9521adc21957SMatthew G. Knepley   Level: intermediate
9522adc21957SMatthew G. Knepley 
9523adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9524adc21957SMatthew G. Knepley @*/
9525adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9526adc21957SMatthew G. Knepley {
9527adc21957SMatthew G. Knepley   PetscFunctionBegin;
9528adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9529adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9530adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9531adc21957SMatthew G. Knepley }
9532adc21957SMatthew G. Knepley 
9533adc21957SMatthew G. Knepley /*@
9534adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9535adc21957SMatthew G. Knepley 
9536adc21957SMatthew G. Knepley   Not collective
9537adc21957SMatthew G. Knepley 
9538adc21957SMatthew G. Knepley   Input Parameter:
9539adc21957SMatthew G. Knepley . dm - The DM
9540adc21957SMatthew G. Knepley 
9541adc21957SMatthew G. Knepley   Output Parameter:
9542adc21957SMatthew G. Knepley . reorder - Flag for reordering
9543adc21957SMatthew G. Knepley 
9544adc21957SMatthew G. Knepley   Level: intermediate
9545adc21957SMatthew G. Knepley 
9546adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9547adc21957SMatthew G. Knepley @*/
9548adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9549adc21957SMatthew G. Knepley {
9550adc21957SMatthew G. Knepley   PetscFunctionBegin;
9551adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9552adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9553adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9554adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9555adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9556adc21957SMatthew G. Knepley }
9557adc21957SMatthew G. Knepley 
9558cc4c1da9SBarry Smith /*@
9559adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9560adc21957SMatthew G. Knepley 
9561adc21957SMatthew G. Knepley   Logically collective
9562adc21957SMatthew G. Knepley 
9563adc21957SMatthew G. Knepley   Input Parameters:
9564adc21957SMatthew G. Knepley + dm      - The DM
9565adc21957SMatthew G. Knepley - reorder - The reordering method
9566adc21957SMatthew G. Knepley 
9567adc21957SMatthew G. Knepley   Level: intermediate
9568adc21957SMatthew G. Knepley 
9569adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9570adc21957SMatthew G. Knepley @*/
9571adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9572adc21957SMatthew G. Knepley {
9573adc21957SMatthew G. Knepley   PetscFunctionBegin;
9574adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9575adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9576adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9577adc21957SMatthew G. Knepley }
9578adc21957SMatthew G. Knepley 
9579cc4c1da9SBarry Smith /*@
9580adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9581adc21957SMatthew G. Knepley 
9582adc21957SMatthew G. Knepley   Not collective
9583adc21957SMatthew G. Knepley 
9584adc21957SMatthew G. Knepley   Input Parameter:
9585adc21957SMatthew G. Knepley . dm - The DM
9586adc21957SMatthew G. Knepley 
9587adc21957SMatthew G. Knepley   Output Parameter:
9588adc21957SMatthew G. Knepley . reorder - The reordering method
9589adc21957SMatthew G. Knepley 
9590adc21957SMatthew G. Knepley   Level: intermediate
9591adc21957SMatthew G. Knepley 
9592adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9593adc21957SMatthew G. Knepley @*/
9594adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9595adc21957SMatthew G. Knepley {
9596adc21957SMatthew G. Knepley   PetscFunctionBegin;
9597adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9598adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9599adc21957SMatthew G. Knepley   *reorder = NULL;
9600adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9601adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9602adc21957SMatthew G. Knepley }
9603