xref: /petsc/src/dm/interface/dm.c (revision b9da1bb3453dee27f36fc5f67fa6245b4c0fc22b)
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;
17708be2fdSJed Brown PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_CreateMassMatrix, DM_Load, DM_AdaptInterpolator, DM_ProjectFunction;
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)
843*b9da1bb3SMatthew 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 
9969566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9983ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9999566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
10009566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
100132c0f0efSBarry Smith   if (isbinary) {
100255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
100332c0f0efSBarry Smith     char     type[256];
100432c0f0efSBarry Smith 
10059566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
1006c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
10079566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
100832c0f0efSBarry Smith   }
1009dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
10103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
101147c6ae99SBarry Smith }
101247c6ae99SBarry Smith 
101347c6ae99SBarry Smith /*@
1014bb7acecfSBarry 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,
1015bb7acecfSBarry Smith   that is it has no ghost locations.
101647c6ae99SBarry Smith 
101720f4b53cSBarry Smith   Collective
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith   Input Parameter:
1020bb7acecfSBarry Smith . dm - the `DM` object
102147c6ae99SBarry Smith 
102247c6ae99SBarry Smith   Output Parameter:
102347c6ae99SBarry Smith . vec - the global vector
102447c6ae99SBarry Smith 
1025073dac72SJed Brown   Level: beginner
102647c6ae99SBarry Smith 
10271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1028bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
102947c6ae99SBarry Smith @*/
1030d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1031d71ae5a4SJacob Faibussowitsch {
103247c6ae99SBarry Smith   PetscFunctionBegin;
1033171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10344f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1035dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
103676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1037c6b011d8SStefano Zampini     DM vdm;
1038c6b011d8SStefano Zampini 
10399566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10407a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1041c6b011d8SStefano Zampini   }
10423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104347c6ae99SBarry Smith }
104447c6ae99SBarry Smith 
104547c6ae99SBarry Smith /*@
1046bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
104747c6ae99SBarry Smith 
104847c6ae99SBarry Smith   Not Collective
104947c6ae99SBarry Smith 
105047c6ae99SBarry Smith   Input Parameter:
1051bb7acecfSBarry Smith . dm - the `DM` object
105247c6ae99SBarry Smith 
105347c6ae99SBarry Smith   Output Parameter:
105447c6ae99SBarry Smith . vec - the local vector
105547c6ae99SBarry Smith 
1056073dac72SJed Brown   Level: beginner
105747c6ae99SBarry Smith 
105820f4b53cSBarry Smith   Note:
1059bb7acecfSBarry 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.
1060bb7acecfSBarry Smith 
10611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1062bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
106347c6ae99SBarry Smith @*/
1064d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1065d71ae5a4SJacob Faibussowitsch {
106647c6ae99SBarry Smith   PetscFunctionBegin;
1067171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10684f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1069dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
107076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1071c6b011d8SStefano Zampini     DM vdm;
1072c6b011d8SStefano Zampini 
10739566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10747a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1075c6b011d8SStefano Zampini   }
10763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
107747c6ae99SBarry Smith }
107847c6ae99SBarry Smith 
10791411c6eeSJed Brown /*@
1080bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10811411c6eeSJed Brown 
108220f4b53cSBarry Smith   Collective
10831411c6eeSJed Brown 
10841411c6eeSJed Brown   Input Parameter:
1085bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10861411c6eeSJed Brown 
10871411c6eeSJed Brown   Output Parameter:
10881411c6eeSJed Brown . ltog - the mapping
10891411c6eeSJed Brown 
1090bb7acecfSBarry Smith   Level: advanced
10911411c6eeSJed Brown 
10921411c6eeSJed Brown   Notes:
1093bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10941411c6eeSJed Brown 
1095bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1096bb7acecfSBarry Smith   need to use this function with those objects.
1097bb7acecfSBarry Smith 
1098bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1099bb7acecfSBarry Smith 
110060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1101bb7acecfSBarry Smith           `DMCreateMatrix()`
11021411c6eeSJed Brown @*/
1103d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1104d71ae5a4SJacob Faibussowitsch {
11050be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
11061411c6eeSJed Brown 
11071411c6eeSJed Brown   PetscFunctionBegin;
11081411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11094f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
11101411c6eeSJed Brown   if (!dm->ltogmap) {
111137d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
111237d0c07bSMatthew G Knepley 
11139566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
111437d0c07bSMatthew G Knepley     if (section) {
1115a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
111637d0c07bSMatthew G Knepley       PetscInt       *ltog;
1117ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
111837d0c07bSMatthew G Knepley 
11199566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11209566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11219566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11229566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
112337d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1124e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
112537d0c07bSMatthew G Knepley 
112637d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11279566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11311a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11321a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1133ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11345227eafbSStefano Zampini 
1135e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11365227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1137e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1138e6befd46SJed Brown             cind++;
1139e6befd46SJed Brown           } else {
11405227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1141e6befd46SJed Brown           }
114237d0c07bSMatthew G Knepley         }
114337d0c07bSMatthew G Knepley       }
1144bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11451690c2aeSBarry Smith       bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
11469371c9d4SSatish Balay       bsLocal[1] = bs;
11479566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11489371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11499371c9d4SSatish Balay         bs = 1;
11509371c9d4SSatish Balay       } else {
11519371c9d4SSatish Balay         bs = bsMinMax[0];
11529371c9d4SSatish Balay       }
11537591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11547591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1155ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1156ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1157ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1158ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1159ca469d19SJed Brown         }
1160ccf3bd66SMatthew G. Knepley         n /= bs;
1161ccf3bd66SMatthew G. Knepley       }
11629566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1163dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
116437d0c07bSMatthew G Knepley   }
11651411c6eeSJed Brown   *ltog = dm->ltogmap;
11663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11671411c6eeSJed Brown }
11681411c6eeSJed Brown 
11691411c6eeSJed Brown /*@
1170bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11711411c6eeSJed Brown 
11721411c6eeSJed Brown   Not Collective
11731411c6eeSJed Brown 
11741411c6eeSJed Brown   Input Parameter:
1175bb7acecfSBarry Smith . dm - the `DM` with block structure
11761411c6eeSJed Brown 
11771411c6eeSJed Brown   Output Parameter:
11781411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11791411c6eeSJed Brown 
11801411c6eeSJed Brown   Level: intermediate
11811411c6eeSJed Brown 
118273ff1848SBarry Smith   Notes:
1183bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1184bb7acecfSBarry Smith 
1185bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1186bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1187bb7acecfSBarry Smith 
11881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11891411c6eeSJed Brown @*/
1190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1191d71ae5a4SJacob Faibussowitsch {
11921411c6eeSJed Brown   PetscFunctionBegin;
11931411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11944f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11957a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11961411c6eeSJed Brown   *bs = dm->bs;
11973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11981411c6eeSJed Brown }
11991411c6eeSJed Brown 
1200ffeef943SBarry Smith /*@
1201bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1202bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
120347c6ae99SBarry Smith 
120420f4b53cSBarry Smith   Collective
120547c6ae99SBarry Smith 
1206d8d19677SJose E. Roman   Input Parameters:
1207bb7acecfSBarry Smith + dmc - the `DM` object
1208bb7acecfSBarry Smith - dmf - the second, finer `DM` object
120947c6ae99SBarry Smith 
1210d8d19677SJose E. Roman   Output Parameters:
121147c6ae99SBarry Smith + mat - the interpolation
1212b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
121347c6ae99SBarry Smith 
121447c6ae99SBarry Smith   Level: developer
121547c6ae99SBarry Smith 
121695452b02SPatrick Sanan   Notes:
1217bb7acecfSBarry 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
1218bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1219d52bd9f3SBarry Smith 
1220bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1221bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
122285afcc9aSBarry Smith 
12231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
122447c6ae99SBarry Smith @*/
1225d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1226d71ae5a4SJacob Faibussowitsch {
122747c6ae99SBarry Smith   PetscFunctionBegin;
1228a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1229a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12304f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12319566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1232dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12339566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
123547c6ae99SBarry Smith }
123647c6ae99SBarry Smith 
12373ad4599aSBarry Smith /*@
1238a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1239a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12402ed6491fSPatrick Sanan 
12412ed6491fSPatrick Sanan   Input Parameters:
1242bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1243bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12442ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12452ed6491fSPatrick Sanan 
12462ed6491fSPatrick Sanan   Output Parameter:
12472ed6491fSPatrick Sanan . scale - the scaled vector
12482ed6491fSPatrick Sanan 
1249bb7acecfSBarry Smith   Level: advanced
12502ed6491fSPatrick Sanan 
125173ff1848SBarry Smith   Note:
1252a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1253a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1254a4e35b19SJacob Faibussowitsch 
125573ff1848SBarry Smith   Developer Note:
1256bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1257e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1258e9c74fd6SRichard Tran Mills 
125960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12602ed6491fSPatrick Sanan @*/
1261d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1262d71ae5a4SJacob Faibussowitsch {
12632ed6491fSPatrick Sanan   Vec         fine;
12642ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12659704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1266e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12679704db99SRichard Tran Mills #endif
12682ed6491fSPatrick Sanan 
12692ed6491fSPatrick Sanan   PetscFunctionBegin;
12709566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12719566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12729566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12739704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12749704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12759704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12769704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12779566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1278e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12799566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12809566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12819566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
128283aa49f4SRichard Tran Mills   }
12839704db99SRichard Tran Mills #endif
12849566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12859566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12869566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12882ed6491fSPatrick Sanan }
12892ed6491fSPatrick Sanan 
12902ed6491fSPatrick Sanan /*@
1291bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1292bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12933ad4599aSBarry Smith 
129420f4b53cSBarry Smith   Collective
12953ad4599aSBarry Smith 
1296d8d19677SJose E. Roman   Input Parameters:
1297bb7acecfSBarry Smith + dmc - the `DM` object
1298bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12993ad4599aSBarry Smith 
13003ad4599aSBarry Smith   Output Parameter:
13013ad4599aSBarry Smith . mat - the restriction
13023ad4599aSBarry Smith 
13033ad4599aSBarry Smith   Level: developer
13043ad4599aSBarry Smith 
1305bb7acecfSBarry Smith   Note:
1306bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1307bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
13083ad4599aSBarry Smith 
13091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
13103ad4599aSBarry Smith @*/
1311d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1312d71ae5a4SJacob Faibussowitsch {
13133ad4599aSBarry Smith   PetscFunctionBegin;
1314a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1315a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13164f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1318dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13213ad4599aSBarry Smith }
13223ad4599aSBarry Smith 
132347c6ae99SBarry Smith /*@
1324a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
132547c6ae99SBarry Smith 
132620f4b53cSBarry Smith   Collective
132747c6ae99SBarry Smith 
1328d8d19677SJose E. Roman   Input Parameters:
1329bb7acecfSBarry Smith + dac - the `DM` object
1330bb7acecfSBarry Smith - daf - the second, finer `DM` object
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith   Output Parameter:
13336dbf9973SLawrence Mitchell . mat - the injection
133447c6ae99SBarry Smith 
133547c6ae99SBarry Smith   Level: developer
133647c6ae99SBarry Smith 
1337a4e35b19SJacob Faibussowitsch   Notes:
1338a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1339a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1340a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1341a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1342a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1343a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1344a4e35b19SJacob Faibussowitsch 
1345bb7acecfSBarry 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
1346bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
134785afcc9aSBarry Smith 
13481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1349bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
135047c6ae99SBarry Smith @*/
1351d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1352d71ae5a4SJacob Faibussowitsch {
135347c6ae99SBarry Smith   PetscFunctionBegin;
1354a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1355a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13564f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13579566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1358dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
136147c6ae99SBarry Smith }
136247c6ae99SBarry Smith 
1363b412c318SBarry Smith /*@
1364bb7acecfSBarry 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
1365bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1366bd041c0cSMatthew G. Knepley 
136720f4b53cSBarry Smith   Collective
1368bd041c0cSMatthew G. Knepley 
1369d8d19677SJose E. Roman   Input Parameters:
1370bb7acecfSBarry Smith + dmc - the target `DM` object
13718c1c0954SStefano Zampini - dmf - the source `DM` object, can be `NULL`
1372bd041c0cSMatthew G. Knepley 
1373bd041c0cSMatthew G. Knepley   Output Parameter:
1374b4937a87SMatthew G. Knepley . mat - the mass matrix
1375bd041c0cSMatthew G. Knepley 
1376bd041c0cSMatthew G. Knepley   Level: developer
1377bd041c0cSMatthew G. Knepley 
1378bb7acecfSBarry Smith   Notes:
1379bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1380bb7acecfSBarry Smith 
13818c1c0954SStefano 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()`
1382bb7acecfSBarry Smith 
138342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1384bd041c0cSMatthew G. Knepley @*/
1385d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1386d71ae5a4SJacob Faibussowitsch {
1387bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1388b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
13898c1c0954SStefano Zampini   if (!dmf) dmf = dmc;
1390b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13914f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13928c1c0954SStefano Zampini   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, dmc, dmf, 0, 0));
1393dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13948c1c0954SStefano Zampini   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, dmc, dmf, 0, 0));
13953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1396b4937a87SMatthew G. Knepley }
1397b4937a87SMatthew G. Knepley 
1398b4937a87SMatthew G. Knepley /*@
1399bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1400b4937a87SMatthew G. Knepley 
140120f4b53cSBarry Smith   Collective
1402b4937a87SMatthew G. Knepley 
1403b4937a87SMatthew G. Knepley   Input Parameter:
1404bb7acecfSBarry Smith . dm - the `DM` object
1405b4937a87SMatthew G. Knepley 
14067dcfde4dSJose E. Roman   Output Parameters:
14078e9849d2SStefano Zampini + llm - the local lumped mass matrix, which is a diagonal matrix, represented as a vector
14088e9849d2SStefano Zampini - lm  - the global lumped mass matrix, which is a diagonal matrix, represented as a vector
1409b4937a87SMatthew G. Knepley 
1410b4937a87SMatthew G. Knepley   Level: developer
1411b4937a87SMatthew G. Knepley 
1412bb7acecfSBarry Smith   Note:
1413bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1414bb7acecfSBarry Smith 
141560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1416b4937a87SMatthew G. Knepley @*/
14178e9849d2SStefano Zampini PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *llm, Vec *lm)
1418d71ae5a4SJacob Faibussowitsch {
1419b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1420b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14218e9849d2SStefano Zampini   if (llm) PetscAssertPointer(llm, 2);
14228e9849d2SStefano Zampini   if (lm) PetscAssertPointer(lm, 3);
14238e9849d2SStefano Zampini   if (llm || lm) PetscUseTypeMethod(dm, createmassmatrixlumped, llm, lm);
14243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1425bd041c0cSMatthew G. Knepley }
1426bd041c0cSMatthew G. Knepley 
1427bd041c0cSMatthew G. Knepley /*@
1428bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1429bb7acecfSBarry Smith   of a PDE on the `DM`.
143047c6ae99SBarry Smith 
143120f4b53cSBarry Smith   Collective
143247c6ae99SBarry Smith 
1433d8d19677SJose E. Roman   Input Parameters:
1434bb7acecfSBarry Smith + dm    - the `DM` object
1435bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
143647c6ae99SBarry Smith 
143747c6ae99SBarry Smith   Output Parameter:
143847c6ae99SBarry Smith . coloring - the coloring
143947c6ae99SBarry Smith 
14401bf8429eSBarry Smith   Level: developer
14411bf8429eSBarry Smith 
1442ec5066bdSBarry Smith   Notes:
1443bb7acecfSBarry 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
1444bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1445ec5066bdSBarry Smith 
1446bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14471bf8429eSBarry 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,
14481bf8429eSBarry Smith   otherwise an error will be generated.
1449ec5066bdSBarry Smith 
14501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1451aab9d709SJed Brown @*/
1452d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1453d71ae5a4SJacob Faibussowitsch {
145447c6ae99SBarry Smith   PetscFunctionBegin;
1455171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14564f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1457dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
145947c6ae99SBarry Smith }
146047c6ae99SBarry Smith 
1461b412c318SBarry Smith /*@
1462bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
146347c6ae99SBarry Smith 
146420f4b53cSBarry Smith   Collective
146547c6ae99SBarry Smith 
146647c6ae99SBarry Smith   Input Parameter:
1467bb7acecfSBarry Smith . dm - the `DM` object
146847c6ae99SBarry Smith 
146947c6ae99SBarry Smith   Output Parameter:
147047c6ae99SBarry Smith . mat - the empty Jacobian
147147c6ae99SBarry Smith 
147220f4b53cSBarry Smith   Options Database Key:
1473bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1474f27dd7c6SMatthew G. Knepley 
147520f4b53cSBarry Smith   Level: beginner
147620f4b53cSBarry Smith 
147795452b02SPatrick Sanan   Notes:
147895452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
147994013140SBarry Smith   do not need to do it yourself.
148094013140SBarry Smith 
148194013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1482bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
148394013140SBarry Smith 
1484bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
148594013140SBarry Smith   internally by PETSc.
148694013140SBarry Smith 
1487bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1488bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
148994013140SBarry Smith 
14901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1491aab9d709SJed Brown @*/
1492d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1493d71ae5a4SJacob Faibussowitsch {
149447c6ae99SBarry Smith   PetscFunctionBegin;
1495171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14964f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14979566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14989566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1499dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
150076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1501c6b011d8SStefano Zampini     DM mdm;
1502c6b011d8SStefano Zampini 
15039566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
15047a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1505c6b011d8SStefano Zampini   }
1506e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1507e5e52638SMatthew G. Knepley   if (dm->Nf) {
1508e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1509649ef022SMatthew Knepley     PetscInt     Nf, f;
1510e571a35bSMatthew G. Knepley 
15119566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1512649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1513649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15149566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15159566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15169566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1517649ef022SMatthew Knepley         break;
1518e571a35bSMatthew G. Knepley       }
1519649ef022SMatthew Knepley     }
1520649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1521649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15229566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15239566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15249566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1525e571a35bSMatthew G. Knepley       }
1526e571a35bSMatthew G. Knepley     }
1527e571a35bSMatthew G. Knepley   }
15289566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
153047c6ae99SBarry Smith }
153147c6ae99SBarry Smith 
1532732e2eb9SMatthew G Knepley /*@
1533a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1534a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1535a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1536aa0f6e3cSJed Brown 
153720f4b53cSBarry Smith   Logically Collective
1538aa0f6e3cSJed Brown 
1539aa0f6e3cSJed Brown   Input Parameters:
1540bb7acecfSBarry Smith + dm   - the `DM`
1541bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1542aa0f6e3cSJed Brown 
1543aa0f6e3cSJed Brown   Level: developer
1544aa0f6e3cSJed Brown 
154573ff1848SBarry Smith   Note:
1546a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1547a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1548a4e35b19SJacob Faibussowitsch 
15491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1550aa0f6e3cSJed Brown @*/
1551d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1552d71ae5a4SJacob Faibussowitsch {
1553aa0f6e3cSJed Brown   PetscFunctionBegin;
1554aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1555aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1557aa0f6e3cSJed Brown }
1558aa0f6e3cSJed Brown 
1559aa0f6e3cSJed Brown /*@
1560bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1561732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1562732e2eb9SMatthew G Knepley 
156320f4b53cSBarry Smith   Logically Collective
1564732e2eb9SMatthew G Knepley 
1565d8d19677SJose E. Roman   Input Parameters:
1566bb7acecfSBarry Smith + dm   - the `DM`
1567bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1568732e2eb9SMatthew G Knepley 
156920f4b53cSBarry Smith   Options Database Key:
1570bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1571f27dd7c6SMatthew G. Knepley 
157220f4b53cSBarry Smith   Level: developer
157320f4b53cSBarry Smith 
15741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1575732e2eb9SMatthew G Knepley @*/
1576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1577d71ae5a4SJacob Faibussowitsch {
1578732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1579732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1580732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1582732e2eb9SMatthew G Knepley }
1583732e2eb9SMatthew G Knepley 
1584b06ff27eSHong Zhang /*@
1585bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1586bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1587b06ff27eSHong Zhang 
158820f4b53cSBarry Smith   Logically Collective
1589b06ff27eSHong Zhang 
1590d8d19677SJose E. Roman   Input Parameters:
1591bb7acecfSBarry Smith + dm   - the `DM`
1592da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1593b06ff27eSHong Zhang 
1594b06ff27eSHong Zhang   Level: developer
1595bb7acecfSBarry Smith 
15961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1597b06ff27eSHong Zhang @*/
1598d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1599d71ae5a4SJacob Faibussowitsch {
1600b06ff27eSHong Zhang   PetscFunctionBegin;
1601b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1602b06ff27eSHong Zhang   dm->structure_only = only;
16033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1604b06ff27eSHong Zhang }
1605b06ff27eSHong Zhang 
1606863027abSJed Brown /*@
1607863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1608863027abSJed Brown 
160920f4b53cSBarry Smith   Logically Collective
1610863027abSJed Brown 
1611863027abSJed Brown   Input Parameters:
1612863027abSJed Brown + dm    - the `DM`
1613863027abSJed Brown - btype - block by topological point or field node
1614863027abSJed Brown 
161520f4b53cSBarry Smith   Options Database Key:
16160e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1617863027abSJed Brown 
161820f4b53cSBarry Smith   Level: advanced
161920f4b53cSBarry Smith 
16201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1621863027abSJed Brown @*/
1622863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1623863027abSJed Brown {
1624863027abSJed Brown   PetscFunctionBegin;
1625863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1626863027abSJed Brown   dm->blocking_type = btype;
16273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1628863027abSJed Brown }
1629863027abSJed Brown 
1630863027abSJed Brown /*@
1631863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1632863027abSJed Brown 
1633863027abSJed Brown   Not Collective
1634863027abSJed Brown 
16352fe279fdSBarry Smith   Input Parameter:
1636863027abSJed Brown . dm - the `DM`
1637863027abSJed Brown 
16382fe279fdSBarry Smith   Output Parameter:
1639863027abSJed Brown . btype - block by topological point or field node
1640863027abSJed Brown 
1641863027abSJed Brown   Level: advanced
1642863027abSJed Brown 
16431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1644863027abSJed Brown @*/
1645863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1646863027abSJed Brown {
1647863027abSJed Brown   PetscFunctionBegin;
1648863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16494f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1650863027abSJed Brown   *btype = dm->blocking_type;
16513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1652863027abSJed Brown }
1653863027abSJed Brown 
1654a89ea682SMatthew G Knepley /*@C
1655bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1656a89ea682SMatthew G Knepley 
1657a89ea682SMatthew G Knepley   Not Collective
1658a89ea682SMatthew G Knepley 
1659a89ea682SMatthew G Knepley   Input Parameters:
1660bb7acecfSBarry Smith + dm    - the `DM` object
1661a5b23f4aSJose E. Roman . count - The minimum size
166220f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1663a89ea682SMatthew G Knepley 
1664a89ea682SMatthew G Knepley   Output Parameter:
166560225df5SJacob Faibussowitsch . mem - the work array
1666a89ea682SMatthew G Knepley 
1667a89ea682SMatthew G Knepley   Level: developer
1668a89ea682SMatthew G Knepley 
166973ff1848SBarry Smith   Notes:
1670da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1671bb7acecfSBarry Smith 
1672bb7acecfSBarry Smith   The array may contain nonzero values
1673bb7acecfSBarry Smith 
16741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1675a89ea682SMatthew G Knepley @*/
1676d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1677d71ae5a4SJacob Faibussowitsch {
1678aa1993deSMatthew G Knepley   DMWorkLink  link;
167969291d52SBarry Smith   PetscMPIInt dsize;
1680a89ea682SMatthew G Knepley 
1681a89ea682SMatthew G Knepley   PetscFunctionBegin;
1682a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16834f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1684442f3b32SStefano Zampini   if (!count) {
1685442f3b32SStefano Zampini     *(void **)mem = NULL;
1686442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1687442f3b32SStefano Zampini   }
1688aa1993deSMatthew G Knepley   if (dm->workin) {
1689aa1993deSMatthew G Knepley     link       = dm->workin;
1690aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1691aa1993deSMatthew G Knepley   } else {
16924dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1693a89ea682SMatthew G Knepley   }
1694b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1695b77fa653SStefano Zampini      Get size directly */
1696b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1697b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1698b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1699b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1700b77fa653SStefano Zampini #endif
1701b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1702b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1703b77fa653SStefano Zampini #endif
1704b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1705b77fa653SStefano Zampini 
17065056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
17079566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
17089566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1709854ce69bSBarry Smith     link->bytes = dsize * count;
1710aa1993deSMatthew G Knepley   }
1711aa1993deSMatthew G Knepley   link->next    = dm->workout;
1712aa1993deSMatthew G Knepley   dm->workout   = link;
1713aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1715a89ea682SMatthew G Knepley }
1716a89ea682SMatthew G Knepley 
1717aa1993deSMatthew G Knepley /*@C
1718bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1719aa1993deSMatthew G Knepley 
1720aa1993deSMatthew G Knepley   Not Collective
1721aa1993deSMatthew G Knepley 
1722aa1993deSMatthew G Knepley   Input Parameters:
1723bb7acecfSBarry Smith + dm    - the `DM` object
1724a5b23f4aSJose E. Roman . count - The minimum size
172520f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1726aa1993deSMatthew G Knepley 
1727aa1993deSMatthew G Knepley   Output Parameter:
172860225df5SJacob Faibussowitsch . mem - the work array
1729aa1993deSMatthew G Knepley 
1730aa1993deSMatthew G Knepley   Level: developer
1731aa1993deSMatthew G Knepley 
173273ff1848SBarry Smith   Developer Note:
1733bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1734147403d9SBarry Smith 
17351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1736aa1993deSMatthew G Knepley @*/
1737d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1738d71ae5a4SJacob Faibussowitsch {
1739aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1740aa1993deSMatthew G Knepley 
1741aa1993deSMatthew G Knepley   PetscFunctionBegin;
1742aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17434f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1744a4e35b19SJacob Faibussowitsch   (void)count;
1745a4e35b19SJacob Faibussowitsch   (void)dtype;
1746442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1747aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1748aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1749aa1993deSMatthew G Knepley       *p            = link->next;
1750aa1993deSMatthew G Knepley       link->next    = dm->workin;
1751aa1993deSMatthew G Knepley       dm->workin    = link;
17520298fd71SBarry Smith       *(void **)mem = NULL;
17533ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1754aa1993deSMatthew G Knepley     }
1755aa1993deSMatthew G Knepley   }
1756aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1757aa1993deSMatthew G Knepley }
1758e7c4fc90SDmitry Karpeev 
17598cda7954SMatthew G. Knepley /*@C
1760bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1761bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17628cda7954SMatthew G. Knepley 
176320f4b53cSBarry Smith   Logically Collective; No Fortran Support
17648cda7954SMatthew G. Knepley 
17658cda7954SMatthew G. Knepley   Input Parameters:
1766bb7acecfSBarry Smith + dm     - The `DM`
17678cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17688cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17698cda7954SMatthew G. Knepley 
177020f4b53cSBarry Smith   Calling sequence of `nullsp`:
1771bb7acecfSBarry Smith + dm        - The present `DM`
1772bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1773147403d9SBarry Smith . field     - The field number in dm
1774147403d9SBarry Smith - nullSpace - The nullspace for the given field
17758cda7954SMatthew G. Knepley 
177649762cbcSSatish Balay   Level: intermediate
177749762cbcSSatish Balay 
17781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1779147403d9SBarry Smith @*/
1780a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1781d71ae5a4SJacob Faibussowitsch {
1782435a35e8SMatthew G Knepley   PetscFunctionBegin;
1783435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17847a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1785435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1787435a35e8SMatthew G Knepley }
1788435a35e8SMatthew G Knepley 
17898cda7954SMatthew G. Knepley /*@C
1790bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17918cda7954SMatthew G. Knepley 
179220f4b53cSBarry Smith   Not Collective; No Fortran Support
17938cda7954SMatthew G. Knepley 
17948cda7954SMatthew G. Knepley   Input Parameters:
1795bb7acecfSBarry Smith + dm    - The `DM`
17968cda7954SMatthew G. Knepley - field - The field number for the nullspace
17978cda7954SMatthew G. Knepley 
17988cda7954SMatthew G. Knepley   Output Parameter:
17998cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
18008cda7954SMatthew G. Knepley 
180120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1802147403d9SBarry Smith + dm        - The present DM
1803147403d9SBarry Smith . origField - The field number given above, in the original DM
1804147403d9SBarry Smith . field     - The field number in dm
1805147403d9SBarry Smith - nullSpace - The nullspace for the given field
18068cda7954SMatthew G. Knepley 
180749762cbcSSatish Balay   Level: intermediate
180849762cbcSSatish Balay 
18091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1810147403d9SBarry Smith @*/
1811a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1812d71ae5a4SJacob Faibussowitsch {
18130a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18140a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18154f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18167a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18170a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18190a50eb56SMatthew G. Knepley }
18200a50eb56SMatthew G. Knepley 
18218cda7954SMatthew G. Knepley /*@C
1822bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18238cda7954SMatthew G. Knepley 
182420f4b53cSBarry Smith   Logically Collective; No Fortran Support
18258cda7954SMatthew G. Knepley 
18268cda7954SMatthew G. Knepley   Input Parameters:
1827bb7acecfSBarry Smith + dm     - The `DM`
18288cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18298cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18308cda7954SMatthew G. Knepley 
183120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1832bb7acecfSBarry Smith + dm        - The present `DM`
1833bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1834147403d9SBarry Smith . field     - The field number in dm
1835147403d9SBarry Smith - nullSpace - The nullspace for the given field
18368cda7954SMatthew G. Knepley 
183749762cbcSSatish Balay   Level: intermediate
183849762cbcSSatish Balay 
18391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1840bb7acecfSBarry Smith           `MatNullSpace`
1841147403d9SBarry Smith @*/
1842a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1843d71ae5a4SJacob Faibussowitsch {
1844f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1845f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18467a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1847f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1849f9d4088aSMatthew G. Knepley }
1850f9d4088aSMatthew G. Knepley 
18518cda7954SMatthew G. Knepley /*@C
1852bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18538cda7954SMatthew G. Knepley 
185420f4b53cSBarry Smith   Not Collective; No Fortran Support
18558cda7954SMatthew G. Knepley 
18568cda7954SMatthew G. Knepley   Input Parameters:
1857bb7acecfSBarry Smith + dm    - The `DM`
18588cda7954SMatthew G. Knepley - field - The field number for the nullspace
18598cda7954SMatthew G. Knepley 
18608cda7954SMatthew G. Knepley   Output Parameter:
18618cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18628cda7954SMatthew G. Knepley 
186320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1864bb7acecfSBarry Smith + dm        - The present `DM`
1865bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1866147403d9SBarry Smith . field     - The field number in dm
1867147403d9SBarry Smith - nullSpace - The nullspace for the given field
18688cda7954SMatthew G. Knepley 
186949762cbcSSatish Balay   Level: intermediate
187049762cbcSSatish Balay 
18711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1872bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1873147403d9SBarry Smith @*/
1874a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1875d71ae5a4SJacob Faibussowitsch {
1876f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1877f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18784f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18797a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1880f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1882f9d4088aSMatthew G. Knepley }
1883f9d4088aSMatthew G. Knepley 
18844f3b5142SJed Brown /*@C
1885bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18864d343eeaSMatthew G Knepley 
188720f4b53cSBarry Smith   Not Collective; No Fortran Support
18884d343eeaSMatthew G Knepley 
18894d343eeaSMatthew G Knepley   Input Parameter:
1890bb7acecfSBarry Smith . dm - the `DM` object
18914d343eeaSMatthew G Knepley 
18924d343eeaSMatthew G Knepley   Output Parameters:
189320f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
189420f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
189520f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18964d343eeaSMatthew G Knepley 
18974d343eeaSMatthew G Knepley   Level: intermediate
18984d343eeaSMatthew G Knepley 
1899bb7acecfSBarry Smith   Note:
190073ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
190173ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1902bb7acecfSBarry Smith   `PetscFree()`.
190321c9b008SJed Brown 
190473ff1848SBarry Smith   Developer Note:
1905bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1906bb7acecfSBarry Smith   likely be removed.
1907bb7acecfSBarry Smith 
19081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1909bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19104d343eeaSMatthew G Knepley @*/
1911d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1912d71ae5a4SJacob Faibussowitsch {
191337d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19144d343eeaSMatthew G Knepley 
19154d343eeaSMatthew G Knepley   PetscFunctionBegin;
19164d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
191769ca1f37SDmitry Karpeev   if (numFields) {
19184f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
191969ca1f37SDmitry Karpeev     *numFields = 0;
192069ca1f37SDmitry Karpeev   }
192137d0c07bSMatthew G Knepley   if (fieldNames) {
19224f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19230298fd71SBarry Smith     *fieldNames = NULL;
192469ca1f37SDmitry Karpeev   }
192569ca1f37SDmitry Karpeev   if (fields) {
19264f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19270298fd71SBarry Smith     *fields = NULL;
192869ca1f37SDmitry Karpeev   }
19299566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
193037d0c07bSMatthew G Knepley   if (section) {
19313a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
193237d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
193337d0c07bSMatthew G Knepley 
19349566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19369566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
193837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
193937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19409566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
194137d0c07bSMatthew G Knepley     }
194237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
194337d0c07bSMatthew G Knepley       PetscInt gdof;
194437d0c07bSMatthew G Knepley 
19459566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
194637d0c07bSMatthew G Knepley       if (gdof > 0) {
194737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19483a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
194937d0c07bSMatthew G Knepley 
19509566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19519566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19523a544194SStefano Zampini           fpdof = fdof - fcdof;
19533a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19543a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19553a544194SStefano Zampini             fieldNc[f] = 1;
19563a544194SStefano Zampini           }
19573a544194SStefano Zampini           fieldSizes[f] += fpdof;
195837d0c07bSMatthew G Knepley         }
195937d0c07bSMatthew G Knepley       }
196037d0c07bSMatthew G Knepley     }
196137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
196337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
196437d0c07bSMatthew G Knepley     }
196537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
196637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
196737d0c07bSMatthew G Knepley 
19689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
196937d0c07bSMatthew G Knepley       if (gdof > 0) {
19709566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
197137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
197237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
197337d0c07bSMatthew G Knepley 
19749566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19759566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1976ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
197737d0c07bSMatthew G Knepley         }
197837d0c07bSMatthew G Knepley       }
197937d0c07bSMatthew G Knepley     }
19808865f1eaSKarl Rupp     if (numFields) *numFields = nF;
198137d0c07bSMatthew G Knepley     if (fieldNames) {
19829566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
198337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
198437d0c07bSMatthew G Knepley         const char *fieldName;
198537d0c07bSMatthew G Knepley 
19869566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
1987835f2295SStefano Zampini         PetscCall(PetscStrallocpy(fieldName, &(*fieldNames)[f]));
198837d0c07bSMatthew G Knepley       }
198937d0c07bSMatthew G Knepley     }
199037d0c07bSMatthew G Knepley     if (fields) {
19919566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
199237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19933a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19943a544194SStefano Zampini 
19959566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19963a544194SStefano Zampini         in[0] = -fieldNc[f];
19973a544194SStefano Zampini         in[1] = fieldNc[f];
1998462c564dSBarry Smith         PetscCallMPI(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19993a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
20009566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
200137d0c07bSMatthew G Knepley       }
200237d0c07bSMatthew G Knepley     }
20039566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
2004dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
20053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20064d343eeaSMatthew G Knepley }
20074d343eeaSMatthew G Knepley 
200816621825SDmitry Karpeev /*@C
2009bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2010a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2011e7c4fc90SDmitry Karpeev 
201220f4b53cSBarry Smith   Not Collective; No Fortran Support
2013e7c4fc90SDmitry Karpeev 
2014e7c4fc90SDmitry Karpeev   Input Parameter:
2015bb7acecfSBarry Smith . dm - the `DM` object
2016e7c4fc90SDmitry Karpeev 
2017e7c4fc90SDmitry Karpeev   Output Parameters:
201820f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
201920f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
202020f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
202120f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2022e7c4fc90SDmitry Karpeev 
2023e7c4fc90SDmitry Karpeev   Level: intermediate
2024e7c4fc90SDmitry Karpeev 
2025a4e35b19SJacob Faibussowitsch   Notes:
2026a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2027a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2028a4e35b19SJacob Faibussowitsch 
2029a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2030a4e35b19SJacob Faibussowitsch 
203173ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
203273ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2033bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2034e7c4fc90SDmitry Karpeev 
203560225df5SJacob Faibussowitsch   Developer Notes:
2036bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2037bb7acecfSBarry Smith 
203873ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
203973ff1848SBarry Smith   decomposition is computed.
204073ff1848SBarry Smith 
204173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2042e7c4fc90SDmitry Karpeev @*/
2043d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2044d71ae5a4SJacob Faibussowitsch {
2045e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2046e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20478865f1eaSKarl Rupp   if (len) {
20484f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20498865f1eaSKarl Rupp     *len = 0;
20508865f1eaSKarl Rupp   }
20518865f1eaSKarl Rupp   if (namelist) {
20524f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2053ea78f98cSLisandro Dalcin     *namelist = NULL;
20548865f1eaSKarl Rupp   }
20558865f1eaSKarl Rupp   if (islist) {
20564f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2057ea78f98cSLisandro Dalcin     *islist = NULL;
20588865f1eaSKarl Rupp   }
20598865f1eaSKarl Rupp   if (dmlist) {
20604f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2061ea78f98cSLisandro Dalcin     *dmlist = NULL;
20628865f1eaSKarl Rupp   }
2063f3f0edfdSDmitry Karpeev   /*
2064f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2065f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2066f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2067f3f0edfdSDmitry Karpeev    */
20687a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
206916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2070435a35e8SMatthew G Knepley     PetscSection section;
2071435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2072435a35e8SMatthew G Knepley 
20739566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20749566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2075435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2076f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20779566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20789566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20799566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2080435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2081435a35e8SMatthew G Knepley         const char *fieldName;
2082435a35e8SMatthew G Knepley 
20839566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
208403dc3394SMatthew G. Knepley         if (namelist) {
20859566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
2086835f2295SStefano Zampini           PetscCall(PetscStrallocpy(fieldName, &(*namelist)[f]));
2087435a35e8SMatthew G Knepley         }
208803dc3394SMatthew G. Knepley       }
2089435a35e8SMatthew G Knepley     } else {
20909566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2091e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20920298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2093e7c4fc90SDmitry Karpeev     }
2094dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
209616621825SDmitry Karpeev }
209716621825SDmitry Karpeev 
20985d83a8b1SBarry Smith /*@
209920f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
210020f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2101435a35e8SMatthew G Knepley 
2102435a35e8SMatthew G Knepley   Not collective
2103435a35e8SMatthew G Knepley 
2104435a35e8SMatthew G Knepley   Input Parameters:
2105bb7acecfSBarry Smith + dm        - The `DM` object
2106bb7acecfSBarry Smith . numFields - The number of fields to select
21072adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2108435a35e8SMatthew G Knepley 
2109435a35e8SMatthew G Knepley   Output Parameters:
2110b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2111b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2112435a35e8SMatthew G Knepley 
211320f4b53cSBarry Smith   Level: intermediate
211420f4b53cSBarry Smith 
2115bb7acecfSBarry Smith   Note:
2116bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21175d3b26e6SMatthew G. Knepley 
211860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2119435a35e8SMatthew G Knepley @*/
2120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2121d71ae5a4SJacob Faibussowitsch {
2122435a35e8SMatthew G Knepley   PetscFunctionBegin;
2123435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21244f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21254f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21264f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2127dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2129435a35e8SMatthew G Knepley }
2130435a35e8SMatthew G Knepley 
21312adcc780SMatthew G. Knepley /*@C
2132bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21332adcc780SMatthew G. Knepley 
21342adcc780SMatthew G. Knepley   Not collective
21352adcc780SMatthew G. Knepley 
2136d8d19677SJose E. Roman   Input Parameters:
2137bb7acecfSBarry Smith + dms - The `DM` objects
2138bb7acecfSBarry Smith - n   - The number of `DM`s
21392adcc780SMatthew G. Knepley 
21402adcc780SMatthew G. Knepley   Output Parameters:
2141bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2142bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21432adcc780SMatthew G. Knepley 
214420f4b53cSBarry Smith   Level: intermediate
214520f4b53cSBarry Smith 
2146bb7acecfSBarry Smith   Note:
2147bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21485d3b26e6SMatthew G. Knepley 
214973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21502adcc780SMatthew G. Knepley @*/
21515d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2152d71ae5a4SJacob Faibussowitsch {
21532adcc780SMatthew G. Knepley   PetscInt i;
21542adcc780SMatthew G. Knepley 
21552adcc780SMatthew G. Knepley   PetscFunctionBegin;
21564f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2157ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21584f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21594f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2160bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2161bb7acecfSBarry Smith   if (n) {
2162b9d85ea2SLisandro Dalcin     DM dm = dms[0];
216300045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2164dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21652adcc780SMatthew G. Knepley   }
21663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21672adcc780SMatthew G. Knepley }
21682adcc780SMatthew G. Knepley 
216916621825SDmitry Karpeev /*@C
2170a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2171a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
217216621825SDmitry Karpeev 
217320f4b53cSBarry Smith   Not Collective
217416621825SDmitry Karpeev 
217516621825SDmitry Karpeev   Input Parameter:
2176bb7acecfSBarry Smith . dm - the `DM` object
217716621825SDmitry Karpeev 
217816621825SDmitry Karpeev   Output Parameters:
217920f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
218020f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
218173ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
218273ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
218373ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
218416621825SDmitry Karpeev 
218516621825SDmitry Karpeev   Level: intermediate
218616621825SDmitry Karpeev 
218773ff1848SBarry Smith   Notes:
2188a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2189a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2190a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2191a4e35b19SJacob Faibussowitsch 
2192a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2193a4e35b19SJacob Faibussowitsch 
219473ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
219573ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2196bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
219716621825SDmitry Karpeev 
2198a4e35b19SJacob Faibussowitsch   Developer Notes:
219920f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2200bb7acecfSBarry Smith 
220173ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
220273ff1848SBarry Smith 
220373ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
220473ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
220516621825SDmitry Karpeev @*/
2206d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2207d71ae5a4SJacob Faibussowitsch {
2208be081cd6SPeter Brune   DMSubDomainHookLink link;
2209be081cd6SPeter Brune   PetscInt            i, l;
221016621825SDmitry Karpeev 
221116621825SDmitry Karpeev   PetscFunctionBegin;
221216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22139371c9d4SSatish Balay   if (n) {
22144f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22159371c9d4SSatish Balay     *n = 0;
22169371c9d4SSatish Balay   }
22179371c9d4SSatish Balay   if (namelist) {
22184f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22199371c9d4SSatish Balay     *namelist = NULL;
22209371c9d4SSatish Balay   }
22219371c9d4SSatish Balay   if (innerislist) {
22224f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22239371c9d4SSatish Balay     *innerislist = NULL;
22249371c9d4SSatish Balay   }
22259371c9d4SSatish Balay   if (outerislist) {
22264f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22279371c9d4SSatish Balay     *outerislist = NULL;
22289371c9d4SSatish Balay   }
22299371c9d4SSatish Balay   if (dmlist) {
22304f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22319371c9d4SSatish Balay     *dmlist = NULL;
22329371c9d4SSatish Balay   }
2233f3f0edfdSDmitry Karpeev   /*
2234f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2235f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2236f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2237f3f0edfdSDmitry Karpeev    */
22387a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
223916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2240dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
224114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2242f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2243be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2244be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22459566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2246be081cd6SPeter Brune         }
2247648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2248e7c4fc90SDmitry Karpeev       }
224914a18fd3SPeter Brune     }
2250bb7acecfSBarry Smith     if (n) *n = l;
225114a18fd3SPeter Brune   }
22523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2253e30e807fSPeter Brune }
2254e30e807fSPeter Brune 
2255e30e807fSPeter Brune /*@C
225673ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
225773ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2258e30e807fSPeter Brune 
225920f4b53cSBarry Smith   Not Collective
2260e30e807fSPeter Brune 
2261e30e807fSPeter Brune   Input Parameters:
2262bb7acecfSBarry Smith + dm     - the `DM` object
226373ff1848SBarry Smith . n      - the number of subdomains
2264e30e807fSPeter Brune - subdms - the local subdomains
2265e30e807fSPeter Brune 
2266e30e807fSPeter Brune   Output Parameters:
22676b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2268e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2269e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2270e30e807fSPeter Brune 
227120f4b53cSBarry Smith   Level: developer
227220f4b53cSBarry Smith 
2273bb7acecfSBarry Smith   Note:
2274bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2275e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2276e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2277e30e807fSPeter Brune   solution and residual data.
2278e30e807fSPeter Brune 
227973ff1848SBarry Smith   Developer Note:
2280a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2281a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2282bb7acecfSBarry Smith 
22831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2284e30e807fSPeter Brune @*/
22855d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2286d71ae5a4SJacob Faibussowitsch {
2287e30e807fSPeter Brune   PetscFunctionBegin;
2288e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22894f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2290dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2292e7c4fc90SDmitry Karpeev }
2293e7c4fc90SDmitry Karpeev 
229447c6ae99SBarry Smith /*@
2295bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
229647c6ae99SBarry Smith 
229720f4b53cSBarry Smith   Collective
229847c6ae99SBarry Smith 
2299d8d19677SJose E. Roman   Input Parameters:
2300bb7acecfSBarry Smith + dm   - the `DM` object
2301bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
230247c6ae99SBarry Smith 
230347c6ae99SBarry Smith   Output Parameter:
230420f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2305ae0a1c52SMatthew G Knepley 
230620f4b53cSBarry Smith   Options Database Key:
2307412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2308412e9a14SMatthew G. Knepley 
230947c6ae99SBarry Smith   Level: developer
231047c6ae99SBarry Smith 
231120f4b53cSBarry Smith   Note:
231220f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
231320f4b53cSBarry Smith 
231473ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
231573ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
231647c6ae99SBarry Smith @*/
2317d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2318d71ae5a4SJacob Faibussowitsch {
2319c833c3b5SJed Brown   DMRefineHookLink link;
232047c6ae99SBarry Smith 
232147c6ae99SBarry Smith   PetscFunctionBegin;
2322732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23239566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2324dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23254057135bSMatthew G Knepley   if (*dmf) {
232643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23278865f1eaSKarl Rupp 
23289566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23298865f1eaSKarl Rupp 
2330644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23310598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2332656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23338865f1eaSKarl Rupp 
23349566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2335c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23361baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2337c833c3b5SJed Brown     }
2338c833c3b5SJed Brown   }
23399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2341c833c3b5SJed Brown }
2342c833c3b5SJed Brown 
2343bb9467b5SJed Brown /*@C
2344c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2345c833c3b5SJed Brown 
234620f4b53cSBarry Smith   Logically Collective; No Fortran Support
2347c833c3b5SJed Brown 
23484165533cSJose E. Roman   Input Parameters:
2349bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2350bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2351f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
235220f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2353c833c3b5SJed Brown 
235420f4b53cSBarry Smith   Calling sequence of `refinehook`:
2355bb7acecfSBarry Smith + coarse - coarse level `DM`
2356bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2357c833c3b5SJed Brown - ctx    - optional user-defined function context
2358c833c3b5SJed Brown 
235920f4b53cSBarry Smith   Calling sequence of `interphook`:
2360bb7acecfSBarry Smith + coarse - coarse level `DM`
2361c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2362bb7acecfSBarry Smith . fine   - fine level `DM` to update
2363c833c3b5SJed Brown - ctx    - optional user-defined function context
2364c833c3b5SJed Brown 
2365c833c3b5SJed Brown   Level: advanced
2366c833c3b5SJed Brown 
2367c833c3b5SJed Brown   Notes:
2368bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2369bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2370bb7acecfSBarry Smith 
2371bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2372c833c3b5SJed Brown 
2373c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2374c833c3b5SJed Brown 
23751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2376c833c3b5SJed Brown @*/
2377a4e35b19SJacob 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)
2378d71ae5a4SJacob Faibussowitsch {
2379c833c3b5SJed Brown   DMRefineHookLink link, *p;
2380c833c3b5SJed Brown 
2381c833c3b5SJed Brown   PetscFunctionBegin;
2382c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23833d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23843ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23853d8e3701SJed Brown   }
23869566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2387c833c3b5SJed Brown   link->refinehook = refinehook;
2388c833c3b5SJed Brown   link->interphook = interphook;
2389c833c3b5SJed Brown   link->ctx        = ctx;
23900298fd71SBarry Smith   link->next       = NULL;
2391c833c3b5SJed Brown   *p               = link;
23923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2393c833c3b5SJed Brown }
2394c833c3b5SJed Brown 
23953d8e3701SJed Brown /*@C
2396bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2397bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23983d8e3701SJed Brown 
239920f4b53cSBarry Smith   Logically Collective; No Fortran Support
24003d8e3701SJed Brown 
24014165533cSJose E. Roman   Input Parameters:
2402bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2403bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2404bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
240520f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
24063d8e3701SJed Brown 
24073d8e3701SJed Brown   Level: advanced
24083d8e3701SJed Brown 
2409bb7acecfSBarry Smith   Note:
24103d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24113d8e3701SJed Brown 
24121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24133d8e3701SJed Brown @*/
2414d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2415d71ae5a4SJacob Faibussowitsch {
24163d8e3701SJed Brown   DMRefineHookLink link, *p;
24173d8e3701SJed Brown 
24183d8e3701SJed Brown   PetscFunctionBegin;
24193d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24203d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24213d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24223d8e3701SJed Brown       link = *p;
24233d8e3701SJed Brown       *p   = link->next;
24249566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24253d8e3701SJed Brown       break;
24263d8e3701SJed Brown     }
24273d8e3701SJed Brown   }
24283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24293d8e3701SJed Brown }
24303d8e3701SJed Brown 
2431c833c3b5SJed Brown /*@
2432bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2433c833c3b5SJed Brown 
2434c833c3b5SJed Brown   Collective if any hooks are
2435c833c3b5SJed Brown 
24364165533cSJose E. Roman   Input Parameters:
2437bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2438bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2439bb7acecfSBarry Smith - fine   - finer `DM` to update
2440c833c3b5SJed Brown 
2441c833c3b5SJed Brown   Level: developer
2442c833c3b5SJed Brown 
244373ff1848SBarry Smith   Developer Note:
2444bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2445bb7acecfSBarry Smith   an API with consistent terminology.
2446bb7acecfSBarry Smith 
24471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2448c833c3b5SJed Brown @*/
2449d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2450d71ae5a4SJacob Faibussowitsch {
2451c833c3b5SJed Brown   DMRefineHookLink link;
2452c833c3b5SJed Brown 
2453c833c3b5SJed Brown   PetscFunctionBegin;
2454c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24551baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24564057135bSMatthew G Knepley   }
24573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
245847c6ae99SBarry Smith }
245947c6ae99SBarry Smith 
2460eb3f98d2SBarry Smith /*@
24611f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24621f3379b2SToby Isaac 
246320f4b53cSBarry Smith   Collective
24641f3379b2SToby Isaac 
24654165533cSJose E. Roman   Input Parameters:
2466bb7acecfSBarry Smith + coarse    - coarse `DM`
2467bb7acecfSBarry Smith . fine      - fine `DM`
2468bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2469bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2470bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24711f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24721f3379b2SToby Isaac 
24734165533cSJose E. Roman   Output Parameter:
24741f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24751f3379b2SToby Isaac 
24761f3379b2SToby Isaac   Level: developer
24771f3379b2SToby Isaac 
2478bb7acecfSBarry Smith   Note:
2479bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24801f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24811f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24821f3379b2SToby Isaac   slope-limiting reconstruction.
24831f3379b2SToby Isaac 
248473ff1848SBarry Smith   Developer Note:
2485bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2486bb7acecfSBarry Smith 
24871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24881f3379b2SToby Isaac @*/
2489d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2490d71ae5a4SJacob Faibussowitsch {
24911f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24921f3379b2SToby Isaac 
24931f3379b2SToby Isaac   PetscFunctionBegin;
24941f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24951f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24961f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24971f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24981f3379b2SToby Isaac 
24999566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
25001f3379b2SToby Isaac   if (interpsol) {
25019566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
25021f3379b2SToby Isaac   } else if (interp) {
25039566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
250498921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
25053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25061f3379b2SToby Isaac }
25071f3379b2SToby Isaac 
25081f3379b2SToby Isaac /*@
2509bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2510eb3f98d2SBarry Smith 
2511eb3f98d2SBarry Smith   Not Collective
2512eb3f98d2SBarry Smith 
2513eb3f98d2SBarry Smith   Input Parameter:
2514bb7acecfSBarry Smith . dm - the `DM` object
2515eb3f98d2SBarry Smith 
2516eb3f98d2SBarry Smith   Output Parameter:
2517eb3f98d2SBarry Smith . level - number of refinements
2518eb3f98d2SBarry Smith 
2519eb3f98d2SBarry Smith   Level: developer
2520eb3f98d2SBarry Smith 
2521bb7acecfSBarry Smith   Note:
2522bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2523bb7acecfSBarry Smith 
25241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2525eb3f98d2SBarry Smith @*/
2526d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2527d71ae5a4SJacob Faibussowitsch {
2528eb3f98d2SBarry Smith   PetscFunctionBegin;
2529eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2530eb3f98d2SBarry Smith   *level = dm->levelup;
25313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2532eb3f98d2SBarry Smith }
2533eb3f98d2SBarry Smith 
2534fef3a512SBarry Smith /*@
2535bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2536fef3a512SBarry Smith 
2537fef3a512SBarry Smith   Not Collective
2538fef3a512SBarry Smith 
2539d8d19677SJose E. Roman   Input Parameters:
2540bb7acecfSBarry Smith + dm    - the `DM` object
2541fef3a512SBarry Smith - level - number of refinements
2542fef3a512SBarry Smith 
2543fef3a512SBarry Smith   Level: advanced
2544fef3a512SBarry Smith 
254595452b02SPatrick Sanan   Notes:
2546bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2547fef3a512SBarry Smith 
2548bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2549bb7acecfSBarry Smith 
25501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2551fef3a512SBarry Smith @*/
2552d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2553d71ae5a4SJacob Faibussowitsch {
2554fef3a512SBarry Smith   PetscFunctionBegin;
2555fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2556fef3a512SBarry Smith   dm->levelup = level;
25573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2558fef3a512SBarry Smith }
2559fef3a512SBarry Smith 
2560d410b0cfSMatthew G. Knepley /*@
2561bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2562d410b0cfSMatthew G. Knepley 
256320f4b53cSBarry Smith   Collective
2564d410b0cfSMatthew G. Knepley 
2565f1a722f8SMatthew G. Knepley   Input Parameters:
2566bb7acecfSBarry Smith + dm     - the `DM` object
2567d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2568d410b0cfSMatthew G. Knepley 
2569d410b0cfSMatthew G. Knepley   Output Parameter:
257020f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2571d410b0cfSMatthew G. Knepley 
2572d410b0cfSMatthew G. Knepley   Level: developer
2573d410b0cfSMatthew G. Knepley 
257420f4b53cSBarry Smith   Note:
257520f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
257620f4b53cSBarry Smith 
25771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2578d410b0cfSMatthew G. Knepley @*/
2579d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2580d71ae5a4SJacob Faibussowitsch {
2581d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2582d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2583dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2584d410b0cfSMatthew G. Knepley   if (*dme) {
2585d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25869566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2587d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25889566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2589d410b0cfSMatthew G. Knepley   }
25903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2591d410b0cfSMatthew G. Knepley }
2592d410b0cfSMatthew G. Knepley 
2593d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2594d71ae5a4SJacob Faibussowitsch {
2595ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2596ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25974f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2598ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2600ca3d3a14SMatthew G. Knepley }
2601ca3d3a14SMatthew G. Knepley 
2602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2603d71ae5a4SJacob Faibussowitsch {
2604ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2605ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26064f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2607ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
26083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2609ca3d3a14SMatthew G. Knepley }
2610ca3d3a14SMatthew G. Knepley 
2611ca3d3a14SMatthew G. Knepley /*@
2612bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2613ca3d3a14SMatthew G. Knepley 
2614ca3d3a14SMatthew G. Knepley   Input Parameter:
261520f4b53cSBarry Smith . dm - The `DM`
2616ca3d3a14SMatthew G. Knepley 
2617ca3d3a14SMatthew G. Knepley   Output Parameter:
261820f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2619ca3d3a14SMatthew G. Knepley 
2620ca3d3a14SMatthew G. Knepley   Level: developer
2621ca3d3a14SMatthew G. Knepley 
26221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2623ca3d3a14SMatthew G. Knepley @*/
2624d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2625d71ae5a4SJacob Faibussowitsch {
2626ca3d3a14SMatthew G. Knepley   Vec tv;
2627ca3d3a14SMatthew G. Knepley 
2628ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2629ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26304f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26319566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2632ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2634ca3d3a14SMatthew G. Knepley }
2635ca3d3a14SMatthew G. Knepley 
2636d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2637d71ae5a4SJacob Faibussowitsch {
2638ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2639ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2640ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2641ca3d3a14SMatthew G. Knepley 
2642ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26439566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26449566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26459566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26469566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26479566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26489566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2651ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2653ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2654ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2655ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26569566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2657ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26589566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26599566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2660ca3d3a14SMatthew G. Knepley     }
2661ca3d3a14SMatthew G. Knepley   }
26629566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26639566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26649566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2665ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2666ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2668ca3d3a14SMatthew G. Knepley       if (dof) {
2669ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2670ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2671ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2672ca3d3a14SMatthew G. Knepley 
2673ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26749566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26759566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26769566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2677ca3d3a14SMatthew G. Knepley       }
2678ca3d3a14SMatthew G. Knepley     }
2679ca3d3a14SMatthew G. Knepley   }
26809566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2682ca3d3a14SMatthew G. Knepley }
2683ca3d3a14SMatthew G. Knepley 
2684d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2685d71ae5a4SJacob Faibussowitsch {
2686ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2687ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2688ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2689ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2690ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2691ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2692ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26939566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2695ca3d3a14SMatthew G. Knepley }
2696ca3d3a14SMatthew G. Knepley 
2697bb9467b5SJed Brown /*@C
2698bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2699baf369e7SPeter Brune 
270020f4b53cSBarry Smith   Logically Collective
2701baf369e7SPeter Brune 
27024165533cSJose E. Roman   Input Parameters:
2703bb7acecfSBarry Smith + dm        - the `DM`
2704bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2705bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
270620f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2707baf369e7SPeter Brune 
270820f4b53cSBarry Smith   Calling sequence of `beginhook`:
2709a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2710baf369e7SPeter Brune . g    - global vector
2711baf369e7SPeter Brune . mode - mode
2712baf369e7SPeter Brune . l    - local vector
2713baf369e7SPeter Brune - ctx  - optional user-defined function context
2714baf369e7SPeter Brune 
271520f4b53cSBarry Smith   Calling sequence of `endhook`:
2716a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2717a4e35b19SJacob Faibussowitsch . g    - global vector
2718a4e35b19SJacob Faibussowitsch . mode - mode
2719a4e35b19SJacob Faibussowitsch . l    - local vector
2720baf369e7SPeter Brune - ctx  - optional user-defined function context
2721baf369e7SPeter Brune 
2722baf369e7SPeter Brune   Level: advanced
2723baf369e7SPeter Brune 
2724bb7acecfSBarry Smith   Note:
2725bb7acecfSBarry 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.
2726bb7acecfSBarry Smith 
27271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2728baf369e7SPeter Brune @*/
2729a4e35b19SJacob 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)
2730d71ae5a4SJacob Faibussowitsch {
2731baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2732baf369e7SPeter Brune 
2733baf369e7SPeter Brune   PetscFunctionBegin;
2734baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2735baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27369566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2737baf369e7SPeter Brune   link->beginhook = beginhook;
2738baf369e7SPeter Brune   link->endhook   = endhook;
2739baf369e7SPeter Brune   link->ctx       = ctx;
27400298fd71SBarry Smith   link->next      = NULL;
2741baf369e7SPeter Brune   *p              = link;
27423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2743baf369e7SPeter Brune }
2744baf369e7SPeter Brune 
2745d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2746d71ae5a4SJacob Faibussowitsch {
27474c274da1SToby Isaac   Mat          cMat;
274879769bd5SJed Brown   Vec          cVec, cBias;
27494c274da1SToby Isaac   PetscSection section, cSec;
27504c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27514c274da1SToby Isaac 
27524c274da1SToby Isaac   PetscFunctionBegin;
2753a4e35b19SJacob Faibussowitsch   (void)g;
2754a4e35b19SJacob Faibussowitsch   (void)ctx;
27554c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27569566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27574c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27585db9a05bSToby Isaac     PetscInt nRows;
27595db9a05bSToby Isaac 
27609566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27613ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27629566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27639566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27649566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27659566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27674c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27694c274da1SToby Isaac       if (dof) {
27704c274da1SToby Isaac         PetscScalar *vals;
27719566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27729566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27734c274da1SToby Isaac       }
27744c274da1SToby Isaac     }
27759566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27764c274da1SToby Isaac   }
27773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27784c274da1SToby Isaac }
27794c274da1SToby Isaac 
278047c6ae99SBarry Smith /*@
278101729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
278201729b5cSPatrick Sanan 
278320f4b53cSBarry Smith   Neighbor-wise Collective
278401729b5cSPatrick Sanan 
278501729b5cSPatrick Sanan   Input Parameters:
2786bb7acecfSBarry Smith + dm   - the `DM` object
278701729b5cSPatrick Sanan . g    - the global vector
2788bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
278901729b5cSPatrick Sanan - l    - the local vector
279001729b5cSPatrick Sanan 
279120f4b53cSBarry Smith   Level: beginner
279220f4b53cSBarry Smith 
279301729b5cSPatrick Sanan   Notes:
2794bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2795bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2796bb7acecfSBarry Smith 
2797bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
279801729b5cSPatrick Sanan 
27991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
280060225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2801bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
280201729b5cSPatrick Sanan @*/
2803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2804d71ae5a4SJacob Faibussowitsch {
280501729b5cSPatrick Sanan   PetscFunctionBegin;
28069566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
28079566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
28083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
280901729b5cSPatrick Sanan }
281001729b5cSPatrick Sanan 
281101729b5cSPatrick Sanan /*@
281247c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
281347c6ae99SBarry Smith 
281420f4b53cSBarry Smith   Neighbor-wise Collective
281547c6ae99SBarry Smith 
281647c6ae99SBarry Smith   Input Parameters:
2817bb7acecfSBarry Smith + dm   - the `DM` object
281847c6ae99SBarry Smith . g    - the global vector
2819bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
282047c6ae99SBarry Smith - l    - the local vector
282147c6ae99SBarry Smith 
282201729b5cSPatrick Sanan   Level: intermediate
282347c6ae99SBarry Smith 
2824bb7acecfSBarry Smith   Notes:
2825bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2826bb7acecfSBarry Smith 
2827bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2828bb7acecfSBarry Smith 
2829bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2830bb7acecfSBarry Smith 
2831bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2832bb7acecfSBarry Smith 
283360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
283447c6ae99SBarry Smith @*/
2835d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2836d71ae5a4SJacob Faibussowitsch {
28377128ae9fSMatthew G Knepley   PetscSF                 sf;
2838baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
283947c6ae99SBarry Smith 
284047c6ae99SBarry Smith   PetscFunctionBegin;
2841171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2842baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28431baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2844baf369e7SPeter Brune   }
28459566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28467128ae9fSMatthew G Knepley   if (sf) {
2847ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2848ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2849d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28507128ae9fSMatthew G Knepley 
28517a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28529566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28539566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28549566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28559566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28569566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28577128ae9fSMatthew G Knepley   } else {
2858fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28597128ae9fSMatthew G Knepley   }
28603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
286147c6ae99SBarry Smith }
286247c6ae99SBarry Smith 
286347c6ae99SBarry Smith /*@
286447c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
286547c6ae99SBarry Smith 
286620f4b53cSBarry Smith   Neighbor-wise Collective
286747c6ae99SBarry Smith 
286847c6ae99SBarry Smith   Input Parameters:
2869bb7acecfSBarry Smith + dm   - the `DM` object
287047c6ae99SBarry Smith . g    - the global vector
2871bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
287247c6ae99SBarry Smith - l    - the local vector
287347c6ae99SBarry Smith 
287401729b5cSPatrick Sanan   Level: intermediate
287547c6ae99SBarry Smith 
2876bb7acecfSBarry Smith   Note:
2877bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2878bb7acecfSBarry Smith 
287960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
288047c6ae99SBarry Smith @*/
2881d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2882d71ae5a4SJacob Faibussowitsch {
28837128ae9fSMatthew G Knepley   PetscSF                 sf;
2884ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2885ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2886ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2887baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2888d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
288947c6ae99SBarry Smith 
289047c6ae99SBarry Smith   PetscFunctionBegin;
2891171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28929566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28939566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28947128ae9fSMatthew G Knepley   if (sf) {
28957a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28967128ae9fSMatthew G Knepley 
28979566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28989566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28999566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
29009566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
29019566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
29029566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
29037128ae9fSMatthew G Knepley   } else {
2904fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
29057128ae9fSMatthew G Knepley   }
29069566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2907baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
29089566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2909baf369e7SPeter Brune   }
29103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
291147c6ae99SBarry Smith }
291247c6ae99SBarry Smith 
2913d4d07f1eSToby Isaac /*@C
2914d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2915d4d07f1eSToby Isaac 
291620f4b53cSBarry Smith   Logically Collective
2917d4d07f1eSToby Isaac 
29184165533cSJose E. Roman   Input Parameters:
2919bb7acecfSBarry Smith + dm        - the `DM`
2920bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2921bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
292220f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2923d4d07f1eSToby Isaac 
292420f4b53cSBarry Smith   Calling sequence of `beginhook`:
2925a4e35b19SJacob Faibussowitsch + global - global `DM`
2926d4d07f1eSToby Isaac . l      - local vector
2927d4d07f1eSToby Isaac . mode   - mode
2928d4d07f1eSToby Isaac . g      - global vector
2929d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2930d4d07f1eSToby Isaac 
293120f4b53cSBarry Smith   Calling sequence of `endhook`:
2932bb7acecfSBarry Smith + global - global `DM`
2933d4d07f1eSToby Isaac . l      - local vector
2934d4d07f1eSToby Isaac . mode   - mode
2935d4d07f1eSToby Isaac . g      - global vector
2936d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2937d4d07f1eSToby Isaac 
2938d4d07f1eSToby Isaac   Level: advanced
2939d4d07f1eSToby Isaac 
29401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2941d4d07f1eSToby Isaac @*/
2942a4e35b19SJacob 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)
2943d71ae5a4SJacob Faibussowitsch {
2944d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2945d4d07f1eSToby Isaac 
2946d4d07f1eSToby Isaac   PetscFunctionBegin;
2947d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2948d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29499566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2950d4d07f1eSToby Isaac   link->beginhook = beginhook;
2951d4d07f1eSToby Isaac   link->endhook   = endhook;
2952d4d07f1eSToby Isaac   link->ctx       = ctx;
2953d4d07f1eSToby Isaac   link->next      = NULL;
2954d4d07f1eSToby Isaac   *p              = link;
29553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2956d4d07f1eSToby Isaac }
2957d4d07f1eSToby Isaac 
2958d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2959d71ae5a4SJacob Faibussowitsch {
29604c274da1SToby Isaac   PetscFunctionBegin;
2961a4e35b19SJacob Faibussowitsch   (void)g;
2962a4e35b19SJacob Faibussowitsch   (void)ctx;
29634c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29642b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29652b6b7d09SToby Isaac     Mat          cMat;
29662b6b7d09SToby Isaac     Vec          cVec;
29675db9a05bSToby Isaac     PetscInt     nRows;
29682b6b7d09SToby Isaac     PetscSection section, cSec;
29692b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29702b6b7d09SToby Isaac 
29712b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29722b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29735db9a05bSToby Isaac 
29749566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29753ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29769566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29779566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29794c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29809566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29814c274da1SToby Isaac       if (dof) {
29824c274da1SToby Isaac         PetscInt     d;
29834c274da1SToby Isaac         PetscScalar *vals;
29849566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29859566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29864c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29874c274da1SToby Isaac          * we just extracted */
2988ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29894c274da1SToby Isaac       }
29904c274da1SToby Isaac     }
29919566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29929566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29934c274da1SToby Isaac   }
29943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29954c274da1SToby Isaac }
299601729b5cSPatrick Sanan /*@
299701729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
299801729b5cSPatrick Sanan 
299920f4b53cSBarry Smith   Neighbor-wise Collective
300001729b5cSPatrick Sanan 
300101729b5cSPatrick Sanan   Input Parameters:
3002bb7acecfSBarry Smith + dm   - the `DM` object
300301729b5cSPatrick Sanan . l    - the local vector
3004bb7acecfSBarry 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.
300501729b5cSPatrick Sanan - g    - the global vector
300601729b5cSPatrick Sanan 
300720f4b53cSBarry Smith   Level: beginner
300820f4b53cSBarry Smith 
300901729b5cSPatrick Sanan   Notes:
301001729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3011bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
301201729b5cSPatrick Sanan 
3013bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3014bb7acecfSBarry Smith 
3015bb7acecfSBarry 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.
3016bb7acecfSBarry Smith 
3017bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
301801729b5cSPatrick Sanan 
30191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
302001729b5cSPatrick Sanan @*/
3021d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3022d71ae5a4SJacob Faibussowitsch {
302301729b5cSPatrick Sanan   PetscFunctionBegin;
30249566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30259566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
302701729b5cSPatrick Sanan }
30284c274da1SToby Isaac 
302947c6ae99SBarry Smith /*@
303001729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30319a42bb27SBarry Smith 
303220f4b53cSBarry Smith   Neighbor-wise Collective
30339a42bb27SBarry Smith 
30349a42bb27SBarry Smith   Input Parameters:
3035bb7acecfSBarry Smith + dm   - the `DM` object
3036f6813fd5SJed Brown . l    - the local vector
3037aa624791SPierre 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.
30381eb28f2eSBarry Smith - g    - the global vector
30399a42bb27SBarry Smith 
304020f4b53cSBarry Smith   Level: intermediate
304120f4b53cSBarry Smith 
304295452b02SPatrick Sanan   Notes:
3043bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3044bb7acecfSBarry Smith 
3045bb7acecfSBarry 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.
3046bb7acecfSBarry Smith 
3047bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3048bb7acecfSBarry Smith 
3049bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3050bb7acecfSBarry Smith 
3051bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30529a42bb27SBarry Smith 
30531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30549a42bb27SBarry Smith @*/
3055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3056d71ae5a4SJacob Faibussowitsch {
30577128ae9fSMatthew G Knepley   PetscSF                 sf;
305884330215SMatthew G. Knepley   PetscSection            s, gs;
3059d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3060ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3061ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3062ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3063fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3064d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30659a42bb27SBarry Smith 
30669a42bb27SBarry Smith   PetscFunctionBegin;
3067171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3068d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30691baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3070d4d07f1eSToby Isaac   }
30719566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30729566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30739566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30747128ae9fSMatthew G Knepley   switch (mode) {
30757128ae9fSMatthew G Knepley   case INSERT_VALUES:
30767128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3077d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3078d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3079d71ae5a4SJacob Faibussowitsch     break;
30807128ae9fSMatthew G Knepley   case ADD_VALUES:
30817128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3082d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3083d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3084d71ae5a4SJacob Faibussowitsch     break;
3085d71ae5a4SJacob Faibussowitsch   default:
3086d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30877128ae9fSMatthew G Knepley   }
3088ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30899566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3090ca3d3a14SMatthew G. Knepley     if (transform) {
30919566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30929566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30939566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30949566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3095fa88e482SJed Brown     } else if (isInsert) {
30969566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3097fa88e482SJed Brown     } else {
30989566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3099fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3100ca3d3a14SMatthew G. Knepley     }
3101fa88e482SJed Brown     if (s && isInsert) {
31029566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3103fa88e482SJed Brown     } else {
31049566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3105fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3106fa88e482SJed Brown     }
3107ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
31089566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
310984330215SMatthew G. Knepley     } else if (s && isInsert) {
311084330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
311184330215SMatthew G. Knepley 
31129566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31149566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
311584330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3116b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
311784330215SMatthew G. Knepley 
31189566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31199566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31209566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31219566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31229566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31239566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3124b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
312503442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31267a8be351SBarry 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);
3127b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3128b3b16f48SMatthew G. Knepley         if (!gcdof) {
312984330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3130b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3131b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
313284330215SMatthew G. Knepley           const PetscInt *cdofs;
313384330215SMatthew G. Knepley           PetscInt        cind = 0;
313484330215SMatthew G. Knepley 
31359566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3136b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31379371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31389371c9d4SSatish Balay               ++cind;
31399371c9d4SSatish Balay               continue;
31409371c9d4SSatish Balay             }
3141b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
314284330215SMatthew G. Knepley           }
31437a8be351SBarry 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);
314484330215SMatthew G. Knepley       }
3145ca3d3a14SMatthew G. Knepley     }
3146fa88e482SJed Brown     if (g_inplace) {
31479566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3148fa88e482SJed Brown     } else {
31499566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3150fa88e482SJed Brown     }
3151ca3d3a14SMatthew G. Knepley     if (transform) {
31529566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31539566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3154fa88e482SJed Brown     } else if (l_inplace) {
31559566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3156ca3d3a14SMatthew G. Knepley     } else {
31579566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3158ca3d3a14SMatthew G. Knepley     }
31597128ae9fSMatthew G Knepley   } else {
3160fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31617128ae9fSMatthew G Knepley   }
31623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31639a42bb27SBarry Smith }
31649a42bb27SBarry Smith 
31659a42bb27SBarry Smith /*@
31669a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
316747c6ae99SBarry Smith 
316820f4b53cSBarry Smith   Neighbor-wise Collective
316947c6ae99SBarry Smith 
317047c6ae99SBarry Smith   Input Parameters:
3171bb7acecfSBarry Smith + dm   - the `DM` object
3172f6813fd5SJed Brown . l    - the local vector
3173bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3174f6813fd5SJed Brown - g    - the global vector
317547c6ae99SBarry Smith 
317601729b5cSPatrick Sanan   Level: intermediate
317747c6ae99SBarry Smith 
3178bb7acecfSBarry Smith   Note:
3179bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3180bb7acecfSBarry Smith 
318160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
318247c6ae99SBarry Smith @*/
3183d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3184d71ae5a4SJacob Faibussowitsch {
31857128ae9fSMatthew G Knepley   PetscSF                 sf;
318684330215SMatthew G. Knepley   PetscSection            s;
3187d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3188ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
318947c6ae99SBarry Smith 
319047c6ae99SBarry Smith   PetscFunctionBegin;
3191171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31929566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31939566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31947128ae9fSMatthew G Knepley   switch (mode) {
31957128ae9fSMatthew G Knepley   case INSERT_VALUES:
3196d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3197d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3198d71ae5a4SJacob Faibussowitsch     break;
31997128ae9fSMatthew G Knepley   case ADD_VALUES:
3200d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3201d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3202d71ae5a4SJacob Faibussowitsch     break;
3203d71ae5a4SJacob Faibussowitsch   default:
3204d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
32057128ae9fSMatthew G Knepley   }
320684330215SMatthew G. Knepley   if (sf && !isInsert) {
3207ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3208ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3209ca3d3a14SMatthew G. Knepley     Vec                tmpl;
321084330215SMatthew G. Knepley 
32119566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3212ca3d3a14SMatthew G. Knepley     if (transform) {
32139566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32149566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3215ca3d3a14SMatthew G. Knepley     } else {
32169566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3217ca3d3a14SMatthew G. Knepley     }
32189566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32199566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3220ca3d3a14SMatthew G. Knepley     if (transform) {
32219566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32229566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3223ca3d3a14SMatthew G. Knepley     } else {
32249566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3225ca3d3a14SMatthew G. Knepley     }
32269566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
322784330215SMatthew G. Knepley   } else if (s && isInsert) {
32287128ae9fSMatthew G Knepley   } else {
3229fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32307128ae9fSMatthew G Knepley   }
3231d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32329566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3233d4d07f1eSToby Isaac   }
32343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
323547c6ae99SBarry Smith }
323647c6ae99SBarry Smith 
3237f089877aSRichard Tran Mills /*@
3238a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3239a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3240a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3241f089877aSRichard Tran Mills 
324220f4b53cSBarry Smith   Neighbor-wise Collective
3243f089877aSRichard Tran Mills 
3244f089877aSRichard Tran Mills   Input Parameters:
3245bb7acecfSBarry Smith + dm   - the `DM` object
3246bc0a1609SRichard Tran Mills . g    - the original local vector
3247bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3248f089877aSRichard Tran Mills 
3249bc0a1609SRichard Tran Mills   Output Parameter:
3250bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3251f089877aSRichard Tran Mills 
3252f089877aSRichard Tran Mills   Level: intermediate
3253f089877aSRichard Tran Mills 
325473ff1848SBarry Smith   Note:
3255a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3256a4e35b19SJacob Faibussowitsch 
325760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3258f089877aSRichard Tran Mills @*/
3259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3260d71ae5a4SJacob Faibussowitsch {
3261f089877aSRichard Tran Mills   PetscFunctionBegin;
3262f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32639f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32649f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32659f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3267f089877aSRichard Tran Mills }
3268f089877aSRichard Tran Mills 
3269f089877aSRichard Tran Mills /*@
3270bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3271bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3272f089877aSRichard Tran Mills 
327320f4b53cSBarry Smith   Neighbor-wise Collective
3274f089877aSRichard Tran Mills 
3275f089877aSRichard Tran Mills   Input Parameters:
327660225df5SJacob Faibussowitsch + dm   - the `DM` object
3277bc0a1609SRichard Tran Mills . g    - the original local vector
3278bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3279f089877aSRichard Tran Mills 
3280bc0a1609SRichard Tran Mills   Output Parameter:
3281bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3282f089877aSRichard Tran Mills 
3283f089877aSRichard Tran Mills   Level: intermediate
3284f089877aSRichard Tran Mills 
328560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3286f089877aSRichard Tran Mills @*/
3287d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3288d71ae5a4SJacob Faibussowitsch {
3289f089877aSRichard Tran Mills   PetscFunctionBegin;
3290f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32919f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32929f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32939f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3295f089877aSRichard Tran Mills }
3296f089877aSRichard Tran Mills 
329747c6ae99SBarry Smith /*@
3298bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
329947c6ae99SBarry Smith 
330020f4b53cSBarry Smith   Collective
330147c6ae99SBarry Smith 
3302d8d19677SJose E. Roman   Input Parameters:
3303bb7acecfSBarry Smith + dm   - the `DM` object
330420f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
330547c6ae99SBarry Smith 
330647c6ae99SBarry Smith   Output Parameter:
3307bb7acecfSBarry Smith . dmc - the coarsened `DM`
330847c6ae99SBarry Smith 
330947c6ae99SBarry Smith   Level: developer
331047c6ae99SBarry Smith 
331173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
331273ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
331347c6ae99SBarry Smith @*/
3314d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3315d71ae5a4SJacob Faibussowitsch {
3316b17ce1afSJed Brown   DMCoarsenHookLink link;
331747c6ae99SBarry Smith 
331847c6ae99SBarry Smith   PetscFunctionBegin;
3319171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3321dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3322b9d85ea2SLisandro Dalcin   if (*dmc) {
3323a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33249566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
332543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33269566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3327644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33280598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3329656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33309566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3331b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33329566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3333b17ce1afSJed Brown     }
3334b9d85ea2SLisandro Dalcin   }
33359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33367a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3338b17ce1afSJed Brown }
3339b17ce1afSJed Brown 
3340bb9467b5SJed Brown /*@C
3341b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3342b17ce1afSJed Brown 
334320f4b53cSBarry Smith   Logically Collective; No Fortran Support
3344b17ce1afSJed Brown 
33454165533cSJose E. Roman   Input Parameters:
3346bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3347b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3348bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
334920f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3350b17ce1afSJed Brown 
335120f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3352bb7acecfSBarry Smith + fine   - fine level `DM`
3353bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3354b17ce1afSJed Brown - ctx    - optional user-defined function context
3355b17ce1afSJed Brown 
335620f4b53cSBarry Smith   Calling sequence of `restricthook`:
3357bb7acecfSBarry Smith + fine      - fine level `DM`
3358bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3359c833c3b5SJed Brown . rscale    - scaling vector for restriction
3360c833c3b5SJed Brown . inject    - matrix restricting by injection
3361b17ce1afSJed Brown . coarse    - coarse level DM to update
3362b17ce1afSJed Brown - ctx       - optional user-defined function context
3363b17ce1afSJed Brown 
3364b17ce1afSJed Brown   Level: advanced
3365b17ce1afSJed Brown 
3366b17ce1afSJed Brown   Notes:
3367bb7acecfSBarry 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`.
3368b17ce1afSJed Brown 
3369b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3370b17ce1afSJed Brown 
3371b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3372bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3373b17ce1afSJed Brown 
3374bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3375bb7acecfSBarry Smith 
33761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3377b17ce1afSJed Brown @*/
3378a4e35b19SJacob 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)
3379d71ae5a4SJacob Faibussowitsch {
3380b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3381b17ce1afSJed Brown 
3382b17ce1afSJed Brown   PetscFunctionBegin;
3383b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33841e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33853ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33861e3d8eccSJed Brown   }
33879566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3388b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3389b17ce1afSJed Brown   link->restricthook = restricthook;
3390b17ce1afSJed Brown   link->ctx          = ctx;
33910298fd71SBarry Smith   link->next         = NULL;
3392b17ce1afSJed Brown   *p                 = link;
33933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3394b17ce1afSJed Brown }
3395b17ce1afSJed Brown 
3396dc822a44SJed Brown /*@C
3397bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3398dc822a44SJed Brown 
339920f4b53cSBarry Smith   Logically Collective; No Fortran Support
3400dc822a44SJed Brown 
34014165533cSJose E. Roman   Input Parameters:
3402bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3403dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3404bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
340520f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3406dc822a44SJed Brown 
3407dc822a44SJed Brown   Level: advanced
3408dc822a44SJed Brown 
340973ff1848SBarry Smith   Notes:
341073ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
341173ff1848SBarry Smith 
341273ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3413dc822a44SJed Brown 
34141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3415dc822a44SJed Brown @*/
3416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3417d71ae5a4SJacob Faibussowitsch {
3418dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3419dc822a44SJed Brown 
3420dc822a44SJed Brown   PetscFunctionBegin;
3421dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3422dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3423dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3424dc822a44SJed Brown       link = *p;
3425dc822a44SJed Brown       *p   = link->next;
34269566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3427dc822a44SJed Brown       break;
3428dc822a44SJed Brown     }
3429dc822a44SJed Brown   }
34303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3431dc822a44SJed Brown }
3432dc822a44SJed Brown 
3433b17ce1afSJed Brown /*@
3434bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3435b17ce1afSJed Brown 
3436b17ce1afSJed Brown   Collective if any hooks are
3437b17ce1afSJed Brown 
34384165533cSJose E. Roman   Input Parameters:
3439bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3440bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3441e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3442bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
344320f4b53cSBarry Smith - coarse  - coarser `DM` to update
3444b17ce1afSJed Brown 
3445b17ce1afSJed Brown   Level: developer
3446b17ce1afSJed Brown 
344773ff1848SBarry Smith   Developer Note:
3448bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3449bb7acecfSBarry Smith 
34501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3451b17ce1afSJed Brown @*/
3452d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3453d71ae5a4SJacob Faibussowitsch {
3454b17ce1afSJed Brown   DMCoarsenHookLink link;
3455b17ce1afSJed Brown 
3456b17ce1afSJed Brown   PetscFunctionBegin;
3457b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34581baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3459b17ce1afSJed Brown   }
34603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
346147c6ae99SBarry Smith }
346247c6ae99SBarry Smith 
3463bb9467b5SJed Brown /*@C
346473ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34655dbd56e3SPeter Brune 
346620f4b53cSBarry Smith   Logically Collective; No Fortran Support
34675dbd56e3SPeter Brune 
34684165533cSJose E. Roman   Input Parameters:
3469bb7acecfSBarry Smith + global       - global `DM`
3470bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34715dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
347220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34735dbd56e3SPeter Brune 
347420f4b53cSBarry Smith   Calling sequence of `ddhook`:
3475bb7acecfSBarry Smith + global - global `DM`
347673ff1848SBarry Smith . block  - subdomain `DM`
3477ec4806b8SPeter Brune - ctx    - optional user-defined function context
3478ec4806b8SPeter Brune 
347920f4b53cSBarry Smith   Calling sequence of `restricthook`:
3480bb7acecfSBarry Smith + global - global `DM`
348173ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
348273ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
348373ff1848SBarry Smith . block  - subdomain `DM`
34845dbd56e3SPeter Brune - ctx    - optional user-defined function context
34855dbd56e3SPeter Brune 
34865dbd56e3SPeter Brune   Level: advanced
34875dbd56e3SPeter Brune 
34885dbd56e3SPeter Brune   Notes:
348973ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34905dbd56e3SPeter Brune 
34915dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34925dbd56e3SPeter Brune 
34935dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3494bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34955dbd56e3SPeter Brune 
349673ff1848SBarry Smith   Developer Note:
349773ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
349873ff1848SBarry Smith 
349973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
35005dbd56e3SPeter Brune @*/
3501a4e35b19SJacob 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)
3502d71ae5a4SJacob Faibussowitsch {
3503be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
35045dbd56e3SPeter Brune 
35055dbd56e3SPeter Brune   PetscFunctionBegin;
35065dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3507b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35083ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3509b3a6b972SJed Brown   }
35109566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35115dbd56e3SPeter Brune   link->restricthook = restricthook;
3512be081cd6SPeter Brune   link->ddhook       = ddhook;
35135dbd56e3SPeter Brune   link->ctx          = ctx;
35140298fd71SBarry Smith   link->next         = NULL;
35155dbd56e3SPeter Brune   *p                 = link;
35163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35175dbd56e3SPeter Brune }
35185dbd56e3SPeter Brune 
3519b3a6b972SJed Brown /*@C
352073ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3521b3a6b972SJed Brown 
352220f4b53cSBarry Smith   Logically Collective; No Fortran Support
3523b3a6b972SJed Brown 
35244165533cSJose E. Roman   Input Parameters:
3525bb7acecfSBarry Smith + global       - global `DM`
3526bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3527b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
352820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3529b3a6b972SJed Brown 
3530b3a6b972SJed Brown   Level: advanced
3531b3a6b972SJed Brown 
353273ff1848SBarry Smith   Note:
353373ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
353473ff1848SBarry Smith 
353573ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
353673ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3537b3a6b972SJed Brown @*/
3538d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3539d71ae5a4SJacob Faibussowitsch {
3540b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3541b3a6b972SJed Brown 
3542b3a6b972SJed Brown   PetscFunctionBegin;
3543b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3544b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3545b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3546b3a6b972SJed Brown       link = *p;
3547b3a6b972SJed Brown       *p   = link->next;
35489566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3549b3a6b972SJed Brown       break;
3550b3a6b972SJed Brown     }
3551b3a6b972SJed Brown   }
35523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3553b3a6b972SJed Brown }
3554b3a6b972SJed Brown 
35555dbd56e3SPeter Brune /*@
355673ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35575dbd56e3SPeter Brune 
35585dbd56e3SPeter Brune   Collective if any hooks are
35595dbd56e3SPeter Brune 
35604165533cSJose E. Roman   Input Parameters:
3561a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3562a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3563a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3564a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35655dbd56e3SPeter Brune 
35665dbd56e3SPeter Brune   Level: developer
35675dbd56e3SPeter Brune 
356873ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35695dbd56e3SPeter Brune @*/
3570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3571d71ae5a4SJacob Faibussowitsch {
3572be081cd6SPeter Brune   DMSubDomainHookLink link;
35735dbd56e3SPeter Brune 
35745dbd56e3SPeter Brune   PetscFunctionBegin;
3575be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35761baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35775dbd56e3SPeter Brune   }
35783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35795dbd56e3SPeter Brune }
35805dbd56e3SPeter Brune 
35815fe1f584SPeter Brune /*@
3582bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35835fe1f584SPeter Brune 
35845fe1f584SPeter Brune   Not Collective
35855fe1f584SPeter Brune 
35865fe1f584SPeter Brune   Input Parameter:
3587bb7acecfSBarry Smith . dm - the `DM` object
35885fe1f584SPeter Brune 
35895fe1f584SPeter Brune   Output Parameter:
35906a7d9d85SPeter Brune . level - number of coarsenings
35915fe1f584SPeter Brune 
35925fe1f584SPeter Brune   Level: developer
35935fe1f584SPeter Brune 
35941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35955fe1f584SPeter Brune @*/
3596d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3597d71ae5a4SJacob Faibussowitsch {
35985fe1f584SPeter Brune   PetscFunctionBegin;
35995fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36004f572ea9SToby Isaac   PetscAssertPointer(level, 2);
36015fe1f584SPeter Brune   *level = dm->leveldown;
36023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36035fe1f584SPeter Brune }
36045fe1f584SPeter Brune 
36059a64c4a8SMatthew G. Knepley /*@
3606bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36079a64c4a8SMatthew G. Knepley 
360820f4b53cSBarry Smith   Collective
36099a64c4a8SMatthew G. Knepley 
36109a64c4a8SMatthew G. Knepley   Input Parameters:
3611bb7acecfSBarry Smith + dm    - the `DM` object
36129a64c4a8SMatthew G. Knepley - level - number of coarsenings
36139a64c4a8SMatthew G. Knepley 
36149a64c4a8SMatthew G. Knepley   Level: developer
36159a64c4a8SMatthew G. Knepley 
3616bb7acecfSBarry Smith   Note:
3617bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3618bb7acecfSBarry Smith 
361942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36209a64c4a8SMatthew G. Knepley @*/
3621d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3622d71ae5a4SJacob Faibussowitsch {
36239a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36249a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36259a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36279a64c4a8SMatthew G. Knepley }
36289a64c4a8SMatthew G. Knepley 
3629cc4c1da9SBarry Smith /*@
3630bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
363147c6ae99SBarry Smith 
363220f4b53cSBarry Smith   Collective
363347c6ae99SBarry Smith 
3634d8d19677SJose E. Roman   Input Parameters:
3635bb7acecfSBarry Smith + dm      - the `DM` object
363647c6ae99SBarry Smith - nlevels - the number of levels of refinement
363747c6ae99SBarry Smith 
363847c6ae99SBarry Smith   Output Parameter:
3639bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
364047c6ae99SBarry Smith 
364147c6ae99SBarry Smith   Level: developer
364247c6ae99SBarry Smith 
36431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
364447c6ae99SBarry Smith @*/
3645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3646d71ae5a4SJacob Faibussowitsch {
364747c6ae99SBarry Smith   PetscFunctionBegin;
3648171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36497a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36503ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36514f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3652213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
365347c6ae99SBarry Smith     PetscInt i;
365447c6ae99SBarry Smith 
36559566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
365648a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3657213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365947c6ae99SBarry Smith }
366047c6ae99SBarry Smith 
3661cc4c1da9SBarry Smith /*@
3662bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
366347c6ae99SBarry Smith 
366420f4b53cSBarry Smith   Collective
366547c6ae99SBarry Smith 
3666d8d19677SJose E. Roman   Input Parameters:
3667bb7acecfSBarry Smith + dm      - the `DM` object
366847c6ae99SBarry Smith - nlevels - the number of levels of coarsening
366947c6ae99SBarry Smith 
367047c6ae99SBarry Smith   Output Parameter:
3671bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
367247c6ae99SBarry Smith 
367347c6ae99SBarry Smith   Level: developer
367447c6ae99SBarry Smith 
36751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
367647c6ae99SBarry Smith @*/
3677d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3678d71ae5a4SJacob Faibussowitsch {
367947c6ae99SBarry Smith   PetscFunctionBegin;
3680171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36817a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36823ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36834f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3684213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
368547c6ae99SBarry Smith     PetscInt i;
368647c6ae99SBarry Smith 
36879566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
368848a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3689213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
369147c6ae99SBarry Smith }
369247c6ae99SBarry Smith 
36931a266240SBarry Smith /*@C
3694bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36951a266240SBarry Smith 
3696bb7acecfSBarry Smith   Logically Collective if the function is collective
36971a266240SBarry Smith 
36981a266240SBarry Smith   Input Parameters:
3699bb7acecfSBarry Smith + dm      - the `DM` object
370049abdd8aSBarry Smith - destroy - the destroy function, see `PetscCtxDestroyFn` for the calling sequence
37011a266240SBarry Smith 
37021a266240SBarry Smith   Level: intermediate
37031a266240SBarry Smith 
370449abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`,
370549abdd8aSBarry Smith           `DMGetApplicationContext()`, `PetscCtxDestroyFn`
3706f07f9ceaSJed Brown @*/
370749abdd8aSBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscCtxDestroyFn *destroy)
3708d71ae5a4SJacob Faibussowitsch {
37091a266240SBarry Smith   PetscFunctionBegin;
3710171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37111a266240SBarry Smith   dm->ctxdestroy = destroy;
37123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37131a266240SBarry Smith }
37141a266240SBarry Smith 
3715b07ff414SBarry Smith /*@
3716bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
371747c6ae99SBarry Smith 
371847c6ae99SBarry Smith   Not Collective
371947c6ae99SBarry Smith 
372047c6ae99SBarry Smith   Input Parameters:
3721bb7acecfSBarry Smith + dm  - the `DM` object
372247c6ae99SBarry Smith - ctx - the user context
372347c6ae99SBarry Smith 
372447c6ae99SBarry Smith   Level: intermediate
372547c6ae99SBarry Smith 
3726e33b3f0fSStefano Zampini   Notes:
372735cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3728e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3729e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3730bb7acecfSBarry Smith 
373160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
373247c6ae99SBarry Smith @*/
3733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3734d71ae5a4SJacob Faibussowitsch {
373547c6ae99SBarry Smith   PetscFunctionBegin;
3736171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373747c6ae99SBarry Smith   dm->ctx = ctx;
37383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
373947c6ae99SBarry Smith }
374047c6ae99SBarry Smith 
374147c6ae99SBarry Smith /*@
3742bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
374347c6ae99SBarry Smith 
374447c6ae99SBarry Smith   Not Collective
374547c6ae99SBarry Smith 
374647c6ae99SBarry Smith   Input Parameter:
3747bb7acecfSBarry Smith . dm - the `DM` object
374847c6ae99SBarry Smith 
374947c6ae99SBarry Smith   Output Parameter:
375047c6ae99SBarry Smith . ctx - the user context
375147c6ae99SBarry Smith 
375247c6ae99SBarry Smith   Level: intermediate
375347c6ae99SBarry Smith 
3754bb7acecfSBarry Smith   Note:
375535cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3756bb7acecfSBarry Smith 
375742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
375847c6ae99SBarry Smith @*/
3759d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3760d71ae5a4SJacob Faibussowitsch {
376147c6ae99SBarry Smith   PetscFunctionBegin;
3762171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37631b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
376547c6ae99SBarry Smith }
376647c6ae99SBarry Smith 
376708da532bSDmitry Karpeev /*@C
3768bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
376908da532bSDmitry Karpeev 
377020f4b53cSBarry Smith   Logically Collective
377108da532bSDmitry Karpeev 
3772d8d19677SJose E. Roman   Input Parameters:
377308da532bSDmitry Karpeev + dm - the DM object
377420f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
377508da532bSDmitry Karpeev 
377608da532bSDmitry Karpeev   Level: intermediate
377708da532bSDmitry Karpeev 
37781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3779db781477SPatrick Sanan          `DMSetJacobian()`
378008da532bSDmitry Karpeev @*/
3781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3782d71ae5a4SJacob Faibussowitsch {
378308da532bSDmitry Karpeev   PetscFunctionBegin;
37845a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
378708da532bSDmitry Karpeev }
378808da532bSDmitry Karpeev 
378908da532bSDmitry Karpeev /*@
3790bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
379108da532bSDmitry Karpeev 
379208da532bSDmitry Karpeev   Not Collective
379308da532bSDmitry Karpeev 
379408da532bSDmitry Karpeev   Input Parameter:
3795bb7acecfSBarry Smith . dm - the `DM` object to destroy
379608da532bSDmitry Karpeev 
379708da532bSDmitry Karpeev   Output Parameter:
3798bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
379908da532bSDmitry Karpeev 
380008da532bSDmitry Karpeev   Level: developer
380108da532bSDmitry Karpeev 
38021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
380308da532bSDmitry Karpeev @*/
3804d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3805d71ae5a4SJacob Faibussowitsch {
380608da532bSDmitry Karpeev   PetscFunctionBegin;
38075a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38084f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
380908da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
381108da532bSDmitry Karpeev }
381208da532bSDmitry Karpeev 
3813cc4c1da9SBarry Smith /*@
3814bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
381508da532bSDmitry Karpeev 
381620f4b53cSBarry Smith   Logically Collective
381708da532bSDmitry Karpeev 
3818f899ff85SJose E. Roman   Input Parameter:
3819bb7acecfSBarry Smith . dm - the `DM` object
382008da532bSDmitry Karpeev 
382160225df5SJacob Faibussowitsch   Output Parameters:
382208da532bSDmitry Karpeev + xl - lower bound
382308da532bSDmitry Karpeev - xu - upper bound
382408da532bSDmitry Karpeev 
3825907376e6SBarry Smith   Level: advanced
3826907376e6SBarry Smith 
382720f4b53cSBarry Smith   Note:
382895452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
382908da532bSDmitry Karpeev 
38301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
383108da532bSDmitry Karpeev @*/
3832d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3833d71ae5a4SJacob Faibussowitsch {
383408da532bSDmitry Karpeev   PetscFunctionBegin;
38355a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
383608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3838dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
384008da532bSDmitry Karpeev }
384108da532bSDmitry Karpeev 
3842b0ae01b7SPeter Brune /*@
3843bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3844b0ae01b7SPeter Brune 
3845b0ae01b7SPeter Brune   Not Collective
3846b0ae01b7SPeter Brune 
3847b0ae01b7SPeter Brune   Input Parameter:
3848b0ae01b7SPeter Brune . dm - the DM object
3849b0ae01b7SPeter Brune 
3850b0ae01b7SPeter Brune   Output Parameter:
3851bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3852b0ae01b7SPeter Brune 
3853b0ae01b7SPeter Brune   Level: developer
3854b0ae01b7SPeter Brune 
38551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3856b0ae01b7SPeter Brune @*/
3857d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3858d71ae5a4SJacob Faibussowitsch {
3859b0ae01b7SPeter Brune   PetscFunctionBegin;
38605a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38614f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3862b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3864b0ae01b7SPeter Brune }
3865b0ae01b7SPeter Brune 
38663ad4599aSBarry Smith /*@
3867bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38683ad4599aSBarry Smith 
38693ad4599aSBarry Smith   Not Collective
38703ad4599aSBarry Smith 
38713ad4599aSBarry Smith   Input Parameter:
3872bb7acecfSBarry Smith . dm - the `DM` object
38733ad4599aSBarry Smith 
38743ad4599aSBarry Smith   Output Parameter:
3875bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38763ad4599aSBarry Smith 
38773ad4599aSBarry Smith   Level: developer
38783ad4599aSBarry Smith 
38791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38803ad4599aSBarry Smith @*/
3881d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3882d71ae5a4SJacob Faibussowitsch {
38833ad4599aSBarry Smith   PetscFunctionBegin;
38845a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38854f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38863ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38883ad4599aSBarry Smith }
38893ad4599aSBarry Smith 
3890a7058e45SLawrence Mitchell /*@
3891bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3892a7058e45SLawrence Mitchell 
3893a7058e45SLawrence Mitchell   Not Collective
3894a7058e45SLawrence Mitchell 
3895a7058e45SLawrence Mitchell   Input Parameter:
3896bb7acecfSBarry Smith . dm - the `DM` object
3897a7058e45SLawrence Mitchell 
3898a7058e45SLawrence Mitchell   Output Parameter:
3899bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3900a7058e45SLawrence Mitchell 
3901a7058e45SLawrence Mitchell   Level: developer
3902a7058e45SLawrence Mitchell 
39031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3904a7058e45SLawrence Mitchell @*/
3905d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3906d71ae5a4SJacob Faibussowitsch {
3907a7058e45SLawrence Mitchell   PetscFunctionBegin;
39085a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39094f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3910dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3911ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3913a7058e45SLawrence Mitchell }
3914a7058e45SLawrence Mitchell 
39150298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3916264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3917264ace61SBarry Smith 
3918cc4c1da9SBarry Smith /*@
3919bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3920264ace61SBarry Smith 
392120f4b53cSBarry Smith   Collective
3922264ace61SBarry Smith 
3923264ace61SBarry Smith   Input Parameters:
3924bb7acecfSBarry Smith + dm     - The `DM` object
3925bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3926264ace61SBarry Smith 
3927264ace61SBarry Smith   Options Database Key:
3928bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3929264ace61SBarry Smith 
3930264ace61SBarry Smith   Level: intermediate
3931264ace61SBarry Smith 
3932bb7acecfSBarry Smith   Note:
39330462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3934bb7acecfSBarry Smith 
39351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3936264ace61SBarry Smith @*/
3937d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3938d71ae5a4SJacob Faibussowitsch {
3939264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3940264ace61SBarry Smith   PetscBool match;
3941264ace61SBarry Smith 
3942264ace61SBarry Smith   PetscFunctionBegin;
3943264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39449566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39453ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3946264ace61SBarry Smith 
39479566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39489566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39497a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3950264ace61SBarry Smith 
3951dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39529566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39539566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39549566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3956264ace61SBarry Smith }
3957264ace61SBarry Smith 
3958cc4c1da9SBarry Smith /*@
3959bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3960264ace61SBarry Smith 
3961264ace61SBarry Smith   Not Collective
3962264ace61SBarry Smith 
3963264ace61SBarry Smith   Input Parameter:
3964bb7acecfSBarry Smith . dm - The `DM`
3965264ace61SBarry Smith 
3966264ace61SBarry Smith   Output Parameter:
3967bb7acecfSBarry Smith . type - The `DMType` name
3968264ace61SBarry Smith 
3969264ace61SBarry Smith   Level: intermediate
3970264ace61SBarry Smith 
39711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3972264ace61SBarry Smith @*/
3973d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3974d71ae5a4SJacob Faibussowitsch {
3975264ace61SBarry Smith   PetscFunctionBegin;
3976264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39774f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39789566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3979264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3981264ace61SBarry Smith }
3982264ace61SBarry Smith 
39835d83a8b1SBarry Smith /*@
3984bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
398567a56275SMatthew G Knepley 
398620f4b53cSBarry Smith   Collective
398767a56275SMatthew G Knepley 
398867a56275SMatthew G Knepley   Input Parameters:
3989bb7acecfSBarry Smith + dm      - the `DM`
3990bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
399167a56275SMatthew G Knepley 
399267a56275SMatthew G Knepley   Output Parameter:
3993bb7acecfSBarry Smith . M - pointer to new `DM`
399467a56275SMatthew G Knepley 
399520f4b53cSBarry Smith   Level: intermediate
399620f4b53cSBarry Smith 
399773ff1848SBarry Smith   Note:
3998bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3999bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
4000bb7acecfSBarry Smith   of the input `DM`.
400167a56275SMatthew G Knepley 
40021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
400367a56275SMatthew G Knepley @*/
4004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
4005d71ae5a4SJacob Faibussowitsch {
400667a56275SMatthew G Knepley   DM        B;
400767a56275SMatthew G Knepley   char      convname[256];
4008c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
400967a56275SMatthew G Knepley 
401067a56275SMatthew G Knepley   PetscFunctionBegin;
401167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
401267a56275SMatthew G Knepley   PetscValidType(dm, 1);
40134f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40149566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40159566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4016c067b6caSMatthew G. Knepley   if (sametype) {
4017c067b6caSMatthew G. Knepley     *M = dm;
40189566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40193ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4020c067b6caSMatthew G. Knepley   } else {
40210298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
402267a56275SMatthew G Knepley 
402367a56275SMatthew G Knepley     /*
402467a56275SMatthew G Knepley        Order of precedence:
402567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
402667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
402767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
402867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
402967a56275SMatthew G Knepley        5) Use a really basic converter.
403067a56275SMatthew G Knepley     */
403167a56275SMatthew G Knepley 
403267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40339566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40349566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40359566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40369566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40379566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40389566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
403967a56275SMatthew G Knepley     if (conv) goto foundconv;
404067a56275SMatthew G Knepley 
404167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40429566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40439566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40449566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40459566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40469566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40479566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40489566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40499566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
405067a56275SMatthew G Knepley     if (conv) {
40519566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
405267a56275SMatthew G Knepley       goto foundconv;
405367a56275SMatthew G Knepley     }
405467a56275SMatthew G Knepley 
405567a56275SMatthew G Knepley #if 0
405667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
405767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40589566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
405967a56275SMatthew G Knepley     if (conv) goto foundconv;
406067a56275SMatthew G Knepley 
406167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
406267a56275SMatthew G Knepley     if (dm->ops->convert) {
406367a56275SMatthew G Knepley       conv = dm->ops->convert;
406467a56275SMatthew G Knepley     }
406567a56275SMatthew G Knepley     if (conv) goto foundconv;
406667a56275SMatthew G Knepley #endif
406767a56275SMatthew G Knepley 
406867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
406998921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
407067a56275SMatthew G Knepley 
407167a56275SMatthew G Knepley   foundconv:
40729566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40739566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
407412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
407590b157c4SStefano Zampini     {
40764fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40776858538eSMatthew G. Knepley 
40784fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40794fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4080c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40819566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40829566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40839566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40849566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
408512fa691eSMatthew G. Knepley     }
40869566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
408767a56275SMatthew G Knepley   }
40889566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
409067a56275SMatthew G Knepley }
4091264ace61SBarry Smith 
4092264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4093264ace61SBarry Smith 
4094264ace61SBarry Smith /*@C
4095bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40961c84c290SBarry Smith 
4097cc4c1da9SBarry Smith   Not Collective, No Fortran Support
40981c84c290SBarry Smith 
40991c84c290SBarry Smith   Input Parameters:
410020f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
410120f4b53cSBarry Smith - function - The creation routine itself
410220f4b53cSBarry Smith 
410320f4b53cSBarry Smith   Level: advanced
41041c84c290SBarry Smith 
410573ff1848SBarry Smith   Note:
410620f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
41071c84c290SBarry Smith 
410860225df5SJacob Faibussowitsch   Example Usage:
41091c84c290SBarry Smith .vb
4110bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41111c84c290SBarry Smith .ve
41121c84c290SBarry Smith 
411320f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41141c84c290SBarry Smith .vb
41151c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41161c84c290SBarry Smith     DMSetType(DM,"my_da");
41171c84c290SBarry Smith .ve
41181c84c290SBarry Smith   or at runtime via the option
41191c84c290SBarry Smith .vb
41201c84c290SBarry Smith     -da_type my_da
41211c84c290SBarry Smith .ve
4122264ace61SBarry Smith 
41231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4124264ace61SBarry Smith @*/
4125d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4126d71ae5a4SJacob Faibussowitsch {
4127264ace61SBarry Smith   PetscFunctionBegin;
41289566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41299566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4131264ace61SBarry Smith }
4132264ace61SBarry Smith 
4133ffeef943SBarry Smith /*@
4134bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4135b859378eSBarry Smith 
413620f4b53cSBarry Smith   Collective
4137b859378eSBarry Smith 
4138b859378eSBarry Smith   Input Parameters:
4139bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4140bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4141bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4142bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4143b859378eSBarry Smith 
4144b859378eSBarry Smith   Level: intermediate
4145b859378eSBarry Smith 
4146b859378eSBarry Smith   Notes:
414755849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4148b859378eSBarry Smith 
4149bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4150bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4151bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4152cd7e8a5eSksagiyam 
41531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4154b859378eSBarry Smith @*/
4155d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4156d71ae5a4SJacob Faibussowitsch {
41579331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4158b859378eSBarry Smith 
4159b859378eSBarry Smith   PetscFunctionBegin;
4160b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4161b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41629566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41639566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41649566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41659566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41669331c7a4SMatthew G. Knepley   if (isbinary) {
41679331c7a4SMatthew G. Knepley     PetscInt classid;
41689331c7a4SMatthew G. Knepley     char     type[256];
4169b859378eSBarry Smith 
41709566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
4171835f2295SStefano Zampini     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %" PetscInt_FMT, classid);
41729566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41739566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4174dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41759331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4176dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41779331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41789566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4180b859378eSBarry Smith }
4181b859378eSBarry Smith 
41824ee01570SBarry Smith /* FEM Support */
41837da65231SMatthew G Knepley 
41842ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41852ecf6ec3SMatthew G. Knepley {
41862ecf6ec3SMatthew G. Knepley   PetscInt f;
41872ecf6ec3SMatthew G. Knepley 
41882ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41892ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41902ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41912ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41922ecf6ec3SMatthew G. Knepley }
41932ecf6ec3SMatthew G. Knepley 
4194d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4195d71ae5a4SJacob Faibussowitsch {
41961d47ebbbSSatish Balay   PetscInt f;
41971b30c384SMatthew G Knepley 
41987da65231SMatthew G Knepley   PetscFunctionBegin;
419963a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
420048a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
42013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42027da65231SMatthew G Knepley }
42037da65231SMatthew G Knepley 
42045962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
42055962854dSMatthew G. Knepley {
42065962854dSMatthew G. Knepley   PetscInt f;
42075962854dSMatthew G. Knepley 
42085962854dSMatthew G. Knepley   PetscFunctionBegin;
42095962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42105962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42115962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42125962854dSMatthew G. Knepley }
42135962854dSMatthew G. Knepley 
4214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4215d71ae5a4SJacob Faibussowitsch {
42161b30c384SMatthew G Knepley   PetscInt f, g;
42177da65231SMatthew G Knepley 
42187da65231SMatthew G Knepley   PetscFunctionBegin;
421963a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42201d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42219566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
422248a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42239566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42247da65231SMatthew G Knepley   }
42253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42267da65231SMatthew G Knepley }
4227e7c4fc90SDmitry Karpeev 
4228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4229d71ae5a4SJacob Faibussowitsch {
42300c5b8624SToby Isaac   PetscInt           localSize, bs;
42310c5b8624SToby Isaac   PetscMPIInt        size;
42320c5b8624SToby Isaac   Vec                x, xglob;
42330c5b8624SToby Isaac   const PetscScalar *xarray;
4234e759306cSMatthew G. Knepley 
4235e759306cSMatthew G. Knepley   PetscFunctionBegin;
42369566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42379566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42389566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
423993ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42409566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42410c5b8624SToby Isaac   if (size > 1) {
42429566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42439566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42449566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42459566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42460c5b8624SToby Isaac   } else {
42470c5b8624SToby Isaac     xglob = x;
42480c5b8624SToby Isaac   }
42499566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42500c5b8624SToby Isaac   if (size > 1) {
42519566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42529566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42530c5b8624SToby Isaac   }
42549566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4256e759306cSMatthew G. Knepley }
4257e759306cSMatthew G. Knepley 
425888ed4aceSMatthew G Knepley /*@
4259bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4260061576a5SJed Brown 
4261061576a5SJed Brown   Input Parameter:
4262bb7acecfSBarry Smith . dm - The `DM`
4263061576a5SJed Brown 
4264061576a5SJed Brown   Output Parameter:
4265bb7acecfSBarry Smith . section - The `PetscSection`
4266061576a5SJed Brown 
426720f4b53cSBarry Smith   Options Database Key:
4268bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4269061576a5SJed Brown 
4270061576a5SJed Brown   Level: advanced
4271061576a5SJed Brown 
4272061576a5SJed Brown   Notes:
4273bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4274061576a5SJed Brown 
4275bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4276061576a5SJed Brown 
42771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4278061576a5SJed Brown @*/
4279d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4280d71ae5a4SJacob Faibussowitsch {
4281061576a5SJed Brown   PetscFunctionBegin;
42829566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4284061576a5SJed Brown }
4285061576a5SJed Brown 
4286061576a5SJed Brown /*@
4287bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
428888ed4aceSMatthew G Knepley 
428988ed4aceSMatthew G Knepley   Input Parameter:
4290bb7acecfSBarry Smith . dm - The `DM`
429188ed4aceSMatthew G Knepley 
429288ed4aceSMatthew G Knepley   Output Parameter:
4293bb7acecfSBarry Smith . section - The `PetscSection`
429488ed4aceSMatthew G Knepley 
429520f4b53cSBarry Smith   Options Database Key:
4296bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4297e5893cccSMatthew G. Knepley 
429888ed4aceSMatthew G Knepley   Level: intermediate
429988ed4aceSMatthew G Knepley 
4300bb7acecfSBarry Smith   Note:
4301bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
430288ed4aceSMatthew G Knepley 
43031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
430488ed4aceSMatthew G Knepley @*/
4305d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4306d71ae5a4SJacob Faibussowitsch {
430788ed4aceSMatthew G Knepley   PetscFunctionBegin;
430888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43094f572ea9SToby Isaac   PetscAssertPointer(section, 2);
43101bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4311e5e52638SMatthew G. Knepley     PetscInt d;
4312e5e52638SMatthew G. Knepley 
431345480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
431445480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
431545480ffeSMatthew G. Knepley       PetscViewer       viewer;
431645480ffeSMatthew G. Knepley       PetscViewerFormat format;
431745480ffeSMatthew G. Knepley       PetscBool         flg;
431845480ffeSMatthew G. Knepley 
4319648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43209566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
432145480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43229566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43239566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
432445480ffeSMatthew G. Knepley       }
432545480ffeSMatthew G. Knepley       if (flg) {
43269566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43279566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4328648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
432945480ffeSMatthew G. Knepley       }
433045480ffeSMatthew G. Knepley     }
4331dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43329566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43332f0f8703SMatthew G. Knepley   }
43341bb6d2a8SBarry Smith   *section = dm->localSection;
43353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
433688ed4aceSMatthew G Knepley }
433788ed4aceSMatthew G Knepley 
433888ed4aceSMatthew G Knepley /*@
4339bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4340061576a5SJed Brown 
4341061576a5SJed Brown   Input Parameters:
4342bb7acecfSBarry Smith + dm      - The `DM`
4343bb7acecfSBarry Smith - section - The `PetscSection`
4344061576a5SJed Brown 
4345061576a5SJed Brown   Level: advanced
4346061576a5SJed Brown 
4347061576a5SJed Brown   Notes:
4348bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4349061576a5SJed Brown 
4350bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4351061576a5SJed Brown 
43521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4353061576a5SJed Brown @*/
4354d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4355d71ae5a4SJacob Faibussowitsch {
4356061576a5SJed Brown   PetscFunctionBegin;
43579566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4359061576a5SJed Brown }
4360061576a5SJed Brown 
4361061576a5SJed Brown /*@
4362bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
436388ed4aceSMatthew G Knepley 
436488ed4aceSMatthew G Knepley   Input Parameters:
4365bb7acecfSBarry Smith + dm      - The `DM`
4366bb7acecfSBarry Smith - section - The `PetscSection`
436788ed4aceSMatthew G Knepley 
436888ed4aceSMatthew G Knepley   Level: intermediate
436988ed4aceSMatthew G Knepley 
4370bb7acecfSBarry Smith   Note:
4371bb7acecfSBarry Smith   Any existing Section will be destroyed
437288ed4aceSMatthew G Knepley 
43731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
437488ed4aceSMatthew G Knepley @*/
4375d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4376d71ae5a4SJacob Faibussowitsch {
4377c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4378af122d2aSMatthew G Knepley   PetscInt f;
437988ed4aceSMatthew G Knepley 
438088ed4aceSMatthew G Knepley   PetscFunctionBegin;
438188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4382b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43839566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43849566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43851bb6d2a8SBarry Smith   dm->localSection = section;
43869566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4387af122d2aSMatthew G Knepley   if (numFields) {
43889566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4389af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43900f21e855SMatthew G. Knepley       PetscObject disc;
4391af122d2aSMatthew G Knepley       const char *name;
4392af122d2aSMatthew G Knepley 
43939566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43949566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43959566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4396af122d2aSMatthew G Knepley     }
4397af122d2aSMatthew G Knepley   }
439850350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
439950350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
44009566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
440150350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
440252947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
440350350c8eSStefano Zampini 
440450350c8eSStefano Zampini   /* Clear scratch vectors */
440550350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
440650350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
440750350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
440850350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
44093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
441088ed4aceSMatthew G Knepley }
441188ed4aceSMatthew G Knepley 
4412adc21957SMatthew G. Knepley /*@C
4413d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4414adc21957SMatthew G. Knepley 
4415adc21957SMatthew G. Knepley   Input Parameter:
4416adc21957SMatthew G. Knepley . dm - The `DM`
4417adc21957SMatthew G. Knepley 
44189cde84edSJose E. Roman   Output Parameters:
4419adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4420b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4421adc21957SMatthew G. Knepley 
4422adc21957SMatthew G. Knepley   Level: developer
4423adc21957SMatthew G. Knepley 
4424adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4425adc21957SMatthew G. Knepley @*/
4426adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4427adc21957SMatthew G. Knepley {
4428adc21957SMatthew G. Knepley   PetscFunctionBegin;
4429adc21957SMatthew G. Knepley   *perm        = NULL;
4430adc21957SMatthew G. Knepley   *blockStarts = NULL;
4431adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4432adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4433adc21957SMatthew G. Knepley }
4434adc21957SMatthew G. Knepley 
44359435951eSToby Isaac /*@
4436bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44379435951eSToby Isaac 
443820f4b53cSBarry Smith   not Collective
4439e228b242SToby Isaac 
44409435951eSToby Isaac   Input Parameter:
4441bb7acecfSBarry Smith . dm - The `DM`
44429435951eSToby Isaac 
4443d8d19677SJose E. Roman   Output Parameters:
444420f4b53cSBarry 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.
444520f4b53cSBarry 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.
444679769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44479435951eSToby Isaac 
44489435951eSToby Isaac   Level: advanced
44499435951eSToby Isaac 
4450bb7acecfSBarry Smith   Note:
4451bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44529435951eSToby Isaac 
44531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44549435951eSToby Isaac @*/
4455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4456d71ae5a4SJacob Faibussowitsch {
44579435951eSToby Isaac   PetscFunctionBegin;
44589435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4459dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44603b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44613b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
446279769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44649435951eSToby Isaac }
44659435951eSToby Isaac 
44669435951eSToby Isaac /*@
4467bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44689435951eSToby Isaac 
446920f4b53cSBarry Smith   Collective
4470e228b242SToby Isaac 
44719435951eSToby Isaac   Input Parameters:
4472bb7acecfSBarry Smith + dm      - The `DM`
4473bb7acecfSBarry 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).
447420f4b53cSBarry 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).
447520f4b53cSBarry 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).
44769435951eSToby Isaac 
44779435951eSToby Isaac   Level: advanced
44789435951eSToby Isaac 
447920f4b53cSBarry Smith   Notes:
448020f4b53cSBarry 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()`.
448120f4b53cSBarry Smith 
448220f4b53cSBarry 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.
448320f4b53cSBarry Smith 
4484bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44859435951eSToby Isaac 
44861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44879435951eSToby Isaac @*/
4488d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4489d71ae5a4SJacob Faibussowitsch {
4490e228b242SToby Isaac   PetscMPIInt result;
44919435951eSToby Isaac 
44929435951eSToby Isaac   PetscFunctionBegin;
44939435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4494e228b242SToby Isaac   if (section) {
4495e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44969566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44977a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4498e228b242SToby Isaac   }
4499e228b242SToby Isaac   if (mat) {
4500e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
45019566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
45027a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4503e228b242SToby Isaac   }
450479769bd5SJed Brown   if (bias) {
450579769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
45069566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
450779769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
450879769bd5SJed Brown   }
45099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45109566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
45113b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45129566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
45139566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45143b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45169566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
451779769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45199435951eSToby Isaac }
45209435951eSToby Isaac 
4521497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4522507e4973SMatthew G. Knepley /*
4523bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4524507e4973SMatthew G. Knepley 
4525507e4973SMatthew G. Knepley   Input Parameters:
4526bb7acecfSBarry Smith + dm - The `DM`
4527bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4528bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4529507e4973SMatthew G. Knepley 
4530507e4973SMatthew G. Knepley   Level: intermediate
4531507e4973SMatthew G. Knepley 
45321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4533507e4973SMatthew G. Knepley */
4534d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4535d71ae5a4SJacob Faibussowitsch {
4536507e4973SMatthew G. Knepley   MPI_Comm        comm;
4537507e4973SMatthew G. Knepley   PetscLayout     layout;
4538507e4973SMatthew G. Knepley   const PetscInt *ranges;
4539507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4540507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4541507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4542507e4973SMatthew G. Knepley 
4543507e4973SMatthew G. Knepley   PetscFunctionBegin;
45449566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4545507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45479566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45509566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45549566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4555507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4556f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4557507e4973SMatthew G. Knepley 
45589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4564507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45659371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45669371c9d4SSatish 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));
45679371c9d4SSatish Balay       valid = PETSC_FALSE;
45689371c9d4SSatish Balay     }
45699371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45709371c9d4SSatish 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));
45719371c9d4SSatish Balay       valid = PETSC_FALSE;
45729371c9d4SSatish Balay     }
4573507e4973SMatthew G. Knepley     if (gdof < 0) {
4574507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4575507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4576507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4577507e4973SMatthew G. Knepley 
45789566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4579507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45809371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45819371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45829371c9d4SSatish Balay           valid = PETSC_FALSE;
45839371c9d4SSatish Balay           break;
45849371c9d4SSatish Balay         }
4585507e4973SMatthew G. Knepley       }
4586507e4973SMatthew G. Knepley     }
4587507e4973SMatthew G. Knepley   }
45889566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45899566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
4590462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4591507e4973SMatthew G. Knepley   if (!gvalid) {
45929566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4593507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4594507e4973SMatthew G. Knepley   }
45953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4596507e4973SMatthew G. Knepley }
4597f741bcd2SMatthew G. Knepley #endif
4598507e4973SMatthew G. Knepley 
45995f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
46006725e60dSJed Brown {
46016725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
46024d86920dSPierre Jolivet 
46036725e60dSJed Brown   PetscFunctionBegin;
46046725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46054f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
46065f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
46076725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
46086725e60dSJed Brown   else *sf = dm->sf;
46093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46106725e60dSJed Brown }
46116725e60dSJed Brown 
461288ed4aceSMatthew G Knepley /*@
4613bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
461488ed4aceSMatthew G Knepley 
461520f4b53cSBarry Smith   Collective
46168b1ab98fSJed Brown 
461788ed4aceSMatthew G Knepley   Input Parameter:
4618bb7acecfSBarry Smith . dm - The `DM`
461988ed4aceSMatthew G Knepley 
462088ed4aceSMatthew G Knepley   Output Parameter:
4621bb7acecfSBarry Smith . section - The `PetscSection`
462288ed4aceSMatthew G Knepley 
462388ed4aceSMatthew G Knepley   Level: intermediate
462488ed4aceSMatthew G Knepley 
4625bb7acecfSBarry Smith   Note:
4626bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
462788ed4aceSMatthew G Knepley 
46281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
462988ed4aceSMatthew G Knepley @*/
4630d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4631d71ae5a4SJacob Faibussowitsch {
463288ed4aceSMatthew G Knepley   PetscFunctionBegin;
463388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46344f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46351bb6d2a8SBarry Smith   if (!dm->globalSection) {
4636fd59a867SMatthew G. Knepley     PetscSection s;
46376725e60dSJed Brown     PetscSF      sf;
4638fd59a867SMatthew G. Knepley 
46399566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46407a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46417a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46425f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4643eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46449566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46459566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46469566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
464788ed4aceSMatthew G Knepley   }
46481bb6d2a8SBarry Smith   *section = dm->globalSection;
46493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
465088ed4aceSMatthew G Knepley }
465188ed4aceSMatthew G Knepley 
4652b21d0597SMatthew G Knepley /*@
4653bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4654b21d0597SMatthew G Knepley 
4655b21d0597SMatthew G Knepley   Input Parameters:
4656bb7acecfSBarry Smith + dm      - The `DM`
46572fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4658b21d0597SMatthew G Knepley 
4659b21d0597SMatthew G Knepley   Level: intermediate
4660b21d0597SMatthew G Knepley 
4661bb7acecfSBarry Smith   Note:
4662bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4663b21d0597SMatthew G Knepley 
46641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4665b21d0597SMatthew G Knepley @*/
4666d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4667d71ae5a4SJacob Faibussowitsch {
4668b21d0597SMatthew G Knepley   PetscFunctionBegin;
4669b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46705080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46719566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46729566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46731bb6d2a8SBarry Smith   dm->globalSection = section;
4674497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46759566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4676507e4973SMatthew G. Knepley #endif
467750350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
467850350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
467952947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
468050350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
468150350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4683b21d0597SMatthew G Knepley }
4684b21d0597SMatthew G Knepley 
468588ed4aceSMatthew G Knepley /*@
4686bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4687bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
468888ed4aceSMatthew G Knepley 
468988ed4aceSMatthew G Knepley   Input Parameter:
4690bb7acecfSBarry Smith . dm - The `DM`
469188ed4aceSMatthew G Knepley 
469288ed4aceSMatthew G Knepley   Output Parameter:
4693bb7acecfSBarry Smith . sf - The `PetscSF`
469488ed4aceSMatthew G Knepley 
469588ed4aceSMatthew G Knepley   Level: intermediate
469688ed4aceSMatthew G Knepley 
4697bb7acecfSBarry Smith   Note:
4698bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
469988ed4aceSMatthew G Knepley 
47001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
470188ed4aceSMatthew G Knepley @*/
4702d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4703d71ae5a4SJacob Faibussowitsch {
470488ed4aceSMatthew G Knepley   PetscInt nroots;
470588ed4aceSMatthew G Knepley 
470688ed4aceSMatthew G Knepley   PetscFunctionBegin;
470788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47084f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
470948a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
47109566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
471188ed4aceSMatthew G Knepley   if (nroots < 0) {
471288ed4aceSMatthew G Knepley     PetscSection section, gSection;
471388ed4aceSMatthew G Knepley 
47149566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
471531ea6d37SMatthew G Knepley     if (section) {
47169566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47179566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
471831ea6d37SMatthew G Knepley     } else {
47190298fd71SBarry Smith       *sf = NULL;
47203ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
472131ea6d37SMatthew G Knepley     }
472288ed4aceSMatthew G Knepley   }
47231bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
472588ed4aceSMatthew G Knepley }
472688ed4aceSMatthew G Knepley 
472788ed4aceSMatthew G Knepley /*@
4728bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
472988ed4aceSMatthew G Knepley 
473088ed4aceSMatthew G Knepley   Input Parameters:
4731bb7acecfSBarry Smith + dm - The `DM`
4732bb7acecfSBarry Smith - sf - The `PetscSF`
473388ed4aceSMatthew G Knepley 
473488ed4aceSMatthew G Knepley   Level: intermediate
473588ed4aceSMatthew G Knepley 
4736bb7acecfSBarry Smith   Note:
4737bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
473888ed4aceSMatthew G Knepley 
47391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
474088ed4aceSMatthew G Knepley @*/
4741d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4742d71ae5a4SJacob Faibussowitsch {
474388ed4aceSMatthew G Knepley   PetscFunctionBegin;
474488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4745b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47469566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47479566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47481bb6d2a8SBarry Smith   dm->sectionSF = sf;
47493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
475088ed4aceSMatthew G Knepley }
475188ed4aceSMatthew G Knepley 
4752cc4c1da9SBarry Smith /*@
4753bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
475488ed4aceSMatthew G Knepley   describing the data layout.
475588ed4aceSMatthew G Knepley 
475688ed4aceSMatthew G Knepley   Input Parameters:
4757bb7acecfSBarry Smith + dm            - The `DM`
4758bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4759bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
476088ed4aceSMatthew G Knepley 
47611bb6d2a8SBarry Smith   Level: developer
47621bb6d2a8SBarry Smith 
4763bb7acecfSBarry Smith   Note:
4764bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4765bb7acecfSBarry Smith 
476673ff1848SBarry Smith   Developer Note:
4767bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4768bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4769b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4770b6971eaeSBarry Smith   they create, this returns nothing
47711bb6d2a8SBarry Smith 
47721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
477388ed4aceSMatthew G Knepley @*/
4774d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4775d71ae5a4SJacob Faibussowitsch {
477688ed4aceSMatthew G Knepley   PetscFunctionBegin;
477788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47789566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
478088ed4aceSMatthew G Knepley }
4781af122d2aSMatthew G Knepley 
4782b21d0597SMatthew G Knepley /*@
4783bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4784bb7acecfSBarry Smith 
4785bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4786b21d0597SMatthew G Knepley 
4787b21d0597SMatthew G Knepley   Input Parameter:
4788bb7acecfSBarry Smith . dm - The `DM`
4789b21d0597SMatthew G Knepley 
4790b21d0597SMatthew G Knepley   Output Parameter:
4791bb7acecfSBarry Smith . sf - The `PetscSF`
4792b21d0597SMatthew G Knepley 
4793b21d0597SMatthew G Knepley   Level: intermediate
4794b21d0597SMatthew G Knepley 
4795bb7acecfSBarry Smith   Note:
4796bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4797b21d0597SMatthew G Knepley 
47981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4799b21d0597SMatthew G Knepley @*/
4800d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4801d71ae5a4SJacob Faibussowitsch {
4802b21d0597SMatthew G Knepley   PetscFunctionBegin;
4803b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48044f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4805b21d0597SMatthew G Knepley   *sf = dm->sf;
48063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4807b21d0597SMatthew G Knepley }
4808b21d0597SMatthew G Knepley 
4809057b4bcdSMatthew G Knepley /*@
4810bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4811bb7acecfSBarry Smith 
481220f4b53cSBarry Smith   Collective
4813057b4bcdSMatthew G Knepley 
4814057b4bcdSMatthew G Knepley   Input Parameters:
4815bb7acecfSBarry Smith + dm - The `DM`
4816bb7acecfSBarry Smith - sf - The `PetscSF`
4817057b4bcdSMatthew G Knepley 
4818057b4bcdSMatthew G Knepley   Level: intermediate
4819057b4bcdSMatthew G Knepley 
48201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4821057b4bcdSMatthew G Knepley @*/
4822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4823d71ae5a4SJacob Faibussowitsch {
4824057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4825057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4826b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48279566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48289566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4829057b4bcdSMatthew G Knepley   dm->sf = sf;
48303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4831057b4bcdSMatthew G Knepley }
4832057b4bcdSMatthew G Knepley 
48334f37162bSMatthew G. Knepley /*@
4834bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48354f37162bSMatthew G. Knepley 
48364f37162bSMatthew G. Knepley   Input Parameter:
4837bb7acecfSBarry Smith . dm - The `DM`
48384f37162bSMatthew G. Knepley 
48394f37162bSMatthew G. Knepley   Output Parameter:
4840bb7acecfSBarry Smith . sf - The `PetscSF`
48414f37162bSMatthew G. Knepley 
48424f37162bSMatthew G. Knepley   Level: intermediate
48434f37162bSMatthew G. Knepley 
4844bb7acecfSBarry Smith   Note:
4845bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48464f37162bSMatthew G. Knepley 
48471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48484f37162bSMatthew G. Knepley @*/
4849d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4850d71ae5a4SJacob Faibussowitsch {
48514f37162bSMatthew G. Knepley   PetscFunctionBegin;
48524f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48534f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48544f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48564f37162bSMatthew G. Knepley }
48574f37162bSMatthew G. Knepley 
48584f37162bSMatthew G. Knepley /*@
48594f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48604f37162bSMatthew G. Knepley 
48614f37162bSMatthew G. Knepley   Input Parameters:
48624f37162bSMatthew G. Knepley + dm - The DM
48634f37162bSMatthew G. Knepley - sf - The PetscSF
48644f37162bSMatthew G. Knepley 
48654f37162bSMatthew G. Knepley   Level: intermediate
48664f37162bSMatthew G. Knepley 
48671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48684f37162bSMatthew G. Knepley @*/
4869d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4870d71ae5a4SJacob Faibussowitsch {
48714f37162bSMatthew G. Knepley   PetscFunctionBegin;
48724f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48734f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48749566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48759566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48764f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48784f37162bSMatthew G. Knepley }
48794f37162bSMatthew G. Knepley 
4880d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4881d71ae5a4SJacob Faibussowitsch {
488234aa8a36SMatthew G. Knepley   PetscClassId id;
488334aa8a36SMatthew G. Knepley 
488434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48859566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
488634aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48879566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
488834aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48899566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
489017c1d62eSMatthew G. Knepley   } else {
48919566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
489234aa8a36SMatthew G. Knepley   }
48933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
489434aa8a36SMatthew G. Knepley }
489534aa8a36SMatthew G. Knepley 
4896d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4897d71ae5a4SJacob Faibussowitsch {
489844a7f3ddSMatthew G. Knepley   RegionField *tmpr;
489944a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
490044a7f3ddSMatthew G. Knepley 
490144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
49023ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
49039566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
490444a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
49059371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
49069371c9d4SSatish Balay     tmpr[f].disc        = NULL;
49079371c9d4SSatish Balay     tmpr[f].label       = NULL;
49089371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
49099371c9d4SSatish Balay   }
49109566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
491144a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
491244a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
49133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
491444a7f3ddSMatthew G. Knepley }
491544a7f3ddSMatthew G. Knepley 
491644a7f3ddSMatthew G. Knepley /*@
491720f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
491844a7f3ddSMatthew G. Knepley 
491920f4b53cSBarry Smith   Logically Collective
492044a7f3ddSMatthew G. Knepley 
492144a7f3ddSMatthew G. Knepley   Input Parameter:
492220f4b53cSBarry Smith . dm - The `DM`
492344a7f3ddSMatthew G. Knepley 
492444a7f3ddSMatthew G. Knepley   Level: intermediate
492544a7f3ddSMatthew G. Knepley 
49261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
492744a7f3ddSMatthew G. Knepley @*/
4928d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4929d71ae5a4SJacob Faibussowitsch {
493044a7f3ddSMatthew G. Knepley   PetscInt f;
493144a7f3ddSMatthew G. Knepley 
493244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
493344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
493444a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49359566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49369566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
493744a7f3ddSMatthew G. Knepley   }
49389566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
493944a7f3ddSMatthew G. Knepley   dm->fields = NULL;
494044a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
494244a7f3ddSMatthew G. Knepley }
494344a7f3ddSMatthew G. Knepley 
4944689b5837SMatthew G. Knepley /*@
494520f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4946689b5837SMatthew G. Knepley 
494720f4b53cSBarry Smith   Not Collective
4948689b5837SMatthew G. Knepley 
4949689b5837SMatthew G. Knepley   Input Parameter:
495020f4b53cSBarry Smith . dm - The `DM`
4951689b5837SMatthew G. Knepley 
4952689b5837SMatthew G. Knepley   Output Parameter:
495360225df5SJacob Faibussowitsch . numFields - The number of fields
4954689b5837SMatthew G. Knepley 
4955689b5837SMatthew G. Knepley   Level: intermediate
4956689b5837SMatthew G. Knepley 
49571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4958689b5837SMatthew G. Knepley @*/
4959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4960d71ae5a4SJacob Faibussowitsch {
49610f21e855SMatthew G. Knepley   PetscFunctionBegin;
49620f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49634f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
496444a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4966af122d2aSMatthew G Knepley }
4967af122d2aSMatthew G Knepley 
4968689b5837SMatthew G. Knepley /*@
496920f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4970689b5837SMatthew G. Knepley 
497120f4b53cSBarry Smith   Logically Collective
4972689b5837SMatthew G. Knepley 
4973689b5837SMatthew G. Knepley   Input Parameters:
497420f4b53cSBarry Smith + dm        - The `DM`
497560225df5SJacob Faibussowitsch - numFields - The number of fields
4976689b5837SMatthew G. Knepley 
4977689b5837SMatthew G. Knepley   Level: intermediate
4978689b5837SMatthew G. Knepley 
49791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4980689b5837SMatthew G. Knepley @*/
4981d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4982d71ae5a4SJacob Faibussowitsch {
49830f21e855SMatthew G. Knepley   PetscInt Nf, f;
4984af122d2aSMatthew G Knepley 
4985af122d2aSMatthew G Knepley   PetscFunctionBegin;
4986af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49879566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49880f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49890f21e855SMatthew G. Knepley     PetscContainer obj;
49900f21e855SMatthew G. Knepley 
49919566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49929566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49939566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4994af122d2aSMatthew G Knepley   }
49953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4996af122d2aSMatthew G Knepley }
4997af122d2aSMatthew G Knepley 
4998c1929be8SMatthew G. Knepley /*@
4999bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
5000c1929be8SMatthew G. Knepley 
500120f4b53cSBarry Smith   Not Collective
5002c1929be8SMatthew G. Knepley 
5003c1929be8SMatthew G. Knepley   Input Parameters:
5004bb7acecfSBarry Smith + dm - The `DM`
5005c1929be8SMatthew G. Knepley - f  - The field number
5006c1929be8SMatthew G. Knepley 
500744a7f3ddSMatthew G. Knepley   Output Parameters:
500820f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
500920f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
5010c1929be8SMatthew G. Knepley 
501144a7f3ddSMatthew G. Knepley   Level: intermediate
5012c1929be8SMatthew G. Knepley 
50131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5014c1929be8SMatthew G. Knepley @*/
5015d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5016d71ae5a4SJacob Faibussowitsch {
5017af122d2aSMatthew G Knepley   PetscFunctionBegin;
5018af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50194f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50207a8be351SBarry 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);
502144a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
5022bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
50233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5024decb47aaSMatthew G. Knepley }
5025decb47aaSMatthew G. Knepley 
5026083401c6SMatthew G. Knepley /* Does not clear the DS */
5027d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5028d71ae5a4SJacob Faibussowitsch {
5029083401c6SMatthew G. Knepley   PetscFunctionBegin;
50309566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50319566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50329566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5033083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5034bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50359566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5036835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
50373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5038083401c6SMatthew G. Knepley }
5039083401c6SMatthew G. Knepley 
5040ffeef943SBarry Smith /*@
5041bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5042bb7acecfSBarry Smith   the field numbering.
5043c1929be8SMatthew G. Knepley 
504420f4b53cSBarry Smith   Logically Collective
5045c1929be8SMatthew G. Knepley 
5046c1929be8SMatthew G. Knepley   Input Parameters:
5047bb7acecfSBarry Smith + dm    - The `DM`
5048c1929be8SMatthew G. Knepley . f     - The field number
504920f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5050bb7acecfSBarry Smith - disc  - The discretization object
5051c1929be8SMatthew G. Knepley 
505244a7f3ddSMatthew G. Knepley   Level: intermediate
5053c1929be8SMatthew G. Knepley 
50541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5055c1929be8SMatthew G. Knepley @*/
5056d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5057d71ae5a4SJacob Faibussowitsch {
5058decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5059decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5060e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5061bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50627a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5063bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5064bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50659566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
506744a7f3ddSMatthew G. Knepley }
506844a7f3ddSMatthew G. Knepley 
5069ffeef943SBarry Smith /*@
5070bb7acecfSBarry 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)
5071bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
507244a7f3ddSMatthew G. Knepley 
507320f4b53cSBarry Smith   Logically Collective
507444a7f3ddSMatthew G. Knepley 
507544a7f3ddSMatthew G. Knepley   Input Parameters:
5076bb7acecfSBarry Smith + dm    - The `DM`
507720f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5078bb7acecfSBarry Smith - disc  - The discretization object
507944a7f3ddSMatthew G. Knepley 
508044a7f3ddSMatthew G. Knepley   Level: intermediate
508144a7f3ddSMatthew G. Knepley 
5082bb7acecfSBarry Smith   Notes:
5083bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5084bb7acecfSBarry Smith 
5085da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5086bb7acecfSBarry 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
5087bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5088bb7acecfSBarry Smith 
50891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
509044a7f3ddSMatthew G. Knepley @*/
5091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5092d71ae5a4SJacob Faibussowitsch {
509344a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
509444a7f3ddSMatthew G. Knepley 
509544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
509644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5097064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5098bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50999566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
510044a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5101bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
51029566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5103835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
5104bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
51059566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
51063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5107af122d2aSMatthew G Knepley }
51086636e97aSMatthew G Knepley 
5109e5e52638SMatthew G. Knepley /*@
5110e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5111e0b68406SMatthew Knepley 
511220f4b53cSBarry Smith   Logically Collective
5113e0b68406SMatthew Knepley 
5114e0b68406SMatthew Knepley   Input Parameters:
5115bb7acecfSBarry Smith + dm          - The `DM`
5116e0b68406SMatthew Knepley . f           - The field index
5117bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5118e0b68406SMatthew Knepley 
5119e0b68406SMatthew Knepley   Level: intermediate
5120e0b68406SMatthew Knepley 
51211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5122e0b68406SMatthew Knepley @*/
5123d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5124d71ae5a4SJacob Faibussowitsch {
5125e0b68406SMatthew Knepley   PetscFunctionBegin;
512663a3b9bcSJacob 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);
5127e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5129e0b68406SMatthew Knepley }
5130e0b68406SMatthew Knepley 
5131e0b68406SMatthew Knepley /*@
5132e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5133e0b68406SMatthew Knepley 
513420f4b53cSBarry Smith   Not Collective
5135e0b68406SMatthew Knepley 
5136e0b68406SMatthew Knepley   Input Parameters:
5137bb7acecfSBarry Smith + dm - The `DM`
5138e0b68406SMatthew Knepley - f  - The field index
5139e0b68406SMatthew Knepley 
5140e0b68406SMatthew Knepley   Output Parameter:
5141e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5142e0b68406SMatthew Knepley 
5143e0b68406SMatthew Knepley   Level: intermediate
5144e0b68406SMatthew Knepley 
514560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5146e0b68406SMatthew Knepley @*/
5147d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5148d71ae5a4SJacob Faibussowitsch {
5149e0b68406SMatthew Knepley   PetscFunctionBegin;
515063a3b9bcSJacob 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);
5151e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5153e0b68406SMatthew Knepley }
5154e0b68406SMatthew Knepley 
5155e0b68406SMatthew Knepley /*@
5156bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5157e5e52638SMatthew G. Knepley 
515820f4b53cSBarry Smith   Collective
5159e5e52638SMatthew G. Knepley 
5160bb4b53efSMatthew G. Knepley   Input Parameters:
5161bb4b53efSMatthew G. Knepley + dm        - The `DM`
5162bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
5163bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
5164e5e52638SMatthew G. Knepley 
5165e5e52638SMatthew G. Knepley   Output Parameter:
5166bb7acecfSBarry Smith . newdm - The `DM`
5167e5e52638SMatthew G. Knepley 
5168e5e52638SMatthew G. Knepley   Level: advanced
5169e5e52638SMatthew G. Knepley 
51701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5171e5e52638SMatthew G. Knepley @*/
5172bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
5173d71ae5a4SJacob Faibussowitsch {
5174e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5175e5e52638SMatthew G. Knepley 
5176e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51773ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51789566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51799566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5180e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5181e5e52638SMatthew G. Knepley     DMLabel      label;
5182e5e52638SMatthew G. Knepley     PetscObject  field;
5183bb4b53efSMatthew G. Knepley     PetscClassId id;
518434aa8a36SMatthew G. Knepley     PetscBool    useCone, useClosure;
5185e5e52638SMatthew G. Knepley 
51869566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
5187bb4b53efSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(field, &id));
5188bb4b53efSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
5189bb4b53efSMatthew G. Knepley       PetscFE newfe;
5190bb4b53efSMatthew G. Knepley 
5191bb4b53efSMatthew G. Knepley       PetscCall(PetscFELimitDegree((PetscFE)field, minDegree, maxDegree, &newfe));
5192bb4b53efSMatthew G. Knepley       PetscCall(DMSetField(newdm, f, label, (PetscObject)newfe));
5193bb4b53efSMatthew G. Knepley       PetscCall(PetscFEDestroy(&newfe));
5194bb4b53efSMatthew G. Knepley     } else {
51959566063dSJacob Faibussowitsch       PetscCall(DMSetField(newdm, f, label, field));
5196bb4b53efSMatthew G. Knepley     }
51979566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51989566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
519934aa8a36SMatthew G. Knepley   }
52003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
520134aa8a36SMatthew G. Knepley }
520234aa8a36SMatthew G. Knepley 
520334aa8a36SMatthew G. Knepley /*@
520434aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
520534aa8a36SMatthew G. Knepley 
520620f4b53cSBarry Smith   Not Collective
520734aa8a36SMatthew G. Knepley 
520834aa8a36SMatthew G. Knepley   Input Parameters:
520920f4b53cSBarry Smith + dm - The `DM` object
521020f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
521134aa8a36SMatthew G. Knepley 
5212d8d19677SJose E. Roman   Output Parameters:
521334aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
521434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
521534aa8a36SMatthew G. Knepley 
521634aa8a36SMatthew G. Knepley   Level: developer
521734aa8a36SMatthew G. Knepley 
521820f4b53cSBarry Smith   Notes:
521920f4b53cSBarry Smith .vb
522020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
522120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
522220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
522320f4b53cSBarry Smith .ve
522420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
522520f4b53cSBarry Smith 
52261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
522734aa8a36SMatthew G. Knepley @*/
5228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5229d71ae5a4SJacob Faibussowitsch {
523034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
523134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52324f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52334f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
523434aa8a36SMatthew G. Knepley   if (f < 0) {
523534aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
523634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
523734aa8a36SMatthew G. Knepley   } else {
523834aa8a36SMatthew G. Knepley     PetscInt Nf;
523934aa8a36SMatthew G. Knepley 
52409566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52417a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
524234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
524334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
524434aa8a36SMatthew G. Knepley   }
52453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
524634aa8a36SMatthew G. Knepley }
524734aa8a36SMatthew G. Knepley 
524834aa8a36SMatthew G. Knepley /*@
524934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
525034aa8a36SMatthew G. Knepley 
525120f4b53cSBarry Smith   Not Collective
525234aa8a36SMatthew G. Knepley 
525334aa8a36SMatthew G. Knepley   Input Parameters:
525420f4b53cSBarry Smith + dm         - The `DM` object
525534aa8a36SMatthew G. Knepley . f          - The field number
525634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
525734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
525834aa8a36SMatthew G. Knepley 
525934aa8a36SMatthew G. Knepley   Level: developer
526034aa8a36SMatthew G. Knepley 
526120f4b53cSBarry Smith   Notes:
526220f4b53cSBarry Smith .vb
526320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
526420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
526520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
526620f4b53cSBarry Smith .ve
526720f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
526820f4b53cSBarry Smith 
52691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
527034aa8a36SMatthew G. Knepley @*/
5271d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5272d71ae5a4SJacob Faibussowitsch {
527334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
527434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
527534aa8a36SMatthew G. Knepley   if (f < 0) {
527634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
527734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
527834aa8a36SMatthew G. Knepley   } else {
527934aa8a36SMatthew G. Knepley     PetscInt Nf;
528034aa8a36SMatthew G. Knepley 
52819566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52827a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
528334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
528434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5285e5e52638SMatthew G. Knepley   }
52863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5287e5e52638SMatthew G. Knepley }
5288e5e52638SMatthew G. Knepley 
5289b0441da4SMatthew G. Knepley /*@
5290b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5291b0441da4SMatthew G. Knepley 
5292b0441da4SMatthew G. Knepley   Not collective
5293b0441da4SMatthew G. Knepley 
5294f899ff85SJose E. Roman   Input Parameter:
529520f4b53cSBarry Smith . dm - The `DM` object
5296b0441da4SMatthew G. Knepley 
5297d8d19677SJose E. Roman   Output Parameters:
5298b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5299b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5300b0441da4SMatthew G. Knepley 
5301b0441da4SMatthew G. Knepley   Level: developer
5302b0441da4SMatthew G. Knepley 
530320f4b53cSBarry Smith   Notes:
530420f4b53cSBarry Smith .vb
530520f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
530620f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
530720f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
530820f4b53cSBarry Smith .ve
530920f4b53cSBarry Smith 
53101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5311b0441da4SMatthew G. Knepley @*/
5312d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5313d71ae5a4SJacob Faibussowitsch {
5314b0441da4SMatthew G. Knepley   PetscInt Nf;
5315b0441da4SMatthew G. Knepley 
5316b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5317b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53184f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
53194f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
53209566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5321b0441da4SMatthew G. Knepley   if (!Nf) {
53229566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5323b0441da4SMatthew G. Knepley   } else {
53249566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5325b0441da4SMatthew G. Knepley   }
53263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5327b0441da4SMatthew G. Knepley }
5328b0441da4SMatthew G. Knepley 
5329b0441da4SMatthew G. Knepley /*@
5330b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5331b0441da4SMatthew G. Knepley 
533220f4b53cSBarry Smith   Not Collective
5333b0441da4SMatthew G. Knepley 
5334b0441da4SMatthew G. Knepley   Input Parameters:
533520f4b53cSBarry Smith + dm         - The `DM` object
5336b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5337b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5338b0441da4SMatthew G. Knepley 
5339b0441da4SMatthew G. Knepley   Level: developer
5340b0441da4SMatthew G. Knepley 
534120f4b53cSBarry Smith   Notes:
534220f4b53cSBarry Smith .vb
534320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
534420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
534520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
534620f4b53cSBarry Smith .ve
534720f4b53cSBarry Smith 
53481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5349b0441da4SMatthew G. Knepley @*/
5350d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5351d71ae5a4SJacob Faibussowitsch {
5352b0441da4SMatthew G. Knepley   PetscInt Nf;
5353b0441da4SMatthew G. Knepley 
5354b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5355b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53569566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5357b0441da4SMatthew G. Knepley   if (!Nf) {
53589566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5359b0441da4SMatthew G. Knepley   } else {
53609566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5361e5e52638SMatthew G. Knepley   }
53623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5363e5e52638SMatthew G. Knepley }
5364e5e52638SMatthew G. Knepley 
5365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5366d71ae5a4SJacob Faibussowitsch {
5367799db056SMatthew G. Knepley   DM           plex;
5368799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5369799db056SMatthew G. Knepley   const char **names;
5370799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5371799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5372799db056SMatthew G. Knepley   size_t       len;
5373799db056SMatthew G. Knepley   MPI_Comm     comm;
5374799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5375783e2ec8SMatthew G. Knepley 
5376783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5377799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5378799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5379799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5380799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5381799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5382799db056SMatthew G. Knepley     PetscDS  dsBC;
5383799db056SMatthew G. Knepley     PetscInt numBd;
5384799db056SMatthew G. Knepley 
538507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5386799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5387799db056SMatthew G. Knepley     maxLabels += numBd;
5388799db056SMatthew G. Knepley   }
5389799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5390799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5391799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5392799db056SMatthew G. Knepley     PetscDS  dsBC;
5393799db056SMatthew G. Knepley     PetscInt numBd, bd;
5394799db056SMatthew G. Knepley 
539507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5396799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5397799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5398799db056SMatthew G. Knepley       DMLabel      label;
5399799db056SMatthew G. Knepley       PetscInt     field;
5400799db056SMatthew G. Knepley       PetscObject  obj;
5401799db056SMatthew G. Knepley       PetscClassId id;
5402799db056SMatthew G. Knepley 
5403799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
54049566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
54059566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5406799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
54079371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
54089371c9d4SSatish Balay         if (labels[l] == label) break;
5409799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5410783e2ec8SMatthew G. Knepley     }
5411799db056SMatthew G. Knepley   }
5412799db056SMatthew G. Knepley   /* Get label names */
5413799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5414799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
54159371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
54169371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
54179371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
54189371c9d4SSatish Balay   }
5419799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5420462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5421799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5422c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5423799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5424799db056SMatthew G. Knepley   /* Put all names on all processes */
5425799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5426799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5427799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5428799db056SMatthew G. Knepley   gNl = displs[size];
54299371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54309371c9d4SSatish Balay     counts[p] *= gmaxLen;
54319371c9d4SSatish Balay     displs[p] *= gmaxLen;
54329371c9d4SSatish Balay   }
5433799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5434799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5435799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5436799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5437799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5438799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5439799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54409371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54415d6b2bfcSJames Wright       if (glabels[m] == glabels[gl]) goto next_label;
54429566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5443799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54449566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5445799db056SMatthew G. Knepley     ++gl;
54465d6b2bfcSJames Wright   next_label:
54475d6b2bfcSJames Wright     continue;
5448783e2ec8SMatthew G. Knepley   }
5449799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5451783e2ec8SMatthew G. Knepley }
5452783e2ec8SMatthew G. Knepley 
5453d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5454d71ae5a4SJacob Faibussowitsch {
5455e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5456e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5457e5e52638SMatthew G. Knepley 
5458e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54593ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54609566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5461e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54629371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54639371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54649371c9d4SSatish Balay     tmpd[s].label  = NULL;
54659371c9d4SSatish Balay     tmpd[s].fields = NULL;
54669371c9d4SSatish Balay   }
54679566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5468e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5469e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5471e5e52638SMatthew G. Knepley }
5472e5e52638SMatthew G. Knepley 
5473e5e52638SMatthew G. Knepley /*@
547420f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5475e5e52638SMatthew G. Knepley 
547620f4b53cSBarry Smith   Not Collective
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley   Input Parameter:
547920f4b53cSBarry Smith . dm - The `DM`
5480e5e52638SMatthew G. Knepley 
5481e5e52638SMatthew G. Knepley   Output Parameter:
548220f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5483e5e52638SMatthew G. Knepley 
5484e5e52638SMatthew G. Knepley   Level: intermediate
5485e5e52638SMatthew G. Knepley 
54861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5487e5e52638SMatthew G. Knepley @*/
5488d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5489d71ae5a4SJacob Faibussowitsch {
5490e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5491e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54924f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5493e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5495e5e52638SMatthew G. Knepley }
5496e5e52638SMatthew G. Knepley 
5497e5e52638SMatthew G. Knepley /*@
549820f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5499e5e52638SMatthew G. Knepley 
550020f4b53cSBarry Smith   Logically Collective
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley   Input Parameter:
550320f4b53cSBarry Smith . dm - The `DM`
5504e5e52638SMatthew G. Knepley 
5505e5e52638SMatthew G. Knepley   Level: intermediate
5506e5e52638SMatthew G. Knepley 
55071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5508e5e52638SMatthew G. Knepley @*/
5509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5510d71ae5a4SJacob Faibussowitsch {
5511e5e52638SMatthew G. Knepley   PetscInt s;
5512e5e52638SMatthew G. Knepley 
5513e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5514e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5515e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
55169566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
551707218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
55189566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
55199566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5520e5e52638SMatthew G. Knepley   }
55219566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5522e5e52638SMatthew G. Knepley   dm->probs = NULL;
5523e5e52638SMatthew G. Knepley   dm->Nds   = 0;
55243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5525e5e52638SMatthew G. Knepley }
5526e5e52638SMatthew G. Knepley 
5527e5e52638SMatthew G. Knepley /*@
552820f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5529e5e52638SMatthew G. Knepley 
553020f4b53cSBarry Smith   Not Collective
5531e5e52638SMatthew G. Knepley 
5532e5e52638SMatthew G. Knepley   Input Parameter:
553320f4b53cSBarry Smith . dm - The `DM`
5534e5e52638SMatthew G. Knepley 
5535e5e52638SMatthew G. Knepley   Output Parameter:
553607218a29SMatthew G. Knepley . ds - The default `PetscDS`
5537e5e52638SMatthew G. Knepley 
5538e5e52638SMatthew G. Knepley   Level: intermediate
5539e5e52638SMatthew G. Knepley 
55401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5541e5e52638SMatthew G. Knepley @*/
554207218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5543d71ae5a4SJacob Faibussowitsch {
5544e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5545e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55464f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
554707218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
554807218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5550e5e52638SMatthew G. Knepley }
5551e5e52638SMatthew G. Knepley 
5552e5e52638SMatthew G. Knepley /*@
555320f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5554e5e52638SMatthew G. Knepley 
555520f4b53cSBarry Smith   Not Collective
5556e5e52638SMatthew G. Knepley 
5557e5e52638SMatthew G. Knepley   Input Parameters:
555820f4b53cSBarry Smith + dm    - The `DM`
555920f4b53cSBarry Smith - point - Cell for the `PetscDS`
5560e5e52638SMatthew G. Knepley 
556107218a29SMatthew G. Knepley   Output Parameters:
556207218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
556307218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5564e5e52638SMatthew G. Knepley 
5565e5e52638SMatthew G. Knepley   Level: developer
5566e5e52638SMatthew G. Knepley 
55671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5568e5e52638SMatthew G. Knepley @*/
556907218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5570d71ae5a4SJacob Faibussowitsch {
557107218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5572e5e52638SMatthew G. Knepley   PetscInt s;
5573e5e52638SMatthew G. Knepley 
5574e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5575e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55764f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55774f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
557863a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
557907218a29SMatthew G. Knepley   if (ds) *ds = NULL;
558007218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5581e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5582e5e52638SMatthew G. Knepley     PetscInt val;
5583e5e52638SMatthew G. Knepley 
55849371c9d4SSatish Balay     if (!dm->probs[s].label) {
558507218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55869371c9d4SSatish Balay     } else {
55879566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55889371c9d4SSatish Balay       if (val >= 0) {
558907218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
559007218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55919371c9d4SSatish Balay         break;
55929371c9d4SSatish Balay       }
5593e5e52638SMatthew G. Knepley     }
5594e5e52638SMatthew G. Knepley   }
559507218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5597e5e52638SMatthew G. Knepley }
5598e5e52638SMatthew G. Knepley 
5599e5e52638SMatthew G. Knepley /*@
560020f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5601e5e52638SMatthew G. Knepley 
560220f4b53cSBarry Smith   Not Collective
5603e5e52638SMatthew G. Knepley 
5604e5e52638SMatthew G. Knepley   Input Parameters:
560520f4b53cSBarry Smith + dm    - The `DM`
560620f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5607e5e52638SMatthew G. Knepley 
5608b3cf3223SMatthew G. Knepley   Output Parameters:
560920f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
561007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
561107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5612e5e52638SMatthew G. Knepley 
5613e5e52638SMatthew G. Knepley   Level: advanced
5614e5e52638SMatthew G. Knepley 
561520f4b53cSBarry Smith   Note:
561620f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
561720f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
561807218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
561920f4b53cSBarry Smith 
56201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5621e5e52638SMatthew G. Knepley @*/
562207218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5623d71ae5a4SJacob Faibussowitsch {
5624e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5625e5e52638SMatthew G. Knepley 
5626e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5627e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5628e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56299371c9d4SSatish Balay   if (fields) {
56304f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56319371c9d4SSatish Balay     *fields = NULL;
56329371c9d4SSatish Balay   }
56339371c9d4SSatish Balay   if (ds) {
56344f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56359371c9d4SSatish Balay     *ds = NULL;
56369371c9d4SSatish Balay   }
563707218a29SMatthew G. Knepley   if (dsIn) {
56384f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
563907218a29SMatthew G. Knepley     *dsIn = NULL;
564007218a29SMatthew G. Knepley   }
5641e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5642154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5643b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5644b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
564507218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56463ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5647b3cf3223SMatthew G. Knepley     }
5648e5e52638SMatthew G. Knepley   }
56493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5650e5e52638SMatthew G. Knepley }
5651e5e52638SMatthew G. Knepley 
5652e5e52638SMatthew G. Knepley /*@
5653bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5654083401c6SMatthew G. Knepley 
565520f4b53cSBarry Smith   Collective
5656083401c6SMatthew G. Knepley 
5657083401c6SMatthew G. Knepley   Input Parameters:
5658bb7acecfSBarry Smith + dm     - The `DM`
565920f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
566007218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
566107218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
566207218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5663083401c6SMatthew G. Knepley 
566420f4b53cSBarry Smith   Level: advanced
566520f4b53cSBarry Smith 
5666bb7acecfSBarry Smith   Note:
5667bb7acecfSBarry 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,
5668083401c6SMatthew G. Knepley   the fields argument is ignored.
5669083401c6SMatthew G. Knepley 
56701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5671083401c6SMatthew G. Knepley @*/
567207218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5673d71ae5a4SJacob Faibussowitsch {
5674083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5675083401c6SMatthew G. Knepley 
5676083401c6SMatthew G. Knepley   PetscFunctionBegin;
5677083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5678083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
567907218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5680064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
568107218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5682083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5683083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56849566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
568507218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5686083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
568707218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56883ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5689083401c6SMatthew G. Knepley     }
5690083401c6SMatthew G. Knepley   }
56919566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56929566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56939566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56949566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
569507218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5696083401c6SMatthew G. Knepley   if (!label) {
5697083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5698083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5699083401c6SMatthew G. Knepley     Nds = 0;
5700083401c6SMatthew G. Knepley   }
5701083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5702083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5703083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
570407218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
57053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5706083401c6SMatthew G. Knepley }
5707083401c6SMatthew G. Knepley 
5708083401c6SMatthew G. Knepley /*@
570920f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5710e5e52638SMatthew G. Knepley 
571120f4b53cSBarry Smith   Not Collective
5712e5e52638SMatthew G. Knepley 
5713e5e52638SMatthew G. Knepley   Input Parameters:
571420f4b53cSBarry Smith + dm  - The `DM`
5715e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5716e5e52638SMatthew G. Knepley 
5717e5e52638SMatthew G. Knepley   Output Parameters:
571820f4b53cSBarry Smith + label  - The region label, or `NULL`
571920f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
572007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
572107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5722e5e52638SMatthew G. Knepley 
5723e5e52638SMatthew G. Knepley   Level: advanced
5724e5e52638SMatthew G. Knepley 
57251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5726e5e52638SMatthew G. Knepley @*/
572707218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5728d71ae5a4SJacob Faibussowitsch {
5729e5e52638SMatthew G. Knepley   PetscInt Nds;
5730e5e52638SMatthew G. Knepley 
5731e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5732e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57339566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
573463a3b9bcSJacob 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);
5735e5e52638SMatthew G. Knepley   if (label) {
57364f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5737e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5738e5e52638SMatthew G. Knepley   }
5739b3cf3223SMatthew G. Knepley   if (fields) {
57404f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5741b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5742b3cf3223SMatthew G. Knepley   }
5743e5e52638SMatthew G. Knepley   if (ds) {
57444f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5745e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5746e5e52638SMatthew G. Knepley   }
574707218a29SMatthew G. Knepley   if (dsIn) {
57484f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
574907218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
575007218a29SMatthew G. Knepley   }
57513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5752e5e52638SMatthew G. Knepley }
5753e5e52638SMatthew G. Knepley 
5754e5e52638SMatthew G. Knepley /*@
575520f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5756e5e52638SMatthew G. Knepley 
575720f4b53cSBarry Smith   Not Collective
5758e5e52638SMatthew G. Knepley 
5759e5e52638SMatthew G. Knepley   Input Parameters:
576020f4b53cSBarry Smith + dm     - The `DM`
5761083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
576220f4b53cSBarry Smith . label  - The region label, or `NULL`
576307218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
576407218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
576507218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5766e5e52638SMatthew G. Knepley 
5767e5e52638SMatthew G. Knepley   Level: advanced
5768e5e52638SMatthew G. Knepley 
57691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5770e5e52638SMatthew G. Knepley @*/
577107218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5772d71ae5a4SJacob Faibussowitsch {
5773083401c6SMatthew G. Knepley   PetscInt Nds;
5774e5e52638SMatthew G. Knepley 
5775e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5776e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5777ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57789566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
577963a3b9bcSJacob 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);
57809566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57819566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5782083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5783083401c6SMatthew G. Knepley   if (fields) {
5784083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57859566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57869566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5787083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5788e5e52638SMatthew G. Knepley   }
5789083401c6SMatthew G. Knepley   if (ds) {
5790083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57919566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57929566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5793083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5794083401c6SMatthew G. Knepley   }
579507218a29SMatthew G. Knepley   if (dsIn) {
579607218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
579707218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
579807218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
579907218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
580007218a29SMatthew G. Knepley   }
58013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5802e5e52638SMatthew G. Knepley }
5803e5e52638SMatthew G. Knepley 
5804e5e52638SMatthew G. Knepley /*@
580520f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
58061d3af9e0SMatthew G. Knepley 
580720f4b53cSBarry Smith   Not Collective
58081d3af9e0SMatthew G. Knepley 
58091d3af9e0SMatthew G. Knepley   Input Parameters:
581020f4b53cSBarry Smith + dm - The `DM`
581120f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
58121d3af9e0SMatthew G. Knepley 
58131d3af9e0SMatthew G. Knepley   Output Parameter:
58141d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
58151d3af9e0SMatthew G. Knepley 
58161d3af9e0SMatthew G. Knepley   Level: advanced
58171d3af9e0SMatthew G. Knepley 
58181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
58191d3af9e0SMatthew G. Knepley @*/
5820d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5821d71ae5a4SJacob Faibussowitsch {
58221d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
58231d3af9e0SMatthew G. Knepley 
58241d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
58251d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58261d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
58274f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58289566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58299371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58309371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58311d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58321d3af9e0SMatthew G. Knepley   else *num = n;
58333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58341d3af9e0SMatthew G. Knepley }
58351d3af9e0SMatthew G. Knepley 
5836cc4c1da9SBarry Smith /*@
5837bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58382df84da0SMatthew G. Knepley 
583920f4b53cSBarry Smith   Not Collective
58402df84da0SMatthew G. Knepley 
5841f1a722f8SMatthew G. Knepley   Input Parameters:
5842bb7acecfSBarry Smith + dm     - The `DM`
58432df84da0SMatthew G. Knepley . Nc     - The number of components for the field
584420f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5845bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58462df84da0SMatthew G. Knepley 
58472df84da0SMatthew G. Knepley   Output Parameter:
5848bb7acecfSBarry Smith . fem - The `PetscFE`
58492df84da0SMatthew G. Knepley 
585020f4b53cSBarry Smith   Level: intermediate
585120f4b53cSBarry Smith 
5852bb7acecfSBarry Smith   Note:
5853bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58542df84da0SMatthew G. Knepley 
58551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58562df84da0SMatthew G. Knepley @*/
5857d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5858d71ae5a4SJacob Faibussowitsch {
58592df84da0SMatthew G. Knepley   DMPolytopeType ct;
58602df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58612df84da0SMatthew G. Knepley 
58622df84da0SMatthew G. Knepley   PetscFunctionBegin;
58632df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58642df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58654f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58662df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58674f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58689566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58719566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58732df84da0SMatthew G. Knepley }
58742df84da0SMatthew G. Knepley 
58751d3af9e0SMatthew G. Knepley /*@
5876bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5877e5e52638SMatthew G. Knepley 
587820f4b53cSBarry Smith   Collective
5879e5e52638SMatthew G. Knepley 
5880e5e52638SMatthew G. Knepley   Input Parameter:
5881bb7acecfSBarry Smith . dm - The `DM`
5882e5e52638SMatthew G. Knepley 
588320f4b53cSBarry Smith   Options Database Key:
5884bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
588545480ffeSMatthew G. Knepley 
588620f4b53cSBarry Smith   Level: intermediate
588720f4b53cSBarry Smith 
58881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5889e5e52638SMatthew G. Knepley @*/
5890d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5891d71ae5a4SJacob Faibussowitsch {
5892e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5893083401c6SMatthew G. Knepley   PetscDS   dsDef;
5894083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5895f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5896f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5897e5e52638SMatthew G. Knepley 
5898e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5899e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59003ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
59019566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
59029566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5903083401c6SMatthew G. Knepley   /* Determine how many regions we have */
59049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5905083401c6SMatthew G. Knepley   Nl   = 0;
5906083401c6SMatthew G. Knepley   Ndef = 0;
5907083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5908083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5909083401c6SMatthew G. Knepley     PetscInt l;
5910083401c6SMatthew G. Knepley 
5911f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5912f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5913f918ec44SMatthew G. Knepley     {
5914f918ec44SMatthew G. Knepley       PetscClassId id;
5915f918ec44SMatthew G. Knepley 
59169566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5917f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5918f918ec44SMatthew G. Knepley         Ceed ceed;
5919f918ec44SMatthew G. Knepley 
59209566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
59219566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5922f918ec44SMatthew G. Knepley       }
5923f918ec44SMatthew G. Knepley     }
5924f918ec44SMatthew G. Knepley #endif
59259371c9d4SSatish Balay     if (!label) {
59269371c9d4SSatish Balay       ++Ndef;
59279371c9d4SSatish Balay       continue;
59289371c9d4SSatish Balay     }
59299371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59309371c9d4SSatish Balay       if (label == labelSet[l]) break;
5931083401c6SMatthew G. Knepley     if (l < Nl) continue;
5932083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5933083401c6SMatthew G. Knepley   }
5934083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
593507218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5936083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5937b3cf3223SMatthew G. Knepley     IS        fields;
5938b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5939b3cf3223SMatthew G. Knepley 
59409371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59419371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59427a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59439566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59449371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59459371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59469566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59479566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59489566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59499566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
595088f0c812SMatthew G. Knepley 
59519566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
595207218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59539566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59549566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59552df9ee95SMatthew G. Knepley   }
595607218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59579566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5958083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5959083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59600122748bSMatthew G. Knepley     DM              plex;
5961083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5962083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5963083401c6SMatthew G. Knepley     PetscInt       *fields;
5964083401c6SMatthew G. Knepley     const PetscInt *cells;
5965083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59660122748bSMatthew G. Knepley 
59679566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59689566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59699566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59709566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59715fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5972083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5973083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5974083401c6SMatthew G. Knepley       IS      pointIS;
5975083401c6SMatthew G. Knepley 
59769566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59779566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59789566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59799566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5980083401c6SMatthew G. Knepley     }
59819566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5982083401c6SMatthew G. Knepley 
59839566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59849566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59869566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59879566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59889566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59899566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5990083401c6SMatthew G. Knepley 
59919566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59929371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59939371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59949566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59959566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59969566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59979566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5998083401c6SMatthew G. Knepley 
59999566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
600007218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
60019566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
60029566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
60039566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
60049566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
60059566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
6006083401c6SMatthew G. Knepley   }
6007083401c6SMatthew G. Knepley   /* Create label DSes
6008083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
6009083401c6SMatthew G. Knepley   */
6010083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
6011083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
6012083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
601307218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
6014083401c6SMatthew G. Knepley     IS        fields;
6015083401c6SMatthew G. Knepley     PetscInt *fld, nf;
6016083401c6SMatthew G. Knepley 
60179566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
60189371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60199371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
60209566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
60219371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60229371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
60239566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
60249566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
60259566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
60269566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
60279566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6028083401c6SMatthew G. Knepley     {
6029083401c6SMatthew G. Knepley       DMPolytopeType ct;
6030083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60315fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60320122748bSMatthew G. Knepley 
60339566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6034665f567fSMatthew G. Knepley       if (lStart >= 0) {
60359566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6036412e9a14SMatthew G. Knepley         switch (ct) {
6037412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6038412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6039412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6040d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6041d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6042d71ae5a4SJacob Faibussowitsch           break;
6043d71ae5a4SJacob Faibussowitsch         default:
6044d71ae5a4SJacob Faibussowitsch           break;
6045412e9a14SMatthew G. Knepley         }
6046665f567fSMatthew G. Knepley       }
6047462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
604807218a29SMatthew G. Knepley       if (isCohesive) {
604907218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
605007218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
605107218a29SMatthew G. Knepley       }
60525fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60535fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60545fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60559566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60569566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
605707218a29SMatthew G. Knepley             if (dsIn) {
605807218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
605907218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
606007218a29SMatthew G. Knepley             }
60615fedec97SMatthew G. Knepley           }
60625fedec97SMatthew G. Knepley           ++nf;
60635fedec97SMatthew G. Knepley         }
60645fedec97SMatthew G. Knepley       }
6065e5e52638SMatthew G. Knepley     }
606607218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
606707218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60689566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
606907218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6070e5e52638SMatthew G. Knepley   }
60719566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6072e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6073083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6074083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
607507218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6076083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6077083401c6SMatthew G. Knepley     const PetscInt *fld;
60785fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60795fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6080e5e52638SMatthew G. Knepley 
60819566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60829566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60839566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60849566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6085083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6086083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60875fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6088e5e52638SMatthew G. Knepley       PetscClassId id;
6089e5e52638SMatthew G. Knepley 
60905fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60919566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60925fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
609307218a29SMatthew G. Knepley       if (isCohesive) {
609407218a29SMatthew G. Knepley         if (!isCohesiveField) {
609507218a29SMatthew G. Knepley           PetscObject bdDisc;
609607218a29SMatthew G. Knepley 
609707218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
609807218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
609907218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
610007218a29SMatthew G. Knepley         } else {
61019566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
610207218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
610307218a29SMatthew G. Knepley         }
610407218a29SMatthew G. Knepley       } else {
610507218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
610607218a29SMatthew G. Knepley       }
6107083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
61089566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6109e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6110e5e52638SMatthew G. Knepley     }
61119566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6112e5e52638SMatthew G. Knepley   }
6113f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
61149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6115f9244615SMatthew G. Knepley   if (flg) {
61163b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
61173b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
611807218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
61193b4aee56SMatthew G. Knepley       PetscInt Nf, f;
61203b4aee56SMatthew G. Knepley 
61219566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
612207218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
612307218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
612407218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
612507218a29SMatthew G. Knepley       }
61263b4aee56SMatthew G. Knepley     }
6127f9244615SMatthew G. Knepley   }
6128e5e52638SMatthew G. Knepley   /* Setup DSes */
6129e5e52638SMatthew G. Knepley   if (doSetup) {
613007218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6131cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6132cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6133cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6134cf1363e4SMatthew G. Knepley       }
613507218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
613607218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
613707218a29SMatthew G. Knepley     }
6138e5e52638SMatthew G. Knepley   }
61393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6140e5e52638SMatthew G. Knepley }
6141e5e52638SMatthew G. Knepley 
6142e5e52638SMatthew G. Knepley /*@
6143d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6144d2b2dc1eSMatthew G. Knepley 
6145d2b2dc1eSMatthew G. Knepley   Input Parameters:
6146d2b2dc1eSMatthew G. Knepley + dm     - The DM
6147d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6148d2b2dc1eSMatthew G. Knepley 
6149d2b2dc1eSMatthew G. Knepley   Level: developer
6150d2b2dc1eSMatthew G. Knepley 
6151d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6152d2b2dc1eSMatthew G. Knepley @*/
6153d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6154d2b2dc1eSMatthew G. Knepley {
6155d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6156d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6157d2b2dc1eSMatthew G. Knepley 
6158d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6159d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6160d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6161d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6162d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6163d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6164d2b2dc1eSMatthew G. Knepley 
6165d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6166d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6167d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6168d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6169d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6170d2b2dc1eSMatthew G. Knepley 
6171d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6172d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6173d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6174d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6175d2b2dc1eSMatthew G. Knepley   }
6176d2b2dc1eSMatthew G. Knepley   if (tensor) {
6177d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6178d2b2dc1eSMatthew G. Knepley   } else {
6179d2b2dc1eSMatthew G. Knepley     PetscSection s;
6180d2b2dc1eSMatthew G. Knepley 
6181d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6182d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6183d2b2dc1eSMatthew G. Knepley   }
6184d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6185d2b2dc1eSMatthew G. Knepley }
6186d2b2dc1eSMatthew G. Knepley 
6187d2b2dc1eSMatthew G. Knepley /*@
6188bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61897f96f943SMatthew G. Knepley 
619020f4b53cSBarry Smith   Collective
6191f2cacb80SMatthew G. Knepley 
61927f96f943SMatthew G. Knepley   Input Parameters:
6193bb7acecfSBarry Smith + dm   - The `DM`
61947f96f943SMatthew G. Knepley - time - The time
61957f96f943SMatthew G. Knepley 
61967f96f943SMatthew G. Knepley   Output Parameters:
619720f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
619820f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
619920f4b53cSBarry Smith 
620020f4b53cSBarry Smith   Level: developer
62017f96f943SMatthew G. Knepley 
6202bb7acecfSBarry Smith   Note:
6203bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
62047f96f943SMatthew G. Knepley 
62051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
62067f96f943SMatthew G. Knepley @*/
6207d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6208d71ae5a4SJacob Faibussowitsch {
62097f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
62107f96f943SMatthew G. Knepley   void   **ectxs;
6211f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
62127f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
62137f96f943SMatthew G. Knepley 
62147f96f943SMatthew G. Knepley   PetscFunctionBegin;
6215f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6216f60fa741SMatthew G. Knepley   if (u) {
6217f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6218f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6219f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6220f60fa741SMatthew G. Knepley   }
6221f60fa741SMatthew G. Knepley   if (u_t) {
6222f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6223f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6224f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6225f60fa741SMatthew G. Knepley   }
62269566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
62279566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62289566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62297f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62307f96f943SMatthew G. Knepley     PetscDS         ds;
62317f96f943SMatthew G. Knepley     DMLabel         label;
62327f96f943SMatthew G. Knepley     IS              fieldIS;
62337f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62347f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62357f96f943SMatthew G. Knepley 
623607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62379566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62389566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62399566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62409566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6241f2cacb80SMatthew G. Knepley     if (u) {
6242f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6243f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6244f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62457f96f943SMatthew G. Knepley     }
6246f2cacb80SMatthew G. Knepley     if (u_t) {
62479566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62489566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6249f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6250f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6251f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6252f2cacb80SMatthew G. Knepley     }
62539566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6254f2cacb80SMatthew G. Knepley   }
6255f2cacb80SMatthew G. Knepley   if (u) {
62569566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62579566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6258f2cacb80SMatthew G. Knepley   }
6259f2cacb80SMatthew G. Knepley   if (u_t) {
62609566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62619566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6262f2cacb80SMatthew G. Knepley   }
62639566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6264f60fa741SMatthew G. Knepley   if (u) {
6265f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6266f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6267f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6268f60fa741SMatthew G. Knepley   }
6269f60fa741SMatthew G. Knepley   if (u_t) {
6270f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6271f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6272f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6273f60fa741SMatthew G. Knepley   }
62743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62757f96f943SMatthew G. Knepley }
62767f96f943SMatthew G. Knepley 
6277bb4b53efSMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscInt minDegree, PetscInt maxDegree, PetscDS ds, PetscDS dsIn)
6278d71ae5a4SJacob Faibussowitsch {
627907218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
628045480ffeSMatthew G. Knepley 
628145480ffeSMatthew G. Knepley   PetscFunctionBegin;
62829566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
6283bb4b53efSMatthew G. Knepley   PetscCall(PetscDSCopy(ds, minDegree, maxDegree, dm, dsNew));
628407218a29SMatthew G. Knepley   if (dsIn) {
628507218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
6286bb4b53efSMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, minDegree, maxDegree, dm, dsInNew));
628745480ffeSMatthew G. Knepley   }
628807218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62899566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
629007218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
629245480ffeSMatthew G. Knepley }
629345480ffeSMatthew G. Knepley 
62947f96f943SMatthew G. Knepley /*@
6295bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6296e5e52638SMatthew G. Knepley 
629720f4b53cSBarry Smith   Collective
6298e5e52638SMatthew G. Knepley 
6299bb4b53efSMatthew G. Knepley   Input Parameters:
6300bb4b53efSMatthew G. Knepley + dm        - The `DM`
6301bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
6302bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
6303e5e52638SMatthew G. Knepley 
6304e5e52638SMatthew G. Knepley   Output Parameter:
6305bb7acecfSBarry Smith . newdm - The `DM`
6306e5e52638SMatthew G. Knepley 
6307e5e52638SMatthew G. Knepley   Level: advanced
6308e5e52638SMatthew G. Knepley 
63091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6310e5e52638SMatthew G. Knepley @*/
6311bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
6312d71ae5a4SJacob Faibussowitsch {
6313e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6314e5e52638SMatthew G. Knepley 
6315e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63163ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
63179566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
63189566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6319e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6320e5e52638SMatthew G. Knepley     DMLabel  label;
6321b3cf3223SMatthew G. Knepley     IS       fields;
632207218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6323783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6324e5e52638SMatthew G. Knepley 
632507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6326b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
6327bb4b53efSMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, minDegree, maxDegree, ds, dsIn));
6328d5b43468SJose E. Roman     /* Complete new labels in the new DS */
632907218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63309566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6331783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6332b8025e53SMatthew G. Knepley       PetscWeakForm wf;
633345480ffeSMatthew G. Knepley       DMLabel       label;
6334783e2ec8SMatthew G. Knepley       PetscInt      field;
6335783e2ec8SMatthew G. Knepley 
63369566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63379566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6338783e2ec8SMatthew G. Knepley     }
6339e5e52638SMatthew G. Knepley   }
6340799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6342e5e52638SMatthew G. Knepley }
6343e5e52638SMatthew G. Knepley 
6344e5e52638SMatthew G. Knepley /*@
6345bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6346e5e52638SMatthew G. Knepley 
634720f4b53cSBarry Smith   Collective
6348e5e52638SMatthew G. Knepley 
6349e5e52638SMatthew G. Knepley   Input Parameter:
6350bb7acecfSBarry Smith . dm - The `DM`
6351e5e52638SMatthew G. Knepley 
6352e5e52638SMatthew G. Knepley   Output Parameter:
6353bb7acecfSBarry Smith . newdm - The `DM`
6354e5e52638SMatthew G. Knepley 
6355e5e52638SMatthew G. Knepley   Level: advanced
6356e5e52638SMatthew G. Knepley 
635773ff1848SBarry Smith   Developer Note:
6358bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6359bb7acecfSBarry Smith 
63601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6361e5e52638SMatthew G. Knepley @*/
6362d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6363d71ae5a4SJacob Faibussowitsch {
6364e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6365bb4b53efSMatthew G. Knepley   PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
6366bb4b53efSMatthew G. Knepley   PetscCall(DMCopyDS(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
63673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6368e5e52638SMatthew G. Knepley }
6369e5e52638SMatthew G. Knepley 
6370c73cfb54SMatthew G. Knepley /*@
6371bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6372c73cfb54SMatthew G. Knepley 
637320f4b53cSBarry Smith   Not Collective
6374c73cfb54SMatthew G. Knepley 
6375c73cfb54SMatthew G. Knepley   Input Parameter:
6376bb7acecfSBarry Smith . dm - The `DM`
6377c73cfb54SMatthew G. Knepley 
6378c73cfb54SMatthew G. Knepley   Output Parameter:
6379c73cfb54SMatthew G. Knepley . dim - The topological dimension
6380c73cfb54SMatthew G. Knepley 
6381c73cfb54SMatthew G. Knepley   Level: beginner
6382c73cfb54SMatthew G. Knepley 
63831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6384c73cfb54SMatthew G. Knepley @*/
6385d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6386d71ae5a4SJacob Faibussowitsch {
6387c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6388c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63894f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6390c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6392c73cfb54SMatthew G. Knepley }
6393c73cfb54SMatthew G. Knepley 
6394c73cfb54SMatthew G. Knepley /*@
6395bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6396c73cfb54SMatthew G. Knepley 
639720f4b53cSBarry Smith   Collective
6398c73cfb54SMatthew G. Knepley 
6399c73cfb54SMatthew G. Knepley   Input Parameters:
6400bb7acecfSBarry Smith + dm  - The `DM`
6401c73cfb54SMatthew G. Knepley - dim - The topological dimension
6402c73cfb54SMatthew G. Knepley 
6403c73cfb54SMatthew G. Knepley   Level: beginner
6404c73cfb54SMatthew G. Knepley 
64051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6406c73cfb54SMatthew G. Knepley @*/
6407d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6408d71ae5a4SJacob Faibussowitsch {
6409e5e52638SMatthew G. Knepley   PetscDS  ds;
641045480ffeSMatthew G. Knepley   PetscInt Nds, n;
6411f17e8794SMatthew G. Knepley 
6412c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6413c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6414c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6415c73cfb54SMatthew G. Knepley   dm->dim = dim;
6416d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
64179566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
641845480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
641907218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
64209566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
642145480ffeSMatthew G. Knepley     }
6422d17bd122SMatthew G. Knepley   }
64233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6424c73cfb54SMatthew G. Knepley }
6425c73cfb54SMatthew G. Knepley 
6426793f3fe5SMatthew G. Knepley /*@
6427793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6428793f3fe5SMatthew G. Knepley 
642920f4b53cSBarry Smith   Collective
6430793f3fe5SMatthew G. Knepley 
6431793f3fe5SMatthew G. Knepley   Input Parameters:
6432bb7acecfSBarry Smith + dm  - the `DM`
6433793f3fe5SMatthew G. Knepley - dim - the dimension
6434793f3fe5SMatthew G. Knepley 
6435793f3fe5SMatthew G. Knepley   Output Parameters:
6436793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6437aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6438793f3fe5SMatthew G. Knepley 
643920f4b53cSBarry Smith   Level: intermediate
644020f4b53cSBarry Smith 
6441793f3fe5SMatthew G. Knepley   Note:
6442793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6443a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6444793f3fe5SMatthew G. Knepley   then the interval is empty.
6445793f3fe5SMatthew G. Knepley 
64461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6447793f3fe5SMatthew G. Knepley @*/
6448d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6449d71ae5a4SJacob Faibussowitsch {
6450793f3fe5SMatthew G. Knepley   PetscInt d;
6451793f3fe5SMatthew G. Knepley 
6452793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6453793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64557a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6456dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6458793f3fe5SMatthew G. Knepley }
6459793f3fe5SMatthew G. Knepley 
64606636e97aSMatthew G Knepley /*@
6461bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6462f4d763aaSMatthew G. Knepley 
646320f4b53cSBarry Smith   Collective
64648f700142SStefano Zampini 
6465f4d763aaSMatthew G. Knepley   Input Parameter:
6466bb7acecfSBarry Smith . dm - The original `DM`
6467f4d763aaSMatthew G. Knepley 
6468f4d763aaSMatthew G. Knepley   Output Parameter:
6469bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6470f4d763aaSMatthew G. Knepley 
6471f4d763aaSMatthew G. Knepley   Level: intermediate
6472f4d763aaSMatthew G. Knepley 
6473bb7acecfSBarry Smith   Note:
6474bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6475bb7acecfSBarry 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
6476bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6477bb7acecfSBarry Smith 
64781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6479f4d763aaSMatthew G. Knepley @*/
6480d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6481d71ae5a4SJacob Faibussowitsch {
6482c26acbdeSMatthew G. Knepley   PetscSection section;
6483eb9d3e4dSMatthew G. Knepley   IS           perm;
6484eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
648514f150ffSMatthew G. Knepley 
648614f150ffSMatthew G. Knepley   PetscFunctionBegin;
648714f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64884f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64899566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64909566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6491eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6492eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6493462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6494eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6495c26acbdeSMatthew G. Knepley     *odm = dm;
64963ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6497c26acbdeSMatthew G. Knepley   }
649814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6499c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
650014f150ffSMatthew G. Knepley     PetscSF      sf;
6501eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
650214f150ffSMatthew G. Knepley 
65039566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
65049566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
65059566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
65069566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
65079566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
65089566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6509eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
65109566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
65119566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
651214f150ffSMatthew G. Knepley   }
651314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
65143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
651514f150ffSMatthew G. Knepley }
6516f4d763aaSMatthew G. Knepley 
6517f4d763aaSMatthew G. Knepley /*@
6518cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6519f4d763aaSMatthew G. Knepley 
6520f4d763aaSMatthew G. Knepley   Input Parameter:
6521bb7acecfSBarry Smith . dm - The original `DM`
6522f4d763aaSMatthew G. Knepley 
6523cdb7a50dSMatthew G. Knepley   Output Parameters:
6524cdb7a50dSMatthew G. Knepley + num - The output sequence number
6525cdb7a50dSMatthew G. Knepley - val - The output sequence value
6526f4d763aaSMatthew G. Knepley 
6527f4d763aaSMatthew G. Knepley   Level: intermediate
6528f4d763aaSMatthew G. Knepley 
6529bb7acecfSBarry Smith   Note:
6530bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6531bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6532bb7acecfSBarry Smith 
653373ff1848SBarry Smith   Developer Note:
6534bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6535bb7acecfSBarry Smith   not directly related to the `DM`.
6536f4d763aaSMatthew G. Knepley 
65371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6538f4d763aaSMatthew G. Knepley @*/
6539d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6540d71ae5a4SJacob Faibussowitsch {
6541f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6542f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65439371c9d4SSatish Balay   if (num) {
65444f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65459371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65469371c9d4SSatish Balay   }
65479371c9d4SSatish Balay   if (val) {
65484f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65499371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65509371c9d4SSatish Balay   }
65513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6552f4d763aaSMatthew G. Knepley }
6553f4d763aaSMatthew G. Knepley 
6554f4d763aaSMatthew G. Knepley /*@
6555cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6556f4d763aaSMatthew G. Knepley 
6557f4d763aaSMatthew G. Knepley   Input Parameters:
6558bb7acecfSBarry Smith + dm  - The original `DM`
6559cdb7a50dSMatthew G. Knepley . num - The output sequence number
6560cdb7a50dSMatthew G. Knepley - val - The output sequence value
6561f4d763aaSMatthew G. Knepley 
6562f4d763aaSMatthew G. Knepley   Level: intermediate
6563f4d763aaSMatthew G. Knepley 
6564bb7acecfSBarry Smith   Note:
6565bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6566bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6567f4d763aaSMatthew G. Knepley 
65681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6569f4d763aaSMatthew G. Knepley @*/
6570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6571d71ae5a4SJacob Faibussowitsch {
6572f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6573f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6574f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6575cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6577cdb7a50dSMatthew G. Knepley }
6578cdb7a50dSMatthew G. Knepley 
65795d83a8b1SBarry Smith /*@
6580bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6581cdb7a50dSMatthew G. Knepley 
6582cdb7a50dSMatthew G. Knepley   Input Parameters:
6583bb7acecfSBarry Smith + dm     - The original `DM`
6584b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6585cdb7a50dSMatthew G. Knepley . name   - The sequence name
6586cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6587cdb7a50dSMatthew G. Knepley 
6588cdb7a50dSMatthew G. Knepley   Output Parameter:
6589cdb7a50dSMatthew G. Knepley . val - The output sequence value
6590cdb7a50dSMatthew G. Knepley 
6591cdb7a50dSMatthew G. Knepley   Level: intermediate
6592cdb7a50dSMatthew G. Knepley 
6593bb7acecfSBarry Smith   Note:
6594bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6595bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6596bb7acecfSBarry Smith 
659773ff1848SBarry Smith   Developer Note:
6598bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6599cdb7a50dSMatthew G. Knepley 
66001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6601cdb7a50dSMatthew G. Knepley @*/
6602b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6603d71ae5a4SJacob Faibussowitsch {
6604cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6605cdb7a50dSMatthew G. Knepley 
6606cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6607cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6608cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6609b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
66104f572ea9SToby Isaac   PetscAssertPointer(val, 5);
66119566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6612cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6613cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6614cdb7a50dSMatthew G. Knepley     PetscScalar value;
6615cdb7a50dSMatthew G. Knepley 
66169566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
66174aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6618cdb7a50dSMatthew G. Knepley #endif
6619cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
66203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6621f4d763aaSMatthew G. Knepley }
66228e4ac7eaSMatthew G. Knepley 
66238e4ac7eaSMatthew G. Knepley /*@
6624b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6625b2033f5dSMatthew G. Knepley 
6626b2033f5dSMatthew G. Knepley   Input Parameters:
6627b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6628b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6629b2033f5dSMatthew G. Knepley - name   - The sequence name
6630b2033f5dSMatthew G. Knepley 
6631b2033f5dSMatthew G. Knepley   Output Parameter:
6632b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6633b2033f5dSMatthew G. Knepley 
6634b2033f5dSMatthew G. Knepley   Level: intermediate
6635b2033f5dSMatthew G. Knepley 
6636b2033f5dSMatthew G. Knepley   Note:
6637b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6638b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6639b2033f5dSMatthew G. Knepley 
6640b2033f5dSMatthew G. Knepley   Developer Note:
6641b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6642b2033f5dSMatthew G. Knepley 
6643b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6644b2033f5dSMatthew G. Knepley @*/
6645b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6646b2033f5dSMatthew G. Knepley {
6647b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6648b2033f5dSMatthew G. Knepley 
6649b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6650b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6651b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6652b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6653b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6654b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6655b2033f5dSMatthew G. Knepley   if (ishdf5) {
6656b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6657b2033f5dSMatthew G. Knepley     PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6658b2033f5dSMatthew G. Knepley #endif
6659b2033f5dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6660b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6661b2033f5dSMatthew G. Knepley }
6662b2033f5dSMatthew G. Knepley 
6663b2033f5dSMatthew G. Knepley /*@
6664bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66658e4ac7eaSMatthew G. Knepley 
666620f4b53cSBarry Smith   Not Collective
66678e4ac7eaSMatthew G. Knepley 
66688e4ac7eaSMatthew G. Knepley   Input Parameter:
6669bb7acecfSBarry Smith . dm - The `DM`
66708e4ac7eaSMatthew G. Knepley 
66718e4ac7eaSMatthew G. Knepley   Output Parameter:
6672bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66738e4ac7eaSMatthew G. Knepley 
66748e4ac7eaSMatthew G. Knepley   Level: beginner
66758e4ac7eaSMatthew G. Knepley 
66761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66778e4ac7eaSMatthew G. Knepley @*/
6678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6679d71ae5a4SJacob Faibussowitsch {
66808e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66818e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66824f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66838e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66858e4ac7eaSMatthew G. Knepley }
66868e4ac7eaSMatthew G. Knepley 
66878e4ac7eaSMatthew G. Knepley /*@
6688bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66898e4ac7eaSMatthew G. Knepley 
669020f4b53cSBarry Smith   Collective
66918e4ac7eaSMatthew G. Knepley 
66928e4ac7eaSMatthew G. Knepley   Input Parameters:
6693bb7acecfSBarry Smith + dm         - The `DM`
6694bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66958e4ac7eaSMatthew G. Knepley 
669673ff1848SBarry Smith   Level: beginner
669773ff1848SBarry Smith 
6698bb7acecfSBarry Smith   Note:
6699bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
67005d3b26e6SMatthew G. Knepley 
67011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
67028e4ac7eaSMatthew G. Knepley @*/
6703d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6704d71ae5a4SJacob Faibussowitsch {
67058e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
67068e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67078833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
67088e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
67093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67108e4ac7eaSMatthew G. Knepley }
6711c58f1c22SToby Isaac 
6712cc4c1da9SBarry Smith /*@
6713bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac   Not Collective
6716c58f1c22SToby Isaac 
6717c58f1c22SToby Isaac   Input Parameters:
6718bb7acecfSBarry Smith + dm   - The `DM` object
6719c58f1c22SToby Isaac - name - The label name
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac   Level: intermediate
6722c58f1c22SToby Isaac 
67231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6724c58f1c22SToby Isaac @*/
6725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6726d71ae5a4SJacob Faibussowitsch {
67275d80c0bfSVaclav Hapla   PetscBool flg;
67285d80c0bfSVaclav Hapla   DMLabel   label;
6729c58f1c22SToby Isaac 
6730c58f1c22SToby Isaac   PetscFunctionBegin;
6731c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67324f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67339566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6734c58f1c22SToby Isaac   if (!flg) {
67359566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67369566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67379566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6738c58f1c22SToby Isaac   }
67393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6740c58f1c22SToby Isaac }
6741c58f1c22SToby Isaac 
6742cc4c1da9SBarry Smith /*@
6743bb7acecfSBarry 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.
67440fdc7489SMatthew Knepley 
67450fdc7489SMatthew Knepley   Not Collective
67460fdc7489SMatthew Knepley 
67470fdc7489SMatthew Knepley   Input Parameters:
6748bb7acecfSBarry Smith + dm   - The `DM` object
67490fdc7489SMatthew Knepley . l    - The index for the label
67500fdc7489SMatthew Knepley - name - The label name
67510fdc7489SMatthew Knepley 
67520fdc7489SMatthew Knepley   Level: intermediate
67530fdc7489SMatthew Knepley 
67541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
67550fdc7489SMatthew Knepley @*/
6756d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6757d71ae5a4SJacob Faibussowitsch {
67580fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
67590fdc7489SMatthew Knepley   DMLabel     label;
67600fdc7489SMatthew Knepley   PetscInt    Nl, m;
67610fdc7489SMatthew Knepley   PetscBool   flg, match;
67620fdc7489SMatthew Knepley   const char *lname;
67630fdc7489SMatthew Knepley 
67640fdc7489SMatthew Knepley   PetscFunctionBegin;
67650fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67664f572ea9SToby Isaac   PetscAssertPointer(name, 3);
67679566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67680fdc7489SMatthew Knepley   if (!flg) {
67699566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67709566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67719566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67720fdc7489SMatthew Knepley   }
67739566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
677463a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67750fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67769566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67779566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67780fdc7489SMatthew Knepley     if (match) break;
67790fdc7489SMatthew Knepley   }
67803ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67810fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67820fdc7489SMatthew Knepley   else prev->next = orig->next;
67830fdc7489SMatthew Knepley   if (!l) {
67840fdc7489SMatthew Knepley     orig->next = dm->labels;
67850fdc7489SMatthew Knepley     dm->labels = orig;
67860fdc7489SMatthew Knepley   } else {
6787fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67880fdc7489SMatthew Knepley     orig->next = prev->next;
67890fdc7489SMatthew Knepley     prev->next = orig;
67900fdc7489SMatthew Knepley   }
67913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67920fdc7489SMatthew Knepley }
67930fdc7489SMatthew Knepley 
6794cc4c1da9SBarry Smith /*@
6795bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Not Collective
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   Input Parameters:
6800bb7acecfSBarry Smith + dm    - The `DM` object
6801c58f1c22SToby Isaac . name  - The label name
6802c58f1c22SToby Isaac - point - The mesh point
6803c58f1c22SToby Isaac 
6804c58f1c22SToby Isaac   Output Parameter:
6805c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac   Level: beginner
6808c58f1c22SToby Isaac 
68091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6810c58f1c22SToby Isaac @*/
6811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6812d71ae5a4SJacob Faibussowitsch {
6813c58f1c22SToby Isaac   DMLabel label;
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   PetscFunctionBegin;
6816c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68174f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68197a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
68209566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
68213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6822c58f1c22SToby Isaac }
6823c58f1c22SToby Isaac 
6824cc4c1da9SBarry Smith /*@
6825bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6826c58f1c22SToby Isaac 
6827c58f1c22SToby Isaac   Not Collective
6828c58f1c22SToby Isaac 
6829c58f1c22SToby Isaac   Input Parameters:
6830bb7acecfSBarry Smith + dm    - The `DM` object
6831c58f1c22SToby Isaac . name  - The label name
6832c58f1c22SToby Isaac . point - The mesh point
6833c58f1c22SToby Isaac - value - The label value for this point
6834c58f1c22SToby Isaac 
6835c58f1c22SToby Isaac   Output Parameter:
6836c58f1c22SToby Isaac 
6837c58f1c22SToby Isaac   Level: beginner
6838c58f1c22SToby Isaac 
68391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6840c58f1c22SToby Isaac @*/
6841d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6842d71ae5a4SJacob Faibussowitsch {
6843c58f1c22SToby Isaac   DMLabel label;
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   PetscFunctionBegin;
6846c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68474f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68489566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6849c58f1c22SToby Isaac   if (!label) {
68509566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68519566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6852c58f1c22SToby Isaac   }
68539566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
68543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6855c58f1c22SToby Isaac }
6856c58f1c22SToby Isaac 
6857cc4c1da9SBarry Smith /*@
6858bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   Not Collective
6861c58f1c22SToby Isaac 
6862c58f1c22SToby Isaac   Input Parameters:
6863bb7acecfSBarry Smith + dm    - The `DM` object
6864c58f1c22SToby Isaac . name  - The label name
6865c58f1c22SToby Isaac . point - The mesh point
6866c58f1c22SToby Isaac - value - The label value for this point
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Level: beginner
6869c58f1c22SToby Isaac 
68701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6871c58f1c22SToby Isaac @*/
6872d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6873d71ae5a4SJacob Faibussowitsch {
6874c58f1c22SToby Isaac   DMLabel label;
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac   PetscFunctionBegin;
6877c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68784f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68799566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68803ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68819566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6883c58f1c22SToby Isaac }
6884c58f1c22SToby Isaac 
6885cc4c1da9SBarry Smith /*@
6886bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6887c58f1c22SToby Isaac 
6888c58f1c22SToby Isaac   Not Collective
6889c58f1c22SToby Isaac 
6890c58f1c22SToby Isaac   Input Parameters:
6891bb7acecfSBarry Smith + dm   - The `DM` object
6892c58f1c22SToby Isaac - name - The label name
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Output Parameter:
6895c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6896c58f1c22SToby Isaac 
6897c58f1c22SToby Isaac   Level: beginner
6898c58f1c22SToby Isaac 
689973ff1848SBarry Smith   Developer Note:
6900bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6901bb7acecfSBarry Smith 
69021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6903c58f1c22SToby Isaac @*/
6904d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6905d71ae5a4SJacob Faibussowitsch {
6906c58f1c22SToby Isaac   DMLabel label;
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   PetscFunctionBegin;
6909c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69104f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69114f572ea9SToby Isaac   PetscAssertPointer(size, 3);
69129566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6913c58f1c22SToby Isaac   *size = 0;
69143ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69159566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
69163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6917c58f1c22SToby Isaac }
6918c58f1c22SToby Isaac 
6919cc4c1da9SBarry Smith /*@
6920bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6921c58f1c22SToby Isaac 
6922c58f1c22SToby Isaac   Not Collective
6923c58f1c22SToby Isaac 
6924c58f1c22SToby Isaac   Input Parameters:
692560225df5SJacob Faibussowitsch + dm   - The `DM` object
6926c58f1c22SToby Isaac - name - The label name
6927c58f1c22SToby Isaac 
6928c58f1c22SToby Isaac   Output Parameter:
692920f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6930c58f1c22SToby Isaac 
6931c58f1c22SToby Isaac   Level: beginner
6932c58f1c22SToby Isaac 
69331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6934c58f1c22SToby Isaac @*/
6935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6936d71ae5a4SJacob Faibussowitsch {
6937c58f1c22SToby Isaac   DMLabel label;
6938c58f1c22SToby Isaac 
6939c58f1c22SToby Isaac   PetscFunctionBegin;
6940c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69414f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69424f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
69439566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6944c58f1c22SToby Isaac   *ids = NULL;
6945dab2e251SBlaise Bourdin   if (label) {
69469566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6947dab2e251SBlaise Bourdin   } else {
6948dab2e251SBlaise Bourdin     /* returning an empty IS */
69499566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6950dab2e251SBlaise Bourdin   }
69513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6952c58f1c22SToby Isaac }
6953c58f1c22SToby Isaac 
6954cc4c1da9SBarry Smith /*@
6955c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6956c58f1c22SToby Isaac 
6957c58f1c22SToby Isaac   Not Collective
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Input Parameters:
6960bb7acecfSBarry Smith + dm    - The `DM` object
6961b6971eaeSBarry Smith . name  - The label name of the stratum
6962c58f1c22SToby Isaac - value - The stratum value
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   Output Parameter:
6965bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6966c58f1c22SToby Isaac 
6967c58f1c22SToby Isaac   Level: beginner
6968c58f1c22SToby Isaac 
69691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6970c58f1c22SToby Isaac @*/
6971d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6972d71ae5a4SJacob Faibussowitsch {
6973c58f1c22SToby Isaac   DMLabel label;
6974c58f1c22SToby Isaac 
6975c58f1c22SToby Isaac   PetscFunctionBegin;
6976c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69774f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69784f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69799566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6980c58f1c22SToby Isaac   *size = 0;
69813ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69829566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6984c58f1c22SToby Isaac }
6985c58f1c22SToby Isaac 
6986cc4c1da9SBarry Smith /*@
6987c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   Not Collective
6990c58f1c22SToby Isaac 
6991c58f1c22SToby Isaac   Input Parameters:
6992bb7acecfSBarry Smith + dm    - The `DM` object
6993c58f1c22SToby Isaac . name  - The label name
6994c58f1c22SToby Isaac - value - The stratum value
6995c58f1c22SToby Isaac 
6996c58f1c22SToby Isaac   Output Parameter:
699720f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6998c58f1c22SToby Isaac 
6999c58f1c22SToby Isaac   Level: beginner
7000c58f1c22SToby Isaac 
70011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
7002c58f1c22SToby Isaac @*/
7003d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7004d71ae5a4SJacob Faibussowitsch {
7005c58f1c22SToby Isaac   DMLabel label;
7006c58f1c22SToby Isaac 
7007c58f1c22SToby Isaac   PetscFunctionBegin;
7008c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70094f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70104f572ea9SToby Isaac   PetscAssertPointer(points, 4);
70119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7012c58f1c22SToby Isaac   *points = NULL;
70133ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
70153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7016c58f1c22SToby Isaac }
7017c58f1c22SToby Isaac 
7018cc4c1da9SBarry Smith /*@
70199044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
70204de306b1SToby Isaac 
70214de306b1SToby Isaac   Not Collective
70224de306b1SToby Isaac 
70234de306b1SToby Isaac   Input Parameters:
7024bb7acecfSBarry Smith + dm     - The `DM` object
70254de306b1SToby Isaac . name   - The label name
70264de306b1SToby Isaac . value  - The stratum value
70274de306b1SToby Isaac - points - The stratum points
70284de306b1SToby Isaac 
70294de306b1SToby Isaac   Level: beginner
70304de306b1SToby Isaac 
70311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
70324de306b1SToby Isaac @*/
7033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
7034d71ae5a4SJacob Faibussowitsch {
70354de306b1SToby Isaac   DMLabel label;
70364de306b1SToby Isaac 
70374de306b1SToby Isaac   PetscFunctionBegin;
70384de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70394f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7040292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
70419566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70423ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70439566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
70443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70454de306b1SToby Isaac }
70464de306b1SToby Isaac 
7047cc4c1da9SBarry Smith /*@
7048bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7049c58f1c22SToby Isaac 
7050c58f1c22SToby Isaac   Not Collective
7051c58f1c22SToby Isaac 
7052c58f1c22SToby Isaac   Input Parameters:
7053bb7acecfSBarry Smith + dm    - The `DM` object
7054c58f1c22SToby Isaac . name  - The label name
7055c58f1c22SToby Isaac - value - The label value for this point
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Output Parameter:
7058c58f1c22SToby Isaac 
7059c58f1c22SToby Isaac   Level: beginner
7060c58f1c22SToby Isaac 
70611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7062c58f1c22SToby Isaac @*/
7063d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7064d71ae5a4SJacob Faibussowitsch {
7065c58f1c22SToby Isaac   DMLabel label;
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac   PetscFunctionBegin;
7068c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70694f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70709566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70713ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70729566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7074c58f1c22SToby Isaac }
7075c58f1c22SToby Isaac 
7076c58f1c22SToby Isaac /*@
7077bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7078c58f1c22SToby Isaac 
7079c58f1c22SToby Isaac   Not Collective
7080c58f1c22SToby Isaac 
7081c58f1c22SToby Isaac   Input Parameter:
7082bb7acecfSBarry Smith . dm - The `DM` object
7083c58f1c22SToby Isaac 
7084c58f1c22SToby Isaac   Output Parameter:
7085c58f1c22SToby Isaac . numLabels - the number of Labels
7086c58f1c22SToby Isaac 
7087c58f1c22SToby Isaac   Level: intermediate
7088c58f1c22SToby Isaac 
70891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7090c58f1c22SToby Isaac @*/
7091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7092d71ae5a4SJacob Faibussowitsch {
70935d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7094c58f1c22SToby Isaac   PetscInt    n    = 0;
7095c58f1c22SToby Isaac 
7096c58f1c22SToby Isaac   PetscFunctionBegin;
7097c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70984f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70999371c9d4SSatish Balay   while (next) {
71009371c9d4SSatish Balay     ++n;
71019371c9d4SSatish Balay     next = next->next;
71029371c9d4SSatish Balay   }
7103c58f1c22SToby Isaac   *numLabels = n;
71043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7105c58f1c22SToby Isaac }
7106c58f1c22SToby Isaac 
7107cc4c1da9SBarry Smith /*@
7108c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7109c58f1c22SToby Isaac 
7110c58f1c22SToby Isaac   Not Collective
7111c58f1c22SToby Isaac 
7112c58f1c22SToby Isaac   Input Parameters:
7113bb7acecfSBarry Smith + dm - The `DM` object
7114c58f1c22SToby Isaac - n  - the label number
7115c58f1c22SToby Isaac 
7116c58f1c22SToby Isaac   Output Parameter:
7117c58f1c22SToby Isaac . name - the label name
7118c58f1c22SToby Isaac 
7119c58f1c22SToby Isaac   Level: intermediate
7120c58f1c22SToby Isaac 
712173ff1848SBarry Smith   Developer Note:
7122bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7123bb7acecfSBarry Smith 
71241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7125c58f1c22SToby Isaac @*/
7126cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7127d71ae5a4SJacob Faibussowitsch {
71285d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7129c58f1c22SToby Isaac   PetscInt    l    = 0;
7130c58f1c22SToby Isaac 
7131c58f1c22SToby Isaac   PetscFunctionBegin;
7132c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71334f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7134c58f1c22SToby Isaac   while (next) {
7135c58f1c22SToby Isaac     if (l == n) {
71369566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
71373ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7138c58f1c22SToby Isaac     }
7139c58f1c22SToby Isaac     ++l;
7140c58f1c22SToby Isaac     next = next->next;
7141c58f1c22SToby Isaac   }
714263a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7143c58f1c22SToby Isaac }
7144c58f1c22SToby Isaac 
7145cc4c1da9SBarry Smith /*@
7146bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7147c58f1c22SToby Isaac 
7148c58f1c22SToby Isaac   Not Collective
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac   Input Parameters:
7151bb7acecfSBarry Smith + dm   - The `DM` object
7152c58f1c22SToby Isaac - name - The label name
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Output Parameter:
7155bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7156c58f1c22SToby Isaac 
7157c58f1c22SToby Isaac   Level: intermediate
7158c58f1c22SToby Isaac 
71591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7160c58f1c22SToby Isaac @*/
7161d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7162d71ae5a4SJacob Faibussowitsch {
71635d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
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(hasLabel, 3);
7170c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7171c58f1c22SToby Isaac   while (next) {
71729566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71739566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7174c58f1c22SToby Isaac     if (*hasLabel) break;
7175c58f1c22SToby Isaac     next = next->next;
7176c58f1c22SToby Isaac   }
71773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7178c58f1c22SToby Isaac }
7179c58f1c22SToby Isaac 
7180a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7181cc4c1da9SBarry Smith /*@
718220f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7183c58f1c22SToby Isaac 
7184c58f1c22SToby Isaac   Not Collective
7185c58f1c22SToby Isaac 
7186c58f1c22SToby Isaac   Input Parameters:
7187bb7acecfSBarry Smith + dm   - The `DM` object
7188c58f1c22SToby Isaac - name - The label name
7189c58f1c22SToby Isaac 
7190c58f1c22SToby Isaac   Output Parameter:
719120f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7192c58f1c22SToby Isaac 
7193bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7194bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7195bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7196bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7197bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7198bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7199bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72006d7c9049SMatthew G. Knepley 
7201c58f1c22SToby Isaac   Level: intermediate
7202c58f1c22SToby Isaac 
720360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7204c58f1c22SToby Isaac @*/
7205d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7206d71ae5a4SJacob Faibussowitsch {
72075d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7208c58f1c22SToby Isaac   PetscBool   hasLabel;
7209d67d17b1SMatthew G. Knepley   const char *lname;
7210c58f1c22SToby Isaac 
7211c58f1c22SToby Isaac   PetscFunctionBegin;
7212c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72134f572ea9SToby Isaac   PetscAssertPointer(name, 2);
72144f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7215c58f1c22SToby Isaac   *label = NULL;
7216c58f1c22SToby Isaac   while (next) {
72179566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72189566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7219c58f1c22SToby Isaac     if (hasLabel) {
7220c58f1c22SToby Isaac       *label = next->label;
7221c58f1c22SToby Isaac       break;
7222c58f1c22SToby Isaac     }
7223c58f1c22SToby Isaac     next = next->next;
7224c58f1c22SToby Isaac   }
72253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7226c58f1c22SToby Isaac }
7227c58f1c22SToby Isaac 
7228cc4c1da9SBarry Smith /*@
7229bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7230c58f1c22SToby Isaac 
7231c58f1c22SToby Isaac   Not Collective
7232c58f1c22SToby Isaac 
7233c58f1c22SToby Isaac   Input Parameters:
7234bb7acecfSBarry Smith + dm - The `DM` object
7235c58f1c22SToby Isaac - n  - the label number
7236c58f1c22SToby Isaac 
7237c58f1c22SToby Isaac   Output Parameter:
7238c58f1c22SToby Isaac . label - the label
7239c58f1c22SToby Isaac 
7240c58f1c22SToby Isaac   Level: intermediate
7241c58f1c22SToby Isaac 
72421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7243c58f1c22SToby Isaac @*/
7244d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7245d71ae5a4SJacob Faibussowitsch {
72465d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7247c58f1c22SToby Isaac   PetscInt    l    = 0;
7248c58f1c22SToby Isaac 
7249c58f1c22SToby Isaac   PetscFunctionBegin;
7250c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72514f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7252c58f1c22SToby Isaac   while (next) {
7253c58f1c22SToby Isaac     if (l == n) {
7254c58f1c22SToby Isaac       *label = next->label;
72553ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7256c58f1c22SToby Isaac     }
7257c58f1c22SToby Isaac     ++l;
7258c58f1c22SToby Isaac     next = next->next;
7259c58f1c22SToby Isaac   }
726063a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7261c58f1c22SToby Isaac }
7262c58f1c22SToby Isaac 
7263cc4c1da9SBarry Smith /*@
7264bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7265c58f1c22SToby Isaac 
7266c58f1c22SToby Isaac   Not Collective
7267c58f1c22SToby Isaac 
7268c58f1c22SToby Isaac   Input Parameters:
7269bb7acecfSBarry Smith + dm    - The `DM` object
7270bb7acecfSBarry Smith - label - The `DMLabel`
7271c58f1c22SToby Isaac 
7272c58f1c22SToby Isaac   Level: developer
7273c58f1c22SToby Isaac 
727460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7275c58f1c22SToby Isaac @*/
7276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7277d71ae5a4SJacob Faibussowitsch {
72785d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7279c58f1c22SToby Isaac   PetscBool   hasLabel;
7280d67d17b1SMatthew G. Knepley   const char *lname;
72815d80c0bfSVaclav Hapla   PetscBool   flg;
7282c58f1c22SToby Isaac 
7283c58f1c22SToby Isaac   PetscFunctionBegin;
7284c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72859566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72869566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72877a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72889566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7289c58f1c22SToby Isaac   tmpLabel->label  = label;
7290c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72915d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72925d80c0bfSVaclav Hapla   *p = tmpLabel;
72939566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
72949566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
72955d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
72969566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7297ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7299c58f1c22SToby Isaac }
7300c58f1c22SToby Isaac 
7301a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7302cc4c1da9SBarry Smith /*@
73034a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
73044a7ee7d0SMatthew G. Knepley 
73054a7ee7d0SMatthew G. Knepley   Not Collective
73064a7ee7d0SMatthew G. Knepley 
73074a7ee7d0SMatthew G. Knepley   Input Parameters:
7308bb7acecfSBarry Smith + dm    - The `DM` object
7309bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
73104a7ee7d0SMatthew G. Knepley 
7311bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7312bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7313bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7314bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7315bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7316bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7317bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
73184a7ee7d0SMatthew G. Knepley 
73194a7ee7d0SMatthew G. Knepley   Level: intermediate
73204a7ee7d0SMatthew G. Knepley 
73211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
73224a7ee7d0SMatthew G. Knepley @*/
7323d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7324d71ae5a4SJacob Faibussowitsch {
73254a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
73264a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
73274a7ee7d0SMatthew G. Knepley   const char *name, *lname;
73284a7ee7d0SMatthew G. Knepley 
73294a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
73304a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73314a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
73329566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
73334a7ee7d0SMatthew G. Knepley   while (next) {
73349566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73359566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
73364a7ee7d0SMatthew G. Knepley     if (hasLabel) {
73379566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
73389566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
73394a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
73409566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
73414a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
73429566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
73434a7ee7d0SMatthew G. Knepley       next->label = label;
73444a7ee7d0SMatthew G. Knepley       break;
73454a7ee7d0SMatthew G. Knepley     }
73464a7ee7d0SMatthew G. Knepley     next = next->next;
73474a7ee7d0SMatthew G. Knepley   }
73483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73494a7ee7d0SMatthew G. Knepley }
73504a7ee7d0SMatthew G. Knepley 
7351cc4c1da9SBarry Smith /*@
7352bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7353c58f1c22SToby Isaac 
7354c58f1c22SToby Isaac   Not Collective
7355c58f1c22SToby Isaac 
7356c58f1c22SToby Isaac   Input Parameters:
7357bb7acecfSBarry Smith + dm   - The `DM` object
7358c58f1c22SToby Isaac - name - The label name
7359c58f1c22SToby Isaac 
7360c58f1c22SToby Isaac   Output Parameter:
736120f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7362bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7363c58f1c22SToby Isaac 
7364c58f1c22SToby Isaac   Level: developer
7365c58f1c22SToby Isaac 
73661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7367c58f1c22SToby Isaac @*/
7368d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7369d71ae5a4SJacob Faibussowitsch {
737095d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7371c58f1c22SToby Isaac   PetscBool   hasLabel;
7372d67d17b1SMatthew G. Knepley   const char *lname;
7373c58f1c22SToby Isaac 
7374c58f1c22SToby Isaac   PetscFunctionBegin;
7375c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73764f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7377e5472504SVaclav Hapla   if (label) {
73784f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7379c58f1c22SToby Isaac     *label = NULL;
7380e5472504SVaclav Hapla   }
73815d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73829566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73839566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7384c58f1c22SToby Isaac     if (hasLabel) {
738595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73869566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
738795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73889566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7389ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
739095d578d6SVaclav Hapla       if (label) *label = link->label;
73919566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73929566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7393c58f1c22SToby Isaac       break;
7394c58f1c22SToby Isaac     }
7395c58f1c22SToby Isaac   }
73963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7397c58f1c22SToby Isaac }
7398c58f1c22SToby Isaac 
7399306894acSVaclav Hapla /*@
7400bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7401306894acSVaclav Hapla 
7402306894acSVaclav Hapla   Not Collective
7403306894acSVaclav Hapla 
7404306894acSVaclav Hapla   Input Parameters:
7405bb7acecfSBarry Smith + dm           - The `DM` object
7406bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
740720f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7408306894acSVaclav Hapla 
7409306894acSVaclav Hapla   Level: developer
7410306894acSVaclav Hapla 
7411bb7acecfSBarry Smith   Note:
7412306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7413bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7414306894acSVaclav Hapla   *label nullified.
7415306894acSVaclav Hapla 
74161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7417306894acSVaclav Hapla @*/
7418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7419d71ae5a4SJacob Faibussowitsch {
742043e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7421306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7422306894acSVaclav Hapla 
7423306894acSVaclav Hapla   PetscFunctionBegin;
7424306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74254f572ea9SToby Isaac   PetscAssertPointer(label, 2);
74263ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7427306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7428306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
74295d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
743043e45a93SVaclav Hapla     if (*label == link->label) {
7431306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
743243e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7433306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7434ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
743543e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
74369566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
74379566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7438306894acSVaclav Hapla       break;
7439306894acSVaclav Hapla     }
7440306894acSVaclav Hapla   }
74417a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
74423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7443306894acSVaclav Hapla }
7444306894acSVaclav Hapla 
7445cc4c1da9SBarry Smith /*@
7446c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7447c58f1c22SToby Isaac 
7448c58f1c22SToby Isaac   Not Collective
7449c58f1c22SToby Isaac 
7450c58f1c22SToby Isaac   Input Parameters:
7451bb7acecfSBarry Smith + dm   - The `DM` object
7452c58f1c22SToby Isaac - name - The label name
7453c58f1c22SToby Isaac 
7454c58f1c22SToby Isaac   Output Parameter:
7455c58f1c22SToby Isaac . output - The flag for output
7456c58f1c22SToby Isaac 
7457c58f1c22SToby Isaac   Level: developer
7458c58f1c22SToby Isaac 
74591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7460c58f1c22SToby Isaac @*/
7461d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7462d71ae5a4SJacob Faibussowitsch {
74635d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7464d67d17b1SMatthew G. Knepley   const char *lname;
7465c58f1c22SToby Isaac 
7466c58f1c22SToby Isaac   PetscFunctionBegin;
7467c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74684f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74694f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7470c58f1c22SToby Isaac   while (next) {
7471c58f1c22SToby Isaac     PetscBool flg;
7472c58f1c22SToby Isaac 
74739566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74749566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74759371c9d4SSatish Balay     if (flg) {
74769371c9d4SSatish Balay       *output = next->output;
74773ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74789371c9d4SSatish Balay     }
7479c58f1c22SToby Isaac     next = next->next;
7480c58f1c22SToby Isaac   }
748198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7482c58f1c22SToby Isaac }
7483c58f1c22SToby Isaac 
7484cc4c1da9SBarry Smith /*@
7485bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7486c58f1c22SToby Isaac 
7487c58f1c22SToby Isaac   Not Collective
7488c58f1c22SToby Isaac 
7489c58f1c22SToby Isaac   Input Parameters:
7490bb7acecfSBarry Smith + dm     - The `DM` object
7491c58f1c22SToby Isaac . name   - The label name
7492bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7493c58f1c22SToby Isaac 
7494c58f1c22SToby Isaac   Level: developer
7495c58f1c22SToby Isaac 
74961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7497c58f1c22SToby Isaac @*/
7498d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7499d71ae5a4SJacob Faibussowitsch {
75005d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7501d67d17b1SMatthew G. Knepley   const char *lname;
7502c58f1c22SToby Isaac 
7503c58f1c22SToby Isaac   PetscFunctionBegin;
7504c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75054f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7506c58f1c22SToby Isaac   while (next) {
7507c58f1c22SToby Isaac     PetscBool flg;
7508c58f1c22SToby Isaac 
75099566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
75109566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
75119371c9d4SSatish Balay     if (flg) {
75129371c9d4SSatish Balay       next->output = output;
75133ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
75149371c9d4SSatish Balay     }
7515c58f1c22SToby Isaac     next = next->next;
7516c58f1c22SToby Isaac   }
751798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7518c58f1c22SToby Isaac }
7519c58f1c22SToby Isaac 
7520c58f1c22SToby Isaac /*@
7521bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7522c58f1c22SToby Isaac 
752320f4b53cSBarry Smith   Collective
7524c58f1c22SToby Isaac 
7525d8d19677SJose E. Roman   Input Parameters:
7526bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7527bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7528bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7529bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7530bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7531c58f1c22SToby Isaac 
7532c58f1c22SToby Isaac   Level: intermediate
7533c58f1c22SToby Isaac 
7534bb7acecfSBarry Smith   Note:
75352cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7536c58f1c22SToby Isaac 
75371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7538c58f1c22SToby Isaac @*/
7539d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7540d71ae5a4SJacob Faibussowitsch {
75412cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7542c58f1c22SToby Isaac   const char *name;
7543c58f1c22SToby Isaac   PetscBool   flg;
75445d80c0bfSVaclav Hapla   DMLabelLink link;
7545c58f1c22SToby Isaac 
75465d80c0bfSVaclav Hapla   PetscFunctionBegin;
75475d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75485d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75495d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75505d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75517a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
75523ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
75535d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
75545d80c0bfSVaclav Hapla     label = link->label;
75559566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
75565d80c0bfSVaclav Hapla     if (!all) {
75579566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7558c58f1c22SToby Isaac       if (flg) continue;
75599566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
75607d5acc75SStefano Zampini       if (flg) continue;
75619566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7562ba2698f1SMatthew G. Knepley       if (flg) continue;
75635d80c0bfSVaclav Hapla     }
75649566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
75652cbb9b06SVaclav Hapla     if (labelOld) {
75662cbb9b06SVaclav Hapla       switch (emode) {
7567d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7568d71ae5a4SJacob Faibussowitsch         continue;
7569d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7570d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7571d71ae5a4SJacob Faibussowitsch         break;
7572d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7573d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7574d71ae5a4SJacob Faibussowitsch       default:
7575d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75762cbb9b06SVaclav Hapla       }
75772cbb9b06SVaclav Hapla     }
75785d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75799566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75805d80c0bfSVaclav Hapla     } else {
75815d80c0bfSVaclav Hapla       labelNew = label;
75825d80c0bfSVaclav Hapla     }
75839566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75849566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7585c58f1c22SToby Isaac   }
75863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7587c58f1c22SToby Isaac }
7588461a15a0SLisandro Dalcin 
7589609dae6eSVaclav Hapla /*@C
7590b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7591609dae6eSVaclav Hapla 
759220f4b53cSBarry Smith   Collective; No Fortran Support
7593609dae6eSVaclav Hapla 
7594609dae6eSVaclav Hapla   Input Parameters:
7595bb7acecfSBarry Smith + dm0 - First `DM` object
7596bb7acecfSBarry Smith - dm1 - Second `DM` object
7597609dae6eSVaclav Hapla 
7598a4e35b19SJacob Faibussowitsch   Output Parameters:
75995efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
760020f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7601609dae6eSVaclav Hapla 
7602609dae6eSVaclav Hapla   Level: intermediate
7603609dae6eSVaclav Hapla 
7604609dae6eSVaclav Hapla   Notes:
7605bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7606bb7acecfSBarry Smith 
760720f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7608bb7acecfSBarry Smith 
760920f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7610609dae6eSVaclav Hapla 
76115efe38ccSVaclav Hapla   The output message is set independently on each rank.
7612bb7acecfSBarry Smith 
7613bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7614bb7acecfSBarry Smith 
761520f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7616bb7acecfSBarry Smith 
761720f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7618609dae6eSVaclav Hapla 
7619609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7620bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7621609dae6eSVaclav Hapla 
7622cc4c1da9SBarry Smith   Developer Note:
7623cc4c1da9SBarry Smith   Can automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7624cc4c1da9SBarry Smith 
76251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7626609dae6eSVaclav Hapla @*/
7627d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7628d71ae5a4SJacob Faibussowitsch {
76295efe38ccSVaclav Hapla   PetscInt    n, i;
7630609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
76315efe38ccSVaclav Hapla   PetscBool   eq;
7632609dae6eSVaclav Hapla   MPI_Comm    comm;
76335efe38ccSVaclav Hapla   PetscMPIInt rank;
7634609dae6eSVaclav Hapla 
7635609dae6eSVaclav Hapla   PetscFunctionBegin;
7636609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7637609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7638609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
76394f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
76404f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
76419566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
76429566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
76435efe38ccSVaclav Hapla   {
76445efe38ccSVaclav Hapla     PetscInt n1;
76455efe38ccSVaclav Hapla 
76469566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76479566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76485efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
764948a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7650462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
76515efe38ccSVaclav Hapla     if (!eq) goto finish;
76525efe38ccSVaclav Hapla   }
76535efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7654609dae6eSVaclav Hapla     DMLabel     l0, l1;
7655609dae6eSVaclav Hapla     const char *name;
7656609dae6eSVaclav Hapla     char       *msgInner;
7657609dae6eSVaclav Hapla 
7658609dae6eSVaclav Hapla     /* Ignore label order */
76599566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
76609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
76619566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7662609dae6eSVaclav Hapla     if (!l1) {
766363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
76645efe38ccSVaclav Hapla       eq = PETSC_FALSE;
76655efe38ccSVaclav Hapla       break;
7666609dae6eSVaclav Hapla     }
76679566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
76689566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
76699566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
76705efe38ccSVaclav Hapla     if (!eq) break;
7671609dae6eSVaclav Hapla   }
7672462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7673609dae6eSVaclav Hapla finish:
76745efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7675609dae6eSVaclav Hapla   if (message) {
7676609dae6eSVaclav Hapla     *message = NULL;
767748a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76785efe38ccSVaclav Hapla   } else {
767948a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76809566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76815efe38ccSVaclav Hapla   }
76825efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76835efe38ccSVaclav Hapla   if (equal) *equal = eq;
76847a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7686609dae6eSVaclav Hapla }
7687609dae6eSVaclav Hapla 
7688d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7689d71ae5a4SJacob Faibussowitsch {
7690461a15a0SLisandro Dalcin   PetscFunctionBegin;
76914f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7692461a15a0SLisandro Dalcin   if (!*label) {
76939566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
76949566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7695461a15a0SLisandro Dalcin   }
76969566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
76973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7698461a15a0SLisandro Dalcin }
7699461a15a0SLisandro Dalcin 
77000fdc7489SMatthew Knepley /*
77010fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
77020fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
77030fdc7489SMatthew Knepley   (label, id) pair in the DM.
77040fdc7489SMatthew Knepley 
77050fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
77060fdc7489SMatthew Knepley   each label.
77070fdc7489SMatthew Knepley */
7708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7709d71ae5a4SJacob Faibussowitsch {
77100fdc7489SMatthew Knepley   DMUniversalLabel ul;
77110fdc7489SMatthew Knepley   PetscBool       *active;
77120fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
77130fdc7489SMatthew Knepley 
77140fdc7489SMatthew Knepley   PetscFunctionBegin;
77159566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
77169566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
77179566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
77189566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
77190fdc7489SMatthew Knepley   ul->Nl = 0;
77200fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77210fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
77220fdc7489SMatthew Knepley     const char *name;
77230fdc7489SMatthew Knepley 
77249566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77259566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
77269566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
77270fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
77280fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
77290fdc7489SMatthew Knepley   }
77309566063dSJacob 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));
77310fdc7489SMatthew Knepley   ul->Nv = 0;
77320fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77330fdc7489SMatthew Knepley     DMLabel     label;
77340fdc7489SMatthew Knepley     PetscInt    nv;
77350fdc7489SMatthew Knepley     const char *name;
77360fdc7489SMatthew Knepley 
77370fdc7489SMatthew Knepley     if (!active[l]) continue;
77389566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77399566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77409566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77419566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
77420fdc7489SMatthew Knepley     ul->indices[m] = l;
77430fdc7489SMatthew Knepley     ul->Nv += nv;
77440fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77450fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77460fdc7489SMatthew Knepley     ++m;
77470fdc7489SMatthew Knepley   }
77480fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77490fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77500fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77510fdc7489SMatthew Knepley   }
77520fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77530fdc7489SMatthew Knepley     PetscInt b;
77540fdc7489SMatthew Knepley 
77550fdc7489SMatthew Knepley     ul->masks[l] = 0;
77560fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
77570fdc7489SMatthew Knepley   }
77589566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
77590fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77600fdc7489SMatthew Knepley     DMLabel         label;
77610fdc7489SMatthew Knepley     IS              valueIS;
77620fdc7489SMatthew Knepley     const PetscInt *varr;
77630fdc7489SMatthew Knepley     PetscInt        nv, v;
77640fdc7489SMatthew Knepley 
77650fdc7489SMatthew Knepley     if (!active[l]) continue;
77669566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77689566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
77699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7770ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77719566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77729566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77739566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77740fdc7489SMatthew Knepley     ++m;
77750fdc7489SMatthew Knepley   }
77769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77770fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77780fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77790fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77800fdc7489SMatthew Knepley 
77810fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77820fdc7489SMatthew Knepley       DMLabel  label;
77830649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77840fdc7489SMatthew Knepley 
77850fdc7489SMatthew Knepley       if (!active[l]) continue;
77869566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77879566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77889566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77899371c9d4SSatish Balay       if (val == defval) {
77909371c9d4SSatish Balay         ++m;
77919371c9d4SSatish Balay         continue;
77929371c9d4SSatish Balay       }
77930649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
77940fdc7489SMatthew Knepley       marked = PETSC_TRUE;
77959566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
779663a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
77970fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
77980fdc7489SMatthew Knepley       ++m;
77990fdc7489SMatthew Knepley     }
78009566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
78010fdc7489SMatthew Knepley   }
78029566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
78030fdc7489SMatthew Knepley   *universal = ul;
78043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78050fdc7489SMatthew Knepley }
78060fdc7489SMatthew Knepley 
7807d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7808d71ae5a4SJacob Faibussowitsch {
78090fdc7489SMatthew Knepley   PetscInt l;
78100fdc7489SMatthew Knepley 
78110fdc7489SMatthew Knepley   PetscFunctionBegin;
78129566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
78139566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
78149566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
78159566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
78169566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
78170fdc7489SMatthew Knepley   *universal = NULL;
78183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78190fdc7489SMatthew Knepley }
78200fdc7489SMatthew Knepley 
7821d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7822d71ae5a4SJacob Faibussowitsch {
78230fdc7489SMatthew Knepley   PetscFunctionBegin;
78244f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
78250fdc7489SMatthew Knepley   *ulabel = ul->label;
78263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78270fdc7489SMatthew Knepley }
78280fdc7489SMatthew Knepley 
7829d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7830d71ae5a4SJacob Faibussowitsch {
78310fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
78320fdc7489SMatthew Knepley 
78330fdc7489SMatthew Knepley   PetscFunctionBegin;
7834064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
78350fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
78369566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
78379566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
78380fdc7489SMatthew Knepley   }
78390fdc7489SMatthew Knepley   if (preserveOrder) {
78400fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
78410fdc7489SMatthew Knepley       const char *name;
78420fdc7489SMatthew Knepley       PetscBool   match;
78430fdc7489SMatthew Knepley 
78449566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78459566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
784663a3b9bcSJacob 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]);
78470fdc7489SMatthew Knepley     }
78480fdc7489SMatthew Knepley   }
78493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78500fdc7489SMatthew Knepley }
78510fdc7489SMatthew Knepley 
7852d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7853d71ae5a4SJacob Faibussowitsch {
78540fdc7489SMatthew Knepley   PetscInt l;
78550fdc7489SMatthew Knepley 
78560fdc7489SMatthew Knepley   PetscFunctionBegin;
78570fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78580fdc7489SMatthew Knepley     DMLabel  label;
78590fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
78600fdc7489SMatthew Knepley 
78610fdc7489SMatthew Knepley     if (lval) {
78629566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
78639566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
78649566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
78650fdc7489SMatthew Knepley     }
78660fdc7489SMatthew Knepley   }
78673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78680fdc7489SMatthew Knepley }
7869a8fb8f29SToby Isaac 
7870a8fb8f29SToby Isaac /*@
7871bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7872bb7acecfSBarry Smith 
787320f4b53cSBarry Smith   Not Collective
7874a8fb8f29SToby Isaac 
7875a8fb8f29SToby Isaac   Input Parameter:
7876bb7acecfSBarry Smith . dm - The `DM` object
7877a8fb8f29SToby Isaac 
7878a8fb8f29SToby Isaac   Output Parameter:
7879bb7acecfSBarry Smith . cdm - The coarse `DM`
7880a8fb8f29SToby Isaac 
7881a8fb8f29SToby Isaac   Level: intermediate
7882a8fb8f29SToby Isaac 
78831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7884a8fb8f29SToby Isaac @*/
7885d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7886d71ae5a4SJacob Faibussowitsch {
7887a8fb8f29SToby Isaac   PetscFunctionBegin;
7888a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78894f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7890a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7892a8fb8f29SToby Isaac }
7893a8fb8f29SToby Isaac 
7894a8fb8f29SToby Isaac /*@
7895bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7896a8fb8f29SToby Isaac 
7897a8fb8f29SToby Isaac   Input Parameters:
7898bb7acecfSBarry Smith + dm  - The `DM` object
7899bb7acecfSBarry Smith - cdm - The coarse `DM`
7900a8fb8f29SToby Isaac 
7901a8fb8f29SToby Isaac   Level: intermediate
7902a8fb8f29SToby Isaac 
7903bb7acecfSBarry Smith   Note:
7904bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7905bb7acecfSBarry Smith 
79061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7907a8fb8f29SToby Isaac @*/
7908d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7909d71ae5a4SJacob Faibussowitsch {
7910a8fb8f29SToby Isaac   PetscFunctionBegin;
7911a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7912a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
791389d734beSBarry Smith   if (dm == cdm) cdm = NULL;
79149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
79159566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7916a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
79173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7918a8fb8f29SToby Isaac }
7919a8fb8f29SToby Isaac 
792088bdff64SToby Isaac /*@
7921bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
792288bdff64SToby Isaac 
792388bdff64SToby Isaac   Input Parameter:
7924bb7acecfSBarry Smith . dm - The `DM` object
792588bdff64SToby Isaac 
792688bdff64SToby Isaac   Output Parameter:
7927bb7acecfSBarry Smith . fdm - The fine `DM`
792888bdff64SToby Isaac 
792988bdff64SToby Isaac   Level: intermediate
793088bdff64SToby Isaac 
79311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
793288bdff64SToby Isaac @*/
7933d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7934d71ae5a4SJacob Faibussowitsch {
793588bdff64SToby Isaac   PetscFunctionBegin;
793688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79374f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
793888bdff64SToby Isaac   *fdm = dm->fineMesh;
79393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
794088bdff64SToby Isaac }
794188bdff64SToby Isaac 
794288bdff64SToby Isaac /*@
7943bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
794488bdff64SToby Isaac 
794588bdff64SToby Isaac   Input Parameters:
7946bb7acecfSBarry Smith + dm  - The `DM` object
7947bb7acecfSBarry Smith - fdm - The fine `DM`
794888bdff64SToby Isaac 
7949bb7acecfSBarry Smith   Level: developer
795088bdff64SToby Isaac 
7951bb7acecfSBarry Smith   Note:
7952bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7953bb7acecfSBarry Smith 
79541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
795588bdff64SToby Isaac @*/
7956d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7957d71ae5a4SJacob Faibussowitsch {
795888bdff64SToby Isaac   PetscFunctionBegin;
795988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
796088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
796189d734beSBarry Smith   if (dm == fdm) fdm = NULL;
79629566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
79639566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
796488bdff64SToby Isaac   dm->fineMesh = fdm;
79653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
796688bdff64SToby Isaac }
796788bdff64SToby Isaac 
7968a6ba4734SToby Isaac /*@C
7969bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7970a6ba4734SToby Isaac 
797120f4b53cSBarry Smith   Collective
7972783e2ec8SMatthew G. Knepley 
7973a6ba4734SToby Isaac   Input Parameters:
7974bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7975bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7976a6ba4734SToby Isaac . name     - The BC name
797745480ffeSMatthew G. Knepley . label    - The label defining constrained points
7978bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
797945480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7980a6ba4734SToby Isaac . field    - The field to constrain
798145480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7982a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7983a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
798456cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7985a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7986a6ba4734SToby Isaac 
798745480ffeSMatthew G. Knepley   Output Parameter:
798845480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
798945480ffeSMatthew G. Knepley 
7990a6ba4734SToby Isaac   Options Database Keys:
7991a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7992a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7993a6ba4734SToby Isaac 
799420f4b53cSBarry Smith   Level: intermediate
799520f4b53cSBarry Smith 
7996bb7acecfSBarry Smith   Notes:
7997aeebefc2SPierre 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\:
799873ff1848SBarry Smith .vb
799973ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
800073ff1848SBarry Smith .ve
800156cf3b9cSMatthew G. Knepley 
8002a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
800356cf3b9cSMatthew G. Knepley 
800420f4b53cSBarry Smith .vb
800520f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
800620f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
800720f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
800820f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
800920f4b53cSBarry Smith .ve
801056cf3b9cSMatthew G. Knepley + dim - the spatial dimension
801156cf3b9cSMatthew G. Knepley . Nf - the number of fields
801256cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
801356cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
801456cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
801556cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
801656cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
801756cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
801856cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
801956cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
802056cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
802156cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
802256cf3b9cSMatthew G. Knepley . t - current time
802356cf3b9cSMatthew G. Knepley . x - coordinates of the current point
802456cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
802556cf3b9cSMatthew G. Knepley . constants - constant parameters
802656cf3b9cSMatthew G. Knepley - bcval - output values at the current point
802756cf3b9cSMatthew G. Knepley 
80281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
8029a6ba4734SToby Isaac @*/
8030d71ae5a4SJacob 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)
8031d71ae5a4SJacob Faibussowitsch {
8032e5e52638SMatthew G. Knepley   PetscDS ds;
8033a6ba4734SToby Isaac 
8034a6ba4734SToby Isaac   PetscFunctionBegin;
8035a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8036783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
803745480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
803845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
803945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
804045480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
804101a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
80429566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8043799db056SMatthew G. Knepley   /* Complete label */
8044799db056SMatthew G. Knepley   if (label) {
8045799db056SMatthew G. Knepley     PetscObject  obj;
8046799db056SMatthew G. Knepley     PetscClassId id;
8047799db056SMatthew G. Knepley 
8048799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8049799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8050799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8051799db056SMatthew G. Knepley       DM plex;
8052799db056SMatthew G. Knepley 
8053799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8054799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8055799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8056799db056SMatthew G. Knepley     }
8057799db056SMatthew G. Knepley   }
80589566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
80593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8060a6ba4734SToby Isaac }
8061a6ba4734SToby Isaac 
806245480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8063d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8064d71ae5a4SJacob Faibussowitsch {
8065e5e52638SMatthew G. Knepley   PetscDS     ds;
8066dff059c6SToby Isaac   DMBoundary *lastnext;
8067e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8068e6f8dbb6SToby Isaac 
8069e6f8dbb6SToby Isaac   PetscFunctionBegin;
80709566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8071e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
807247a1f5adSToby Isaac   if (dm->boundary) {
807347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
807447a1f5adSToby Isaac 
807547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80763ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
807747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
807847a1f5adSToby Isaac     while (next) {
807947a1f5adSToby Isaac       DMBoundary b = next;
808047a1f5adSToby Isaac 
808147a1f5adSToby Isaac       next = b->next;
80829566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8083a6ba4734SToby Isaac     }
808447a1f5adSToby Isaac     dm->boundary = NULL;
8085a6ba4734SToby Isaac   }
808647a1f5adSToby Isaac 
8087f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8088e6f8dbb6SToby Isaac   while (dsbound) {
8089e6f8dbb6SToby Isaac     DMBoundary dmbound;
8090e6f8dbb6SToby Isaac 
80919566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8092e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
809345480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
809447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8095dff059c6SToby Isaac     *lastnext = dmbound;
8096f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8097dff059c6SToby Isaac     dsbound   = dsbound->next;
8098a6ba4734SToby Isaac   }
80993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8100a6ba4734SToby Isaac }
8101a6ba4734SToby Isaac 
8102bb7acecfSBarry Smith /* TODO: missing manual page */
8103d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8104d71ae5a4SJacob Faibussowitsch {
8105b95f2879SToby Isaac   DMBoundary b;
8106a6ba4734SToby Isaac 
8107a6ba4734SToby Isaac   PetscFunctionBegin;
8108a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81094f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8110a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
81119566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8112b95f2879SToby Isaac   b = dm->boundary;
811357508eceSPierre Jolivet   while (b && !*isBd) {
8114e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8115e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8116a6ba4734SToby Isaac     PetscInt   i;
8117a6ba4734SToby Isaac 
811845480ffeSMatthew G. Knepley     if (label) {
811957508eceSPierre Jolivet       for (i = 0; i < dsb->Nv && !*isBd; ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8120a6ba4734SToby Isaac     }
8121a6ba4734SToby Isaac     b = b->next;
8122a6ba4734SToby Isaac   }
81233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8124a6ba4734SToby Isaac }
81254d6f44ffSToby Isaac 
81264d6f44ffSToby Isaac /*@C
8127bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8128a6e0b375SMatthew G. Knepley 
812920f4b53cSBarry Smith   Collective
81304d6f44ffSToby Isaac 
81314d6f44ffSToby Isaac   Input Parameters:
8132bb7acecfSBarry Smith + dm    - The `DM`
81330709b2feSToby Isaac . time  - The time
81344d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
81354d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
81364d6f44ffSToby Isaac - mode  - The insertion mode for values
81374d6f44ffSToby Isaac 
81384d6f44ffSToby Isaac   Output Parameter:
81394d6f44ffSToby Isaac . X - vector
81404d6f44ffSToby Isaac 
814120f4b53cSBarry Smith   Calling sequence of `funcs`:
81424d6f44ffSToby Isaac + dim  - The spatial dimension
81438ec8862eSJed Brown . time - The time at which to sample
81444d6f44ffSToby Isaac . x    - The coordinates
814577b739a6SMatthew Knepley . Nc   - The number of components
81464d6f44ffSToby Isaac . u    - The output field values
81474d6f44ffSToby Isaac - ctx  - optional user-defined function context
81484d6f44ffSToby Isaac 
81494d6f44ffSToby Isaac   Level: developer
81504d6f44ffSToby Isaac 
8151bb7acecfSBarry Smith   Developer Notes:
8152bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8153bb7acecfSBarry Smith 
8154bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8155bb7acecfSBarry Smith 
81561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
81574d6f44ffSToby Isaac @*/
8158a4e35b19SJacob 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)
8159d71ae5a4SJacob Faibussowitsch {
81604d6f44ffSToby Isaac   Vec localX;
81614d6f44ffSToby Isaac 
81624d6f44ffSToby Isaac   PetscFunctionBegin;
81634d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8164708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
81659566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8166f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81679566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
81689566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81699566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81709566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8171708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
81723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81734d6f44ffSToby Isaac }
81744d6f44ffSToby Isaac 
8175a6e0b375SMatthew G. Knepley /*@C
8176bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8177a6e0b375SMatthew G. Knepley 
817820f4b53cSBarry Smith   Not Collective
8179a6e0b375SMatthew G. Knepley 
8180a6e0b375SMatthew G. Knepley   Input Parameters:
8181bb7acecfSBarry Smith + dm    - The `DM`
8182a6e0b375SMatthew G. Knepley . time  - The time
8183a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8184a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8185a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8186a6e0b375SMatthew G. Knepley 
8187a6e0b375SMatthew G. Knepley   Output Parameter:
8188a6e0b375SMatthew G. Knepley . localX - vector
8189a6e0b375SMatthew G. Knepley 
819020f4b53cSBarry Smith   Calling sequence of `funcs`:
8191a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8192a4e35b19SJacob Faibussowitsch . time - The current timestep
8193a6e0b375SMatthew G. Knepley . x    - The coordinates
819477b739a6SMatthew Knepley . Nc   - The number of components
8195a6e0b375SMatthew G. Knepley . u    - The output field values
8196a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8197a6e0b375SMatthew G. Knepley 
8198a6e0b375SMatthew G. Knepley   Level: developer
8199a6e0b375SMatthew G. Knepley 
8200bb7acecfSBarry Smith   Developer Notes:
8201bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8202bb7acecfSBarry Smith 
8203bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8204bb7acecfSBarry Smith 
82051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8206a6e0b375SMatthew G. Knepley @*/
8207a4e35b19SJacob 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)
8208d71ae5a4SJacob Faibussowitsch {
82094d6f44ffSToby Isaac   PetscFunctionBegin;
82104d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8211064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8212fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
82133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82144d6f44ffSToby Isaac }
82154d6f44ffSToby Isaac 
8216a6e0b375SMatthew G. Knepley /*@C
8217bb7acecfSBarry 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.
8218a6e0b375SMatthew G. Knepley 
821920f4b53cSBarry Smith   Collective
8220a6e0b375SMatthew G. Knepley 
8221a6e0b375SMatthew G. Knepley   Input Parameters:
8222bb7acecfSBarry Smith + dm     - The `DM`
8223a6e0b375SMatthew G. Knepley . time   - The time
8224a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8225a4e35b19SJacob Faibussowitsch . ids    - The ids
8226a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8227a4e35b19SJacob Faibussowitsch . comps  - The components
8228bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8229a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8230bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8231a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8232a6e0b375SMatthew G. Knepley 
8233a6e0b375SMatthew G. Knepley   Output Parameter:
8234a6e0b375SMatthew G. Knepley . X - vector
8235a6e0b375SMatthew G. Knepley 
823620f4b53cSBarry Smith   Calling sequence of `funcs`:
8237a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8238a4e35b19SJacob Faibussowitsch . time - The current timestep
8239a6e0b375SMatthew G. Knepley . x    - The coordinates
824077b739a6SMatthew Knepley . Nc   - The number of components
8241a6e0b375SMatthew G. Knepley . u    - The output field values
8242a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8243a6e0b375SMatthew G. Knepley 
8244a6e0b375SMatthew G. Knepley   Level: developer
8245a6e0b375SMatthew G. Knepley 
8246bb7acecfSBarry Smith   Developer Notes:
8247bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8248bb7acecfSBarry Smith 
8249bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8250bb7acecfSBarry Smith 
82511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8252a6e0b375SMatthew G. Knepley @*/
8253a4e35b19SJacob 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)
8254d71ae5a4SJacob Faibussowitsch {
82552c53366bSMatthew G. Knepley   Vec localX;
82562c53366bSMatthew G. Knepley 
82572c53366bSMatthew G. Knepley   PetscFunctionBegin;
82582c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82599566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8260f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82619566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
82629566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82639566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82649566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
82653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82662c53366bSMatthew G. Knepley }
82672c53366bSMatthew G. Knepley 
8268a6e0b375SMatthew G. Knepley /*@C
8269bb7acecfSBarry 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.
8270a6e0b375SMatthew G. Knepley 
827120f4b53cSBarry Smith   Not Collective
8272a6e0b375SMatthew G. Knepley 
8273a6e0b375SMatthew G. Knepley   Input Parameters:
8274bb7acecfSBarry Smith + dm     - The `DM`
8275a6e0b375SMatthew G. Knepley . time   - The time
8276bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8277a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8278a4e35b19SJacob Faibussowitsch . ids    - The ids
8279a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8280a4e35b19SJacob Faibussowitsch . comps  - The components
8281a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8282a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8283a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8284a6e0b375SMatthew G. Knepley 
8285a6e0b375SMatthew G. Knepley   Output Parameter:
8286a6e0b375SMatthew G. Knepley . localX - vector
8287a6e0b375SMatthew G. Knepley 
828820f4b53cSBarry Smith   Calling sequence of `funcs`:
8289a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8290a4e35b19SJacob Faibussowitsch . time - The current time
8291a6e0b375SMatthew G. Knepley . x    - The coordinates
829277b739a6SMatthew Knepley . Nc   - The number of components
8293a6e0b375SMatthew G. Knepley . u    - The output field values
8294a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8295a6e0b375SMatthew G. Knepley 
8296a6e0b375SMatthew G. Knepley   Level: developer
8297a6e0b375SMatthew G. Knepley 
8298bb7acecfSBarry Smith   Developer Notes:
8299bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8300bb7acecfSBarry Smith 
8301bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8302bb7acecfSBarry Smith 
83031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8304a6e0b375SMatthew G. Knepley @*/
8305a4e35b19SJacob 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)
8306d71ae5a4SJacob Faibussowitsch {
83074d6f44ffSToby Isaac   PetscFunctionBegin;
83084d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8309064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8310fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
83113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83124d6f44ffSToby Isaac }
83132716604bSToby Isaac 
8314a6e0b375SMatthew G. Knepley /*@C
8315bb7acecfSBarry 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.
8316a6e0b375SMatthew G. Knepley 
831720f4b53cSBarry Smith   Not Collective
8318a6e0b375SMatthew G. Knepley 
8319a6e0b375SMatthew G. Knepley   Input Parameters:
8320bb7acecfSBarry Smith + dm     - The `DM`
8321a6e0b375SMatthew G. Knepley . time   - The time
832220f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8323a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8324a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8325a6e0b375SMatthew G. Knepley 
8326a6e0b375SMatthew G. Knepley   Output Parameter:
8327a6e0b375SMatthew G. Knepley . localX - The output vector
8328a6e0b375SMatthew G. Knepley 
832920f4b53cSBarry Smith   Calling sequence of `funcs`:
8330a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8331a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8332a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8333a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8334a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8335a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8336a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8337a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8338a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8339a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8340a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8341a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8342a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8343a6e0b375SMatthew G. Knepley . t            - The current time
8344a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8345a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8346a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8347a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8348a6e0b375SMatthew G. Knepley 
834973ff1848SBarry Smith   Level: intermediate
835073ff1848SBarry Smith 
8351bb7acecfSBarry Smith   Note:
8352bb7acecfSBarry 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.
8353bb7acecfSBarry 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
8354bb7acecfSBarry 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
8355a6e0b375SMatthew 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.
8356a6e0b375SMatthew G. Knepley 
8357bb7acecfSBarry Smith   Developer Notes:
8358bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8359bb7acecfSBarry Smith 
8360bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8361bb7acecfSBarry Smith 
8362a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8363a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8364a6e0b375SMatthew G. Knepley @*/
8365a4e35b19SJacob 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)
8366d71ae5a4SJacob Faibussowitsch {
83678c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83688c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8369eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
83708c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8371fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
83723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83738c6c5593SMatthew G. Knepley }
83748c6c5593SMatthew G. Knepley 
8375a6e0b375SMatthew G. Knepley /*@C
8376a6e0b375SMatthew 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.
8377a6e0b375SMatthew G. Knepley 
837820f4b53cSBarry Smith   Not Collective
8379a6e0b375SMatthew G. Knepley 
8380a6e0b375SMatthew G. Knepley   Input Parameters:
8381bb7acecfSBarry Smith + dm     - The `DM`
8382a6e0b375SMatthew G. Knepley . time   - The time
8383bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8384a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8385a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8386bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
838720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8388a6e0b375SMatthew G. Knepley . localU - The input field vector
8389a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8390a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8391a6e0b375SMatthew G. Knepley 
8392a6e0b375SMatthew G. Knepley   Output Parameter:
8393a6e0b375SMatthew G. Knepley . localX - The output vector
8394a6e0b375SMatthew G. Knepley 
839520f4b53cSBarry Smith   Calling sequence of `funcs`:
8396a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8397a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8398a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8399a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8400a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8401a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8402a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8403a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8404a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8405a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8406a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8407a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8408a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8409a6e0b375SMatthew G. Knepley . t            - The current time
8410a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8411a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8412a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8413a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8414a6e0b375SMatthew G. Knepley 
841573ff1848SBarry Smith   Level: intermediate
841673ff1848SBarry Smith 
8417bb7acecfSBarry Smith   Note:
8418bb7acecfSBarry 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.
8419bb7acecfSBarry 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
8420bb7acecfSBarry 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
8421a6e0b375SMatthew 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.
8422a6e0b375SMatthew G. Knepley 
8423bb7acecfSBarry Smith   Developer Notes:
8424bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8425bb7acecfSBarry Smith 
8426bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8427bb7acecfSBarry Smith 
84281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8429a6e0b375SMatthew G. Knepley @*/
8430a4e35b19SJacob 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)
8431d71ae5a4SJacob Faibussowitsch {
84328c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84338c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8434064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8435064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8436fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84388c6c5593SMatthew G. Knepley }
84398c6c5593SMatthew G. Knepley 
84402716604bSToby Isaac /*@C
8441d29d7c6eSMatthew 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.
8442d29d7c6eSMatthew G. Knepley 
844320f4b53cSBarry Smith   Not Collective
8444d29d7c6eSMatthew G. Knepley 
8445d29d7c6eSMatthew G. Knepley   Input Parameters:
8446bb7acecfSBarry Smith + dm     - The `DM`
8447d29d7c6eSMatthew G. Knepley . time   - The time
8448bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8449d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8450d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8451bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
845220f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8453d29d7c6eSMatthew G. Knepley . U      - The input field vector
8454d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8455d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8456d29d7c6eSMatthew G. Knepley 
8457d29d7c6eSMatthew G. Knepley   Output Parameter:
8458d29d7c6eSMatthew G. Knepley . X - The output vector
8459d29d7c6eSMatthew G. Knepley 
846020f4b53cSBarry Smith   Calling sequence of `funcs`:
8461d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8462d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8463d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8464d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8465d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8466d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8467d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8468d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8469d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8470d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8471d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8472d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8473d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8474d29d7c6eSMatthew G. Knepley . t            - The current time
8475d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8476d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8477d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8478d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8479d29d7c6eSMatthew G. Knepley 
848073ff1848SBarry Smith   Level: intermediate
848173ff1848SBarry Smith 
8482bb7acecfSBarry Smith   Note:
8483bb7acecfSBarry 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.
8484bb7acecfSBarry 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
8485bb7acecfSBarry 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
8486d29d7c6eSMatthew 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.
8487d29d7c6eSMatthew G. Knepley 
8488bb7acecfSBarry Smith   Developer Notes:
8489bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8490bb7acecfSBarry Smith 
8491bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8492bb7acecfSBarry Smith 
84931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8494d29d7c6eSMatthew G. Knepley @*/
8495a4e35b19SJacob 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)
8496d71ae5a4SJacob Faibussowitsch {
8497d29d7c6eSMatthew G. Knepley   DM  dmIn;
8498d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8499d29d7c6eSMatthew G. Knepley 
8500d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8501d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8502d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8503d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8504d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8505f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
850672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
850772fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8508d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8509d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8510d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8511d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8512d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
85133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8514d29d7c6eSMatthew G. Knepley }
8515d29d7c6eSMatthew G. Knepley 
8516d29d7c6eSMatthew G. Knepley /*@C
8517ece3a9fcSMatthew 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.
8518ece3a9fcSMatthew G. Knepley 
851920f4b53cSBarry Smith   Not Collective
8520ece3a9fcSMatthew G. Knepley 
8521ece3a9fcSMatthew G. Knepley   Input Parameters:
8522bb7acecfSBarry Smith + dm     - The `DM`
8523ece3a9fcSMatthew G. Knepley . time   - The time
8524bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8525ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8526ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8527bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
852820f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8529ece3a9fcSMatthew G. Knepley . localU - The input field vector
8530ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8531ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8532ece3a9fcSMatthew G. Knepley 
8533ece3a9fcSMatthew G. Knepley   Output Parameter:
8534ece3a9fcSMatthew G. Knepley . localX - The output vector
8535ece3a9fcSMatthew G. Knepley 
853620f4b53cSBarry Smith   Calling sequence of `funcs`:
8537ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8538ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8539ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8540ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8541ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8542ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8543ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8544ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8545ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8546ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8547ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8548ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8549ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8550ece3a9fcSMatthew G. Knepley . t            - The current time
8551ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8552ece3a9fcSMatthew G. Knepley . n            - The face normal
8553ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8554ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8555ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8556ece3a9fcSMatthew G. Knepley 
855773ff1848SBarry Smith   Level: intermediate
855873ff1848SBarry Smith 
8559ece3a9fcSMatthew G. Knepley   Note:
8560bb7acecfSBarry 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.
8561bb7acecfSBarry 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
8562bb7acecfSBarry 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
8563ece3a9fcSMatthew 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.
8564ece3a9fcSMatthew G. Knepley 
8565bb7acecfSBarry Smith   Developer Notes:
8566bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8567bb7acecfSBarry Smith 
8568bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8569bb7acecfSBarry Smith 
85701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8571ece3a9fcSMatthew G. Knepley @*/
8572a4e35b19SJacob 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)
8573d71ae5a4SJacob Faibussowitsch {
8574ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8575ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8576064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8577064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8578fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8580ece3a9fcSMatthew G. Knepley }
8581ece3a9fcSMatthew G. Knepley 
8582ece3a9fcSMatthew G. Knepley /*@C
85832716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85842716604bSToby Isaac 
858520f4b53cSBarry Smith   Collective
8586bb7acecfSBarry Smith 
85872716604bSToby Isaac   Input Parameters:
8588bb7acecfSBarry Smith + dm    - The `DM`
85890709b2feSToby Isaac . time  - The time
85902716604bSToby Isaac . funcs - The functions to evaluate for each field component
85912716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8592574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85932716604bSToby Isaac 
85942716604bSToby Isaac   Output Parameter:
85952716604bSToby Isaac . diff - The diff ||u - u_h||_2
85962716604bSToby Isaac 
85972716604bSToby Isaac   Level: developer
85982716604bSToby Isaac 
8599bb7acecfSBarry Smith   Developer Notes:
8600bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8601bb7acecfSBarry Smith 
8602bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8603bb7acecfSBarry Smith 
86041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
86052716604bSToby Isaac @*/
8606d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8607d71ae5a4SJacob Faibussowitsch {
86082716604bSToby Isaac   PetscFunctionBegin;
86092716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8610b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8611fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
86123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86132716604bSToby Isaac }
8614b698f381SToby Isaac 
8615b698f381SToby Isaac /*@C
8616b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8617b698f381SToby Isaac 
861820f4b53cSBarry Smith   Collective
8619d083f849SBarry Smith 
8620b698f381SToby Isaac   Input Parameters:
8621bb7acecfSBarry Smith + dm    - The `DM`
8622a4e35b19SJacob Faibussowitsch . time  - The time
8623b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8624b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8625574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8626b698f381SToby Isaac - n     - The vector to project along
8627b698f381SToby Isaac 
8628b698f381SToby Isaac   Output Parameter:
8629b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8630b698f381SToby Isaac 
8631b698f381SToby Isaac   Level: developer
8632b698f381SToby Isaac 
8633bb7acecfSBarry Smith   Developer Notes:
8634bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8635bb7acecfSBarry Smith 
8636bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8637bb7acecfSBarry Smith 
86381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8639b698f381SToby Isaac @*/
8640d71ae5a4SJacob 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)
8641d71ae5a4SJacob Faibussowitsch {
8642b698f381SToby Isaac   PetscFunctionBegin;
8643b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8644b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8645fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
86463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8647b698f381SToby Isaac }
8648b698f381SToby Isaac 
86492a16baeaSToby Isaac /*@C
86502a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86512a16baeaSToby Isaac 
865220f4b53cSBarry Smith   Collective
8653d083f849SBarry Smith 
86542a16baeaSToby Isaac   Input Parameters:
8655bb7acecfSBarry Smith + dm    - The `DM`
86562a16baeaSToby Isaac . time  - The time
86572a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86582a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8659574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86602a16baeaSToby Isaac 
86612a16baeaSToby Isaac   Output Parameter:
86622a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86632a16baeaSToby Isaac 
86642a16baeaSToby Isaac   Level: developer
86652a16baeaSToby Isaac 
8666bb7acecfSBarry Smith   Developer Notes:
8667bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8668bb7acecfSBarry Smith 
8669bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8670bb7acecfSBarry Smith 
867142747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
86722a16baeaSToby Isaac @*/
8673d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8674d71ae5a4SJacob Faibussowitsch {
86752a16baeaSToby Isaac   PetscFunctionBegin;
86762a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86772a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8678fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
86793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86802a16baeaSToby Isaac }
86812a16baeaSToby Isaac 
8682df0b854cSToby Isaac /*@C
8683bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8684502a2867SDave May 
8685502a2867SDave May   Not Collective
8686502a2867SDave May 
8687502a2867SDave May   Input Parameter:
8688bb7acecfSBarry Smith . dm - The `DM`
8689502a2867SDave May 
86900a19bb7dSprj-   Output Parameters:
86910a19bb7dSprj- + nranks - the number of neighbours
86920a19bb7dSprj- - ranks  - the neighbors ranks
8693502a2867SDave May 
86949bdbcad8SBarry Smith   Level: beginner
86959bdbcad8SBarry Smith 
8696bb7acecfSBarry Smith   Note:
8697bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8698502a2867SDave May 
86991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8700502a2867SDave May @*/
8701d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8702d71ae5a4SJacob Faibussowitsch {
8703502a2867SDave May   PetscFunctionBegin;
8704502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8705fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
87063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8707502a2867SDave May }
8708502a2867SDave May 
8709531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8710531c7667SBarry Smith 
8711531c7667SBarry Smith /*
8712531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
87132b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8714531c7667SBarry Smith */
871566976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8716d71ae5a4SJacob Faibussowitsch {
8717531c7667SBarry Smith   PetscFunctionBegin;
8718531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8719531c7667SBarry Smith     Vec x1local;
8720531c7667SBarry Smith     DM  dm;
87219566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87227a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
87239566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
87249566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
87259566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8726531c7667SBarry Smith     x1 = x1local;
8727531c7667SBarry Smith   }
87289566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8729531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8730531c7667SBarry Smith     DM dm;
87319566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87329566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8733531c7667SBarry Smith   }
87343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8735531c7667SBarry Smith }
8736531c7667SBarry Smith 
8737531c7667SBarry Smith /*@
8738bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8739531c7667SBarry Smith 
8740a4e35b19SJacob Faibussowitsch   Input Parameters:
8741a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8742a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8743531c7667SBarry Smith 
87449bdbcad8SBarry Smith   Level: advanced
87459bdbcad8SBarry Smith 
874673ff1848SBarry Smith   Developer Note:
874773ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8748531c7667SBarry Smith 
87491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8750531c7667SBarry Smith @*/
8751d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8752d71ae5a4SJacob Faibussowitsch {
8753531c7667SBarry Smith   PetscFunctionBegin;
8754531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
87553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8756531c7667SBarry Smith }
87578320bc6fSPatrick Sanan 
87588320bc6fSPatrick Sanan /*@
8759bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
87608320bc6fSPatrick Sanan 
87618320bc6fSPatrick Sanan   Collective
87628320bc6fSPatrick Sanan 
87638320bc6fSPatrick Sanan   Input Parameters:
8764bb7acecfSBarry Smith + dm1 - the first `DM`
8765bb7acecfSBarry Smith - dm2 - the second `DM`
87668320bc6fSPatrick Sanan 
87678320bc6fSPatrick Sanan   Output Parameters:
8768bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8769bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
87708320bc6fSPatrick Sanan 
877120f4b53cSBarry Smith   Level: advanced
877220f4b53cSBarry Smith 
87738320bc6fSPatrick Sanan   Notes:
8774bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
87753d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
87768320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8777bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
87783d862458SPatrick Sanan   decomposition, but hold different data.
87798320bc6fSPatrick Sanan 
87808320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8781bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87828320bc6fSPatrick Sanan 
8783bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87848320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87858320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8786bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87878320bc6fSPatrick Sanan 
8788bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8789bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87908320bc6fSPatrick Sanan .vb
87918320bc6fSPatrick Sanan   ...
87929566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
87938320bc6fSPatrick Sanan   if (set && compatible)  {
87949566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
87959566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
87969566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
87978320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
87988320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
87998320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88008320bc6fSPatrick Sanan       }
88018320bc6fSPatrick Sanan     }
88029566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
88039566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
88048320bc6fSPatrick Sanan   } else {
88058320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88068320bc6fSPatrick Sanan   }
88078320bc6fSPatrick Sanan   ...
88088320bc6fSPatrick Sanan .ve
88098320bc6fSPatrick Sanan 
8810bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
88118320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
88128320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
88138320bc6fSPatrick Sanan   always check the "set" output parameter.
88148320bc6fSPatrick Sanan 
8815bb7acecfSBarry Smith   A `DM` is always compatible with itself.
88168320bc6fSPatrick Sanan 
8817bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
88188320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
88198320bc6fSPatrick Sanan   incompatible.
88208320bc6fSPatrick Sanan 
88218320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8822bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
88238320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
88248320bc6fSPatrick Sanan 
882573ff1848SBarry Smith   Developer Note:
8826bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
88273d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8828a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8829bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
88308320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
88313d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8832bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
88338320bc6fSPatrick Sanan 
88341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
88358320bc6fSPatrick Sanan @*/
8836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8837d71ae5a4SJacob Faibussowitsch {
88388320bc6fSPatrick Sanan   PetscMPIInt compareResult;
88398320bc6fSPatrick Sanan   DMType      type, type2;
88408320bc6fSPatrick Sanan   PetscBool   sameType;
88418320bc6fSPatrick Sanan 
88428320bc6fSPatrick Sanan   PetscFunctionBegin;
8843a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
88448320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
88458320bc6fSPatrick Sanan 
88468320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8847a5bc1bf3SBarry Smith   if (dm1 == dm2) {
88488320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88498320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
88503ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88518320bc6fSPatrick Sanan   }
88528320bc6fSPatrick Sanan 
88538320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
88548320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
88558320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
88568320bc6fSPatrick Sanan      determined by the implementation-specific logic */
88579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
88588320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
88598320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88608320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
88613ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88628320bc6fSPatrick Sanan   }
88638320bc6fSPatrick Sanan 
88648320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8865a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8866dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
88673ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
88688320bc6fSPatrick Sanan   }
88698320bc6fSPatrick Sanan 
8870a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
88718320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
88729566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
88739566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
88749566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
88758320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8876dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
88778320bc6fSPatrick Sanan   } else {
88788320bc6fSPatrick Sanan     *set = PETSC_FALSE;
88798320bc6fSPatrick Sanan   }
88803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88818320bc6fSPatrick Sanan }
8882c0f0dcc3SMatthew G. Knepley 
8883c0f0dcc3SMatthew G. Knepley /*@C
8884bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8885c0f0dcc3SMatthew G. Knepley 
888620f4b53cSBarry Smith   Logically Collective
8887c0f0dcc3SMatthew G. Knepley 
8888c0f0dcc3SMatthew G. Knepley   Input Parameters:
888960225df5SJacob Faibussowitsch + dm             - the `DM`
8890c0f0dcc3SMatthew G. Knepley . f              - the monitor function
889120f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
889249abdd8aSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`), see `PetscCtxDestroyFn` for the calling sequence
8893c0f0dcc3SMatthew G. Knepley 
889420f4b53cSBarry Smith   Options Database Key:
889560225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8896c0f0dcc3SMatthew G. Knepley                        does not cancel those set via the options database.
8897c0f0dcc3SMatthew G. Knepley 
88989bdbcad8SBarry Smith   Level: intermediate
88999bdbcad8SBarry Smith 
8900bb7acecfSBarry Smith   Note:
8901c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8902bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8903c0f0dcc3SMatthew G. Knepley   order in which they were set.
8904c0f0dcc3SMatthew G. Knepley 
890573ff1848SBarry Smith   Fortran Note:
8906bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8907bb7acecfSBarry Smith 
890873ff1848SBarry Smith   Developer Note:
8909bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8910c0f0dcc3SMatthew G. Knepley 
891149abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`, `PetscCtxDestroyFn`
8912c0f0dcc3SMatthew G. Knepley @*/
891349abdd8aSBarry Smith PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscCtxDestroyFn *monitordestroy)
8914d71ae5a4SJacob Faibussowitsch {
8915c0f0dcc3SMatthew G. Knepley   PetscInt m;
8916c0f0dcc3SMatthew G. Knepley 
8917c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8918c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8919c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8920c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8921c0f0dcc3SMatthew G. Knepley 
89229566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f, mctx, monitordestroy, (PetscErrorCode (*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
89233ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8924c0f0dcc3SMatthew G. Knepley   }
89257a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8926c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8927c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8928835f2295SStefano Zampini   dm->monitorcontext[dm->numbermonitors++] = mctx;
89293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8930c0f0dcc3SMatthew G. Knepley }
8931c0f0dcc3SMatthew G. Knepley 
8932c0f0dcc3SMatthew G. Knepley /*@
8933bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8934c0f0dcc3SMatthew G. Knepley 
893520f4b53cSBarry Smith   Logically Collective
8936c0f0dcc3SMatthew G. Knepley 
8937c0f0dcc3SMatthew G. Knepley   Input Parameter:
8938c0f0dcc3SMatthew G. Knepley . dm - the DM
8939c0f0dcc3SMatthew G. Knepley 
8940c0f0dcc3SMatthew G. Knepley   Options Database Key:
8941c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8942bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8943c0f0dcc3SMatthew G. Knepley   set via the options database
8944c0f0dcc3SMatthew G. Knepley 
89459bdbcad8SBarry Smith   Level: intermediate
89469bdbcad8SBarry Smith 
8947bb7acecfSBarry Smith   Note:
8948bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8949c0f0dcc3SMatthew G. Knepley 
89501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8951c0f0dcc3SMatthew G. Knepley @*/
8952d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8953d71ae5a4SJacob Faibussowitsch {
8954c0f0dcc3SMatthew G. Knepley   PetscInt m;
8955c0f0dcc3SMatthew G. Knepley 
8956c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8957c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8958c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
89599566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8960c0f0dcc3SMatthew G. Knepley   }
8961c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
89623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8963c0f0dcc3SMatthew G. Knepley }
8964c0f0dcc3SMatthew G. Knepley 
8965c0f0dcc3SMatthew G. Knepley /*@C
8966c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8967c0f0dcc3SMatthew G. Knepley 
896820f4b53cSBarry Smith   Collective
8969c0f0dcc3SMatthew G. Knepley 
8970c0f0dcc3SMatthew G. Knepley   Input Parameters:
8971bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8972c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8973c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8974c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
897549abdd8aSBarry Smith . monitor      - the monitor function, this must use a `PetscViewerFormat` as its context
8976bb7acecfSBarry 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
8977c0f0dcc3SMatthew G. Knepley 
8978c0f0dcc3SMatthew G. Knepley   Output Parameter:
8979c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8980c0f0dcc3SMatthew G. Knepley 
8981c0f0dcc3SMatthew G. Knepley   Level: developer
8982c0f0dcc3SMatthew G. Knepley 
8983648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8984db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
898560225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8986db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8987c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8988db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8989bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8990c0f0dcc3SMatthew G. Knepley @*/
8991d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8992d71ae5a4SJacob Faibussowitsch {
8993c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8994c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8995c0f0dcc3SMatthew G. Knepley 
8996c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8997c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8998648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8999c0f0dcc3SMatthew G. Knepley   if (*flg) {
9000c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9001c0f0dcc3SMatthew G. Knepley 
90029566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
9003648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
90049566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
9005835f2295SStefano Zampini     PetscCall(DMMonitorSet(dm, monitor, vf, (PetscCtxDestroyFn *)PetscViewerAndFormatDestroy));
9006c0f0dcc3SMatthew G. Knepley   }
90073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9008c0f0dcc3SMatthew G. Knepley }
9009c0f0dcc3SMatthew G. Knepley 
9010c0f0dcc3SMatthew G. Knepley /*@
9011c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
9012c0f0dcc3SMatthew G. Knepley 
901320f4b53cSBarry Smith   Collective
9014c0f0dcc3SMatthew G. Knepley 
90152fe279fdSBarry Smith   Input Parameter:
9016bb7acecfSBarry Smith . dm - The `DM`
9017c0f0dcc3SMatthew G. Knepley 
9018c0f0dcc3SMatthew G. Knepley   Level: developer
9019c0f0dcc3SMatthew G. Knepley 
902073ff1848SBarry Smith   Developer Note:
9021a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
9022a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
9023a4e35b19SJacob Faibussowitsch   concept of discretization.
9024bb7acecfSBarry Smith 
90251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
9026c0f0dcc3SMatthew G. Knepley @*/
9027d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
9028d71ae5a4SJacob Faibussowitsch {
9029c0f0dcc3SMatthew G. Knepley   PetscInt m;
9030c0f0dcc3SMatthew G. Knepley 
9031c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
90323ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
9033c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
903448a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
90353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9036c0f0dcc3SMatthew G. Knepley }
90372e4af2aeSMatthew G. Knepley 
90382e4af2aeSMatthew G. Knepley /*@
9039bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
90402e4af2aeSMatthew G. Knepley 
904120f4b53cSBarry Smith   Collective
90422e4af2aeSMatthew G. Knepley 
90432e4af2aeSMatthew G. Knepley   Input Parameters:
9044bb7acecfSBarry Smith + dm  - The `DM`
90456b867d5aSJose E. Roman - sol - The solution vector
90462e4af2aeSMatthew G. Knepley 
90476b867d5aSJose E. Roman   Input/Output Parameter:
904820f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
90496b867d5aSJose E. Roman            contains the error in each field
90506b867d5aSJose E. Roman 
90516b867d5aSJose E. Roman   Output Parameter:
905220f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
905320f4b53cSBarry Smith 
905420f4b53cSBarry Smith   Level: developer
90552e4af2aeSMatthew G. Knepley 
9056bb7acecfSBarry Smith   Note:
9057bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
90582e4af2aeSMatthew G. Knepley 
90591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
90602e4af2aeSMatthew G. Knepley @*/
9061d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9062d71ae5a4SJacob Faibussowitsch {
90632e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
90642e4af2aeSMatthew G. Knepley   void    **ctxs;
90652e4af2aeSMatthew G. Knepley   PetscReal time;
90662e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
90672e4af2aeSMatthew G. Knepley 
90682e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
90699566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
90709566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
90719566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
90722e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
90732e4af2aeSMatthew G. Knepley     PetscDS         ds;
90742e4af2aeSMatthew G. Knepley     DMLabel         label;
90752e4af2aeSMatthew G. Knepley     IS              fieldIS;
90762e4af2aeSMatthew G. Knepley     const PetscInt *fields;
90772e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
90782e4af2aeSMatthew G. Knepley 
907907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
90809566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90819566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90822e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90832e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90849566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90852e4af2aeSMatthew G. Knepley     }
90869566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90872e4af2aeSMatthew G. Knepley   }
9088ad540459SPierre 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);
90899566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90909566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90912e4af2aeSMatthew G. Knepley   if (errorVec) {
90922e4af2aeSMatthew G. Knepley     DM             edm;
90932e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
90942e4af2aeSMatthew G. Knepley     PetscBool      simplex;
90952e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
90962e4af2aeSMatthew G. Knepley 
90979566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
90989566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
90999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
91009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
91012e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
91029566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
91032e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
91042e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
91052e4af2aeSMatthew G. Knepley       PetscQuadrature q;
91062e4af2aeSMatthew G. Knepley       const char     *name;
91072e4af2aeSMatthew G. Knepley 
91089566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
91099566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
91109566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
91119566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
91129566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
91139566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
91149566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
91159566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
91162e4af2aeSMatthew G. Knepley     }
91179566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
91182e4af2aeSMatthew G. Knepley 
91199566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
91209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
91219566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
91229566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
91232e4af2aeSMatthew G. Knepley   }
91249566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
91253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91262e4af2aeSMatthew G. Knepley }
91279a2a23afSMatthew G. Knepley 
91289a2a23afSMatthew G. Knepley /*@
9129bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
91309a2a23afSMatthew G. Knepley 
913120f4b53cSBarry Smith   Not Collective
91329a2a23afSMatthew G. Knepley 
91339a2a23afSMatthew G. Knepley   Input Parameter:
9134bb7acecfSBarry Smith . dm - The `DM`
91359a2a23afSMatthew G. Knepley 
91369a2a23afSMatthew G. Knepley   Output Parameter:
9137a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
91389a2a23afSMatthew G. Knepley 
91399a2a23afSMatthew G. Knepley   Level: advanced
91409a2a23afSMatthew G. Knepley 
9141e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
91429a2a23afSMatthew G. Knepley @*/
9143d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9144d71ae5a4SJacob Faibussowitsch {
91459a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91469a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91479566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
91483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91499a2a23afSMatthew G. Knepley }
91509a2a23afSMatthew G. Knepley 
91519a2a23afSMatthew G. Knepley /*@
9152ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
91539a2a23afSMatthew G. Knepley 
915420f4b53cSBarry Smith   Not Collective
91559a2a23afSMatthew G. Knepley 
91569a2a23afSMatthew G. Knepley   Input Parameters:
9157bb7acecfSBarry Smith + dm    - The `DM`
9158bb7acecfSBarry Smith . label - The `DMLabel`
9159ac17215fSMatthew G. Knepley . value - The label value indicating the region
9160ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
91619a2a23afSMatthew G. Knepley 
91629a2a23afSMatthew G. Knepley   Output Parameter:
9163bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
91649a2a23afSMatthew G. Knepley 
91659bdbcad8SBarry Smith   Level: advanced
91669bdbcad8SBarry Smith 
9167bb7acecfSBarry Smith   Note:
9168bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
916904c51a94SMatthew G. Knepley 
9170e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
91719a2a23afSMatthew G. Knepley @*/
9172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9173d71ae5a4SJacob Faibussowitsch {
9174ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
917504c51a94SMatthew G. Knepley   PetscBool       has;
91769a2a23afSMatthew G. Knepley 
91779a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91789a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91799a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91809a2a23afSMatthew G. Knepley   key.label = label;
91819a2a23afSMatthew G. Knepley   key.value = value;
9182ac17215fSMatthew G. Knepley   key.part  = part;
91839566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91849566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91859566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91879a2a23afSMatthew G. Knepley }
91889a2a23afSMatthew G. Knepley 
91899a2a23afSMatthew G. Knepley /*@
9190bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91919a2a23afSMatthew G. Knepley 
919220f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
91939a2a23afSMatthew G. Knepley 
91949a2a23afSMatthew G. Knepley   Input Parameters:
9195bb7acecfSBarry Smith + dm    - The `DM`
9196bb7acecfSBarry Smith . label - The `DMLabel`
91979a2a23afSMatthew G. Knepley . value - The label value indicating the region
9198ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9199bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
92009a2a23afSMatthew G. Knepley 
92019a2a23afSMatthew G. Knepley   Level: advanced
92029a2a23afSMatthew G. Knepley 
9203e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
92049a2a23afSMatthew G. Knepley @*/
9205d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9206d71ae5a4SJacob Faibussowitsch {
92079a2a23afSMatthew G. Knepley   Vec             old;
92089a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
92099a2a23afSMatthew G. Knepley 
92109a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92119a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92129a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
92139a2a23afSMatthew G. Knepley   key.label = label;
92149a2a23afSMatthew G. Knepley   key.value = value;
9215ac17215fSMatthew G. Knepley   key.part  = part;
92169566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
92179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
92189566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
92199566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9220d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
92213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92229a2a23afSMatthew G. Knepley }
92239a2a23afSMatthew G. Knepley 
9224cc4c1da9SBarry Smith /*@
9225bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
92269a2a23afSMatthew G. Knepley 
922720f4b53cSBarry Smith   Not Collective
92289a2a23afSMatthew G. Knepley 
92299a2a23afSMatthew G. Knepley   Input Parameter:
9230bb7acecfSBarry Smith . dm - The `DM`
92319a2a23afSMatthew G. Knepley 
92329a2a23afSMatthew G. Knepley   Output Parameters:
9233bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9234bb7acecfSBarry Smith . values - The label values for each `Vec`
9235bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
92369a2a23afSMatthew G. Knepley 
92379bdbcad8SBarry Smith   Level: advanced
92389bdbcad8SBarry Smith 
9239bb7acecfSBarry Smith   Note:
9240bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
92419a2a23afSMatthew G. Knepley 
9242e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
92439a2a23afSMatthew G. Knepley @*/
9244d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9245d71ae5a4SJacob Faibussowitsch {
92469a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
92479a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
92489a2a23afSMatthew G. Knepley 
92499a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92509a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92514f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
92524f572ea9SToby Isaac   PetscAssertPointer(values, 3);
92534f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
92549566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
92559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
92569566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
92579371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
92589371c9d4SSatish Balay     labels[i] = keys[i].label;
92599371c9d4SSatish Balay     values[i] = keys[i].value;
92609371c9d4SSatish Balay     parts[i]  = keys[i].part;
92619371c9d4SSatish Balay   }
92629566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
92633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92649a2a23afSMatthew G. Knepley }
92659a2a23afSMatthew G. Knepley 
92669a2a23afSMatthew G. Knepley /*@
9267bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
92689a2a23afSMatthew G. Knepley 
926920f4b53cSBarry Smith   Not Collective
92709a2a23afSMatthew G. Knepley 
92719a2a23afSMatthew G. Knepley   Input Parameter:
9272bb7acecfSBarry Smith . dm - The `DM`
92739a2a23afSMatthew G. Knepley 
92749a2a23afSMatthew G. Knepley   Output Parameter:
9275bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
92769a2a23afSMatthew G. Knepley 
92779a2a23afSMatthew G. Knepley   Level: advanced
92789a2a23afSMatthew G. Knepley 
9279bb7acecfSBarry Smith   Note:
9280bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9281bb7acecfSBarry Smith 
9282e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92839a2a23afSMatthew G. Knepley @*/
9284d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9285d71ae5a4SJacob Faibussowitsch {
92869a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92879a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9288d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9289d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9290e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9291e4d5475eSStefano Zampini 
9292e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
92939566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9294d705d0eaSStefano Zampini   {
9295d705d0eaSStefano Zampini     Vec     *auxData;
9296d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9297d705d0eaSStefano Zampini 
9298d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9299d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9300d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9301d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9302d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9303e4d5475eSStefano Zampini   }
9304e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9305e4d5475eSStefano Zampini }
9306e4d5475eSStefano Zampini 
9307e4d5475eSStefano Zampini /*@
9308e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9309e4d5475eSStefano Zampini 
9310e4d5475eSStefano Zampini   Not Collective
9311e4d5475eSStefano Zampini 
9312e4d5475eSStefano Zampini   Input Parameter:
9313e4d5475eSStefano Zampini . dm - The `DM`
9314e4d5475eSStefano Zampini 
9315e4d5475eSStefano Zampini   Level: advanced
9316e4d5475eSStefano Zampini 
9317e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9318e4d5475eSStefano Zampini @*/
9319e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9320e4d5475eSStefano Zampini {
9321e4d5475eSStefano Zampini   Vec     *auxData;
9322e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9323e4d5475eSStefano Zampini 
9324e4d5475eSStefano Zampini   PetscFunctionBegin;
9325e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9326d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9327e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9328d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9329d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9330e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9331e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
93323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93339a2a23afSMatthew G. Knepley }
9334b5a892a1SMatthew G. Knepley 
9335cc4c1da9SBarry Smith /*@
9336bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9337b5a892a1SMatthew G. Knepley 
933820f4b53cSBarry Smith   Not Collective
9339b5a892a1SMatthew G. Knepley 
9340b5a892a1SMatthew G. Knepley   Input Parameters:
9341bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9342b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9343b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9344b5a892a1SMatthew G. Knepley 
9345b5a892a1SMatthew G. Knepley   Output Parameters:
9346bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9347b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9348b5a892a1SMatthew G. Knepley 
9349b5a892a1SMatthew G. Knepley   Level: advanced
9350b5a892a1SMatthew G. Knepley 
9351bb7acecfSBarry Smith   Note:
9352bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9353bb7acecfSBarry Smith 
935485036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9355bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9356bb7acecfSBarry Smith 
9357bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9358bb7acecfSBarry Smith 
93591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9360b5a892a1SMatthew G. Knepley @*/
9361d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9362d71ae5a4SJacob Faibussowitsch {
9363b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
936485036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9365b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9366b5a892a1SMatthew G. Knepley 
9367b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93689371c9d4SSatish Balay   if (!nO) {
93699371c9d4SSatish Balay     *ornt  = 0;
93709371c9d4SSatish Balay     *found = PETSC_TRUE;
93713ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93729371c9d4SSatish Balay   }
9373b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
937485036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9375b5a892a1SMatthew G. Knepley 
93769371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93779371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
93789371c9d4SSatish Balay     if (c == cS) {
93799371c9d4SSatish Balay       *ornt = o;
93809371c9d4SSatish Balay       break;
93819371c9d4SSatish Balay     }
9382b5a892a1SMatthew G. Knepley   }
9383b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9385b5a892a1SMatthew G. Knepley }
9386b5a892a1SMatthew G. Knepley 
9387cc4c1da9SBarry Smith /*@
9388bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9389b5a892a1SMatthew G. Knepley 
939020f4b53cSBarry Smith   Not Collective
9391b5a892a1SMatthew G. Knepley 
9392b5a892a1SMatthew G. Knepley   Input Parameters:
9393bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9394b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9395b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9396b5a892a1SMatthew G. Knepley 
93972fe279fdSBarry Smith   Output Parameter:
9398bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9399b5a892a1SMatthew G. Knepley 
9400b5a892a1SMatthew G. Knepley   Level: advanced
9401b5a892a1SMatthew G. Knepley 
9402bb7acecfSBarry Smith   Note:
9403bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9404bb7acecfSBarry Smith 
940573ff1848SBarry Smith   Developer Note:
9406bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9407bb7acecfSBarry Smith 
94081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9409b5a892a1SMatthew G. Knepley @*/
9410d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9411d71ae5a4SJacob Faibussowitsch {
9412b5a892a1SMatthew G. Knepley   PetscBool found;
9413b5a892a1SMatthew G. Knepley 
9414b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94159566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
94167a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9418b5a892a1SMatthew G. Knepley }
9419b5a892a1SMatthew G. Knepley 
9420cc4c1da9SBarry Smith /*@
9421bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9422b5a892a1SMatthew G. Knepley 
942320f4b53cSBarry Smith   Not Collective
9424b5a892a1SMatthew G. Knepley 
9425b5a892a1SMatthew G. Knepley   Input Parameters:
9426bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9427b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9428b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9429b5a892a1SMatthew G. Knepley 
9430b5a892a1SMatthew G. Knepley   Output Parameters:
9431bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9432b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9433b5a892a1SMatthew G. Knepley 
9434b5a892a1SMatthew G. Knepley   Level: advanced
9435b5a892a1SMatthew G. Knepley 
943673ff1848SBarry Smith   Notes:
9437bb7acecfSBarry Smith   An arrangement is a vertex order
9438bb7acecfSBarry Smith 
943985036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9440bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9441bb7acecfSBarry Smith 
9442bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9443bb7acecfSBarry Smith 
944485036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9445b5a892a1SMatthew G. Knepley @*/
9446d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9447d71ae5a4SJacob Faibussowitsch {
9448b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
944985036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9450b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9451b5a892a1SMatthew G. Knepley 
9452b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94539371c9d4SSatish Balay   if (!nO) {
94549371c9d4SSatish Balay     *ornt  = 0;
94559371c9d4SSatish Balay     *found = PETSC_TRUE;
94563ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94579371c9d4SSatish Balay   }
9458b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
945985036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9460b5a892a1SMatthew G. Knepley 
94619371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94629371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
94639371c9d4SSatish Balay     if (c == cS) {
94649371c9d4SSatish Balay       *ornt = o;
94659371c9d4SSatish Balay       break;
94669371c9d4SSatish Balay     }
9467b5a892a1SMatthew G. Knepley   }
9468b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9470b5a892a1SMatthew G. Knepley }
9471b5a892a1SMatthew G. Knepley 
9472cc4c1da9SBarry Smith /*@
9473bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9474b5a892a1SMatthew G. Knepley 
947520f4b53cSBarry Smith   Not Collective
9476b5a892a1SMatthew G. Knepley 
9477b5a892a1SMatthew G. Knepley   Input Parameters:
9478bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9479b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9480b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9481b5a892a1SMatthew G. Knepley 
94822fe279fdSBarry Smith   Output Parameter:
9483bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9484b5a892a1SMatthew G. Knepley 
9485b5a892a1SMatthew G. Knepley   Level: advanced
9486b5a892a1SMatthew G. Knepley 
9487bb7acecfSBarry Smith   Note:
9488bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9489bb7acecfSBarry Smith 
949073ff1848SBarry Smith   Developer Note:
9491bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9492bb7acecfSBarry Smith 
94931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9494b5a892a1SMatthew G. Knepley @*/
9495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9496d71ae5a4SJacob Faibussowitsch {
9497b5a892a1SMatthew G. Knepley   PetscBool found;
9498b5a892a1SMatthew G. Knepley 
9499b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
95009566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
95017a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
95023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9503b5a892a1SMatthew G. Knepley }
9504012bc364SMatthew G. Knepley 
9505cc4c1da9SBarry Smith /*@
9506012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9507012bc364SMatthew G. Knepley 
950820f4b53cSBarry Smith   Not Collective
9509012bc364SMatthew G. Knepley 
9510012bc364SMatthew G. Knepley   Input Parameters:
9511bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9512012bc364SMatthew G. Knepley - point - Coordinates of the point
9513012bc364SMatthew G. Knepley 
95142fe279fdSBarry Smith   Output Parameter:
9515012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9516012bc364SMatthew G. Knepley 
9517012bc364SMatthew G. Knepley   Level: advanced
9518012bc364SMatthew G. Knepley 
95191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9520012bc364SMatthew G. Knepley @*/
9521d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9522d71ae5a4SJacob Faibussowitsch {
9523012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9524012bc364SMatthew G. Knepley   PetscInt  d;
9525012bc364SMatthew G. Knepley 
9526012bc364SMatthew G. Knepley   PetscFunctionBegin;
9527012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9528012bc364SMatthew G. Knepley   switch (ct) {
9529012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9530012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9531012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
95329371c9d4SSatish Balay       if (point[d] < -1.0) {
95339371c9d4SSatish Balay         *inside = PETSC_FALSE;
95349371c9d4SSatish Balay         break;
95359371c9d4SSatish Balay       }
9536012bc364SMatthew G. Knepley       sum += point[d];
9537012bc364SMatthew G. Knepley     }
95389371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
95399371c9d4SSatish Balay       *inside = PETSC_FALSE;
95409371c9d4SSatish Balay       break;
95419371c9d4SSatish Balay     }
9542012bc364SMatthew G. Knepley     break;
9543012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9544012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9545012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
95469371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
95479371c9d4SSatish Balay         *inside = PETSC_FALSE;
9548012bc364SMatthew G. Knepley         break;
95499371c9d4SSatish Balay       }
95509371c9d4SSatish Balay     break;
9551d71ae5a4SJacob Faibussowitsch   default:
9552d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9553012bc364SMatthew G. Knepley   }
95543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9555012bc364SMatthew G. Knepley }
9556adc21957SMatthew G. Knepley 
9557adc21957SMatthew G. Knepley /*@
9558adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9559adc21957SMatthew G. Knepley 
9560adc21957SMatthew G. Knepley   Logically collective
9561adc21957SMatthew G. Knepley 
9562adc21957SMatthew G. Knepley   Input Parameters:
9563adc21957SMatthew G. Knepley + dm      - The DM
9564adc21957SMatthew G. Knepley - reorder - Flag for reordering
9565adc21957SMatthew G. Knepley 
9566adc21957SMatthew G. Knepley   Level: intermediate
9567adc21957SMatthew G. Knepley 
9568adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9569adc21957SMatthew G. Knepley @*/
9570adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9571adc21957SMatthew G. Knepley {
9572adc21957SMatthew G. Knepley   PetscFunctionBegin;
9573adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9574adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9575adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9576adc21957SMatthew G. Knepley }
9577adc21957SMatthew G. Knepley 
9578adc21957SMatthew G. Knepley /*@
9579adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9580adc21957SMatthew G. Knepley 
9581adc21957SMatthew G. Knepley   Not collective
9582adc21957SMatthew G. Knepley 
9583adc21957SMatthew G. Knepley   Input Parameter:
9584adc21957SMatthew G. Knepley . dm - The DM
9585adc21957SMatthew G. Knepley 
9586adc21957SMatthew G. Knepley   Output Parameter:
9587adc21957SMatthew G. Knepley . reorder - Flag for reordering
9588adc21957SMatthew G. Knepley 
9589adc21957SMatthew G. Knepley   Level: intermediate
9590adc21957SMatthew G. Knepley 
9591adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9592adc21957SMatthew G. Knepley @*/
9593adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9594adc21957SMatthew G. Knepley {
9595adc21957SMatthew G. Knepley   PetscFunctionBegin;
9596adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9597adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9598adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9599adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9600adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9601adc21957SMatthew G. Knepley }
9602adc21957SMatthew G. Knepley 
9603cc4c1da9SBarry Smith /*@
9604adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9605adc21957SMatthew G. Knepley 
9606adc21957SMatthew G. Knepley   Logically collective
9607adc21957SMatthew G. Knepley 
9608adc21957SMatthew G. Knepley   Input Parameters:
9609adc21957SMatthew G. Knepley + dm      - The DM
9610adc21957SMatthew G. Knepley - reorder - The reordering method
9611adc21957SMatthew G. Knepley 
9612adc21957SMatthew G. Knepley   Level: intermediate
9613adc21957SMatthew G. Knepley 
9614adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9615adc21957SMatthew G. Knepley @*/
9616adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9617adc21957SMatthew G. Knepley {
9618adc21957SMatthew G. Knepley   PetscFunctionBegin;
9619adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9620adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9621adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9622adc21957SMatthew G. Knepley }
9623adc21957SMatthew G. Knepley 
9624cc4c1da9SBarry Smith /*@
9625adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9626adc21957SMatthew G. Knepley 
9627adc21957SMatthew G. Knepley   Not collective
9628adc21957SMatthew G. Knepley 
9629adc21957SMatthew G. Knepley   Input Parameter:
9630adc21957SMatthew G. Knepley . dm - The DM
9631adc21957SMatthew G. Knepley 
9632adc21957SMatthew G. Knepley   Output Parameter:
9633adc21957SMatthew G. Knepley . reorder - The reordering method
9634adc21957SMatthew G. Knepley 
9635adc21957SMatthew G. Knepley   Level: intermediate
9636adc21957SMatthew G. Knepley 
9637adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9638adc21957SMatthew G. Knepley @*/
9639adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9640adc21957SMatthew G. Knepley {
9641adc21957SMatthew G. Knepley   PetscFunctionBegin;
9642adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9643adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9644adc21957SMatthew G. Knepley   *reorder = NULL;
9645adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9646adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9647adc21957SMatthew G. Knepley }
9648