xref: /petsc/src/dm/interface/dm.c (revision 0b4b7b1c20c2ed4ade67e3d50a7710fe0ffbfca5)
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 
15cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
16cea3dcb8SSatish Balay   #include <petsc/private/valgrind/memcheck.h>
1700d952a4SJed Brown #endif
1800d952a4SJed Brown 
19732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
20d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
21708be2fdSJed 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;
2267a56275SMatthew G Knepley 
23ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
24d1b3049bSMatthew 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};
250e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
26476787b7SMatthew G. Knepley const char *const DMPolytopeTypes[] =
27476787b7SMatthew 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",
28476787b7SMatthew G. Knepley    "unknown", "unknown_cell", "unknown_face",   "invalid",  "DMPolytopeType", "DM_POLYTOPE_", NULL};
292cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
3060c22052SBarry Smith 
31a4121054SBarry Smith /*@
32bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
33*0b4b7b1cSBarry Smith   algebraic solvers, time integrators, and optimization algorithms in PETSc.
34a4121054SBarry Smith 
35d083f849SBarry Smith   Collective
36a4121054SBarry Smith 
37a4121054SBarry Smith   Input Parameter:
38bb7acecfSBarry Smith . comm - The communicator for the `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Output Parameter:
41bb7acecfSBarry Smith . dm - The `DM` object
42a4121054SBarry Smith 
43a4121054SBarry Smith   Level: beginner
44a4121054SBarry Smith 
45bb7acecfSBarry Smith   Notes:
46bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
47bb7acecfSBarry Smith 
48bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
49*0b4b7b1cSBarry Smith   error when you try to use the `dm`.
50*0b4b7b1cSBarry Smith 
51*0b4b7b1cSBarry Smith   `DM` is an orphan initialism or orphan acronym, the letters have no meaning and never did.
52bb7acecfSBarry Smith 
531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
54a4121054SBarry Smith @*/
55d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
56d71ae5a4SJacob Faibussowitsch {
57a4121054SBarry Smith   DM      v;
58e5e52638SMatthew G. Knepley   PetscDS ds;
59a4121054SBarry Smith 
60a4121054SBarry Smith   PetscFunctionBegin;
614f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
62377f809aSBarry Smith 
639566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
649566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
6562e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6649be4549SMatthew G. Knepley   v->setupcalled                          = PETSC_FALSE;
6749be4549SMatthew G. Knepley   v->setfromoptionscalled                 = PETSC_FALSE;
680298fd71SBarry Smith   v->ltogmap                              = NULL;
69a4ea9b21SRichard Tran Mills   v->bind_below                           = 0;
701411c6eeSJed Brown   v->bs                                   = 1;
71171400e9SBarry Smith   v->coloringtype                         = IS_COLORING_GLOBAL;
729566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
739566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
74c58f1c22SToby Isaac   v->labels                    = NULL;
7534aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7634aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
77c58f1c22SToby Isaac   v->depthLabel                = NULL;
78ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
791bb6d2a8SBarry Smith   v->localSection              = NULL;
801bb6d2a8SBarry Smith   v->globalSection             = NULL;
813b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
823b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8379769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
846858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
856858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
866858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8796173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
88435a35e8SMatthew G Knepley   {
89435a35e8SMatthew G Knepley     PetscInt i;
90435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
910298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
92f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
93435a35e8SMatthew G Knepley     }
94435a35e8SMatthew G Knepley   }
959566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9607218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
979566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
989566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9914f150ffSMatthew G. Knepley   v->dmBC              = NULL;
100a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
101f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
102cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1039566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1049566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1054a7a4c06SLawrence Mitchell 
1061411c6eeSJed Brown   *dm = v;
1073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
108a4121054SBarry Smith }
109a4121054SBarry Smith 
11038221697SMatthew G. Knepley /*@
111bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11238221697SMatthew G. Knepley 
113d083f849SBarry Smith   Collective
11438221697SMatthew G. Knepley 
11538221697SMatthew G. Knepley   Input Parameter:
116bb7acecfSBarry Smith . dm - The original `DM` object
11738221697SMatthew G. Knepley 
11838221697SMatthew G. Knepley   Output Parameter:
119bb7acecfSBarry Smith . newdm - The new `DM` object
12038221697SMatthew G. Knepley 
12138221697SMatthew G. Knepley   Level: beginner
12238221697SMatthew G. Knepley 
1231cb8cacdSPatrick Sanan   Notes:
124bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
125bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
126bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1271bb6d2a8SBarry Smith 
128bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12989706ed2SPatrick Sanan 
130bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
131bb7acecfSBarry Smith 
13260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
13338221697SMatthew G. Knepley @*/
134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
135d71ae5a4SJacob Faibussowitsch {
13638221697SMatthew G. Knepley   PetscSF              sf;
13738221697SMatthew G. Knepley   Vec                  coords;
13838221697SMatthew G. Knepley   void                *ctx;
139ec196627SMatthew G. Knepley   MatOrderingType      otype;
140ec196627SMatthew G. Knepley   DMReorderDefaultFlag flg;
1416858538eSMatthew G. Knepley   PetscInt             dim, cdim, i;
14238221697SMatthew G. Knepley 
14338221697SMatthew G. Knepley   PetscFunctionBegin;
14438221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1454f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1469566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1479566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
148ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
149ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
150c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
151fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1529566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1539566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1549566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1559566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1569566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
158dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1593f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1609566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1619566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1629566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1639566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
164ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetDefault(dm, &flg));
165ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetDefault(*newdm, flg));
166ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetType(dm, &otype));
167ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetType(*newdm, otype));
1686858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1696858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
170be4c1c3eSMatthew G. Knepley       DM           ncdm;
171be4c1c3eSMatthew G. Knepley       PetscSection cs;
1725a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
173be4c1c3eSMatthew G. Knepley 
1746858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1759566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
176462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1775a0206caSToby Isaac       if (pEndMax >= 0) {
1786858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1796858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1809566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1815e50ca95SJames Wright         if (dm->coordinates[i].dm->periodic.setup) {
1825e50ca95SJames Wright           ncdm->periodic.setup = dm->coordinates[i].dm->periodic.setup;
1835e50ca95SJames Wright           PetscCall(ncdm->periodic.setup(ncdm));
1845e50ca95SJames Wright         }
1856858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1866858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1879566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
188be4c1c3eSMatthew G. Knepley       }
189be4c1c3eSMatthew G. Knepley     }
1906858538eSMatthew G. Knepley   }
1919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1929566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1939566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
19438221697SMatthew G. Knepley   if (coords) {
1959566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
19638221697SMatthew G. Knepley   } else {
1979566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1989566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
19938221697SMatthew G. Knepley   }
2006858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
2016858538eSMatthew G. Knepley   if (coords) {
2026858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
2036858538eSMatthew G. Knepley   } else {
2046858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
2056858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
2066858538eSMatthew G. Knepley   }
20790b157c4SStefano Zampini   {
2084fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
2096858538eSMatthew G. Knepley 
2104fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2114fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
212c6b900c6SMatthew G. Knepley   }
21334aa8a36SMatthew G. Knepley   {
21434aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
21534aa8a36SMatthew G. Knepley 
2169566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2179566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
21834aa8a36SMatthew G. Knepley   }
2193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22038221697SMatthew G. Knepley }
22138221697SMatthew G. Knepley 
2225d83a8b1SBarry Smith /*@
2235d83a8b1SBarry Smith   DMSetVecType - Sets the type of vector to be created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2249a42bb27SBarry Smith 
22520f4b53cSBarry Smith   Logically Collective
2269a42bb27SBarry Smith 
227147403d9SBarry Smith   Input Parameters:
22832546409SMatthew G. Knepley + dm    - initial distributed array
229bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2309a42bb27SBarry Smith 
23120f4b53cSBarry Smith   Options Database Key:
232147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2339a42bb27SBarry Smith 
2349a42bb27SBarry Smith   Level: intermediate
2359a42bb27SBarry Smith 
23660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
237bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2389a42bb27SBarry Smith @*/
23932546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
240d71ae5a4SJacob Faibussowitsch {
24132546409SMatthew G. Knepley   char *tmp;
24232546409SMatthew G. Knepley 
2439a42bb27SBarry Smith   PetscFunctionBegin;
24432546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24532546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
24632546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
24732546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
24832546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2509a42bb27SBarry Smith }
2519a42bb27SBarry Smith 
2525d83a8b1SBarry Smith /*@
253bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
254c0dedaeaSBarry Smith 
25520f4b53cSBarry Smith   Logically Collective
256c0dedaeaSBarry Smith 
257c0dedaeaSBarry Smith   Input Parameter:
258c0dedaeaSBarry Smith . da - initial distributed array
259c0dedaeaSBarry Smith 
260c0dedaeaSBarry Smith   Output Parameter:
261c0dedaeaSBarry Smith . ctype - the vector type
262c0dedaeaSBarry Smith 
263c0dedaeaSBarry Smith   Level: intermediate
264c0dedaeaSBarry Smith 
26560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
266c0dedaeaSBarry Smith @*/
267d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
268d71ae5a4SJacob Faibussowitsch {
269c0dedaeaSBarry Smith   PetscFunctionBegin;
270c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
271c0dedaeaSBarry Smith   *ctype = da->vectype;
2723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
273c0dedaeaSBarry Smith }
274c0dedaeaSBarry Smith 
2755f1ad066SMatthew G Knepley /*@
276bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2775f1ad066SMatthew G Knepley 
27820f4b53cSBarry Smith   Not Collective
2795f1ad066SMatthew G Knepley 
2805f1ad066SMatthew G Knepley   Input Parameter:
281bb7acecfSBarry Smith . v - The `Vec`
2825f1ad066SMatthew G Knepley 
2835f1ad066SMatthew G Knepley   Output Parameter:
284bb7acecfSBarry Smith . dm - The `DM`
2855f1ad066SMatthew G Knepley 
2865f1ad066SMatthew G Knepley   Level: intermediate
2875f1ad066SMatthew G Knepley 
288bb7acecfSBarry Smith   Note:
289bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
290bb7acecfSBarry Smith 
2911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2925f1ad066SMatthew G Knepley @*/
293d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
294d71ae5a4SJacob Faibussowitsch {
2955f1ad066SMatthew G Knepley   PetscFunctionBegin;
2965f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2974f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2989566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3005f1ad066SMatthew G Knepley }
3015f1ad066SMatthew G Knepley 
3025f1ad066SMatthew G Knepley /*@
303bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
3045f1ad066SMatthew G Knepley 
30520f4b53cSBarry Smith   Not Collective
3065f1ad066SMatthew G Knepley 
3075f1ad066SMatthew G Knepley   Input Parameters:
308bb7acecfSBarry Smith + v  - The `Vec`
309bb7acecfSBarry Smith - dm - The `DM`
3105f1ad066SMatthew G Knepley 
31120f4b53cSBarry Smith   Level: developer
31220f4b53cSBarry Smith 
31373ff1848SBarry Smith   Notes:
314bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
315d9805387SMatthew G. Knepley 
316bb7acecfSBarry 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.
317bb7acecfSBarry Smith 
3181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3195f1ad066SMatthew G Knepley @*/
320d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
321d71ae5a4SJacob Faibussowitsch {
3225f1ad066SMatthew G Knepley   PetscFunctionBegin;
3235f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
324d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3259566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3275f1ad066SMatthew G Knepley }
3285f1ad066SMatthew G Knepley 
329cc4c1da9SBarry Smith /*@
330bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3318f1509bcSBarry Smith 
33220f4b53cSBarry Smith   Logically Collective
3338f1509bcSBarry Smith 
3348f1509bcSBarry Smith   Input Parameters:
335bb7acecfSBarry Smith + dm    - the `DM` context
3368f1509bcSBarry Smith - ctype - the matrix type
3378f1509bcSBarry Smith 
33820f4b53cSBarry Smith   Options Database Key:
3398f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3408f1509bcSBarry Smith 
3418f1509bcSBarry Smith   Level: intermediate
3428f1509bcSBarry Smith 
3431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
344bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3458f1509bcSBarry Smith @*/
346d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
347d71ae5a4SJacob Faibussowitsch {
3488f1509bcSBarry Smith   PetscFunctionBegin;
3498f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3508f1509bcSBarry Smith   dm->coloringtype = ctype;
3513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3528f1509bcSBarry Smith }
3538f1509bcSBarry Smith 
354cc4c1da9SBarry Smith /*@
355bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
356521d9a4cSLisandro Dalcin 
35720f4b53cSBarry Smith   Logically Collective
358521d9a4cSLisandro Dalcin 
359521d9a4cSLisandro Dalcin   Input Parameter:
360bb7acecfSBarry Smith . dm - the `DM` context
3618f1509bcSBarry Smith 
3628f1509bcSBarry Smith   Output Parameter:
3638f1509bcSBarry Smith . ctype - the matrix type
3648f1509bcSBarry Smith 
36520f4b53cSBarry Smith   Options Database Key:
3668f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3678f1509bcSBarry Smith 
3688f1509bcSBarry Smith   Level: intermediate
3698f1509bcSBarry Smith 
3701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
37142747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3728f1509bcSBarry Smith @*/
373d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
374d71ae5a4SJacob Faibussowitsch {
3758f1509bcSBarry Smith   PetscFunctionBegin;
3768f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3778f1509bcSBarry Smith   *ctype = dm->coloringtype;
3783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3798f1509bcSBarry Smith }
3808f1509bcSBarry Smith 
381cc4c1da9SBarry Smith /*@
382bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3838f1509bcSBarry Smith 
38420f4b53cSBarry Smith   Logically Collective
3858f1509bcSBarry Smith 
3868f1509bcSBarry Smith   Input Parameters:
387bb7acecfSBarry Smith + dm    - the `DM` context
388bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
389521d9a4cSLisandro Dalcin 
39020f4b53cSBarry Smith   Options Database Key:
391bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
392521d9a4cSLisandro Dalcin 
393521d9a4cSLisandro Dalcin   Level: intermediate
394521d9a4cSLisandro Dalcin 
39542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
396521d9a4cSLisandro Dalcin @*/
397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
398d71ae5a4SJacob Faibussowitsch {
39932546409SMatthew G. Knepley   char *tmp;
40032546409SMatthew G. Knepley 
401521d9a4cSLisandro Dalcin   PetscFunctionBegin;
402521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40332546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
40432546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
4059566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
40632546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
4073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
408521d9a4cSLisandro Dalcin }
409521d9a4cSLisandro Dalcin 
410cc4c1da9SBarry Smith /*@
41120f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
412c0dedaeaSBarry Smith 
41320f4b53cSBarry Smith   Logically Collective
414c0dedaeaSBarry Smith 
415c0dedaeaSBarry Smith   Input Parameter:
416bb7acecfSBarry Smith . dm - the `DM` context
417c0dedaeaSBarry Smith 
418c0dedaeaSBarry Smith   Output Parameter:
419c0dedaeaSBarry Smith . ctype - the matrix type
420c0dedaeaSBarry Smith 
421c0dedaeaSBarry Smith   Level: intermediate
422c0dedaeaSBarry Smith 
42342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
424c0dedaeaSBarry Smith @*/
425d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
426d71ae5a4SJacob Faibussowitsch {
427c0dedaeaSBarry Smith   PetscFunctionBegin;
428c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429c0dedaeaSBarry Smith   *ctype = dm->mattype;
4303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
431c0dedaeaSBarry Smith }
432c0dedaeaSBarry Smith 
433c688c046SMatthew G Knepley /*@
434bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
435c688c046SMatthew G Knepley 
43620f4b53cSBarry Smith   Not Collective
437c688c046SMatthew G Knepley 
438c688c046SMatthew G Knepley   Input Parameter:
439bb7acecfSBarry Smith . A - The `Mat`
440c688c046SMatthew G Knepley 
441c688c046SMatthew G Knepley   Output Parameter:
442bb7acecfSBarry Smith . dm - The `DM`
443c688c046SMatthew G Knepley 
444c688c046SMatthew G Knepley   Level: intermediate
445c688c046SMatthew G Knepley 
446bb7acecfSBarry Smith   Note:
447bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
448bb7acecfSBarry Smith 
44973ff1848SBarry Smith   Developer Note:
450bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4518f1509bcSBarry Smith 
4521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
453c688c046SMatthew G Knepley @*/
454d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
455d71ae5a4SJacob Faibussowitsch {
456c688c046SMatthew G Knepley   PetscFunctionBegin;
457c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4584f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4599566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
461c688c046SMatthew G Knepley }
462c688c046SMatthew G Knepley 
463c688c046SMatthew G Knepley /*@
464bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
465c688c046SMatthew G Knepley 
46620f4b53cSBarry Smith   Not Collective
467c688c046SMatthew G Knepley 
468c688c046SMatthew G Knepley   Input Parameters:
46920f4b53cSBarry Smith + A  - The `Mat`
47020f4b53cSBarry Smith - dm - The `DM`
471c688c046SMatthew G Knepley 
472bb7acecfSBarry Smith   Level: developer
473c688c046SMatthew G Knepley 
474bb7acecfSBarry Smith   Note:
475bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
476bb7acecfSBarry Smith 
47773ff1848SBarry Smith   Developer Note:
478bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
479bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4808f1509bcSBarry Smith 
4811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
482c688c046SMatthew G Knepley @*/
483d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
484d71ae5a4SJacob Faibussowitsch {
485c688c046SMatthew G Knepley   PetscFunctionBegin;
486c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4878865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4889566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490c688c046SMatthew G Knepley }
491c688c046SMatthew G Knepley 
492cc4c1da9SBarry Smith /*@
493bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4949a42bb27SBarry Smith 
49520f4b53cSBarry Smith   Logically Collective
4969a42bb27SBarry Smith 
497d8d19677SJose E. Roman   Input Parameters:
49860225df5SJacob Faibussowitsch + dm     - the `DM` context
499bb7acecfSBarry Smith - prefix - the prefix to prepend
5009a42bb27SBarry Smith 
50120f4b53cSBarry Smith   Level: advanced
50220f4b53cSBarry Smith 
50320f4b53cSBarry Smith   Note:
5049a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
5059a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
5069a42bb27SBarry Smith 
5071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
5089a42bb27SBarry Smith @*/
509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
510d71ae5a4SJacob Faibussowitsch {
5119a42bb27SBarry Smith   PetscFunctionBegin;
5129a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5139566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5141baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5151baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5179a42bb27SBarry Smith }
5189a42bb27SBarry Smith 
519cc4c1da9SBarry Smith /*@
520da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
521bb7acecfSBarry Smith   `DM` options in the options database.
52231697293SDave May 
52320f4b53cSBarry Smith   Logically Collective
52431697293SDave May 
52531697293SDave May   Input Parameters:
526bb7acecfSBarry Smith + dm     - the `DM` context
527bb7acecfSBarry Smith - prefix - the string to append to the current prefix
52831697293SDave May 
52920f4b53cSBarry Smith   Level: advanced
53020f4b53cSBarry Smith 
53120f4b53cSBarry Smith   Note:
532bb7acecfSBarry 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.
53331697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
53431697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
53531697293SDave May 
5361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
53731697293SDave May @*/
538d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
539d71ae5a4SJacob Faibussowitsch {
54031697293SDave May   PetscFunctionBegin;
54131697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5429566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54431697293SDave May }
54531697293SDave May 
546cc4c1da9SBarry Smith /*@
54731697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
548bb7acecfSBarry Smith   DM options in the options database.
54931697293SDave May 
55031697293SDave May   Not Collective
55131697293SDave May 
5522fe279fdSBarry Smith   Input Parameter:
553bb7acecfSBarry Smith . dm - the `DM` context
55431697293SDave May 
5552fe279fdSBarry Smith   Output Parameter:
55631697293SDave May . prefix - pointer to the prefix string used is returned
55731697293SDave May 
55831697293SDave May   Level: advanced
55931697293SDave May 
56073ff1848SBarry Smith   Fortran Note:
56120f4b53cSBarry Smith   Pass in a string 'prefix' of
56220f4b53cSBarry Smith   sufficient length to hold the prefix.
56320f4b53cSBarry Smith 
5641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
56531697293SDave May @*/
566d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
567d71ae5a4SJacob Faibussowitsch {
56831697293SDave May   PetscFunctionBegin;
56931697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5709566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57231697293SDave May }
57331697293SDave May 
57462e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
575d71ae5a4SJacob Faibussowitsch {
5766eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
57788bdff64SToby Isaac 
57888bdff64SToby Isaac   PetscFunctionBegin;
579aab5bcd8SJed Brown   *ncrefct = 0;
58088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
58188bdff64SToby Isaac     refct--;
58288bdff64SToby Isaac     if (recurseCoarse) {
58388bdff64SToby Isaac       PetscInt coarseCount;
58488bdff64SToby Isaac 
58562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
58688bdff64SToby Isaac       refct += coarseCount;
58788bdff64SToby Isaac     }
58888bdff64SToby Isaac   }
58988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
59088bdff64SToby Isaac     refct--;
59188bdff64SToby Isaac     if (recurseFine) {
59288bdff64SToby Isaac       PetscInt fineCount;
59388bdff64SToby Isaac 
59462e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
59588bdff64SToby Isaac       refct += fineCount;
59688bdff64SToby Isaac     }
59788bdff64SToby Isaac   }
59888bdff64SToby Isaac   *ncrefct = refct;
5993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60088bdff64SToby Isaac }
60188bdff64SToby Isaac 
60262e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
60362e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
60462e5d2d2SJDBetteridge {
60562e5d2d2SJDBetteridge   PetscFunctionBegin;
60662e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
6073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60862e5d2d2SJDBetteridge }
60962e5d2d2SJDBetteridge 
610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
611d71ae5a4SJacob Faibussowitsch {
6125d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
613354557abSToby Isaac 
614354557abSToby Isaac   PetscFunctionBegin;
615354557abSToby Isaac   /* destroy the labels */
616354557abSToby Isaac   while (next) {
617354557abSToby Isaac     DMLabelLink tmp = next->next;
618354557abSToby Isaac 
6195d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
620ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6219566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6229566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
623354557abSToby Isaac     next = tmp;
624354557abSToby Isaac   }
6255d80c0bfSVaclav Hapla   dm->labels = NULL;
6263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
627354557abSToby Isaac }
628354557abSToby Isaac 
62966976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
630d71ae5a4SJacob Faibussowitsch {
6316858538eSMatthew G. Knepley   PetscFunctionBegin;
6326858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6336858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6346858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6356858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6366858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6386858538eSMatthew G. Knepley }
6396858538eSMatthew G. Knepley 
6400764c050SBarry Smith /*@
641bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
64247c6ae99SBarry Smith 
64320f4b53cSBarry Smith   Collective
64447c6ae99SBarry Smith 
64547c6ae99SBarry Smith   Input Parameter:
646bb7acecfSBarry Smith . dm - the `DM` object to destroy
64747c6ae99SBarry Smith 
64847c6ae99SBarry Smith   Level: developer
64947c6ae99SBarry Smith 
6501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
65147c6ae99SBarry Smith @*/
652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
653d71ae5a4SJacob Faibussowitsch {
6546eb26441SStefano Zampini   PetscInt cnt;
65547c6ae99SBarry Smith 
65647c6ae99SBarry Smith   PetscFunctionBegin;
6573ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
658f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*dm, DM_CLASSID, 1);
65987e657c6SBarry Smith 
66088bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
66162e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
662f4f49eeaSPierre Jolivet   --((PetscObject)*dm)->refct;
6639371c9d4SSatish Balay   if (--cnt > 0) {
6649371c9d4SSatish Balay     *dm = NULL;
6653ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6669371c9d4SSatish Balay   }
667f4f49eeaSPierre Jolivet   if (((PetscObject)*dm)->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
668f4f49eeaSPierre Jolivet   ((PetscObject)*dm)->refct = 0;
6696eb26441SStefano Zampini 
6709566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6719566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
672974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
673974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6742348bcf4SPeter Brune 
675b17ce1afSJed Brown   /* Destroy the list of hooks */
676c833c3b5SJed Brown   {
677c833c3b5SJed Brown     DMCoarsenHookLink link, next;
678b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
679b17ce1afSJed Brown       next = link->next;
6809566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
681b17ce1afSJed Brown     }
6820298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
683c833c3b5SJed Brown   }
684c833c3b5SJed Brown   {
685c833c3b5SJed Brown     DMRefineHookLink link, next;
686c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
687c833c3b5SJed Brown       next = link->next;
6889566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
689c833c3b5SJed Brown     }
6900298fd71SBarry Smith     (*dm)->refinehook = NULL;
691c833c3b5SJed Brown   }
692be081cd6SPeter Brune   {
693be081cd6SPeter Brune     DMSubDomainHookLink link, next;
694be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
695be081cd6SPeter Brune       next = link->next;
6969566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
697be081cd6SPeter Brune     }
6980298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
699be081cd6SPeter Brune   }
700baf369e7SPeter Brune   {
701baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
702baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
703baf369e7SPeter Brune       next = link->next;
7049566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
705baf369e7SPeter Brune     }
7060298fd71SBarry Smith     (*dm)->gtolhook = NULL;
707baf369e7SPeter Brune   }
708d4d07f1eSToby Isaac   {
709d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
710d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
711d4d07f1eSToby Isaac       next = link->next;
7129566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
713d4d07f1eSToby Isaac     }
714d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
715d4d07f1eSToby Isaac   }
716aa1993deSMatthew G Knepley   /* Destroy the work arrays */
717aa1993deSMatthew G Knepley   {
718aa1993deSMatthew G Knepley     DMWorkLink link, next;
719936381afSPierre Jolivet     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (void *)(*dm)->workout->mem);
720aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
721aa1993deSMatthew G Knepley       next = link->next;
7229566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7239566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
724aa1993deSMatthew G Knepley     }
7250298fd71SBarry Smith     (*dm)->workin = NULL;
726aa1993deSMatthew G Knepley   }
727c58f1c22SToby Isaac   /* destroy the labels */
7289566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
729f4cdcedcSVaclav Hapla   /* destroy the fields */
7309566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
731f4cdcedcSVaclav Hapla   /* destroy the boundaries */
732e6f8dbb6SToby Isaac   {
733e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
734e6f8dbb6SToby Isaac     while (next) {
735e6f8dbb6SToby Isaac       DMBoundary b = next;
736e6f8dbb6SToby Isaac 
737e6f8dbb6SToby Isaac       next = b->next;
7389566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
739e6f8dbb6SToby Isaac     }
740e6f8dbb6SToby Isaac   }
741b17ce1afSJed Brown 
7429566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7439566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7449566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
74552536dc3SBarry Smith 
74648a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7479566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7489566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7499566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
75188ed4aceSMatthew G Knepley 
7529566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7539566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
754adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7559566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7569566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7579566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7589566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7599566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
76048a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7619566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
762e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7639566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
76448a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7656eb26441SStefano Zampini 
7669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76748a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7689566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7694fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7709566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7719566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7726858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7736858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7749566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7759566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7769566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
777ddedc8f6SJames Wright   for (PetscInt i = 0; i < (*dm)->periodic.num_affines; i++) {
778ddedc8f6SJames Wright     PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local[i]));
779ddedc8f6SJames Wright     PetscCall(VecDestroy(&(*dm)->periodic.affine[i]));
780ddedc8f6SJames Wright   }
781ddedc8f6SJames Wright   if ((*dm)->periodic.num_affines > 0) PetscCall(PetscFree2((*dm)->periodic.affine_to_local, (*dm)->periodic.affine));
7826636e97aSMatthew G Knepley 
7839566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7849566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
785e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7869566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
787732e2eb9SMatthew G Knepley 
788213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7899566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
790d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
791f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7929566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7939566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
794f918ec44SMatthew G. Knepley #endif
795435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7969566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79847c6ae99SBarry Smith }
79947c6ae99SBarry Smith 
800d7bf68aeSBarry Smith /*@
801bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
802d7bf68aeSBarry Smith 
80320f4b53cSBarry Smith   Collective
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith   Input Parameter:
806bb7acecfSBarry Smith . dm - the `DM` object to setup
807d7bf68aeSBarry Smith 
808bb7acecfSBarry Smith   Level: intermediate
809d7bf68aeSBarry Smith 
810bb7acecfSBarry Smith   Note:
811bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
812bb7acecfSBarry Smith 
8131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
814d7bf68aeSBarry Smith @*/
815d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
816d71ae5a4SJacob Faibussowitsch {
817d7bf68aeSBarry Smith   PetscFunctionBegin;
818171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8193ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
820dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8218387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
823d7bf68aeSBarry Smith }
824d7bf68aeSBarry Smith 
825d7bf68aeSBarry Smith /*@
826bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
827d7bf68aeSBarry Smith 
82820f4b53cSBarry Smith   Collective
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith   Input Parameter:
831bb7acecfSBarry Smith . dm - the `DM` object to set options for
832d7bf68aeSBarry Smith 
83320f4b53cSBarry Smith   Options Database Keys:
834bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
835bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
836bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
837a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83820f4b53cSBarry 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`
83920f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8409318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
841cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8425dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8439318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8449318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8459318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
846bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
847bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8489318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
849be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
850be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
851be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8529318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8539318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8549318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
855bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8569318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8579318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8589318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8599318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
860bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8619318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8629318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8639318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8649318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
865bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
866d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
871909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
872909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8739318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8749318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8759318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
87620f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
877d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
87820f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
879bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
880bb7acecfSBarry Smith . -dm_plex_check_faces                               - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - `DMPlexCheckFaces()`
881bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
882bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
883bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
884384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
885d7bf68aeSBarry Smith 
88695eb5ee5SVaclav Hapla   Level: intermediate
88795eb5ee5SVaclav Hapla 
8888e704042SBarry Smith   Note:
8898e704042SBarry Smith   For some `DMType` such as `DMDA` this cannot be called after `DMSetUp()` has been called.
8908e704042SBarry Smith 
8911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
892bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
8938e704042SBarry Smith          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`, `DMSetUp()`
894d7bf68aeSBarry Smith @*/
895d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
896d71ae5a4SJacob Faibussowitsch {
8977781c08eSBarry Smith   char      typeName[256];
898ca266f36SBarry Smith   PetscBool flg;
899d7bf68aeSBarry Smith 
900d7bf68aeSBarry Smith   PetscFunctionBegin;
901171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90249be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9039566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9049566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
905dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
906d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9091baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9111baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
912863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9149566063dSJacob 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));
915eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
916dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
917f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
918dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
919d0609cedSBarry Smith   PetscOptionsEnd();
9203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
921d7bf68aeSBarry Smith }
922d7bf68aeSBarry Smith 
923ffeef943SBarry Smith /*@
924bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
925fe2efc57SMark 
92620f4b53cSBarry Smith   Collective
927fe2efc57SMark 
928fe2efc57SMark   Input Parameters:
929bb7acecfSBarry Smith + dm   - the `DM` object
93020f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
93160225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
932fe2efc57SMark 
933fe2efc57SMark   Level: intermediate
934bb7acecfSBarry Smith 
935bb7acecfSBarry Smith   Note:
936bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
937bb7acecfSBarry Smith 
93860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
939fe2efc57SMark @*/
940d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
941d71ae5a4SJacob Faibussowitsch {
942fe2efc57SMark   PetscFunctionBegin;
943fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9449566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
946fe2efc57SMark }
947fe2efc57SMark 
948ffeef943SBarry Smith /*@
949bb7acecfSBarry 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
950bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
95147c6ae99SBarry Smith 
95220f4b53cSBarry Smith   Collective
95347c6ae99SBarry Smith 
954d8d19677SJose E. Roman   Input Parameters:
955bb7acecfSBarry Smith + dm - the `DM` object to view
95647c6ae99SBarry Smith - v  - the viewer
95747c6ae99SBarry Smith 
95820f4b53cSBarry Smith   Level: beginner
95920f4b53cSBarry Smith 
96049c89c76SBlaise Bourdin   Notes:
96149c89c76SBlaise Bourdin 
96249c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWERHDF5` i.e. HDF5 format can be used with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` to save multiple `DMPLEX`
963bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
964bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
965cd7e8a5eSksagiyam 
96649c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWEREXODUSII` i.e. ExodusII format assumes that element blocks (mapped to "Cell sets" labels)
96749c89c76SBlaise Bourdin   consists of sequentially numbered cells.
96849c89c76SBlaise Bourdin 
96949c89c76SBlaise Bourdin   If `dm` has been distributed, only the part of the `DM` on MPI rank 0 (including "ghost" cells and vertices) will be written.
97049c89c76SBlaise Bourdin 
97149c89c76SBlaise Bourdin   Only TRI, TET, QUAD, and HEX cells are supported.
97249c89c76SBlaise Bourdin 
97349c89c76SBlaise 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.
97449c89c76SBlaise Bourdin   The order of the mesh shall be set using `PetscViewerExodusIISetOrder()`
97549c89c76SBlaise Bourdin 
976d7c1f440SPierre Jolivet   Variable names can be set and queried using `PetscViewerExodusII[Set/Get][Nodal/Zonal]VariableNames[s]`.
97749c89c76SBlaise Bourdin 
9781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
97947c6ae99SBarry Smith @*/
980d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
981d71ae5a4SJacob Faibussowitsch {
98232c0f0efSBarry Smith   PetscBool         isbinary;
98376a8abe0SBarry Smith   PetscMPIInt       size;
98476a8abe0SBarry Smith   PetscViewerFormat format;
98547c6ae99SBarry Smith 
98647c6ae99SBarry Smith   PetscFunctionBegin;
987171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98848a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
989b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
99074903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
99174903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
99274903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
99374903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
99474903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
99574903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
99674903a4fSStefano Zampini      in an error here */
99774903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9989566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
999b1b135c8SBarry Smith 
10009566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
10019566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
10023ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
10039566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
10049566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
100532c0f0efSBarry Smith   if (isbinary) {
100655849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
100732c0f0efSBarry Smith     char     type[256];
100832c0f0efSBarry Smith 
10099566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
1010c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
10119566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
101232c0f0efSBarry Smith   }
1013dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
10143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
101547c6ae99SBarry Smith }
101647c6ae99SBarry Smith 
101747c6ae99SBarry Smith /*@
1018bb7acecfSBarry 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,
1019bb7acecfSBarry Smith   that is it has no ghost locations.
102047c6ae99SBarry Smith 
102120f4b53cSBarry Smith   Collective
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith   Input Parameter:
1024bb7acecfSBarry Smith . dm - the `DM` object
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith   Output Parameter:
102747c6ae99SBarry Smith . vec - the global vector
102847c6ae99SBarry Smith 
1029073dac72SJed Brown   Level: beginner
103047c6ae99SBarry Smith 
10311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1032bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
103347c6ae99SBarry Smith @*/
1034d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1035d71ae5a4SJacob Faibussowitsch {
103647c6ae99SBarry Smith   PetscFunctionBegin;
1037171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10384f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1039dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
104076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1041c6b011d8SStefano Zampini     DM vdm;
1042c6b011d8SStefano Zampini 
10439566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10447a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1045c6b011d8SStefano Zampini   }
10463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104747c6ae99SBarry Smith }
104847c6ae99SBarry Smith 
104947c6ae99SBarry Smith /*@
1050bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith   Not Collective
105347c6ae99SBarry Smith 
105447c6ae99SBarry Smith   Input Parameter:
1055bb7acecfSBarry Smith . dm - the `DM` object
105647c6ae99SBarry Smith 
105747c6ae99SBarry Smith   Output Parameter:
105847c6ae99SBarry Smith . vec - the local vector
105947c6ae99SBarry Smith 
1060073dac72SJed Brown   Level: beginner
106147c6ae99SBarry Smith 
106220f4b53cSBarry Smith   Note:
1063bb7acecfSBarry 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.
1064bb7acecfSBarry Smith 
10651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1066bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
106747c6ae99SBarry Smith @*/
1068d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1069d71ae5a4SJacob Faibussowitsch {
107047c6ae99SBarry Smith   PetscFunctionBegin;
1071171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10724f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1073dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
107476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1075c6b011d8SStefano Zampini     DM vdm;
1076c6b011d8SStefano Zampini 
10779566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10787a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1079c6b011d8SStefano Zampini   }
10803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
108147c6ae99SBarry Smith }
108247c6ae99SBarry Smith 
10831411c6eeSJed Brown /*@
1084bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10851411c6eeSJed Brown 
108620f4b53cSBarry Smith   Collective
10871411c6eeSJed Brown 
10881411c6eeSJed Brown   Input Parameter:
1089bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10901411c6eeSJed Brown 
10911411c6eeSJed Brown   Output Parameter:
10921411c6eeSJed Brown . ltog - the mapping
10931411c6eeSJed Brown 
1094bb7acecfSBarry Smith   Level: advanced
10951411c6eeSJed Brown 
10961411c6eeSJed Brown   Notes:
1097bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10981411c6eeSJed Brown 
1099bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1100bb7acecfSBarry Smith   need to use this function with those objects.
1101bb7acecfSBarry Smith 
1102bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1103bb7acecfSBarry Smith 
110460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1105bb7acecfSBarry Smith           `DMCreateMatrix()`
11061411c6eeSJed Brown @*/
1107d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1108d71ae5a4SJacob Faibussowitsch {
11090be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
11101411c6eeSJed Brown 
11111411c6eeSJed Brown   PetscFunctionBegin;
11121411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11134f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
11141411c6eeSJed Brown   if (!dm->ltogmap) {
111537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
111637d0c07bSMatthew G Knepley 
11179566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
111837d0c07bSMatthew G Knepley     if (section) {
1119a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
112037d0c07bSMatthew G Knepley       PetscInt       *ltog;
1121ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
112237d0c07bSMatthew G Knepley 
11239566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11259566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11269566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
112737d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1128e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
112937d0c07bSMatthew G Knepley 
113037d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11329566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11339566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11349566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11351a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11361a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1137ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11385227eafbSStefano Zampini 
1139e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11405227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1141e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1142e6befd46SJed Brown             cind++;
1143e6befd46SJed Brown           } else {
11445227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1145e6befd46SJed Brown           }
114637d0c07bSMatthew G Knepley         }
114737d0c07bSMatthew G Knepley       }
1148bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11491690c2aeSBarry Smith       bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
11509371c9d4SSatish Balay       bsLocal[1] = bs;
11519566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11529371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11539371c9d4SSatish Balay         bs = 1;
11549371c9d4SSatish Balay       } else {
11559371c9d4SSatish Balay         bs = bsMinMax[0];
11569371c9d4SSatish Balay       }
11577591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11587591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1159ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1160ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1161ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1162ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1163ca469d19SJed Brown         }
1164ccf3bd66SMatthew G. Knepley         n /= bs;
1165ccf3bd66SMatthew G. Knepley       }
11669566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1167dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
116837d0c07bSMatthew G Knepley   }
11691411c6eeSJed Brown   *ltog = dm->ltogmap;
11703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11711411c6eeSJed Brown }
11721411c6eeSJed Brown 
11731411c6eeSJed Brown /*@
1174bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11751411c6eeSJed Brown 
11761411c6eeSJed Brown   Not Collective
11771411c6eeSJed Brown 
11781411c6eeSJed Brown   Input Parameter:
1179bb7acecfSBarry Smith . dm - the `DM` with block structure
11801411c6eeSJed Brown 
11811411c6eeSJed Brown   Output Parameter:
11821411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11831411c6eeSJed Brown 
11841411c6eeSJed Brown   Level: intermediate
11851411c6eeSJed Brown 
118673ff1848SBarry Smith   Notes:
1187bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1188bb7acecfSBarry Smith 
1189bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1190bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1191bb7acecfSBarry Smith 
11921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11931411c6eeSJed Brown @*/
1194d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1195d71ae5a4SJacob Faibussowitsch {
11961411c6eeSJed Brown   PetscFunctionBegin;
11971411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11984f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11997a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
12001411c6eeSJed Brown   *bs = dm->bs;
12013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12021411c6eeSJed Brown }
12031411c6eeSJed Brown 
1204ffeef943SBarry Smith /*@
1205bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1206bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
120747c6ae99SBarry Smith 
120820f4b53cSBarry Smith   Collective
120947c6ae99SBarry Smith 
1210d8d19677SJose E. Roman   Input Parameters:
1211bb7acecfSBarry Smith + dmc - the `DM` object
1212bb7acecfSBarry Smith - dmf - the second, finer `DM` object
121347c6ae99SBarry Smith 
1214d8d19677SJose E. Roman   Output Parameters:
121547c6ae99SBarry Smith + mat - the interpolation
1216b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
121747c6ae99SBarry Smith 
121847c6ae99SBarry Smith   Level: developer
121947c6ae99SBarry Smith 
122095452b02SPatrick Sanan   Notes:
1221bb7acecfSBarry 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
1222bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1223d52bd9f3SBarry Smith 
1224bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1225bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
122685afcc9aSBarry Smith 
12271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
122847c6ae99SBarry Smith @*/
1229d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1230d71ae5a4SJacob Faibussowitsch {
123147c6ae99SBarry Smith   PetscFunctionBegin;
1232a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1233a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12344f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1236dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
123947c6ae99SBarry Smith }
124047c6ae99SBarry Smith 
12413ad4599aSBarry Smith /*@
1242a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1243a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12442ed6491fSPatrick Sanan 
12452ed6491fSPatrick Sanan   Input Parameters:
1246bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1247bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12482ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12492ed6491fSPatrick Sanan 
12502ed6491fSPatrick Sanan   Output Parameter:
12512ed6491fSPatrick Sanan . scale - the scaled vector
12522ed6491fSPatrick Sanan 
1253bb7acecfSBarry Smith   Level: advanced
12542ed6491fSPatrick Sanan 
125573ff1848SBarry Smith   Note:
1256a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1257a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1258a4e35b19SJacob Faibussowitsch 
125973ff1848SBarry Smith   Developer Note:
1260bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1261e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1262e9c74fd6SRichard Tran Mills 
126360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12642ed6491fSPatrick Sanan @*/
1265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1266d71ae5a4SJacob Faibussowitsch {
12672ed6491fSPatrick Sanan   Vec         fine;
12682ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12699704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1270e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12719704db99SRichard Tran Mills #endif
12722ed6491fSPatrick Sanan 
12732ed6491fSPatrick Sanan   PetscFunctionBegin;
12749566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12759566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12769566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12779704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12789704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12799704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12809704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12819566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1282e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12839566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12849566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12859566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
128683aa49f4SRichard Tran Mills   }
12879704db99SRichard Tran Mills #endif
12889566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12899566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12909566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12922ed6491fSPatrick Sanan }
12932ed6491fSPatrick Sanan 
12942ed6491fSPatrick Sanan /*@
1295bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1296bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12973ad4599aSBarry Smith 
129820f4b53cSBarry Smith   Collective
12993ad4599aSBarry Smith 
1300d8d19677SJose E. Roman   Input Parameters:
1301bb7acecfSBarry Smith + dmc - the `DM` object
1302bb7acecfSBarry Smith - dmf - the second, finer `DM` object
13033ad4599aSBarry Smith 
13043ad4599aSBarry Smith   Output Parameter:
13053ad4599aSBarry Smith . mat - the restriction
13063ad4599aSBarry Smith 
13073ad4599aSBarry Smith   Level: developer
13083ad4599aSBarry Smith 
1309bb7acecfSBarry Smith   Note:
1310bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1311bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
13123ad4599aSBarry Smith 
13131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
13143ad4599aSBarry Smith @*/
1315d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1316d71ae5a4SJacob Faibussowitsch {
13173ad4599aSBarry Smith   PetscFunctionBegin;
1318a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1319a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13204f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1322dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13239566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13253ad4599aSBarry Smith }
13263ad4599aSBarry Smith 
132747c6ae99SBarry Smith /*@
1328a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
132947c6ae99SBarry Smith 
133020f4b53cSBarry Smith   Collective
133147c6ae99SBarry Smith 
1332d8d19677SJose E. Roman   Input Parameters:
1333bb7acecfSBarry Smith + dac - the `DM` object
1334bb7acecfSBarry Smith - daf - the second, finer `DM` object
133547c6ae99SBarry Smith 
133647c6ae99SBarry Smith   Output Parameter:
13376dbf9973SLawrence Mitchell . mat - the injection
133847c6ae99SBarry Smith 
133947c6ae99SBarry Smith   Level: developer
134047c6ae99SBarry Smith 
1341a4e35b19SJacob Faibussowitsch   Notes:
1342a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1343a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1344a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1345a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1346a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1347a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1348a4e35b19SJacob Faibussowitsch 
1349bb7acecfSBarry 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
1350bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
135185afcc9aSBarry Smith 
13521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1353bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
135447c6ae99SBarry Smith @*/
1355d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1356d71ae5a4SJacob Faibussowitsch {
135747c6ae99SBarry Smith   PetscFunctionBegin;
1358a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1359a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13604f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13619566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1362dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13639566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
136547c6ae99SBarry Smith }
136647c6ae99SBarry Smith 
1367b412c318SBarry Smith /*@
1368bb7acecfSBarry 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
1369bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1370bd041c0cSMatthew G. Knepley 
137120f4b53cSBarry Smith   Collective
1372bd041c0cSMatthew G. Knepley 
1373d8d19677SJose E. Roman   Input Parameters:
1374bb7acecfSBarry Smith + dmc - the target `DM` object
13758c1c0954SStefano Zampini - dmf - the source `DM` object, can be `NULL`
1376bd041c0cSMatthew G. Knepley 
1377bd041c0cSMatthew G. Knepley   Output Parameter:
1378b4937a87SMatthew G. Knepley . mat - the mass matrix
1379bd041c0cSMatthew G. Knepley 
1380bd041c0cSMatthew G. Knepley   Level: developer
1381bd041c0cSMatthew G. Knepley 
1382bb7acecfSBarry Smith   Notes:
1383bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1384bb7acecfSBarry Smith 
13858c1c0954SStefano 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()`
1386bb7acecfSBarry Smith 
138742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1388bd041c0cSMatthew G. Knepley @*/
1389d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1390d71ae5a4SJacob Faibussowitsch {
1391bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1392b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
13938c1c0954SStefano Zampini   if (!dmf) dmf = dmc;
1394b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13954f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13968c1c0954SStefano Zampini   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, dmc, dmf, 0, 0));
1397dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13988c1c0954SStefano Zampini   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, dmc, dmf, 0, 0));
13993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1400b4937a87SMatthew G. Knepley }
1401b4937a87SMatthew G. Knepley 
1402b4937a87SMatthew G. Knepley /*@
1403bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1404b4937a87SMatthew G. Knepley 
140520f4b53cSBarry Smith   Collective
1406b4937a87SMatthew G. Knepley 
1407b4937a87SMatthew G. Knepley   Input Parameter:
1408bb7acecfSBarry Smith . dm - the `DM` object
1409b4937a87SMatthew G. Knepley 
14107dcfde4dSJose E. Roman   Output Parameters:
14118e9849d2SStefano Zampini + llm - the local lumped mass matrix, which is a diagonal matrix, represented as a vector
14128e9849d2SStefano Zampini - lm  - the global lumped mass matrix, which is a diagonal matrix, represented as a vector
1413b4937a87SMatthew G. Knepley 
1414b4937a87SMatthew G. Knepley   Level: developer
1415b4937a87SMatthew G. Knepley 
1416bb7acecfSBarry Smith   Note:
1417bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1418bb7acecfSBarry Smith 
141960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1420b4937a87SMatthew G. Knepley @*/
14218e9849d2SStefano Zampini PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *llm, Vec *lm)
1422d71ae5a4SJacob Faibussowitsch {
1423b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1424b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14258e9849d2SStefano Zampini   if (llm) PetscAssertPointer(llm, 2);
14268e9849d2SStefano Zampini   if (lm) PetscAssertPointer(lm, 3);
14278e9849d2SStefano Zampini   if (llm || lm) PetscUseTypeMethod(dm, createmassmatrixlumped, llm, lm);
14283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1429bd041c0cSMatthew G. Knepley }
1430bd041c0cSMatthew G. Knepley 
1431bd041c0cSMatthew G. Knepley /*@
1432bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1433bb7acecfSBarry Smith   of a PDE on the `DM`.
143447c6ae99SBarry Smith 
143520f4b53cSBarry Smith   Collective
143647c6ae99SBarry Smith 
1437d8d19677SJose E. Roman   Input Parameters:
1438bb7acecfSBarry Smith + dm    - the `DM` object
1439bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
144047c6ae99SBarry Smith 
144147c6ae99SBarry Smith   Output Parameter:
144247c6ae99SBarry Smith . coloring - the coloring
144347c6ae99SBarry Smith 
14441bf8429eSBarry Smith   Level: developer
14451bf8429eSBarry Smith 
1446ec5066bdSBarry Smith   Notes:
1447bb7acecfSBarry 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
1448bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1449ec5066bdSBarry Smith 
1450bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14511bf8429eSBarry 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,
14521bf8429eSBarry Smith   otherwise an error will be generated.
1453ec5066bdSBarry Smith 
14541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1455aab9d709SJed Brown @*/
1456d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1457d71ae5a4SJacob Faibussowitsch {
145847c6ae99SBarry Smith   PetscFunctionBegin;
1459171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14604f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1461dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
146347c6ae99SBarry Smith }
146447c6ae99SBarry Smith 
1465b412c318SBarry Smith /*@
1466bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
146747c6ae99SBarry Smith 
146820f4b53cSBarry Smith   Collective
146947c6ae99SBarry Smith 
147047c6ae99SBarry Smith   Input Parameter:
1471bb7acecfSBarry Smith . dm - the `DM` object
147247c6ae99SBarry Smith 
147347c6ae99SBarry Smith   Output Parameter:
147447c6ae99SBarry Smith . mat - the empty Jacobian
147547c6ae99SBarry Smith 
147620f4b53cSBarry Smith   Options Database Key:
1477bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1478f27dd7c6SMatthew G. Knepley 
147920f4b53cSBarry Smith   Level: beginner
148020f4b53cSBarry Smith 
148195452b02SPatrick Sanan   Notes:
148295452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
148394013140SBarry Smith   do not need to do it yourself.
148494013140SBarry Smith 
148594013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1486bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
148794013140SBarry Smith 
1488bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
148994013140SBarry Smith   internally by PETSc.
149094013140SBarry Smith 
1491bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1492bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
149394013140SBarry Smith 
14941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1495aab9d709SJed Brown @*/
1496d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1497d71ae5a4SJacob Faibussowitsch {
149847c6ae99SBarry Smith   PetscFunctionBegin;
1499171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15004f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
15019566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
15029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1503dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
150476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1505c6b011d8SStefano Zampini     DM mdm;
1506c6b011d8SStefano Zampini 
15079566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
15087a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1509c6b011d8SStefano Zampini   }
1510e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1511e5e52638SMatthew G. Knepley   if (dm->Nf) {
1512e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1513649ef022SMatthew Knepley     PetscInt     Nf, f;
1514e571a35bSMatthew G. Knepley 
15159566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1516649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1517649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15189566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15199566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15209566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1521649ef022SMatthew Knepley         break;
1522e571a35bSMatthew G. Knepley       }
1523649ef022SMatthew Knepley     }
1524649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1525649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15269566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15279566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15289566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1529e571a35bSMatthew G. Knepley       }
1530e571a35bSMatthew G. Knepley     }
1531e571a35bSMatthew G. Knepley   }
15329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
153447c6ae99SBarry Smith }
153547c6ae99SBarry Smith 
1536732e2eb9SMatthew G Knepley /*@
1537a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1538a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1539a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1540aa0f6e3cSJed Brown 
154120f4b53cSBarry Smith   Logically Collective
1542aa0f6e3cSJed Brown 
1543aa0f6e3cSJed Brown   Input Parameters:
1544bb7acecfSBarry Smith + dm   - the `DM`
1545bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1546aa0f6e3cSJed Brown 
1547aa0f6e3cSJed Brown   Level: developer
1548aa0f6e3cSJed Brown 
154973ff1848SBarry Smith   Note:
1550a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1551a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1552a4e35b19SJacob Faibussowitsch 
15531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1554aa0f6e3cSJed Brown @*/
1555d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1556d71ae5a4SJacob Faibussowitsch {
1557aa0f6e3cSJed Brown   PetscFunctionBegin;
1558aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1559aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1561aa0f6e3cSJed Brown }
1562aa0f6e3cSJed Brown 
1563aa0f6e3cSJed Brown /*@
1564bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1565732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1566732e2eb9SMatthew G Knepley 
156720f4b53cSBarry Smith   Logically Collective
1568732e2eb9SMatthew G Knepley 
1569d8d19677SJose E. Roman   Input Parameters:
1570bb7acecfSBarry Smith + dm   - the `DM`
1571bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1572732e2eb9SMatthew G Knepley 
157320f4b53cSBarry Smith   Options Database Key:
1574bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1575f27dd7c6SMatthew G. Knepley 
157620f4b53cSBarry Smith   Level: developer
157720f4b53cSBarry Smith 
15781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1579732e2eb9SMatthew G Knepley @*/
1580d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1581d71ae5a4SJacob Faibussowitsch {
1582732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1583732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1584732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1586732e2eb9SMatthew G Knepley }
1587732e2eb9SMatthew G Knepley 
1588b06ff27eSHong Zhang /*@
1589*0b4b7b1cSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix nonzero structure will be created
1590bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1591b06ff27eSHong Zhang 
159220f4b53cSBarry Smith   Logically Collective
1593b06ff27eSHong Zhang 
1594d8d19677SJose E. Roman   Input Parameters:
1595bb7acecfSBarry Smith + dm   - the `DM`
1596*0b4b7b1cSBarry Smith - only - `PETSC_TRUE` if you only want matrix nonzero structure
1597b06ff27eSHong Zhang 
1598b06ff27eSHong Zhang   Level: developer
1599bb7acecfSBarry Smith 
16001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1601b06ff27eSHong Zhang @*/
1602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1603d71ae5a4SJacob Faibussowitsch {
1604b06ff27eSHong Zhang   PetscFunctionBegin;
1605b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1606b06ff27eSHong Zhang   dm->structure_only = only;
16073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1608b06ff27eSHong Zhang }
1609b06ff27eSHong Zhang 
1610863027abSJed Brown /*@
1611863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1612863027abSJed Brown 
161320f4b53cSBarry Smith   Logically Collective
1614863027abSJed Brown 
1615863027abSJed Brown   Input Parameters:
1616863027abSJed Brown + dm    - the `DM`
1617863027abSJed Brown - btype - block by topological point or field node
1618863027abSJed Brown 
161920f4b53cSBarry Smith   Options Database Key:
16200e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1621863027abSJed Brown 
162220f4b53cSBarry Smith   Level: advanced
162320f4b53cSBarry Smith 
16241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1625863027abSJed Brown @*/
1626863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1627863027abSJed Brown {
1628863027abSJed Brown   PetscFunctionBegin;
1629863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1630863027abSJed Brown   dm->blocking_type = btype;
16313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1632863027abSJed Brown }
1633863027abSJed Brown 
1634863027abSJed Brown /*@
1635863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1636863027abSJed Brown 
1637863027abSJed Brown   Not Collective
1638863027abSJed Brown 
16392fe279fdSBarry Smith   Input Parameter:
1640863027abSJed Brown . dm - the `DM`
1641863027abSJed Brown 
16422fe279fdSBarry Smith   Output Parameter:
1643863027abSJed Brown . btype - block by topological point or field node
1644863027abSJed Brown 
1645863027abSJed Brown   Level: advanced
1646863027abSJed Brown 
16471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1648863027abSJed Brown @*/
1649863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1650863027abSJed Brown {
1651863027abSJed Brown   PetscFunctionBegin;
1652863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16534f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1654863027abSJed Brown   *btype = dm->blocking_type;
16553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1656863027abSJed Brown }
1657863027abSJed Brown 
1658a89ea682SMatthew G Knepley /*@C
1659bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1660a89ea682SMatthew G Knepley 
1661a89ea682SMatthew G Knepley   Not Collective
1662a89ea682SMatthew G Knepley 
1663a89ea682SMatthew G Knepley   Input Parameters:
1664bb7acecfSBarry Smith + dm    - the `DM` object
1665a5b23f4aSJose E. Roman . count - The minimum size
166620f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1667a89ea682SMatthew G Knepley 
1668a89ea682SMatthew G Knepley   Output Parameter:
166960225df5SJacob Faibussowitsch . mem - the work array
1670a89ea682SMatthew G Knepley 
1671a89ea682SMatthew G Knepley   Level: developer
1672a89ea682SMatthew G Knepley 
167373ff1848SBarry Smith   Notes:
1674da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1675bb7acecfSBarry Smith 
1676bb7acecfSBarry Smith   The array may contain nonzero values
1677bb7acecfSBarry Smith 
16781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1679a89ea682SMatthew G Knepley @*/
1680d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1681d71ae5a4SJacob Faibussowitsch {
1682aa1993deSMatthew G Knepley   DMWorkLink  link;
168369291d52SBarry Smith   PetscMPIInt dsize;
1684a89ea682SMatthew G Knepley 
1685a89ea682SMatthew G Knepley   PetscFunctionBegin;
1686a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16874f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1688442f3b32SStefano Zampini   if (!count) {
1689442f3b32SStefano Zampini     *(void **)mem = NULL;
1690442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1691442f3b32SStefano Zampini   }
1692aa1993deSMatthew G Knepley   if (dm->workin) {
1693aa1993deSMatthew G Knepley     link       = dm->workin;
1694aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1695aa1993deSMatthew G Knepley   } else {
16964dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1697a89ea682SMatthew G Knepley   }
1698b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1699b77fa653SStefano Zampini      Get size directly */
1700b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1701b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1702b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1703b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1704b77fa653SStefano Zampini #endif
1705b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1706b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1707b77fa653SStefano Zampini #endif
1708b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1709b77fa653SStefano Zampini 
17105056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
17119566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
17129566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1713854ce69bSBarry Smith     link->bytes = dsize * count;
1714aa1993deSMatthew G Knepley   }
1715aa1993deSMatthew G Knepley   link->next  = dm->workout;
1716aa1993deSMatthew G Knepley   dm->workout = link;
1717cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
171800d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
171900d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
172000d952a4SJed Brown #endif
1721aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1723a89ea682SMatthew G Knepley }
1724a89ea682SMatthew G Knepley 
1725aa1993deSMatthew G Knepley /*@C
1726bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1727aa1993deSMatthew G Knepley 
1728aa1993deSMatthew G Knepley   Not Collective
1729aa1993deSMatthew G Knepley 
1730aa1993deSMatthew G Knepley   Input Parameters:
1731bb7acecfSBarry Smith + dm    - the `DM` object
1732a5b23f4aSJose E. Roman . count - The minimum size
173320f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1734aa1993deSMatthew G Knepley 
1735aa1993deSMatthew G Knepley   Output Parameter:
173660225df5SJacob Faibussowitsch . mem - the work array
1737aa1993deSMatthew G Knepley 
1738aa1993deSMatthew G Knepley   Level: developer
1739aa1993deSMatthew G Knepley 
174073ff1848SBarry Smith   Developer Note:
1741bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1742147403d9SBarry Smith 
17431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1744aa1993deSMatthew G Knepley @*/
1745d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1746d71ae5a4SJacob Faibussowitsch {
1747aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1748aa1993deSMatthew G Knepley 
1749aa1993deSMatthew G Knepley   PetscFunctionBegin;
1750aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17514f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1752a4e35b19SJacob Faibussowitsch   (void)count;
1753a4e35b19SJacob Faibussowitsch   (void)dtype;
1754442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1755aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1756aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1757aa1993deSMatthew G Knepley       *p            = link->next;
1758aa1993deSMatthew G Knepley       link->next    = dm->workin;
1759aa1993deSMatthew G Knepley       dm->workin    = link;
17600298fd71SBarry Smith       *(void **)mem = NULL;
17613ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1762aa1993deSMatthew G Knepley     }
1763aa1993deSMatthew G Knepley   }
1764aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1765aa1993deSMatthew G Knepley }
1766e7c4fc90SDmitry Karpeev 
17678cda7954SMatthew G. Knepley /*@C
1768bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1769bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17708cda7954SMatthew G. Knepley 
177120f4b53cSBarry Smith   Logically Collective; No Fortran Support
17728cda7954SMatthew G. Knepley 
17738cda7954SMatthew G. Knepley   Input Parameters:
1774bb7acecfSBarry Smith + dm     - The `DM`
17758cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17768cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17778cda7954SMatthew G. Knepley 
177820f4b53cSBarry Smith   Calling sequence of `nullsp`:
1779bb7acecfSBarry Smith + dm        - The present `DM`
1780bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1781147403d9SBarry Smith . field     - The field number in dm
1782147403d9SBarry Smith - nullSpace - The nullspace for the given field
17838cda7954SMatthew G. Knepley 
178449762cbcSSatish Balay   Level: intermediate
178549762cbcSSatish Balay 
17861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1787147403d9SBarry Smith @*/
1788a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1789d71ae5a4SJacob Faibussowitsch {
1790435a35e8SMatthew G Knepley   PetscFunctionBegin;
1791435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17927a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1793435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1795435a35e8SMatthew G Knepley }
1796435a35e8SMatthew G Knepley 
17978cda7954SMatthew G. Knepley /*@C
1798bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17998cda7954SMatthew G. Knepley 
180020f4b53cSBarry Smith   Not Collective; No Fortran Support
18018cda7954SMatthew G. Knepley 
18028cda7954SMatthew G. Knepley   Input Parameters:
1803bb7acecfSBarry Smith + dm    - The `DM`
18048cda7954SMatthew G. Knepley - field - The field number for the nullspace
18058cda7954SMatthew G. Knepley 
18068cda7954SMatthew G. Knepley   Output Parameter:
18078cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
18088cda7954SMatthew G. Knepley 
180920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1810147403d9SBarry Smith + dm        - The present DM
1811147403d9SBarry Smith . origField - The field number given above, in the original DM
1812147403d9SBarry Smith . field     - The field number in dm
1813147403d9SBarry Smith - nullSpace - The nullspace for the given field
18148cda7954SMatthew G. Knepley 
181549762cbcSSatish Balay   Level: intermediate
181649762cbcSSatish Balay 
18171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1818147403d9SBarry Smith @*/
1819a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1820d71ae5a4SJacob Faibussowitsch {
18210a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18220a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18234f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18247a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18250a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18270a50eb56SMatthew G. Knepley }
18280a50eb56SMatthew G. Knepley 
18298cda7954SMatthew G. Knepley /*@C
1830bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18318cda7954SMatthew G. Knepley 
183220f4b53cSBarry Smith   Logically Collective; No Fortran Support
18338cda7954SMatthew G. Knepley 
18348cda7954SMatthew G. Knepley   Input Parameters:
1835bb7acecfSBarry Smith + dm     - The `DM`
18368cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18378cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18388cda7954SMatthew G. Knepley 
183920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1840bb7acecfSBarry Smith + dm        - The present `DM`
1841bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1842147403d9SBarry Smith . field     - The field number in dm
1843147403d9SBarry Smith - nullSpace - The nullspace for the given field
18448cda7954SMatthew G. Knepley 
184549762cbcSSatish Balay   Level: intermediate
184649762cbcSSatish Balay 
18471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1848bb7acecfSBarry Smith           `MatNullSpace`
1849147403d9SBarry Smith @*/
1850a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1851d71ae5a4SJacob Faibussowitsch {
1852f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1853f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18547a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1855f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1857f9d4088aSMatthew G. Knepley }
1858f9d4088aSMatthew G. Knepley 
18598cda7954SMatthew G. Knepley /*@C
1860bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18618cda7954SMatthew G. Knepley 
186220f4b53cSBarry Smith   Not Collective; No Fortran Support
18638cda7954SMatthew G. Knepley 
18648cda7954SMatthew G. Knepley   Input Parameters:
1865bb7acecfSBarry Smith + dm    - The `DM`
18668cda7954SMatthew G. Knepley - field - The field number for the nullspace
18678cda7954SMatthew G. Knepley 
18688cda7954SMatthew G. Knepley   Output Parameter:
18698cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18708cda7954SMatthew G. Knepley 
187120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1872bb7acecfSBarry Smith + dm        - The present `DM`
1873bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1874147403d9SBarry Smith . field     - The field number in dm
1875147403d9SBarry Smith - nullSpace - The nullspace for the given field
18768cda7954SMatthew G. Knepley 
187749762cbcSSatish Balay   Level: intermediate
187849762cbcSSatish Balay 
18791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1880bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1881147403d9SBarry Smith @*/
1882a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1883d71ae5a4SJacob Faibussowitsch {
1884f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1885f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18864f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18877a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1888f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1890f9d4088aSMatthew G. Knepley }
1891f9d4088aSMatthew G. Knepley 
18924f3b5142SJed Brown /*@C
1893bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18944d343eeaSMatthew G Knepley 
189520f4b53cSBarry Smith   Not Collective; No Fortran Support
18964d343eeaSMatthew G Knepley 
18974d343eeaSMatthew G Knepley   Input Parameter:
1898bb7acecfSBarry Smith . dm - the `DM` object
18994d343eeaSMatthew G Knepley 
19004d343eeaSMatthew G Knepley   Output Parameters:
190120f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
190220f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
190320f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
19044d343eeaSMatthew G Knepley 
19054d343eeaSMatthew G Knepley   Level: intermediate
19064d343eeaSMatthew G Knepley 
1907bb7acecfSBarry Smith   Note:
190873ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
190973ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1910bb7acecfSBarry Smith   `PetscFree()`.
191121c9b008SJed Brown 
191273ff1848SBarry Smith   Developer Note:
1913bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1914bb7acecfSBarry Smith   likely be removed.
1915bb7acecfSBarry Smith 
19161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1917bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19184d343eeaSMatthew G Knepley @*/
1919d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1920d71ae5a4SJacob Faibussowitsch {
192137d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19224d343eeaSMatthew G Knepley 
19234d343eeaSMatthew G Knepley   PetscFunctionBegin;
19244d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
192569ca1f37SDmitry Karpeev   if (numFields) {
19264f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
192769ca1f37SDmitry Karpeev     *numFields = 0;
192869ca1f37SDmitry Karpeev   }
192937d0c07bSMatthew G Knepley   if (fieldNames) {
19304f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19310298fd71SBarry Smith     *fieldNames = NULL;
193269ca1f37SDmitry Karpeev   }
193369ca1f37SDmitry Karpeev   if (fields) {
19344f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19350298fd71SBarry Smith     *fields = NULL;
193669ca1f37SDmitry Karpeev   }
19379566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
193837d0c07bSMatthew G Knepley   if (section) {
19393a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
194037d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
194137d0c07bSMatthew G Knepley 
19429566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19459566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
194637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
194737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
194937d0c07bSMatthew G Knepley     }
195037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195137d0c07bSMatthew G Knepley       PetscInt gdof;
195237d0c07bSMatthew G Knepley 
19539566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195437d0c07bSMatthew G Knepley       if (gdof > 0) {
195537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19563a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
195737d0c07bSMatthew G Knepley 
19589566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19599566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19603a544194SStefano Zampini           fpdof = fdof - fcdof;
19613a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19623a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19633a544194SStefano Zampini             fieldNc[f] = 1;
19643a544194SStefano Zampini           }
19653a544194SStefano Zampini           fieldSizes[f] += fpdof;
196637d0c07bSMatthew G Knepley         }
196737d0c07bSMatthew G Knepley       }
196837d0c07bSMatthew G Knepley     }
196937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19709566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
197137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
197237d0c07bSMatthew G Knepley     }
197337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
197437d0c07bSMatthew G Knepley       PetscInt gdof, goff;
197537d0c07bSMatthew G Knepley 
19769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
197737d0c07bSMatthew G Knepley       if (gdof > 0) {
19789566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
197937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
198037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
198137d0c07bSMatthew G Knepley 
19829566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19839566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1984ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
198537d0c07bSMatthew G Knepley         }
198637d0c07bSMatthew G Knepley       }
198737d0c07bSMatthew G Knepley     }
19888865f1eaSKarl Rupp     if (numFields) *numFields = nF;
198937d0c07bSMatthew G Knepley     if (fieldNames) {
19909566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
199137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
199237d0c07bSMatthew G Knepley         const char *fieldName;
199337d0c07bSMatthew G Knepley 
19949566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19959566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
199637d0c07bSMatthew G Knepley       }
199737d0c07bSMatthew G Knepley     }
199837d0c07bSMatthew G Knepley     if (fields) {
19999566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
200037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
20013a544194SStefano Zampini         PetscInt bs, in[2], out[2];
20023a544194SStefano Zampini 
20039566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
20043a544194SStefano Zampini         in[0] = -fieldNc[f];
20053a544194SStefano Zampini         in[1] = fieldNc[f];
2006462c564dSBarry Smith         PetscCallMPI(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
20073a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
20089566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
200937d0c07bSMatthew G Knepley       }
201037d0c07bSMatthew G Knepley     }
20119566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
2012dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
20133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20144d343eeaSMatthew G Knepley }
20154d343eeaSMatthew G Knepley 
201616621825SDmitry Karpeev /*@C
2017bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2018a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2019e7c4fc90SDmitry Karpeev 
202020f4b53cSBarry Smith   Not Collective; No Fortran Support
2021e7c4fc90SDmitry Karpeev 
2022e7c4fc90SDmitry Karpeev   Input Parameter:
2023bb7acecfSBarry Smith . dm - the `DM` object
2024e7c4fc90SDmitry Karpeev 
2025e7c4fc90SDmitry Karpeev   Output Parameters:
202620f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
202720f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
202820f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
202920f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2030e7c4fc90SDmitry Karpeev 
2031e7c4fc90SDmitry Karpeev   Level: intermediate
2032e7c4fc90SDmitry Karpeev 
2033a4e35b19SJacob Faibussowitsch   Notes:
2034a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2035a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2036a4e35b19SJacob Faibussowitsch 
2037a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2038a4e35b19SJacob Faibussowitsch 
203973ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
204073ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2041bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2042e7c4fc90SDmitry Karpeev 
204360225df5SJacob Faibussowitsch   Developer Notes:
2044bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2045bb7acecfSBarry Smith 
204673ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
204773ff1848SBarry Smith   decomposition is computed.
204873ff1848SBarry Smith 
204973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2050e7c4fc90SDmitry Karpeev @*/
2051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2052d71ae5a4SJacob Faibussowitsch {
2053e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2054e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20558865f1eaSKarl Rupp   if (len) {
20564f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20578865f1eaSKarl Rupp     *len = 0;
20588865f1eaSKarl Rupp   }
20598865f1eaSKarl Rupp   if (namelist) {
20604f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2061ea78f98cSLisandro Dalcin     *namelist = NULL;
20628865f1eaSKarl Rupp   }
20638865f1eaSKarl Rupp   if (islist) {
20644f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2065ea78f98cSLisandro Dalcin     *islist = NULL;
20668865f1eaSKarl Rupp   }
20678865f1eaSKarl Rupp   if (dmlist) {
20684f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2069ea78f98cSLisandro Dalcin     *dmlist = NULL;
20708865f1eaSKarl Rupp   }
2071f3f0edfdSDmitry Karpeev   /*
2072f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2073f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2074f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2075f3f0edfdSDmitry Karpeev    */
20767a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
207716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2078435a35e8SMatthew G Knepley     PetscSection section;
2079435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2080435a35e8SMatthew G Knepley 
20819566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20829566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2083435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2084f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20859566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20869566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20879566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2088435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2089435a35e8SMatthew G Knepley         const char *fieldName;
2090435a35e8SMatthew G Knepley 
20919566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
209203dc3394SMatthew G. Knepley         if (namelist) {
20939566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20949566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2095435a35e8SMatthew G Knepley         }
209603dc3394SMatthew G. Knepley       }
2097435a35e8SMatthew G Knepley     } else {
20989566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2099e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
21000298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2101e7c4fc90SDmitry Karpeev     }
2102dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
21033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
210416621825SDmitry Karpeev }
210516621825SDmitry Karpeev 
21065d83a8b1SBarry Smith /*@
210720f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
210820f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2109435a35e8SMatthew G Knepley 
2110435a35e8SMatthew G Knepley   Not collective
2111435a35e8SMatthew G Knepley 
2112435a35e8SMatthew G Knepley   Input Parameters:
2113bb7acecfSBarry Smith + dm        - The `DM` object
2114bb7acecfSBarry Smith . numFields - The number of fields to select
21152adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2116435a35e8SMatthew G Knepley 
2117435a35e8SMatthew G Knepley   Output Parameters:
2118b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2119b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2120435a35e8SMatthew G Knepley 
212120f4b53cSBarry Smith   Level: intermediate
212220f4b53cSBarry Smith 
2123bb7acecfSBarry Smith   Note:
2124bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21255d3b26e6SMatthew G. Knepley 
212660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2127435a35e8SMatthew G Knepley @*/
2128d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2129d71ae5a4SJacob Faibussowitsch {
2130435a35e8SMatthew G Knepley   PetscFunctionBegin;
2131435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21324f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21334f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21344f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2135dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2137435a35e8SMatthew G Knepley }
2138435a35e8SMatthew G Knepley 
21392adcc780SMatthew G. Knepley /*@C
2140bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21412adcc780SMatthew G. Knepley 
21422adcc780SMatthew G. Knepley   Not collective
21432adcc780SMatthew G. Knepley 
2144d8d19677SJose E. Roman   Input Parameters:
2145bb7acecfSBarry Smith + dms - The `DM` objects
2146bb7acecfSBarry Smith - n   - The number of `DM`s
21472adcc780SMatthew G. Knepley 
21482adcc780SMatthew G. Knepley   Output Parameters:
2149bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2150bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21512adcc780SMatthew G. Knepley 
215220f4b53cSBarry Smith   Level: intermediate
215320f4b53cSBarry Smith 
2154bb7acecfSBarry Smith   Note:
2155bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21565d3b26e6SMatthew G. Knepley 
215773ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21582adcc780SMatthew G. Knepley @*/
21595d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2160d71ae5a4SJacob Faibussowitsch {
21612adcc780SMatthew G. Knepley   PetscInt i;
21622adcc780SMatthew G. Knepley 
21632adcc780SMatthew G. Knepley   PetscFunctionBegin;
21644f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2165ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21664f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21674f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2168bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2169bb7acecfSBarry Smith   if (n) {
2170b9d85ea2SLisandro Dalcin     DM dm = dms[0];
217100045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2172dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21732adcc780SMatthew G. Knepley   }
21743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21752adcc780SMatthew G. Knepley }
21762adcc780SMatthew G. Knepley 
217716621825SDmitry Karpeev /*@C
2178a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2179a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
218016621825SDmitry Karpeev 
218120f4b53cSBarry Smith   Not Collective
218216621825SDmitry Karpeev 
218316621825SDmitry Karpeev   Input Parameter:
2184bb7acecfSBarry Smith . dm - the `DM` object
218516621825SDmitry Karpeev 
218616621825SDmitry Karpeev   Output Parameters:
218720f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
218820f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
218973ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
219073ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
219173ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
219216621825SDmitry Karpeev 
219316621825SDmitry Karpeev   Level: intermediate
219416621825SDmitry Karpeev 
219573ff1848SBarry Smith   Notes:
2196a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2197a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2198a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2199a4e35b19SJacob Faibussowitsch 
2200a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2201a4e35b19SJacob Faibussowitsch 
220273ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
220373ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2204bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
220516621825SDmitry Karpeev 
2206a4e35b19SJacob Faibussowitsch   Developer Notes:
220720f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2208bb7acecfSBarry Smith 
220973ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
221073ff1848SBarry Smith 
221173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
221273ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
221316621825SDmitry Karpeev @*/
2214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2215d71ae5a4SJacob Faibussowitsch {
2216be081cd6SPeter Brune   DMSubDomainHookLink link;
2217be081cd6SPeter Brune   PetscInt            i, l;
221816621825SDmitry Karpeev 
221916621825SDmitry Karpeev   PetscFunctionBegin;
222016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22219371c9d4SSatish Balay   if (n) {
22224f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22239371c9d4SSatish Balay     *n = 0;
22249371c9d4SSatish Balay   }
22259371c9d4SSatish Balay   if (namelist) {
22264f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22279371c9d4SSatish Balay     *namelist = NULL;
22289371c9d4SSatish Balay   }
22299371c9d4SSatish Balay   if (innerislist) {
22304f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22319371c9d4SSatish Balay     *innerislist = NULL;
22329371c9d4SSatish Balay   }
22339371c9d4SSatish Balay   if (outerislist) {
22344f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22359371c9d4SSatish Balay     *outerislist = NULL;
22369371c9d4SSatish Balay   }
22379371c9d4SSatish Balay   if (dmlist) {
22384f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22399371c9d4SSatish Balay     *dmlist = NULL;
22409371c9d4SSatish Balay   }
2241f3f0edfdSDmitry Karpeev   /*
2242f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2243f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2244f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2245f3f0edfdSDmitry Karpeev    */
22467a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
224716621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2248dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
224914a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2250f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2251be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2252be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22539566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2254be081cd6SPeter Brune         }
2255648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2256e7c4fc90SDmitry Karpeev       }
225714a18fd3SPeter Brune     }
2258bb7acecfSBarry Smith     if (n) *n = l;
225914a18fd3SPeter Brune   }
22603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2261e30e807fSPeter Brune }
2262e30e807fSPeter Brune 
2263e30e807fSPeter Brune /*@C
226473ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
226573ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2266e30e807fSPeter Brune 
226720f4b53cSBarry Smith   Not Collective
2268e30e807fSPeter Brune 
2269e30e807fSPeter Brune   Input Parameters:
2270bb7acecfSBarry Smith + dm     - the `DM` object
227173ff1848SBarry Smith . n      - the number of subdomains
2272e30e807fSPeter Brune - subdms - the local subdomains
2273e30e807fSPeter Brune 
2274e30e807fSPeter Brune   Output Parameters:
22756b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2276e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2277e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2278e30e807fSPeter Brune 
227920f4b53cSBarry Smith   Level: developer
228020f4b53cSBarry Smith 
2281bb7acecfSBarry Smith   Note:
2282bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2283e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2284e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2285e30e807fSPeter Brune   solution and residual data.
2286e30e807fSPeter Brune 
228773ff1848SBarry Smith   Developer Note:
2288a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2289a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2290bb7acecfSBarry Smith 
22911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2292e30e807fSPeter Brune @*/
22935d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2294d71ae5a4SJacob Faibussowitsch {
2295e30e807fSPeter Brune   PetscFunctionBegin;
2296e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22974f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2298dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2300e7c4fc90SDmitry Karpeev }
2301e7c4fc90SDmitry Karpeev 
230247c6ae99SBarry Smith /*@
2303bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
230447c6ae99SBarry Smith 
230520f4b53cSBarry Smith   Collective
230647c6ae99SBarry Smith 
2307d8d19677SJose E. Roman   Input Parameters:
2308bb7acecfSBarry Smith + dm   - the `DM` object
2309bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
231047c6ae99SBarry Smith 
231147c6ae99SBarry Smith   Output Parameter:
231220f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2313ae0a1c52SMatthew G Knepley 
231420f4b53cSBarry Smith   Options Database Key:
2315412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2316412e9a14SMatthew G. Knepley 
231747c6ae99SBarry Smith   Level: developer
231847c6ae99SBarry Smith 
231920f4b53cSBarry Smith   Note:
232020f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
232120f4b53cSBarry Smith 
232273ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
232373ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
232447c6ae99SBarry Smith @*/
2325d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2326d71ae5a4SJacob Faibussowitsch {
2327c833c3b5SJed Brown   DMRefineHookLink link;
232847c6ae99SBarry Smith 
232947c6ae99SBarry Smith   PetscFunctionBegin;
2330732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23319566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2332dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23334057135bSMatthew G Knepley   if (*dmf) {
233443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23358865f1eaSKarl Rupp 
23369566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23378865f1eaSKarl Rupp 
2338644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23390598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2340656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23418865f1eaSKarl Rupp 
23429566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2343c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23441baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2345c833c3b5SJed Brown     }
2346c833c3b5SJed Brown   }
23479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2349c833c3b5SJed Brown }
2350c833c3b5SJed Brown 
2351bb9467b5SJed Brown /*@C
2352c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2353c833c3b5SJed Brown 
235420f4b53cSBarry Smith   Logically Collective; No Fortran Support
2355c833c3b5SJed Brown 
23564165533cSJose E. Roman   Input Parameters:
2357bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2358bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2359f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
236020f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2361c833c3b5SJed Brown 
236220f4b53cSBarry Smith   Calling sequence of `refinehook`:
2363bb7acecfSBarry Smith + coarse - coarse level `DM`
2364bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2365c833c3b5SJed Brown - ctx    - optional user-defined function context
2366c833c3b5SJed Brown 
236720f4b53cSBarry Smith   Calling sequence of `interphook`:
2368bb7acecfSBarry Smith + coarse - coarse level `DM`
2369c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2370bb7acecfSBarry Smith . fine   - fine level `DM` to update
2371c833c3b5SJed Brown - ctx    - optional user-defined function context
2372c833c3b5SJed Brown 
2373c833c3b5SJed Brown   Level: advanced
2374c833c3b5SJed Brown 
2375c833c3b5SJed Brown   Notes:
2376bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2377bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2378bb7acecfSBarry Smith 
2379bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2380c833c3b5SJed Brown 
2381c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2382c833c3b5SJed Brown 
23831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2384c833c3b5SJed Brown @*/
2385a4e35b19SJacob 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)
2386d71ae5a4SJacob Faibussowitsch {
2387c833c3b5SJed Brown   DMRefineHookLink link, *p;
2388c833c3b5SJed Brown 
2389c833c3b5SJed Brown   PetscFunctionBegin;
2390c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23913d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23923ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23933d8e3701SJed Brown   }
23949566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2395c833c3b5SJed Brown   link->refinehook = refinehook;
2396c833c3b5SJed Brown   link->interphook = interphook;
2397c833c3b5SJed Brown   link->ctx        = ctx;
23980298fd71SBarry Smith   link->next       = NULL;
2399c833c3b5SJed Brown   *p               = link;
24003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2401c833c3b5SJed Brown }
2402c833c3b5SJed Brown 
24033d8e3701SJed Brown /*@C
2404bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2405bb7acecfSBarry Smith   a nonlinear problem to a finer grid
24063d8e3701SJed Brown 
240720f4b53cSBarry Smith   Logically Collective; No Fortran Support
24083d8e3701SJed Brown 
24094165533cSJose E. Roman   Input Parameters:
2410bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2411bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2412bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
241320f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
24143d8e3701SJed Brown 
24153d8e3701SJed Brown   Level: advanced
24163d8e3701SJed Brown 
2417bb7acecfSBarry Smith   Note:
24183d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24193d8e3701SJed Brown 
24201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24213d8e3701SJed Brown @*/
2422d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2423d71ae5a4SJacob Faibussowitsch {
24243d8e3701SJed Brown   DMRefineHookLink link, *p;
24253d8e3701SJed Brown 
24263d8e3701SJed Brown   PetscFunctionBegin;
24273d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24283d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24293d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24303d8e3701SJed Brown       link = *p;
24313d8e3701SJed Brown       *p   = link->next;
24329566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24333d8e3701SJed Brown       break;
24343d8e3701SJed Brown     }
24353d8e3701SJed Brown   }
24363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24373d8e3701SJed Brown }
24383d8e3701SJed Brown 
2439c833c3b5SJed Brown /*@
2440bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2441c833c3b5SJed Brown 
2442c833c3b5SJed Brown   Collective if any hooks are
2443c833c3b5SJed Brown 
24444165533cSJose E. Roman   Input Parameters:
2445bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2446bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2447bb7acecfSBarry Smith - fine   - finer `DM` to update
2448c833c3b5SJed Brown 
2449c833c3b5SJed Brown   Level: developer
2450c833c3b5SJed Brown 
245173ff1848SBarry Smith   Developer Note:
2452bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2453bb7acecfSBarry Smith   an API with consistent terminology.
2454bb7acecfSBarry Smith 
24551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2456c833c3b5SJed Brown @*/
2457d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2458d71ae5a4SJacob Faibussowitsch {
2459c833c3b5SJed Brown   DMRefineHookLink link;
2460c833c3b5SJed Brown 
2461c833c3b5SJed Brown   PetscFunctionBegin;
2462c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24631baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24644057135bSMatthew G Knepley   }
24653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
246647c6ae99SBarry Smith }
246747c6ae99SBarry Smith 
2468eb3f98d2SBarry Smith /*@
24691f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24701f3379b2SToby Isaac 
247120f4b53cSBarry Smith   Collective
24721f3379b2SToby Isaac 
24734165533cSJose E. Roman   Input Parameters:
2474bb7acecfSBarry Smith + coarse    - coarse `DM`
2475bb7acecfSBarry Smith . fine      - fine `DM`
2476bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2477bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2478bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24791f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24801f3379b2SToby Isaac 
24814165533cSJose E. Roman   Output Parameter:
24821f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24831f3379b2SToby Isaac 
24841f3379b2SToby Isaac   Level: developer
24851f3379b2SToby Isaac 
2486bb7acecfSBarry Smith   Note:
2487bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24881f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24891f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24901f3379b2SToby Isaac   slope-limiting reconstruction.
24911f3379b2SToby Isaac 
249273ff1848SBarry Smith   Developer Note:
2493bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2494bb7acecfSBarry Smith 
24951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24961f3379b2SToby Isaac @*/
2497d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2498d71ae5a4SJacob Faibussowitsch {
24991f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
25001f3379b2SToby Isaac 
25011f3379b2SToby Isaac   PetscFunctionBegin;
25021f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
25031f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
25041f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
25051f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
25061f3379b2SToby Isaac 
25079566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
25081f3379b2SToby Isaac   if (interpsol) {
25099566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
25101f3379b2SToby Isaac   } else if (interp) {
25119566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
251298921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
25133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25141f3379b2SToby Isaac }
25151f3379b2SToby Isaac 
25161f3379b2SToby Isaac /*@
2517bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2518eb3f98d2SBarry Smith 
2519eb3f98d2SBarry Smith   Not Collective
2520eb3f98d2SBarry Smith 
2521eb3f98d2SBarry Smith   Input Parameter:
2522bb7acecfSBarry Smith . dm - the `DM` object
2523eb3f98d2SBarry Smith 
2524eb3f98d2SBarry Smith   Output Parameter:
2525eb3f98d2SBarry Smith . level - number of refinements
2526eb3f98d2SBarry Smith 
2527eb3f98d2SBarry Smith   Level: developer
2528eb3f98d2SBarry Smith 
2529bb7acecfSBarry Smith   Note:
2530bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2531bb7acecfSBarry Smith 
25321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2533eb3f98d2SBarry Smith @*/
2534d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2535d71ae5a4SJacob Faibussowitsch {
2536eb3f98d2SBarry Smith   PetscFunctionBegin;
2537eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2538eb3f98d2SBarry Smith   *level = dm->levelup;
25393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2540eb3f98d2SBarry Smith }
2541eb3f98d2SBarry Smith 
2542fef3a512SBarry Smith /*@
2543bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2544fef3a512SBarry Smith 
2545fef3a512SBarry Smith   Not Collective
2546fef3a512SBarry Smith 
2547d8d19677SJose E. Roman   Input Parameters:
2548bb7acecfSBarry Smith + dm    - the `DM` object
2549fef3a512SBarry Smith - level - number of refinements
2550fef3a512SBarry Smith 
2551fef3a512SBarry Smith   Level: advanced
2552fef3a512SBarry Smith 
255395452b02SPatrick Sanan   Notes:
2554bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2555fef3a512SBarry Smith 
2556bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2557bb7acecfSBarry Smith 
25581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2559fef3a512SBarry Smith @*/
2560d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2561d71ae5a4SJacob Faibussowitsch {
2562fef3a512SBarry Smith   PetscFunctionBegin;
2563fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2564fef3a512SBarry Smith   dm->levelup = level;
25653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2566fef3a512SBarry Smith }
2567fef3a512SBarry Smith 
2568d410b0cfSMatthew G. Knepley /*@
2569bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2570d410b0cfSMatthew G. Knepley 
257120f4b53cSBarry Smith   Collective
2572d410b0cfSMatthew G. Knepley 
2573f1a722f8SMatthew G. Knepley   Input Parameters:
2574bb7acecfSBarry Smith + dm     - the `DM` object
2575d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2576d410b0cfSMatthew G. Knepley 
2577d410b0cfSMatthew G. Knepley   Output Parameter:
257820f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2579d410b0cfSMatthew G. Knepley 
2580d410b0cfSMatthew G. Knepley   Level: developer
2581d410b0cfSMatthew G. Knepley 
258220f4b53cSBarry Smith   Note:
258320f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
258420f4b53cSBarry Smith 
25851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2586d410b0cfSMatthew G. Knepley @*/
2587d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2588d71ae5a4SJacob Faibussowitsch {
2589d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2590d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2591dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2592d410b0cfSMatthew G. Knepley   if (*dme) {
2593d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25949566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2595d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25969566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2597d410b0cfSMatthew G. Knepley   }
25983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2599d410b0cfSMatthew G. Knepley }
2600d410b0cfSMatthew G. Knepley 
2601d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2602d71ae5a4SJacob Faibussowitsch {
2603ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2604ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26054f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2606ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
26073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2608ca3d3a14SMatthew G. Knepley }
2609ca3d3a14SMatthew G. Knepley 
2610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2611d71ae5a4SJacob Faibussowitsch {
2612ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2613ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26144f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2615ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
26163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2617ca3d3a14SMatthew G. Knepley }
2618ca3d3a14SMatthew G. Knepley 
2619ca3d3a14SMatthew G. Knepley /*@
2620bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2621ca3d3a14SMatthew G. Knepley 
2622ca3d3a14SMatthew G. Knepley   Input Parameter:
262320f4b53cSBarry Smith . dm - The `DM`
2624ca3d3a14SMatthew G. Knepley 
2625ca3d3a14SMatthew G. Knepley   Output Parameter:
262620f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2627ca3d3a14SMatthew G. Knepley 
2628ca3d3a14SMatthew G. Knepley   Level: developer
2629ca3d3a14SMatthew G. Knepley 
26301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2631ca3d3a14SMatthew G. Knepley @*/
2632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2633d71ae5a4SJacob Faibussowitsch {
2634ca3d3a14SMatthew G. Knepley   Vec tv;
2635ca3d3a14SMatthew G. Knepley 
2636ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2637ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26384f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26399566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2640ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2642ca3d3a14SMatthew G. Knepley }
2643ca3d3a14SMatthew G. Knepley 
2644d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2645d71ae5a4SJacob Faibussowitsch {
2646ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2647ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2648ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2649ca3d3a14SMatthew G. Knepley 
2650ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26519566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26529566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26539566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26549566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26559566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26569566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2659ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2661ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2662ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2663ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26649566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2665ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26669566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26679566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2668ca3d3a14SMatthew G. Knepley     }
2669ca3d3a14SMatthew G. Knepley   }
26709566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26719566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26729566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2673ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2674ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26759566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2676ca3d3a14SMatthew G. Knepley       if (dof) {
2677ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2678ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2679ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2680ca3d3a14SMatthew G. Knepley 
2681ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26829566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26839566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26849566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2685ca3d3a14SMatthew G. Knepley       }
2686ca3d3a14SMatthew G. Knepley     }
2687ca3d3a14SMatthew G. Knepley   }
26889566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2690ca3d3a14SMatthew G. Knepley }
2691ca3d3a14SMatthew G. Knepley 
2692d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2693d71ae5a4SJacob Faibussowitsch {
2694ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2695ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2696ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2697ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2698ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2699ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2700ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
27019566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
27023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2703ca3d3a14SMatthew G. Knepley }
2704ca3d3a14SMatthew G. Knepley 
2705bb9467b5SJed Brown /*@C
2706bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2707baf369e7SPeter Brune 
270820f4b53cSBarry Smith   Logically Collective
2709baf369e7SPeter Brune 
27104165533cSJose E. Roman   Input Parameters:
2711bb7acecfSBarry Smith + dm        - the `DM`
2712bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2713bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
271420f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2715baf369e7SPeter Brune 
271620f4b53cSBarry Smith   Calling sequence of `beginhook`:
2717a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2718baf369e7SPeter Brune . g    - global vector
2719baf369e7SPeter Brune . mode - mode
2720baf369e7SPeter Brune . l    - local vector
2721baf369e7SPeter Brune - ctx  - optional user-defined function context
2722baf369e7SPeter Brune 
272320f4b53cSBarry Smith   Calling sequence of `endhook`:
2724a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2725a4e35b19SJacob Faibussowitsch . g    - global vector
2726a4e35b19SJacob Faibussowitsch . mode - mode
2727a4e35b19SJacob Faibussowitsch . l    - local vector
2728baf369e7SPeter Brune - ctx  - optional user-defined function context
2729baf369e7SPeter Brune 
2730baf369e7SPeter Brune   Level: advanced
2731baf369e7SPeter Brune 
2732bb7acecfSBarry Smith   Note:
2733bb7acecfSBarry 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.
2734bb7acecfSBarry Smith 
27351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2736baf369e7SPeter Brune @*/
2737a4e35b19SJacob 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)
2738d71ae5a4SJacob Faibussowitsch {
2739baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2740baf369e7SPeter Brune 
2741baf369e7SPeter Brune   PetscFunctionBegin;
2742baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2743baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27449566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2745baf369e7SPeter Brune   link->beginhook = beginhook;
2746baf369e7SPeter Brune   link->endhook   = endhook;
2747baf369e7SPeter Brune   link->ctx       = ctx;
27480298fd71SBarry Smith   link->next      = NULL;
2749baf369e7SPeter Brune   *p              = link;
27503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2751baf369e7SPeter Brune }
2752baf369e7SPeter Brune 
2753d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2754d71ae5a4SJacob Faibussowitsch {
27554c274da1SToby Isaac   Mat          cMat;
275679769bd5SJed Brown   Vec          cVec, cBias;
27574c274da1SToby Isaac   PetscSection section, cSec;
27584c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27594c274da1SToby Isaac 
27604c274da1SToby Isaac   PetscFunctionBegin;
2761a4e35b19SJacob Faibussowitsch   (void)g;
2762a4e35b19SJacob Faibussowitsch   (void)ctx;
27634c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27649566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27654c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27665db9a05bSToby Isaac     PetscInt nRows;
27675db9a05bSToby Isaac 
27689566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27693ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27709566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27719566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27729566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27739566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27754c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27774c274da1SToby Isaac       if (dof) {
27784c274da1SToby Isaac         PetscScalar *vals;
27799566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27809566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27814c274da1SToby Isaac       }
27824c274da1SToby Isaac     }
27839566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27844c274da1SToby Isaac   }
27853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27864c274da1SToby Isaac }
27874c274da1SToby Isaac 
278847c6ae99SBarry Smith /*@
278901729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
279001729b5cSPatrick Sanan 
279120f4b53cSBarry Smith   Neighbor-wise Collective
279201729b5cSPatrick Sanan 
279301729b5cSPatrick Sanan   Input Parameters:
2794bb7acecfSBarry Smith + dm   - the `DM` object
279501729b5cSPatrick Sanan . g    - the global vector
2796bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
279701729b5cSPatrick Sanan - l    - the local vector
279801729b5cSPatrick Sanan 
279920f4b53cSBarry Smith   Level: beginner
280020f4b53cSBarry Smith 
280101729b5cSPatrick Sanan   Notes:
2802bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2803bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2804bb7acecfSBarry Smith 
2805bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
280601729b5cSPatrick Sanan 
28071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
280860225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2809bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
281001729b5cSPatrick Sanan @*/
2811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2812d71ae5a4SJacob Faibussowitsch {
281301729b5cSPatrick Sanan   PetscFunctionBegin;
28149566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
28159566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
28163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
281701729b5cSPatrick Sanan }
281801729b5cSPatrick Sanan 
281901729b5cSPatrick Sanan /*@
282047c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
282147c6ae99SBarry Smith 
282220f4b53cSBarry Smith   Neighbor-wise Collective
282347c6ae99SBarry Smith 
282447c6ae99SBarry Smith   Input Parameters:
2825bb7acecfSBarry Smith + dm   - the `DM` object
282647c6ae99SBarry Smith . g    - the global vector
2827bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
282847c6ae99SBarry Smith - l    - the local vector
282947c6ae99SBarry Smith 
283001729b5cSPatrick Sanan   Level: intermediate
283147c6ae99SBarry Smith 
2832bb7acecfSBarry Smith   Notes:
2833bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2834bb7acecfSBarry Smith 
2835bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2836bb7acecfSBarry Smith 
2837bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2838bb7acecfSBarry Smith 
2839bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2840bb7acecfSBarry Smith 
284160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
284247c6ae99SBarry Smith @*/
2843d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2844d71ae5a4SJacob Faibussowitsch {
28457128ae9fSMatthew G Knepley   PetscSF                 sf;
2846baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
284747c6ae99SBarry Smith 
284847c6ae99SBarry Smith   PetscFunctionBegin;
2849171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2850baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28511baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2852baf369e7SPeter Brune   }
28539566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28547128ae9fSMatthew G Knepley   if (sf) {
2855ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2856ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2857d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28587128ae9fSMatthew G Knepley 
28597a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28609566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28619566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28629566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28639566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28649566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28657128ae9fSMatthew G Knepley   } else {
2866fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28677128ae9fSMatthew G Knepley   }
28683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
286947c6ae99SBarry Smith }
287047c6ae99SBarry Smith 
287147c6ae99SBarry Smith /*@
287247c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
287347c6ae99SBarry Smith 
287420f4b53cSBarry Smith   Neighbor-wise Collective
287547c6ae99SBarry Smith 
287647c6ae99SBarry Smith   Input Parameters:
2877bb7acecfSBarry Smith + dm   - the `DM` object
287847c6ae99SBarry Smith . g    - the global vector
2879bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
288047c6ae99SBarry Smith - l    - the local vector
288147c6ae99SBarry Smith 
288201729b5cSPatrick Sanan   Level: intermediate
288347c6ae99SBarry Smith 
2884bb7acecfSBarry Smith   Note:
2885bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2886bb7acecfSBarry Smith 
288760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
288847c6ae99SBarry Smith @*/
2889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2890d71ae5a4SJacob Faibussowitsch {
28917128ae9fSMatthew G Knepley   PetscSF                 sf;
2892ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2893ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2894ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2895baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2896d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
289747c6ae99SBarry Smith 
289847c6ae99SBarry Smith   PetscFunctionBegin;
2899171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29009566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
29019566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
29027128ae9fSMatthew G Knepley   if (sf) {
29037a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
29047128ae9fSMatthew G Knepley 
29059566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
29069566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
29079566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
29089566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
29099566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
29109566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
29117128ae9fSMatthew G Knepley   } else {
2912fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
29137128ae9fSMatthew G Knepley   }
29149566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2915baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
29169566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2917baf369e7SPeter Brune   }
29183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
291947c6ae99SBarry Smith }
292047c6ae99SBarry Smith 
2921d4d07f1eSToby Isaac /*@C
2922d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2923d4d07f1eSToby Isaac 
292420f4b53cSBarry Smith   Logically Collective
2925d4d07f1eSToby Isaac 
29264165533cSJose E. Roman   Input Parameters:
2927bb7acecfSBarry Smith + dm        - the `DM`
2928bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2929bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
293020f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2931d4d07f1eSToby Isaac 
293220f4b53cSBarry Smith   Calling sequence of `beginhook`:
2933a4e35b19SJacob Faibussowitsch + global - global `DM`
2934d4d07f1eSToby Isaac . l      - local vector
2935d4d07f1eSToby Isaac . mode   - mode
2936d4d07f1eSToby Isaac . g      - global vector
2937d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2938d4d07f1eSToby Isaac 
293920f4b53cSBarry Smith   Calling sequence of `endhook`:
2940bb7acecfSBarry Smith + global - global `DM`
2941d4d07f1eSToby Isaac . l      - local vector
2942d4d07f1eSToby Isaac . mode   - mode
2943d4d07f1eSToby Isaac . g      - global vector
2944d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2945d4d07f1eSToby Isaac 
2946d4d07f1eSToby Isaac   Level: advanced
2947d4d07f1eSToby Isaac 
29481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2949d4d07f1eSToby Isaac @*/
2950a4e35b19SJacob 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)
2951d71ae5a4SJacob Faibussowitsch {
2952d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2953d4d07f1eSToby Isaac 
2954d4d07f1eSToby Isaac   PetscFunctionBegin;
2955d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2956d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29579566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2958d4d07f1eSToby Isaac   link->beginhook = beginhook;
2959d4d07f1eSToby Isaac   link->endhook   = endhook;
2960d4d07f1eSToby Isaac   link->ctx       = ctx;
2961d4d07f1eSToby Isaac   link->next      = NULL;
2962d4d07f1eSToby Isaac   *p              = link;
29633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2964d4d07f1eSToby Isaac }
2965d4d07f1eSToby Isaac 
2966d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2967d71ae5a4SJacob Faibussowitsch {
29684c274da1SToby Isaac   PetscFunctionBegin;
2969a4e35b19SJacob Faibussowitsch   (void)g;
2970a4e35b19SJacob Faibussowitsch   (void)ctx;
29714c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29722b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29732b6b7d09SToby Isaac     Mat          cMat;
29742b6b7d09SToby Isaac     Vec          cVec;
29755db9a05bSToby Isaac     PetscInt     nRows;
29762b6b7d09SToby Isaac     PetscSection section, cSec;
29772b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29782b6b7d09SToby Isaac 
29792b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29802b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29815db9a05bSToby Isaac 
29829566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29833ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29849566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29859566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29869566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29874c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29889566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29894c274da1SToby Isaac       if (dof) {
29904c274da1SToby Isaac         PetscInt     d;
29914c274da1SToby Isaac         PetscScalar *vals;
29929566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29939566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29944c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29954c274da1SToby Isaac          * we just extracted */
2996ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29974c274da1SToby Isaac       }
29984c274da1SToby Isaac     }
29999566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
30009566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
30014c274da1SToby Isaac   }
30023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30034c274da1SToby Isaac }
300401729b5cSPatrick Sanan /*@
300501729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
300601729b5cSPatrick Sanan 
300720f4b53cSBarry Smith   Neighbor-wise Collective
300801729b5cSPatrick Sanan 
300901729b5cSPatrick Sanan   Input Parameters:
3010bb7acecfSBarry Smith + dm   - the `DM` object
301101729b5cSPatrick Sanan . l    - the local vector
3012bb7acecfSBarry 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.
301301729b5cSPatrick Sanan - g    - the global vector
301401729b5cSPatrick Sanan 
301520f4b53cSBarry Smith   Level: beginner
301620f4b53cSBarry Smith 
301701729b5cSPatrick Sanan   Notes:
301801729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3019bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
302001729b5cSPatrick Sanan 
3021bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3022bb7acecfSBarry Smith 
3023bb7acecfSBarry 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.
3024bb7acecfSBarry Smith 
3025bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
302601729b5cSPatrick Sanan 
30271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
302801729b5cSPatrick Sanan @*/
3029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3030d71ae5a4SJacob Faibussowitsch {
303101729b5cSPatrick Sanan   PetscFunctionBegin;
30329566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30339566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
303501729b5cSPatrick Sanan }
30364c274da1SToby Isaac 
303747c6ae99SBarry Smith /*@
303801729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30399a42bb27SBarry Smith 
304020f4b53cSBarry Smith   Neighbor-wise Collective
30419a42bb27SBarry Smith 
30429a42bb27SBarry Smith   Input Parameters:
3043bb7acecfSBarry Smith + dm   - the `DM` object
3044f6813fd5SJed Brown . l    - the local vector
3045aa624791SPierre 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.
30461eb28f2eSBarry Smith - g    - the global vector
30479a42bb27SBarry Smith 
304820f4b53cSBarry Smith   Level: intermediate
304920f4b53cSBarry Smith 
305095452b02SPatrick Sanan   Notes:
3051bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3052bb7acecfSBarry Smith 
3053bb7acecfSBarry 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.
3054bb7acecfSBarry Smith 
3055bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3056bb7acecfSBarry Smith 
3057bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3058bb7acecfSBarry Smith 
3059bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30609a42bb27SBarry Smith 
30611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30629a42bb27SBarry Smith @*/
3063d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3064d71ae5a4SJacob Faibussowitsch {
30657128ae9fSMatthew G Knepley   PetscSF                 sf;
306684330215SMatthew G. Knepley   PetscSection            s, gs;
3067d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3068ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3069ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3070ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3071fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3072d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30739a42bb27SBarry Smith 
30749a42bb27SBarry Smith   PetscFunctionBegin;
3075171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3076d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30771baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3078d4d07f1eSToby Isaac   }
30799566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30809566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30819566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30827128ae9fSMatthew G Knepley   switch (mode) {
30837128ae9fSMatthew G Knepley   case INSERT_VALUES:
30847128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3085d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3086d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3087d71ae5a4SJacob Faibussowitsch     break;
30887128ae9fSMatthew G Knepley   case ADD_VALUES:
30897128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3090d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3091d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3092d71ae5a4SJacob Faibussowitsch     break;
3093d71ae5a4SJacob Faibussowitsch   default:
3094d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30957128ae9fSMatthew G Knepley   }
3096ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30979566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3098ca3d3a14SMatthew G. Knepley     if (transform) {
30999566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31009566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
31019566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
31029566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3103fa88e482SJed Brown     } else if (isInsert) {
31049566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3105fa88e482SJed Brown     } else {
31069566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3107fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3108ca3d3a14SMatthew G. Knepley     }
3109fa88e482SJed Brown     if (s && isInsert) {
31109566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3111fa88e482SJed Brown     } else {
31129566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3113fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3114fa88e482SJed Brown     }
3115ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
31169566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
311784330215SMatthew G. Knepley     } else if (s && isInsert) {
311884330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
311984330215SMatthew G. Knepley 
31209566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31219566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31229566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
312384330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3124b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
312584330215SMatthew G. Knepley 
31269566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31279566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3132b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
313303442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31347a8be351SBarry 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);
3135b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3136b3b16f48SMatthew G. Knepley         if (!gcdof) {
313784330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3138b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3139b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
314084330215SMatthew G. Knepley           const PetscInt *cdofs;
314184330215SMatthew G. Knepley           PetscInt        cind = 0;
314284330215SMatthew G. Knepley 
31439566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3144b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31459371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31469371c9d4SSatish Balay               ++cind;
31479371c9d4SSatish Balay               continue;
31489371c9d4SSatish Balay             }
3149b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
315084330215SMatthew G. Knepley           }
31517a8be351SBarry 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);
315284330215SMatthew G. Knepley       }
3153ca3d3a14SMatthew G. Knepley     }
3154fa88e482SJed Brown     if (g_inplace) {
31559566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3156fa88e482SJed Brown     } else {
31579566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3158fa88e482SJed Brown     }
3159ca3d3a14SMatthew G. Knepley     if (transform) {
31609566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31619566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3162fa88e482SJed Brown     } else if (l_inplace) {
31639566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3164ca3d3a14SMatthew G. Knepley     } else {
31659566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3166ca3d3a14SMatthew G. Knepley     }
31677128ae9fSMatthew G Knepley   } else {
3168fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31697128ae9fSMatthew G Knepley   }
31703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31719a42bb27SBarry Smith }
31729a42bb27SBarry Smith 
31739a42bb27SBarry Smith /*@
31749a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
317547c6ae99SBarry Smith 
317620f4b53cSBarry Smith   Neighbor-wise Collective
317747c6ae99SBarry Smith 
317847c6ae99SBarry Smith   Input Parameters:
3179bb7acecfSBarry Smith + dm   - the `DM` object
3180f6813fd5SJed Brown . l    - the local vector
3181bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3182f6813fd5SJed Brown - g    - the global vector
318347c6ae99SBarry Smith 
318401729b5cSPatrick Sanan   Level: intermediate
318547c6ae99SBarry Smith 
3186bb7acecfSBarry Smith   Note:
3187bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3188bb7acecfSBarry Smith 
318960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
319047c6ae99SBarry Smith @*/
3191d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3192d71ae5a4SJacob Faibussowitsch {
31937128ae9fSMatthew G Knepley   PetscSF                 sf;
319484330215SMatthew G. Knepley   PetscSection            s;
3195d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3196ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
319747c6ae99SBarry Smith 
319847c6ae99SBarry Smith   PetscFunctionBegin;
3199171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32009566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
32019566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
32027128ae9fSMatthew G Knepley   switch (mode) {
32037128ae9fSMatthew G Knepley   case INSERT_VALUES:
3204d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3205d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3206d71ae5a4SJacob Faibussowitsch     break;
32077128ae9fSMatthew G Knepley   case ADD_VALUES:
3208d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3209d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3210d71ae5a4SJacob Faibussowitsch     break;
3211d71ae5a4SJacob Faibussowitsch   default:
3212d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
32137128ae9fSMatthew G Knepley   }
321484330215SMatthew G. Knepley   if (sf && !isInsert) {
3215ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3216ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3217ca3d3a14SMatthew G. Knepley     Vec                tmpl;
321884330215SMatthew G. Knepley 
32199566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3220ca3d3a14SMatthew G. Knepley     if (transform) {
32219566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32229566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3223ca3d3a14SMatthew G. Knepley     } else {
32249566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3225ca3d3a14SMatthew G. Knepley     }
32269566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32279566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3228ca3d3a14SMatthew G. Knepley     if (transform) {
32299566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32309566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3231ca3d3a14SMatthew G. Knepley     } else {
32329566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3233ca3d3a14SMatthew G. Knepley     }
32349566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
323584330215SMatthew G. Knepley   } else if (s && isInsert) {
32367128ae9fSMatthew G Knepley   } else {
3237fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32387128ae9fSMatthew G Knepley   }
3239d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32409566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3241d4d07f1eSToby Isaac   }
32423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
324347c6ae99SBarry Smith }
324447c6ae99SBarry Smith 
3245f089877aSRichard Tran Mills /*@
3246a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3247a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3248a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3249f089877aSRichard Tran Mills 
325020f4b53cSBarry Smith   Neighbor-wise Collective
3251f089877aSRichard Tran Mills 
3252f089877aSRichard Tran Mills   Input Parameters:
3253bb7acecfSBarry Smith + dm   - the `DM` object
3254bc0a1609SRichard Tran Mills . g    - the original local vector
3255bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3256f089877aSRichard Tran Mills 
3257bc0a1609SRichard Tran Mills   Output Parameter:
3258bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3259f089877aSRichard Tran Mills 
3260f089877aSRichard Tran Mills   Level: intermediate
3261f089877aSRichard Tran Mills 
326273ff1848SBarry Smith   Note:
3263a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3264a4e35b19SJacob Faibussowitsch 
326560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3266f089877aSRichard Tran Mills @*/
3267d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3268d71ae5a4SJacob Faibussowitsch {
3269f089877aSRichard Tran Mills   PetscFunctionBegin;
3270f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32719f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32729f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32739f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3275f089877aSRichard Tran Mills }
3276f089877aSRichard Tran Mills 
3277f089877aSRichard Tran Mills /*@
3278bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3279bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3280f089877aSRichard Tran Mills 
328120f4b53cSBarry Smith   Neighbor-wise Collective
3282f089877aSRichard Tran Mills 
3283f089877aSRichard Tran Mills   Input Parameters:
328460225df5SJacob Faibussowitsch + dm   - the `DM` object
3285bc0a1609SRichard Tran Mills . g    - the original local vector
3286bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3287f089877aSRichard Tran Mills 
3288bc0a1609SRichard Tran Mills   Output Parameter:
3289bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3290f089877aSRichard Tran Mills 
3291f089877aSRichard Tran Mills   Level: intermediate
3292f089877aSRichard Tran Mills 
329360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3294f089877aSRichard Tran Mills @*/
3295d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3296d71ae5a4SJacob Faibussowitsch {
3297f089877aSRichard Tran Mills   PetscFunctionBegin;
3298f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32999f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
33009f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
33019f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
33023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3303f089877aSRichard Tran Mills }
3304f089877aSRichard Tran Mills 
330547c6ae99SBarry Smith /*@
3306bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
330747c6ae99SBarry Smith 
330820f4b53cSBarry Smith   Collective
330947c6ae99SBarry Smith 
3310d8d19677SJose E. Roman   Input Parameters:
3311bb7acecfSBarry Smith + dm   - the `DM` object
331220f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
331347c6ae99SBarry Smith 
331447c6ae99SBarry Smith   Output Parameter:
3315bb7acecfSBarry Smith . dmc - the coarsened `DM`
331647c6ae99SBarry Smith 
331747c6ae99SBarry Smith   Level: developer
331847c6ae99SBarry Smith 
331973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
332073ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
332147c6ae99SBarry Smith @*/
3322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3323d71ae5a4SJacob Faibussowitsch {
3324b17ce1afSJed Brown   DMCoarsenHookLink link;
332547c6ae99SBarry Smith 
332647c6ae99SBarry Smith   PetscFunctionBegin;
3327171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33289566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3329dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3330b9d85ea2SLisandro Dalcin   if (*dmc) {
3331a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33329566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
333343842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33349566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3335644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33360598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3337656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33389566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3339b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33409566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3341b17ce1afSJed Brown     }
3342b9d85ea2SLisandro Dalcin   }
33439566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33447a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3346b17ce1afSJed Brown }
3347b17ce1afSJed Brown 
3348bb9467b5SJed Brown /*@C
3349b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3350b17ce1afSJed Brown 
335120f4b53cSBarry Smith   Logically Collective; No Fortran Support
3352b17ce1afSJed Brown 
33534165533cSJose E. Roman   Input Parameters:
3354bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3355b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3356bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
335720f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3358b17ce1afSJed Brown 
335920f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3360bb7acecfSBarry Smith + fine   - fine level `DM`
3361bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3362b17ce1afSJed Brown - ctx    - optional user-defined function context
3363b17ce1afSJed Brown 
336420f4b53cSBarry Smith   Calling sequence of `restricthook`:
3365bb7acecfSBarry Smith + fine      - fine level `DM`
3366bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3367c833c3b5SJed Brown . rscale    - scaling vector for restriction
3368c833c3b5SJed Brown . inject    - matrix restricting by injection
3369b17ce1afSJed Brown . coarse    - coarse level DM to update
3370b17ce1afSJed Brown - ctx       - optional user-defined function context
3371b17ce1afSJed Brown 
3372b17ce1afSJed Brown   Level: advanced
3373b17ce1afSJed Brown 
3374b17ce1afSJed Brown   Notes:
3375bb7acecfSBarry 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`.
3376b17ce1afSJed Brown 
3377b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3378b17ce1afSJed Brown 
3379b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3380bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3381b17ce1afSJed Brown 
3382bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3383bb7acecfSBarry Smith 
33841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3385b17ce1afSJed Brown @*/
3386a4e35b19SJacob 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)
3387d71ae5a4SJacob Faibussowitsch {
3388b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3389b17ce1afSJed Brown 
3390b17ce1afSJed Brown   PetscFunctionBegin;
3391b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33921e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33933ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33941e3d8eccSJed Brown   }
33959566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3396b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3397b17ce1afSJed Brown   link->restricthook = restricthook;
3398b17ce1afSJed Brown   link->ctx          = ctx;
33990298fd71SBarry Smith   link->next         = NULL;
3400b17ce1afSJed Brown   *p                 = link;
34013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3402b17ce1afSJed Brown }
3403b17ce1afSJed Brown 
3404dc822a44SJed Brown /*@C
3405bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3406dc822a44SJed Brown 
340720f4b53cSBarry Smith   Logically Collective; No Fortran Support
3408dc822a44SJed Brown 
34094165533cSJose E. Roman   Input Parameters:
3410bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3411dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3412bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
341320f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3414dc822a44SJed Brown 
3415dc822a44SJed Brown   Level: advanced
3416dc822a44SJed Brown 
341773ff1848SBarry Smith   Notes:
341873ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
341973ff1848SBarry Smith 
342073ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3421dc822a44SJed Brown 
34221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3423dc822a44SJed Brown @*/
3424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3425d71ae5a4SJacob Faibussowitsch {
3426dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3427dc822a44SJed Brown 
3428dc822a44SJed Brown   PetscFunctionBegin;
3429dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3430dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3431dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3432dc822a44SJed Brown       link = *p;
3433dc822a44SJed Brown       *p   = link->next;
34349566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3435dc822a44SJed Brown       break;
3436dc822a44SJed Brown     }
3437dc822a44SJed Brown   }
34383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3439dc822a44SJed Brown }
3440dc822a44SJed Brown 
3441b17ce1afSJed Brown /*@
3442bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3443b17ce1afSJed Brown 
3444b17ce1afSJed Brown   Collective if any hooks are
3445b17ce1afSJed Brown 
34464165533cSJose E. Roman   Input Parameters:
3447bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3448bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3449e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3450bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
345120f4b53cSBarry Smith - coarse  - coarser `DM` to update
3452b17ce1afSJed Brown 
3453b17ce1afSJed Brown   Level: developer
3454b17ce1afSJed Brown 
345573ff1848SBarry Smith   Developer Note:
3456bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3457bb7acecfSBarry Smith 
34581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3459b17ce1afSJed Brown @*/
3460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3461d71ae5a4SJacob Faibussowitsch {
3462b17ce1afSJed Brown   DMCoarsenHookLink link;
3463b17ce1afSJed Brown 
3464b17ce1afSJed Brown   PetscFunctionBegin;
3465b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34661baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3467b17ce1afSJed Brown   }
34683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
346947c6ae99SBarry Smith }
347047c6ae99SBarry Smith 
3471bb9467b5SJed Brown /*@C
347273ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34735dbd56e3SPeter Brune 
347420f4b53cSBarry Smith   Logically Collective; No Fortran Support
34755dbd56e3SPeter Brune 
34764165533cSJose E. Roman   Input Parameters:
3477bb7acecfSBarry Smith + global       - global `DM`
3478bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34795dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
348020f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34815dbd56e3SPeter Brune 
348220f4b53cSBarry Smith   Calling sequence of `ddhook`:
3483bb7acecfSBarry Smith + global - global `DM`
348473ff1848SBarry Smith . block  - subdomain `DM`
3485ec4806b8SPeter Brune - ctx    - optional user-defined function context
3486ec4806b8SPeter Brune 
348720f4b53cSBarry Smith   Calling sequence of `restricthook`:
3488bb7acecfSBarry Smith + global - global `DM`
348973ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
349073ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
349173ff1848SBarry Smith . block  - subdomain `DM`
34925dbd56e3SPeter Brune - ctx    - optional user-defined function context
34935dbd56e3SPeter Brune 
34945dbd56e3SPeter Brune   Level: advanced
34955dbd56e3SPeter Brune 
34965dbd56e3SPeter Brune   Notes:
349773ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34985dbd56e3SPeter Brune 
34995dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
35005dbd56e3SPeter Brune 
35015dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3502bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
35035dbd56e3SPeter Brune 
350473ff1848SBarry Smith   Developer Note:
350573ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
350673ff1848SBarry Smith 
350773ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
35085dbd56e3SPeter Brune @*/
3509a4e35b19SJacob 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)
3510d71ae5a4SJacob Faibussowitsch {
3511be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
35125dbd56e3SPeter Brune 
35135dbd56e3SPeter Brune   PetscFunctionBegin;
35145dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3515b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35163ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3517b3a6b972SJed Brown   }
35189566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35195dbd56e3SPeter Brune   link->restricthook = restricthook;
3520be081cd6SPeter Brune   link->ddhook       = ddhook;
35215dbd56e3SPeter Brune   link->ctx          = ctx;
35220298fd71SBarry Smith   link->next         = NULL;
35235dbd56e3SPeter Brune   *p                 = link;
35243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35255dbd56e3SPeter Brune }
35265dbd56e3SPeter Brune 
3527b3a6b972SJed Brown /*@C
352873ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3529b3a6b972SJed Brown 
353020f4b53cSBarry Smith   Logically Collective; No Fortran Support
3531b3a6b972SJed Brown 
35324165533cSJose E. Roman   Input Parameters:
3533bb7acecfSBarry Smith + global       - global `DM`
3534bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3535b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
353620f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3537b3a6b972SJed Brown 
3538b3a6b972SJed Brown   Level: advanced
3539b3a6b972SJed Brown 
354073ff1848SBarry Smith   Note:
354173ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
354273ff1848SBarry Smith 
354373ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
354473ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3545b3a6b972SJed Brown @*/
3546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3547d71ae5a4SJacob Faibussowitsch {
3548b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3549b3a6b972SJed Brown 
3550b3a6b972SJed Brown   PetscFunctionBegin;
3551b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3552b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3553b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3554b3a6b972SJed Brown       link = *p;
3555b3a6b972SJed Brown       *p   = link->next;
35569566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3557b3a6b972SJed Brown       break;
3558b3a6b972SJed Brown     }
3559b3a6b972SJed Brown   }
35603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3561b3a6b972SJed Brown }
3562b3a6b972SJed Brown 
35635dbd56e3SPeter Brune /*@
356473ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35655dbd56e3SPeter Brune 
35665dbd56e3SPeter Brune   Collective if any hooks are
35675dbd56e3SPeter Brune 
35684165533cSJose E. Roman   Input Parameters:
3569a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3570a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3571a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3572a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35735dbd56e3SPeter Brune 
35745dbd56e3SPeter Brune   Level: developer
35755dbd56e3SPeter Brune 
357673ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35775dbd56e3SPeter Brune @*/
3578d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3579d71ae5a4SJacob Faibussowitsch {
3580be081cd6SPeter Brune   DMSubDomainHookLink link;
35815dbd56e3SPeter Brune 
35825dbd56e3SPeter Brune   PetscFunctionBegin;
3583be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35841baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35855dbd56e3SPeter Brune   }
35863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35875dbd56e3SPeter Brune }
35885dbd56e3SPeter Brune 
35895fe1f584SPeter Brune /*@
3590bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35915fe1f584SPeter Brune 
35925fe1f584SPeter Brune   Not Collective
35935fe1f584SPeter Brune 
35945fe1f584SPeter Brune   Input Parameter:
3595bb7acecfSBarry Smith . dm - the `DM` object
35965fe1f584SPeter Brune 
35975fe1f584SPeter Brune   Output Parameter:
35986a7d9d85SPeter Brune . level - number of coarsenings
35995fe1f584SPeter Brune 
36005fe1f584SPeter Brune   Level: developer
36015fe1f584SPeter Brune 
36021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36035fe1f584SPeter Brune @*/
3604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3605d71ae5a4SJacob Faibussowitsch {
36065fe1f584SPeter Brune   PetscFunctionBegin;
36075fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36084f572ea9SToby Isaac   PetscAssertPointer(level, 2);
36095fe1f584SPeter Brune   *level = dm->leveldown;
36103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36115fe1f584SPeter Brune }
36125fe1f584SPeter Brune 
36139a64c4a8SMatthew G. Knepley /*@
3614bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36159a64c4a8SMatthew G. Knepley 
361620f4b53cSBarry Smith   Collective
36179a64c4a8SMatthew G. Knepley 
36189a64c4a8SMatthew G. Knepley   Input Parameters:
3619bb7acecfSBarry Smith + dm    - the `DM` object
36209a64c4a8SMatthew G. Knepley - level - number of coarsenings
36219a64c4a8SMatthew G. Knepley 
36229a64c4a8SMatthew G. Knepley   Level: developer
36239a64c4a8SMatthew G. Knepley 
3624bb7acecfSBarry Smith   Note:
3625bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3626bb7acecfSBarry Smith 
362742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36289a64c4a8SMatthew G. Knepley @*/
3629d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3630d71ae5a4SJacob Faibussowitsch {
36319a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36329a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36339a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36359a64c4a8SMatthew G. Knepley }
36369a64c4a8SMatthew G. Knepley 
3637cc4c1da9SBarry Smith /*@
3638bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
363947c6ae99SBarry Smith 
364020f4b53cSBarry Smith   Collective
364147c6ae99SBarry Smith 
3642d8d19677SJose E. Roman   Input Parameters:
3643bb7acecfSBarry Smith + dm      - the `DM` object
364447c6ae99SBarry Smith - nlevels - the number of levels of refinement
364547c6ae99SBarry Smith 
364647c6ae99SBarry Smith   Output Parameter:
3647bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
364847c6ae99SBarry Smith 
364947c6ae99SBarry Smith   Level: developer
365047c6ae99SBarry Smith 
36511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
365247c6ae99SBarry Smith @*/
3653d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3654d71ae5a4SJacob Faibussowitsch {
365547c6ae99SBarry Smith   PetscFunctionBegin;
3656171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36577a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36583ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36594f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3660213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
366147c6ae99SBarry Smith     PetscInt i;
366247c6ae99SBarry Smith 
36639566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
366448a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3665213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
366747c6ae99SBarry Smith }
366847c6ae99SBarry Smith 
3669cc4c1da9SBarry Smith /*@
3670bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
367147c6ae99SBarry Smith 
367220f4b53cSBarry Smith   Collective
367347c6ae99SBarry Smith 
3674d8d19677SJose E. Roman   Input Parameters:
3675bb7acecfSBarry Smith + dm      - the `DM` object
367647c6ae99SBarry Smith - nlevels - the number of levels of coarsening
367747c6ae99SBarry Smith 
367847c6ae99SBarry Smith   Output Parameter:
3679bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
368047c6ae99SBarry Smith 
368147c6ae99SBarry Smith   Level: developer
368247c6ae99SBarry Smith 
36831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
368447c6ae99SBarry Smith @*/
3685d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3686d71ae5a4SJacob Faibussowitsch {
368747c6ae99SBarry Smith   PetscFunctionBegin;
3688171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36897a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36903ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36914f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3692213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
369347c6ae99SBarry Smith     PetscInt i;
369447c6ae99SBarry Smith 
36959566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
369648a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3697213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
369947c6ae99SBarry Smith }
370047c6ae99SBarry Smith 
37011a266240SBarry Smith /*@C
3702bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
37031a266240SBarry Smith 
3704bb7acecfSBarry Smith   Logically Collective if the function is collective
37051a266240SBarry Smith 
37061a266240SBarry Smith   Input Parameters:
3707bb7acecfSBarry Smith + dm      - the `DM` object
37081a266240SBarry Smith - destroy - the destroy function
37091a266240SBarry Smith 
37101a266240SBarry Smith   Level: intermediate
37111a266240SBarry Smith 
37121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3713f07f9ceaSJed Brown @*/
3714d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3715d71ae5a4SJacob Faibussowitsch {
37161a266240SBarry Smith   PetscFunctionBegin;
3717171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37181a266240SBarry Smith   dm->ctxdestroy = destroy;
37193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37201a266240SBarry Smith }
37211a266240SBarry Smith 
3722b07ff414SBarry Smith /*@
3723bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
372447c6ae99SBarry Smith 
372547c6ae99SBarry Smith   Not Collective
372647c6ae99SBarry Smith 
372747c6ae99SBarry Smith   Input Parameters:
3728bb7acecfSBarry Smith + dm  - the `DM` object
372947c6ae99SBarry Smith - ctx - the user context
373047c6ae99SBarry Smith 
373147c6ae99SBarry Smith   Level: intermediate
373247c6ae99SBarry Smith 
3733e33b3f0fSStefano Zampini   Notes:
373435cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3735e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3736e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3737bb7acecfSBarry Smith 
373860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
373947c6ae99SBarry Smith @*/
3740d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3741d71ae5a4SJacob Faibussowitsch {
374247c6ae99SBarry Smith   PetscFunctionBegin;
3743171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374447c6ae99SBarry Smith   dm->ctx = ctx;
37453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374647c6ae99SBarry Smith }
374747c6ae99SBarry Smith 
374847c6ae99SBarry Smith /*@
3749bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
375047c6ae99SBarry Smith 
375147c6ae99SBarry Smith   Not Collective
375247c6ae99SBarry Smith 
375347c6ae99SBarry Smith   Input Parameter:
3754bb7acecfSBarry Smith . dm - the `DM` object
375547c6ae99SBarry Smith 
375647c6ae99SBarry Smith   Output Parameter:
375747c6ae99SBarry Smith . ctx - the user context
375847c6ae99SBarry Smith 
375947c6ae99SBarry Smith   Level: intermediate
376047c6ae99SBarry Smith 
3761bb7acecfSBarry Smith   Note:
376235cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3763bb7acecfSBarry Smith 
376442747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
376547c6ae99SBarry Smith @*/
3766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3767d71ae5a4SJacob Faibussowitsch {
376847c6ae99SBarry Smith   PetscFunctionBegin;
3769171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37701b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377247c6ae99SBarry Smith }
377347c6ae99SBarry Smith 
377408da532bSDmitry Karpeev /*@C
3775bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
377608da532bSDmitry Karpeev 
377720f4b53cSBarry Smith   Logically Collective
377808da532bSDmitry Karpeev 
3779d8d19677SJose E. Roman   Input Parameters:
378008da532bSDmitry Karpeev + dm - the DM object
378120f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
378208da532bSDmitry Karpeev 
378308da532bSDmitry Karpeev   Level: intermediate
378408da532bSDmitry Karpeev 
37851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3786db781477SPatrick Sanan          `DMSetJacobian()`
378708da532bSDmitry Karpeev @*/
3788d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3789d71ae5a4SJacob Faibussowitsch {
379008da532bSDmitry Karpeev   PetscFunctionBegin;
37915a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379408da532bSDmitry Karpeev }
379508da532bSDmitry Karpeev 
379608da532bSDmitry Karpeev /*@
3797bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
379808da532bSDmitry Karpeev 
379908da532bSDmitry Karpeev   Not Collective
380008da532bSDmitry Karpeev 
380108da532bSDmitry Karpeev   Input Parameter:
3802bb7acecfSBarry Smith . dm - the `DM` object to destroy
380308da532bSDmitry Karpeev 
380408da532bSDmitry Karpeev   Output Parameter:
3805bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
380608da532bSDmitry Karpeev 
380708da532bSDmitry Karpeev   Level: developer
380808da532bSDmitry Karpeev 
38091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
381008da532bSDmitry Karpeev @*/
3811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3812d71ae5a4SJacob Faibussowitsch {
381308da532bSDmitry Karpeev   PetscFunctionBegin;
38145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38154f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
381608da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
381808da532bSDmitry Karpeev }
381908da532bSDmitry Karpeev 
3820cc4c1da9SBarry Smith /*@
3821bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
382208da532bSDmitry Karpeev 
382320f4b53cSBarry Smith   Logically Collective
382408da532bSDmitry Karpeev 
3825f899ff85SJose E. Roman   Input Parameter:
3826bb7acecfSBarry Smith . dm - the `DM` object
382708da532bSDmitry Karpeev 
382860225df5SJacob Faibussowitsch   Output Parameters:
382908da532bSDmitry Karpeev + xl - lower bound
383008da532bSDmitry Karpeev - xu - upper bound
383108da532bSDmitry Karpeev 
3832907376e6SBarry Smith   Level: advanced
3833907376e6SBarry Smith 
383420f4b53cSBarry Smith   Note:
383595452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
383608da532bSDmitry Karpeev 
38371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
383808da532bSDmitry Karpeev @*/
3839d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3840d71ae5a4SJacob Faibussowitsch {
384108da532bSDmitry Karpeev   PetscFunctionBegin;
38425a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
384308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38445a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3845dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
384708da532bSDmitry Karpeev }
384808da532bSDmitry Karpeev 
3849b0ae01b7SPeter Brune /*@
3850bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3851b0ae01b7SPeter Brune 
3852b0ae01b7SPeter Brune   Not Collective
3853b0ae01b7SPeter Brune 
3854b0ae01b7SPeter Brune   Input Parameter:
3855b0ae01b7SPeter Brune . dm - the DM object
3856b0ae01b7SPeter Brune 
3857b0ae01b7SPeter Brune   Output Parameter:
3858bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3859b0ae01b7SPeter Brune 
3860b0ae01b7SPeter Brune   Level: developer
3861b0ae01b7SPeter Brune 
38621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3863b0ae01b7SPeter Brune @*/
3864d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3865d71ae5a4SJacob Faibussowitsch {
3866b0ae01b7SPeter Brune   PetscFunctionBegin;
38675a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38684f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3869b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3871b0ae01b7SPeter Brune }
3872b0ae01b7SPeter Brune 
38733ad4599aSBarry Smith /*@
3874bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38753ad4599aSBarry Smith 
38763ad4599aSBarry Smith   Not Collective
38773ad4599aSBarry Smith 
38783ad4599aSBarry Smith   Input Parameter:
3879bb7acecfSBarry Smith . dm - the `DM` object
38803ad4599aSBarry Smith 
38813ad4599aSBarry Smith   Output Parameter:
3882bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38833ad4599aSBarry Smith 
38843ad4599aSBarry Smith   Level: developer
38853ad4599aSBarry Smith 
38861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38873ad4599aSBarry Smith @*/
3888d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3889d71ae5a4SJacob Faibussowitsch {
38903ad4599aSBarry Smith   PetscFunctionBegin;
38915a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38924f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38933ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38953ad4599aSBarry Smith }
38963ad4599aSBarry Smith 
3897a7058e45SLawrence Mitchell /*@
3898bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3899a7058e45SLawrence Mitchell 
3900a7058e45SLawrence Mitchell   Not Collective
3901a7058e45SLawrence Mitchell 
3902a7058e45SLawrence Mitchell   Input Parameter:
3903bb7acecfSBarry Smith . dm - the `DM` object
3904a7058e45SLawrence Mitchell 
3905a7058e45SLawrence Mitchell   Output Parameter:
3906bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3907a7058e45SLawrence Mitchell 
3908a7058e45SLawrence Mitchell   Level: developer
3909a7058e45SLawrence Mitchell 
39101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3911a7058e45SLawrence Mitchell @*/
3912d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3913d71ae5a4SJacob Faibussowitsch {
3914a7058e45SLawrence Mitchell   PetscFunctionBegin;
39155a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39164f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3917dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3918ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3920a7058e45SLawrence Mitchell }
3921a7058e45SLawrence Mitchell 
39220298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3923264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3924264ace61SBarry Smith 
3925cc4c1da9SBarry Smith /*@
3926bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3927264ace61SBarry Smith 
392820f4b53cSBarry Smith   Collective
3929264ace61SBarry Smith 
3930264ace61SBarry Smith   Input Parameters:
3931bb7acecfSBarry Smith + dm     - The `DM` object
3932bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3933264ace61SBarry Smith 
3934264ace61SBarry Smith   Options Database Key:
3935bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3936264ace61SBarry Smith 
3937264ace61SBarry Smith   Level: intermediate
3938264ace61SBarry Smith 
3939bb7acecfSBarry Smith   Note:
39400462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3941bb7acecfSBarry Smith 
39421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3943264ace61SBarry Smith @*/
3944d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3945d71ae5a4SJacob Faibussowitsch {
3946264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3947264ace61SBarry Smith   PetscBool match;
3948264ace61SBarry Smith 
3949264ace61SBarry Smith   PetscFunctionBegin;
3950264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39519566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39523ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3953264ace61SBarry Smith 
39549566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39559566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39567a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3957264ace61SBarry Smith 
3958dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39599566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39609566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39619566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3963264ace61SBarry Smith }
3964264ace61SBarry Smith 
3965cc4c1da9SBarry Smith /*@
3966bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3967264ace61SBarry Smith 
3968264ace61SBarry Smith   Not Collective
3969264ace61SBarry Smith 
3970264ace61SBarry Smith   Input Parameter:
3971bb7acecfSBarry Smith . dm - The `DM`
3972264ace61SBarry Smith 
3973264ace61SBarry Smith   Output Parameter:
3974bb7acecfSBarry Smith . type - The `DMType` name
3975264ace61SBarry Smith 
3976264ace61SBarry Smith   Level: intermediate
3977264ace61SBarry Smith 
39781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3979264ace61SBarry Smith @*/
3980d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3981d71ae5a4SJacob Faibussowitsch {
3982264ace61SBarry Smith   PetscFunctionBegin;
3983264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39844f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39859566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3986264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3988264ace61SBarry Smith }
3989264ace61SBarry Smith 
39905d83a8b1SBarry Smith /*@
3991bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
399267a56275SMatthew G Knepley 
399320f4b53cSBarry Smith   Collective
399467a56275SMatthew G Knepley 
399567a56275SMatthew G Knepley   Input Parameters:
3996bb7acecfSBarry Smith + dm      - the `DM`
3997bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
399867a56275SMatthew G Knepley 
399967a56275SMatthew G Knepley   Output Parameter:
4000bb7acecfSBarry Smith . M - pointer to new `DM`
400167a56275SMatthew G Knepley 
400220f4b53cSBarry Smith   Level: intermediate
400320f4b53cSBarry Smith 
400473ff1848SBarry Smith   Note:
4005bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
4006bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
4007bb7acecfSBarry Smith   of the input `DM`.
400867a56275SMatthew G Knepley 
40091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
401067a56275SMatthew G Knepley @*/
4011d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
4012d71ae5a4SJacob Faibussowitsch {
401367a56275SMatthew G Knepley   DM        B;
401467a56275SMatthew G Knepley   char      convname[256];
4015c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
401667a56275SMatthew G Knepley 
401767a56275SMatthew G Knepley   PetscFunctionBegin;
401867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
401967a56275SMatthew G Knepley   PetscValidType(dm, 1);
40204f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40219566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40229566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4023c067b6caSMatthew G. Knepley   if (sametype) {
4024c067b6caSMatthew G. Knepley     *M = dm;
40259566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40263ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4027c067b6caSMatthew G. Knepley   } else {
40280298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
402967a56275SMatthew G Knepley 
403067a56275SMatthew G Knepley     /*
403167a56275SMatthew G Knepley        Order of precedence:
403267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
403367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
403467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
403567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
403667a56275SMatthew G Knepley        5) Use a really basic converter.
403767a56275SMatthew G Knepley     */
403867a56275SMatthew G Knepley 
403967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40409566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40419566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40429566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40439566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40449566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40459566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
404667a56275SMatthew G Knepley     if (conv) goto foundconv;
404767a56275SMatthew G Knepley 
404867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40499566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40509566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40519566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40529566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40539566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40549566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40559566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40569566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
405767a56275SMatthew G Knepley     if (conv) {
40589566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
405967a56275SMatthew G Knepley       goto foundconv;
406067a56275SMatthew G Knepley     }
406167a56275SMatthew G Knepley 
406267a56275SMatthew G Knepley #if 0
406367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
406467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40659566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
406667a56275SMatthew G Knepley     if (conv) goto foundconv;
406767a56275SMatthew G Knepley 
406867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
406967a56275SMatthew G Knepley     if (dm->ops->convert) {
407067a56275SMatthew G Knepley       conv = dm->ops->convert;
407167a56275SMatthew G Knepley     }
407267a56275SMatthew G Knepley     if (conv) goto foundconv;
407367a56275SMatthew G Knepley #endif
407467a56275SMatthew G Knepley 
407567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
407698921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
407767a56275SMatthew G Knepley 
407867a56275SMatthew G Knepley   foundconv:
40799566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40809566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
408112fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
408290b157c4SStefano Zampini     {
40834fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40846858538eSMatthew G. Knepley 
40854fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40864fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4087c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40889566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40899566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40909566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40919566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
409212fa691eSMatthew G. Knepley     }
40939566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
409467a56275SMatthew G Knepley   }
40959566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
409767a56275SMatthew G Knepley }
4098264ace61SBarry Smith 
4099264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4100264ace61SBarry Smith 
4101264ace61SBarry Smith /*@C
4102bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
41031c84c290SBarry Smith 
4104cc4c1da9SBarry Smith   Not Collective, No Fortran Support
41051c84c290SBarry Smith 
41061c84c290SBarry Smith   Input Parameters:
410720f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
410820f4b53cSBarry Smith - function - The creation routine itself
410920f4b53cSBarry Smith 
411020f4b53cSBarry Smith   Level: advanced
41111c84c290SBarry Smith 
411273ff1848SBarry Smith   Note:
411320f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
41141c84c290SBarry Smith 
411560225df5SJacob Faibussowitsch   Example Usage:
41161c84c290SBarry Smith .vb
4117bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41181c84c290SBarry Smith .ve
41191c84c290SBarry Smith 
412020f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41211c84c290SBarry Smith .vb
41221c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41231c84c290SBarry Smith     DMSetType(DM,"my_da");
41241c84c290SBarry Smith .ve
41251c84c290SBarry Smith   or at runtime via the option
41261c84c290SBarry Smith .vb
41271c84c290SBarry Smith     -da_type my_da
41281c84c290SBarry Smith .ve
4129264ace61SBarry Smith 
41301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4131264ace61SBarry Smith @*/
4132d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4133d71ae5a4SJacob Faibussowitsch {
4134264ace61SBarry Smith   PetscFunctionBegin;
41359566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41369566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4138264ace61SBarry Smith }
4139264ace61SBarry Smith 
4140ffeef943SBarry Smith /*@
4141bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4142b859378eSBarry Smith 
414320f4b53cSBarry Smith   Collective
4144b859378eSBarry Smith 
4145b859378eSBarry Smith   Input Parameters:
4146bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4147bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4148bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4149bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4150b859378eSBarry Smith 
4151b859378eSBarry Smith   Level: intermediate
4152b859378eSBarry Smith 
4153b859378eSBarry Smith   Notes:
415455849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4155b859378eSBarry Smith 
4156bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4157bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4158bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4159cd7e8a5eSksagiyam 
41601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4161b859378eSBarry Smith @*/
4162d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4163d71ae5a4SJacob Faibussowitsch {
41649331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4165b859378eSBarry Smith 
4166b859378eSBarry Smith   PetscFunctionBegin;
4167b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4168b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41699566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41709566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41719566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41739331c7a4SMatthew G. Knepley   if (isbinary) {
41749331c7a4SMatthew G. Knepley     PetscInt classid;
41759331c7a4SMatthew G. Knepley     char     type[256];
4176b859378eSBarry Smith 
41779566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41787a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41799566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41809566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4181dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41829331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4183dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41849331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41859566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4187b859378eSBarry Smith }
4188b859378eSBarry Smith 
41894ee01570SBarry Smith /* FEM Support */
41907da65231SMatthew G Knepley 
41912ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41922ecf6ec3SMatthew G. Knepley {
41932ecf6ec3SMatthew G. Knepley   PetscInt f;
41942ecf6ec3SMatthew G. Knepley 
41952ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41962ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41972ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41982ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41992ecf6ec3SMatthew G. Knepley }
42002ecf6ec3SMatthew G. Knepley 
4201d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4202d71ae5a4SJacob Faibussowitsch {
42031d47ebbbSSatish Balay   PetscInt f;
42041b30c384SMatthew G Knepley 
42057da65231SMatthew G Knepley   PetscFunctionBegin;
420663a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
420748a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
42083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42097da65231SMatthew G Knepley }
42107da65231SMatthew G Knepley 
42115962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
42125962854dSMatthew G. Knepley {
42135962854dSMatthew G. Knepley   PetscInt f;
42145962854dSMatthew G. Knepley 
42155962854dSMatthew G. Knepley   PetscFunctionBegin;
42165962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42175962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42185962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42195962854dSMatthew G. Knepley }
42205962854dSMatthew G. Knepley 
4221d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4222d71ae5a4SJacob Faibussowitsch {
42231b30c384SMatthew G Knepley   PetscInt f, g;
42247da65231SMatthew G Knepley 
42257da65231SMatthew G Knepley   PetscFunctionBegin;
422663a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42271d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42289566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
422948a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42309566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42317da65231SMatthew G Knepley   }
42323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42337da65231SMatthew G Knepley }
4234e7c4fc90SDmitry Karpeev 
4235d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4236d71ae5a4SJacob Faibussowitsch {
42370c5b8624SToby Isaac   PetscInt           localSize, bs;
42380c5b8624SToby Isaac   PetscMPIInt        size;
42390c5b8624SToby Isaac   Vec                x, xglob;
42400c5b8624SToby Isaac   const PetscScalar *xarray;
4241e759306cSMatthew G. Knepley 
4242e759306cSMatthew G. Knepley   PetscFunctionBegin;
42439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42449566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42459566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
424693ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42479566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42480c5b8624SToby Isaac   if (size > 1) {
42499566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42509566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42519566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42529566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42530c5b8624SToby Isaac   } else {
42540c5b8624SToby Isaac     xglob = x;
42550c5b8624SToby Isaac   }
42569566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42570c5b8624SToby Isaac   if (size > 1) {
42589566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42599566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42600c5b8624SToby Isaac   }
42619566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4263e759306cSMatthew G. Knepley }
4264e759306cSMatthew G. Knepley 
426588ed4aceSMatthew G Knepley /*@
4266bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4267061576a5SJed Brown 
4268061576a5SJed Brown   Input Parameter:
4269bb7acecfSBarry Smith . dm - The `DM`
4270061576a5SJed Brown 
4271061576a5SJed Brown   Output Parameter:
4272bb7acecfSBarry Smith . section - The `PetscSection`
4273061576a5SJed Brown 
427420f4b53cSBarry Smith   Options Database Key:
4275bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4276061576a5SJed Brown 
4277061576a5SJed Brown   Level: advanced
4278061576a5SJed Brown 
4279061576a5SJed Brown   Notes:
4280bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4281061576a5SJed Brown 
4282bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4283061576a5SJed Brown 
42841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4285061576a5SJed Brown @*/
4286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4287d71ae5a4SJacob Faibussowitsch {
4288061576a5SJed Brown   PetscFunctionBegin;
42899566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4291061576a5SJed Brown }
4292061576a5SJed Brown 
4293061576a5SJed Brown /*@
4294bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
429588ed4aceSMatthew G Knepley 
429688ed4aceSMatthew G Knepley   Input Parameter:
4297bb7acecfSBarry Smith . dm - The `DM`
429888ed4aceSMatthew G Knepley 
429988ed4aceSMatthew G Knepley   Output Parameter:
4300bb7acecfSBarry Smith . section - The `PetscSection`
430188ed4aceSMatthew G Knepley 
430220f4b53cSBarry Smith   Options Database Key:
4303bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4304e5893cccSMatthew G. Knepley 
430588ed4aceSMatthew G Knepley   Level: intermediate
430688ed4aceSMatthew G Knepley 
4307bb7acecfSBarry Smith   Note:
4308bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
430988ed4aceSMatthew G Knepley 
43101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
431188ed4aceSMatthew G Knepley @*/
4312d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4313d71ae5a4SJacob Faibussowitsch {
431488ed4aceSMatthew G Knepley   PetscFunctionBegin;
431588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43164f572ea9SToby Isaac   PetscAssertPointer(section, 2);
43171bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4318e5e52638SMatthew G. Knepley     PetscInt d;
4319e5e52638SMatthew G. Knepley 
432045480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
432145480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
432245480ffeSMatthew G. Knepley       PetscViewer       viewer;
432345480ffeSMatthew G. Knepley       PetscViewerFormat format;
432445480ffeSMatthew G. Knepley       PetscBool         flg;
432545480ffeSMatthew G. Knepley 
4326648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43279566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
432845480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43299566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43309566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
433145480ffeSMatthew G. Knepley       }
433245480ffeSMatthew G. Knepley       if (flg) {
43339566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43349566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4335648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
433645480ffeSMatthew G. Knepley       }
433745480ffeSMatthew G. Knepley     }
4338dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43399566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43402f0f8703SMatthew G. Knepley   }
43411bb6d2a8SBarry Smith   *section = dm->localSection;
43423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
434388ed4aceSMatthew G Knepley }
434488ed4aceSMatthew G Knepley 
434588ed4aceSMatthew G Knepley /*@
4346bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4347061576a5SJed Brown 
4348061576a5SJed Brown   Input Parameters:
4349bb7acecfSBarry Smith + dm      - The `DM`
4350bb7acecfSBarry Smith - section - The `PetscSection`
4351061576a5SJed Brown 
4352061576a5SJed Brown   Level: advanced
4353061576a5SJed Brown 
4354061576a5SJed Brown   Notes:
4355bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4356061576a5SJed Brown 
4357bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4358061576a5SJed Brown 
43591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4360061576a5SJed Brown @*/
4361d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4362d71ae5a4SJacob Faibussowitsch {
4363061576a5SJed Brown   PetscFunctionBegin;
43649566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4366061576a5SJed Brown }
4367061576a5SJed Brown 
4368061576a5SJed Brown /*@
4369bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
437088ed4aceSMatthew G Knepley 
437188ed4aceSMatthew G Knepley   Input Parameters:
4372bb7acecfSBarry Smith + dm      - The `DM`
4373bb7acecfSBarry Smith - section - The `PetscSection`
437488ed4aceSMatthew G Knepley 
437588ed4aceSMatthew G Knepley   Level: intermediate
437688ed4aceSMatthew G Knepley 
4377bb7acecfSBarry Smith   Note:
4378bb7acecfSBarry Smith   Any existing Section will be destroyed
437988ed4aceSMatthew G Knepley 
43801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
438188ed4aceSMatthew G Knepley @*/
4382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4383d71ae5a4SJacob Faibussowitsch {
4384c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4385af122d2aSMatthew G Knepley   PetscInt f;
438688ed4aceSMatthew G Knepley 
438788ed4aceSMatthew G Knepley   PetscFunctionBegin;
438888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4389b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43909566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43919566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43921bb6d2a8SBarry Smith   dm->localSection = section;
43939566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4394af122d2aSMatthew G Knepley   if (numFields) {
43959566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4396af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43970f21e855SMatthew G. Knepley       PetscObject disc;
4398af122d2aSMatthew G Knepley       const char *name;
4399af122d2aSMatthew G Knepley 
44009566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
44019566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
44029566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4403af122d2aSMatthew G Knepley     }
4404af122d2aSMatthew G Knepley   }
440550350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
440650350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
44079566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
440850350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
440952947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
441050350c8eSStefano Zampini 
441150350c8eSStefano Zampini   /* Clear scratch vectors */
441250350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
441350350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
441450350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
441550350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
44163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
441788ed4aceSMatthew G Knepley }
441888ed4aceSMatthew G Knepley 
4419adc21957SMatthew G. Knepley /*@C
4420d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4421adc21957SMatthew G. Knepley 
4422adc21957SMatthew G. Knepley   Input Parameter:
4423adc21957SMatthew G. Knepley . dm - The `DM`
4424adc21957SMatthew G. Knepley 
44259cde84edSJose E. Roman   Output Parameters:
4426adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4427b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4428adc21957SMatthew G. Knepley 
4429adc21957SMatthew G. Knepley   Level: developer
4430adc21957SMatthew G. Knepley 
4431adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4432adc21957SMatthew G. Knepley @*/
4433adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4434adc21957SMatthew G. Knepley {
4435adc21957SMatthew G. Knepley   PetscFunctionBegin;
4436adc21957SMatthew G. Knepley   *perm        = NULL;
4437adc21957SMatthew G. Knepley   *blockStarts = NULL;
4438adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4439adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4440adc21957SMatthew G. Knepley }
4441adc21957SMatthew G. Knepley 
44429435951eSToby Isaac /*@
4443bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44449435951eSToby Isaac 
444520f4b53cSBarry Smith   not Collective
4446e228b242SToby Isaac 
44479435951eSToby Isaac   Input Parameter:
4448bb7acecfSBarry Smith . dm - The `DM`
44499435951eSToby Isaac 
4450d8d19677SJose E. Roman   Output Parameters:
445120f4b53cSBarry 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.
445220f4b53cSBarry 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.
445379769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44549435951eSToby Isaac 
44559435951eSToby Isaac   Level: advanced
44569435951eSToby Isaac 
4457bb7acecfSBarry Smith   Note:
4458bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44599435951eSToby Isaac 
44601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44619435951eSToby Isaac @*/
4462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4463d71ae5a4SJacob Faibussowitsch {
44649435951eSToby Isaac   PetscFunctionBegin;
44659435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4466dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44673b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44683b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
446979769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44719435951eSToby Isaac }
44729435951eSToby Isaac 
44739435951eSToby Isaac /*@
4474bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44759435951eSToby Isaac 
447620f4b53cSBarry Smith   Collective
4477e228b242SToby Isaac 
44789435951eSToby Isaac   Input Parameters:
4479bb7acecfSBarry Smith + dm      - The `DM`
4480bb7acecfSBarry 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).
448120f4b53cSBarry 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).
448220f4b53cSBarry 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).
44839435951eSToby Isaac 
44849435951eSToby Isaac   Level: advanced
44859435951eSToby Isaac 
448620f4b53cSBarry Smith   Notes:
448720f4b53cSBarry 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()`.
448820f4b53cSBarry Smith 
448920f4b53cSBarry 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.
449020f4b53cSBarry Smith 
4491bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44929435951eSToby Isaac 
44931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44949435951eSToby Isaac @*/
4495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4496d71ae5a4SJacob Faibussowitsch {
4497e228b242SToby Isaac   PetscMPIInt result;
44989435951eSToby Isaac 
44999435951eSToby Isaac   PetscFunctionBegin;
45009435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4501e228b242SToby Isaac   if (section) {
4502e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45039566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
45047a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4505e228b242SToby Isaac   }
4506e228b242SToby Isaac   if (mat) {
4507e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
45089566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
45097a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4510e228b242SToby Isaac   }
451179769bd5SJed Brown   if (bias) {
451279769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
45139566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
451479769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
451579769bd5SJed Brown   }
45169566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45179566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
45183b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45199566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
45209566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45213b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45229566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45239566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
452479769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45269435951eSToby Isaac }
45279435951eSToby Isaac 
4528497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4529507e4973SMatthew G. Knepley /*
4530bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4531507e4973SMatthew G. Knepley 
4532507e4973SMatthew G. Knepley   Input Parameters:
4533bb7acecfSBarry Smith + dm - The `DM`
4534bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4535bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4536507e4973SMatthew G. Knepley 
4537507e4973SMatthew G. Knepley   Level: intermediate
4538507e4973SMatthew G. Knepley 
45391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4540507e4973SMatthew G. Knepley */
4541d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4542d71ae5a4SJacob Faibussowitsch {
4543507e4973SMatthew G. Knepley   MPI_Comm        comm;
4544507e4973SMatthew G. Knepley   PetscLayout     layout;
4545507e4973SMatthew G. Knepley   const PetscInt *ranges;
4546507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4547507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4548507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4549507e4973SMatthew G. Knepley 
4550507e4973SMatthew G. Knepley   PetscFunctionBegin;
45519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4552507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45539566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45549566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45569566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45579566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45589566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45599566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45609566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45619566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4562507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4563f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4564507e4973SMatthew G. Knepley 
45659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45699566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45709566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4571507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45729371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45739371c9d4SSatish 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));
45749371c9d4SSatish Balay       valid = PETSC_FALSE;
45759371c9d4SSatish Balay     }
45769371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45779371c9d4SSatish 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));
45789371c9d4SSatish Balay       valid = PETSC_FALSE;
45799371c9d4SSatish Balay     }
4580507e4973SMatthew G. Knepley     if (gdof < 0) {
4581507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4582507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4583507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4584507e4973SMatthew G. Knepley 
45859566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4586507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45879371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45889371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45899371c9d4SSatish Balay           valid = PETSC_FALSE;
45909371c9d4SSatish Balay           break;
45919371c9d4SSatish Balay         }
4592507e4973SMatthew G. Knepley       }
4593507e4973SMatthew G. Knepley     }
4594507e4973SMatthew G. Knepley   }
45959566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45969566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
4597462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4598507e4973SMatthew G. Knepley   if (!gvalid) {
45999566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4600507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4601507e4973SMatthew G. Knepley   }
46023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4603507e4973SMatthew G. Knepley }
4604f741bcd2SMatthew G. Knepley #endif
4605507e4973SMatthew G. Knepley 
46065f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
46076725e60dSJed Brown {
46086725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
46094d86920dSPierre Jolivet 
46106725e60dSJed Brown   PetscFunctionBegin;
46116725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46124f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
46135f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
46146725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
46156725e60dSJed Brown   else *sf = dm->sf;
46163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46176725e60dSJed Brown }
46186725e60dSJed Brown 
461988ed4aceSMatthew G Knepley /*@
4620bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
462188ed4aceSMatthew G Knepley 
462220f4b53cSBarry Smith   Collective
46238b1ab98fSJed Brown 
462488ed4aceSMatthew G Knepley   Input Parameter:
4625bb7acecfSBarry Smith . dm - The `DM`
462688ed4aceSMatthew G Knepley 
462788ed4aceSMatthew G Knepley   Output Parameter:
4628bb7acecfSBarry Smith . section - The `PetscSection`
462988ed4aceSMatthew G Knepley 
463088ed4aceSMatthew G Knepley   Level: intermediate
463188ed4aceSMatthew G Knepley 
4632bb7acecfSBarry Smith   Note:
4633bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
463488ed4aceSMatthew G Knepley 
46351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
463688ed4aceSMatthew G Knepley @*/
4637d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4638d71ae5a4SJacob Faibussowitsch {
463988ed4aceSMatthew G Knepley   PetscFunctionBegin;
464088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46414f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46421bb6d2a8SBarry Smith   if (!dm->globalSection) {
4643fd59a867SMatthew G. Knepley     PetscSection s;
46446725e60dSJed Brown     PetscSF      sf;
4645fd59a867SMatthew G. Knepley 
46469566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46477a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46487a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46495f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4650eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46519566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46539566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
465488ed4aceSMatthew G Knepley   }
46551bb6d2a8SBarry Smith   *section = dm->globalSection;
46563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
465788ed4aceSMatthew G Knepley }
465888ed4aceSMatthew G Knepley 
4659b21d0597SMatthew G Knepley /*@
4660bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4661b21d0597SMatthew G Knepley 
4662b21d0597SMatthew G Knepley   Input Parameters:
4663bb7acecfSBarry Smith + dm      - The `DM`
46642fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4665b21d0597SMatthew G Knepley 
4666b21d0597SMatthew G Knepley   Level: intermediate
4667b21d0597SMatthew G Knepley 
4668bb7acecfSBarry Smith   Note:
4669bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4670b21d0597SMatthew G Knepley 
46711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4672b21d0597SMatthew G Knepley @*/
4673d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4674d71ae5a4SJacob Faibussowitsch {
4675b21d0597SMatthew G Knepley   PetscFunctionBegin;
4676b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46775080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46789566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46799566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46801bb6d2a8SBarry Smith   dm->globalSection = section;
4681497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46829566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4683507e4973SMatthew G. Knepley #endif
468450350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
468550350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
468652947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
468750350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
468850350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4690b21d0597SMatthew G Knepley }
4691b21d0597SMatthew G Knepley 
469288ed4aceSMatthew G Knepley /*@
4693bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4694bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
469588ed4aceSMatthew G Knepley 
469688ed4aceSMatthew G Knepley   Input Parameter:
4697bb7acecfSBarry Smith . dm - The `DM`
469888ed4aceSMatthew G Knepley 
469988ed4aceSMatthew G Knepley   Output Parameter:
4700bb7acecfSBarry Smith . sf - The `PetscSF`
470188ed4aceSMatthew G Knepley 
470288ed4aceSMatthew G Knepley   Level: intermediate
470388ed4aceSMatthew G Knepley 
4704bb7acecfSBarry Smith   Note:
4705bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
470688ed4aceSMatthew G Knepley 
47071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
470888ed4aceSMatthew G Knepley @*/
4709d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4710d71ae5a4SJacob Faibussowitsch {
471188ed4aceSMatthew G Knepley   PetscInt nroots;
471288ed4aceSMatthew G Knepley 
471388ed4aceSMatthew G Knepley   PetscFunctionBegin;
471488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47154f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
471648a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
47179566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
471888ed4aceSMatthew G Knepley   if (nroots < 0) {
471988ed4aceSMatthew G Knepley     PetscSection section, gSection;
472088ed4aceSMatthew G Knepley 
47219566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
472231ea6d37SMatthew G Knepley     if (section) {
47239566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47249566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
472531ea6d37SMatthew G Knepley     } else {
47260298fd71SBarry Smith       *sf = NULL;
47273ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
472831ea6d37SMatthew G Knepley     }
472988ed4aceSMatthew G Knepley   }
47301bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
473288ed4aceSMatthew G Knepley }
473388ed4aceSMatthew G Knepley 
473488ed4aceSMatthew G Knepley /*@
4735bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
473688ed4aceSMatthew G Knepley 
473788ed4aceSMatthew G Knepley   Input Parameters:
4738bb7acecfSBarry Smith + dm - The `DM`
4739bb7acecfSBarry Smith - sf - The `PetscSF`
474088ed4aceSMatthew G Knepley 
474188ed4aceSMatthew G Knepley   Level: intermediate
474288ed4aceSMatthew G Knepley 
4743bb7acecfSBarry Smith   Note:
4744bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
474588ed4aceSMatthew G Knepley 
47461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
474788ed4aceSMatthew G Knepley @*/
4748d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4749d71ae5a4SJacob Faibussowitsch {
475088ed4aceSMatthew G Knepley   PetscFunctionBegin;
475188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4752b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47549566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47551bb6d2a8SBarry Smith   dm->sectionSF = sf;
47563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
475788ed4aceSMatthew G Knepley }
475888ed4aceSMatthew G Knepley 
4759cc4c1da9SBarry Smith /*@
4760bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
476188ed4aceSMatthew G Knepley   describing the data layout.
476288ed4aceSMatthew G Knepley 
476388ed4aceSMatthew G Knepley   Input Parameters:
4764bb7acecfSBarry Smith + dm            - The `DM`
4765bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4766bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
476788ed4aceSMatthew G Knepley 
47681bb6d2a8SBarry Smith   Level: developer
47691bb6d2a8SBarry Smith 
4770bb7acecfSBarry Smith   Note:
4771bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4772bb7acecfSBarry Smith 
477373ff1848SBarry Smith   Developer Note:
4774bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4775bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4776b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4777b6971eaeSBarry Smith   they create, this returns nothing
47781bb6d2a8SBarry Smith 
47791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
478088ed4aceSMatthew G Knepley @*/
4781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4782d71ae5a4SJacob Faibussowitsch {
478388ed4aceSMatthew G Knepley   PetscFunctionBegin;
478488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47859566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
478788ed4aceSMatthew G Knepley }
4788af122d2aSMatthew G Knepley 
4789b21d0597SMatthew G Knepley /*@
4790bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4791bb7acecfSBarry Smith 
4792bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4793b21d0597SMatthew G Knepley 
4794b21d0597SMatthew G Knepley   Input Parameter:
4795bb7acecfSBarry Smith . dm - The `DM`
4796b21d0597SMatthew G Knepley 
4797b21d0597SMatthew G Knepley   Output Parameter:
4798bb7acecfSBarry Smith . sf - The `PetscSF`
4799b21d0597SMatthew G Knepley 
4800b21d0597SMatthew G Knepley   Level: intermediate
4801b21d0597SMatthew G Knepley 
4802bb7acecfSBarry Smith   Note:
4803bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4804b21d0597SMatthew G Knepley 
48051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4806b21d0597SMatthew G Knepley @*/
4807d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4808d71ae5a4SJacob Faibussowitsch {
4809b21d0597SMatthew G Knepley   PetscFunctionBegin;
4810b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48114f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4812b21d0597SMatthew G Knepley   *sf = dm->sf;
48133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4814b21d0597SMatthew G Knepley }
4815b21d0597SMatthew G Knepley 
4816057b4bcdSMatthew G Knepley /*@
4817bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4818bb7acecfSBarry Smith 
481920f4b53cSBarry Smith   Collective
4820057b4bcdSMatthew G Knepley 
4821057b4bcdSMatthew G Knepley   Input Parameters:
4822bb7acecfSBarry Smith + dm - The `DM`
4823bb7acecfSBarry Smith - sf - The `PetscSF`
4824057b4bcdSMatthew G Knepley 
4825057b4bcdSMatthew G Knepley   Level: intermediate
4826057b4bcdSMatthew G Knepley 
48271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4828057b4bcdSMatthew G Knepley @*/
4829d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4830d71ae5a4SJacob Faibussowitsch {
4831057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4832057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4833b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48359566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4836057b4bcdSMatthew G Knepley   dm->sf = sf;
48373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4838057b4bcdSMatthew G Knepley }
4839057b4bcdSMatthew G Knepley 
48404f37162bSMatthew G. Knepley /*@
4841bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48424f37162bSMatthew G. Knepley 
48434f37162bSMatthew G. Knepley   Input Parameter:
4844bb7acecfSBarry Smith . dm - The `DM`
48454f37162bSMatthew G. Knepley 
48464f37162bSMatthew G. Knepley   Output Parameter:
4847bb7acecfSBarry Smith . sf - The `PetscSF`
48484f37162bSMatthew G. Knepley 
48494f37162bSMatthew G. Knepley   Level: intermediate
48504f37162bSMatthew G. Knepley 
4851bb7acecfSBarry Smith   Note:
4852bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48534f37162bSMatthew G. Knepley 
48541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48554f37162bSMatthew G. Knepley @*/
4856d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4857d71ae5a4SJacob Faibussowitsch {
48584f37162bSMatthew G. Knepley   PetscFunctionBegin;
48594f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48604f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48614f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48634f37162bSMatthew G. Knepley }
48644f37162bSMatthew G. Knepley 
48654f37162bSMatthew G. Knepley /*@
48664f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48674f37162bSMatthew G. Knepley 
48684f37162bSMatthew G. Knepley   Input Parameters:
48694f37162bSMatthew G. Knepley + dm - The DM
48704f37162bSMatthew G. Knepley - sf - The PetscSF
48714f37162bSMatthew G. Knepley 
48724f37162bSMatthew G. Knepley   Level: intermediate
48734f37162bSMatthew G. Knepley 
48741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48754f37162bSMatthew G. Knepley @*/
4876d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4877d71ae5a4SJacob Faibussowitsch {
48784f37162bSMatthew G. Knepley   PetscFunctionBegin;
48794f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48804f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48829566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48834f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48854f37162bSMatthew G. Knepley }
48864f37162bSMatthew G. Knepley 
4887d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4888d71ae5a4SJacob Faibussowitsch {
488934aa8a36SMatthew G. Knepley   PetscClassId id;
489034aa8a36SMatthew G. Knepley 
489134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48929566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
489334aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48949566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
489534aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48969566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
489717c1d62eSMatthew G. Knepley   } else {
48989566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
489934aa8a36SMatthew G. Knepley   }
49003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490134aa8a36SMatthew G. Knepley }
490234aa8a36SMatthew G. Knepley 
4903d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4904d71ae5a4SJacob Faibussowitsch {
490544a7f3ddSMatthew G. Knepley   RegionField *tmpr;
490644a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
490744a7f3ddSMatthew G. Knepley 
490844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
49093ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
49109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
491144a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
49129371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
49139371c9d4SSatish Balay     tmpr[f].disc        = NULL;
49149371c9d4SSatish Balay     tmpr[f].label       = NULL;
49159371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
49169371c9d4SSatish Balay   }
49179566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
491844a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
491944a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
49203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
492144a7f3ddSMatthew G. Knepley }
492244a7f3ddSMatthew G. Knepley 
492344a7f3ddSMatthew G. Knepley /*@
492420f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
492544a7f3ddSMatthew G. Knepley 
492620f4b53cSBarry Smith   Logically Collective
492744a7f3ddSMatthew G. Knepley 
492844a7f3ddSMatthew G. Knepley   Input Parameter:
492920f4b53cSBarry Smith . dm - The `DM`
493044a7f3ddSMatthew G. Knepley 
493144a7f3ddSMatthew G. Knepley   Level: intermediate
493244a7f3ddSMatthew G. Knepley 
49331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
493444a7f3ddSMatthew G. Knepley @*/
4935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4936d71ae5a4SJacob Faibussowitsch {
493744a7f3ddSMatthew G. Knepley   PetscInt f;
493844a7f3ddSMatthew G. Knepley 
493944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
494044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
494144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49429566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49439566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
494444a7f3ddSMatthew G. Knepley   }
49459566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
494644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
494744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
494944a7f3ddSMatthew G. Knepley }
495044a7f3ddSMatthew G. Knepley 
4951689b5837SMatthew G. Knepley /*@
495220f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4953689b5837SMatthew G. Knepley 
495420f4b53cSBarry Smith   Not Collective
4955689b5837SMatthew G. Knepley 
4956689b5837SMatthew G. Knepley   Input Parameter:
495720f4b53cSBarry Smith . dm - The `DM`
4958689b5837SMatthew G. Knepley 
4959689b5837SMatthew G. Knepley   Output Parameter:
496060225df5SJacob Faibussowitsch . numFields - The number of fields
4961689b5837SMatthew G. Knepley 
4962689b5837SMatthew G. Knepley   Level: intermediate
4963689b5837SMatthew G. Knepley 
49641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4965689b5837SMatthew G. Knepley @*/
4966d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4967d71ae5a4SJacob Faibussowitsch {
49680f21e855SMatthew G. Knepley   PetscFunctionBegin;
49690f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49704f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
497144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4973af122d2aSMatthew G Knepley }
4974af122d2aSMatthew G Knepley 
4975689b5837SMatthew G. Knepley /*@
497620f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4977689b5837SMatthew G. Knepley 
497820f4b53cSBarry Smith   Logically Collective
4979689b5837SMatthew G. Knepley 
4980689b5837SMatthew G. Knepley   Input Parameters:
498120f4b53cSBarry Smith + dm        - The `DM`
498260225df5SJacob Faibussowitsch - numFields - The number of fields
4983689b5837SMatthew G. Knepley 
4984689b5837SMatthew G. Knepley   Level: intermediate
4985689b5837SMatthew G. Knepley 
49861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4987689b5837SMatthew G. Knepley @*/
4988d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4989d71ae5a4SJacob Faibussowitsch {
49900f21e855SMatthew G. Knepley   PetscInt Nf, f;
4991af122d2aSMatthew G Knepley 
4992af122d2aSMatthew G Knepley   PetscFunctionBegin;
4993af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49949566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49950f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49960f21e855SMatthew G. Knepley     PetscContainer obj;
49970f21e855SMatthew G. Knepley 
49989566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49999566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
50009566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
5001af122d2aSMatthew G Knepley   }
50023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5003af122d2aSMatthew G Knepley }
5004af122d2aSMatthew G Knepley 
5005c1929be8SMatthew G. Knepley /*@
5006bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
5007c1929be8SMatthew G. Knepley 
500820f4b53cSBarry Smith   Not Collective
5009c1929be8SMatthew G. Knepley 
5010c1929be8SMatthew G. Knepley   Input Parameters:
5011bb7acecfSBarry Smith + dm - The `DM`
5012c1929be8SMatthew G. Knepley - f  - The field number
5013c1929be8SMatthew G. Knepley 
501444a7f3ddSMatthew G. Knepley   Output Parameters:
501520f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
501620f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
5017c1929be8SMatthew G. Knepley 
501844a7f3ddSMatthew G. Knepley   Level: intermediate
5019c1929be8SMatthew G. Knepley 
50201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5021c1929be8SMatthew G. Knepley @*/
5022d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5023d71ae5a4SJacob Faibussowitsch {
5024af122d2aSMatthew G Knepley   PetscFunctionBegin;
5025af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50264f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50277a8be351SBarry 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);
502844a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
5029bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
50303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5031decb47aaSMatthew G. Knepley }
5032decb47aaSMatthew G. Knepley 
5033083401c6SMatthew G. Knepley /* Does not clear the DS */
5034d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5035d71ae5a4SJacob Faibussowitsch {
5036083401c6SMatthew G. Knepley   PetscFunctionBegin;
50379566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50389566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50399566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5040083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5041bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50429566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5043bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
50443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5045083401c6SMatthew G. Knepley }
5046083401c6SMatthew G. Knepley 
5047ffeef943SBarry Smith /*@
5048bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5049bb7acecfSBarry Smith   the field numbering.
5050c1929be8SMatthew G. Knepley 
505120f4b53cSBarry Smith   Logically Collective
5052c1929be8SMatthew G. Knepley 
5053c1929be8SMatthew G. Knepley   Input Parameters:
5054bb7acecfSBarry Smith + dm    - The `DM`
5055c1929be8SMatthew G. Knepley . f     - The field number
505620f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5057bb7acecfSBarry Smith - disc  - The discretization object
5058c1929be8SMatthew G. Knepley 
505944a7f3ddSMatthew G. Knepley   Level: intermediate
5060c1929be8SMatthew G. Knepley 
50611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5062c1929be8SMatthew G. Knepley @*/
5063d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5064d71ae5a4SJacob Faibussowitsch {
5065decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5066decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5067e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5068bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50697a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5070bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5071bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50729566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
507444a7f3ddSMatthew G. Knepley }
507544a7f3ddSMatthew G. Knepley 
5076ffeef943SBarry Smith /*@
5077bb7acecfSBarry 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)
5078bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
507944a7f3ddSMatthew G. Knepley 
508020f4b53cSBarry Smith   Logically Collective
508144a7f3ddSMatthew G. Knepley 
508244a7f3ddSMatthew G. Knepley   Input Parameters:
5083bb7acecfSBarry Smith + dm    - The `DM`
508420f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5085bb7acecfSBarry Smith - disc  - The discretization object
508644a7f3ddSMatthew G. Knepley 
508744a7f3ddSMatthew G. Knepley   Level: intermediate
508844a7f3ddSMatthew G. Knepley 
5089bb7acecfSBarry Smith   Notes:
5090bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5091bb7acecfSBarry Smith 
5092da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5093bb7acecfSBarry 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
5094bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5095bb7acecfSBarry Smith 
50961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
509744a7f3ddSMatthew G. Knepley @*/
5098d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5099d71ae5a4SJacob Faibussowitsch {
510044a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
510144a7f3ddSMatthew G. Knepley 
510244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
510344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5104064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5105bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
51069566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
510744a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5108bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
51099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5110bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5111bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
51129566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
51133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5114af122d2aSMatthew G Knepley }
51156636e97aSMatthew G Knepley 
5116e5e52638SMatthew G. Knepley /*@
5117e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5118e0b68406SMatthew Knepley 
511920f4b53cSBarry Smith   Logically Collective
5120e0b68406SMatthew Knepley 
5121e0b68406SMatthew Knepley   Input Parameters:
5122bb7acecfSBarry Smith + dm          - The `DM`
5123e0b68406SMatthew Knepley . f           - The field index
5124bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5125e0b68406SMatthew Knepley 
5126e0b68406SMatthew Knepley   Level: intermediate
5127e0b68406SMatthew Knepley 
51281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5129e0b68406SMatthew Knepley @*/
5130d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5131d71ae5a4SJacob Faibussowitsch {
5132e0b68406SMatthew Knepley   PetscFunctionBegin;
513363a3b9bcSJacob 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);
5134e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5136e0b68406SMatthew Knepley }
5137e0b68406SMatthew Knepley 
5138e0b68406SMatthew Knepley /*@
5139e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5140e0b68406SMatthew Knepley 
514120f4b53cSBarry Smith   Not Collective
5142e0b68406SMatthew Knepley 
5143e0b68406SMatthew Knepley   Input Parameters:
5144bb7acecfSBarry Smith + dm - The `DM`
5145e0b68406SMatthew Knepley - f  - The field index
5146e0b68406SMatthew Knepley 
5147e0b68406SMatthew Knepley   Output Parameter:
5148e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5149e0b68406SMatthew Knepley 
5150e0b68406SMatthew Knepley   Level: intermediate
5151e0b68406SMatthew Knepley 
515260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5153e0b68406SMatthew Knepley @*/
5154d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5155d71ae5a4SJacob Faibussowitsch {
5156e0b68406SMatthew Knepley   PetscFunctionBegin;
515763a3b9bcSJacob 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);
5158e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5160e0b68406SMatthew Knepley }
5161e0b68406SMatthew Knepley 
5162e0b68406SMatthew Knepley /*@
5163bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5164e5e52638SMatthew G. Knepley 
516520f4b53cSBarry Smith   Collective
5166e5e52638SMatthew G. Knepley 
5167bb4b53efSMatthew G. Knepley   Input Parameters:
5168bb4b53efSMatthew G. Knepley + dm        - The `DM`
5169bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
5170bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
5171e5e52638SMatthew G. Knepley 
5172e5e52638SMatthew G. Knepley   Output Parameter:
5173bb7acecfSBarry Smith . newdm - The `DM`
5174e5e52638SMatthew G. Knepley 
5175e5e52638SMatthew G. Knepley   Level: advanced
5176e5e52638SMatthew G. Knepley 
51771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5178e5e52638SMatthew G. Knepley @*/
5179bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
5180d71ae5a4SJacob Faibussowitsch {
5181e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5182e5e52638SMatthew G. Knepley 
5183e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51843ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51859566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51869566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5187e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5188e5e52638SMatthew G. Knepley     DMLabel      label;
5189e5e52638SMatthew G. Knepley     PetscObject  field;
5190bb4b53efSMatthew G. Knepley     PetscClassId id;
519134aa8a36SMatthew G. Knepley     PetscBool    useCone, useClosure;
5192e5e52638SMatthew G. Knepley 
51939566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
5194bb4b53efSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(field, &id));
5195bb4b53efSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
5196bb4b53efSMatthew G. Knepley       PetscFE newfe;
5197bb4b53efSMatthew G. Knepley 
5198bb4b53efSMatthew G. Knepley       PetscCall(PetscFELimitDegree((PetscFE)field, minDegree, maxDegree, &newfe));
5199bb4b53efSMatthew G. Knepley       PetscCall(DMSetField(newdm, f, label, (PetscObject)newfe));
5200bb4b53efSMatthew G. Knepley       PetscCall(PetscFEDestroy(&newfe));
5201bb4b53efSMatthew G. Knepley     } else {
52029566063dSJacob Faibussowitsch       PetscCall(DMSetField(newdm, f, label, field));
5203bb4b53efSMatthew G. Knepley     }
52049566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
52059566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
520634aa8a36SMatthew G. Knepley   }
52073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
520834aa8a36SMatthew G. Knepley }
520934aa8a36SMatthew G. Knepley 
521034aa8a36SMatthew G. Knepley /*@
521134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
521234aa8a36SMatthew G. Knepley 
521320f4b53cSBarry Smith   Not Collective
521434aa8a36SMatthew G. Knepley 
521534aa8a36SMatthew G. Knepley   Input Parameters:
521620f4b53cSBarry Smith + dm - The `DM` object
521720f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
521834aa8a36SMatthew G. Knepley 
5219d8d19677SJose E. Roman   Output Parameters:
522034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
522134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
522234aa8a36SMatthew G. Knepley 
522334aa8a36SMatthew G. Knepley   Level: developer
522434aa8a36SMatthew G. Knepley 
522520f4b53cSBarry Smith   Notes:
522620f4b53cSBarry Smith .vb
522720f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
522820f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
522920f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
523020f4b53cSBarry Smith .ve
523120f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
523220f4b53cSBarry Smith 
52331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
523434aa8a36SMatthew G. Knepley @*/
5235d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5236d71ae5a4SJacob Faibussowitsch {
523734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
523834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52394f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52404f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
524134aa8a36SMatthew G. Knepley   if (f < 0) {
524234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
524334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
524434aa8a36SMatthew G. Knepley   } else {
524534aa8a36SMatthew G. Knepley     PetscInt Nf;
524634aa8a36SMatthew G. Knepley 
52479566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52487a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
524934aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
525034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
525134aa8a36SMatthew G. Knepley   }
52523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
525334aa8a36SMatthew G. Knepley }
525434aa8a36SMatthew G. Knepley 
525534aa8a36SMatthew G. Knepley /*@
525634aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
525734aa8a36SMatthew G. Knepley 
525820f4b53cSBarry Smith   Not Collective
525934aa8a36SMatthew G. Knepley 
526034aa8a36SMatthew G. Knepley   Input Parameters:
526120f4b53cSBarry Smith + dm         - The `DM` object
526234aa8a36SMatthew G. Knepley . f          - The field number
526334aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
526434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
526534aa8a36SMatthew G. Knepley 
526634aa8a36SMatthew G. Knepley   Level: developer
526734aa8a36SMatthew G. Knepley 
526820f4b53cSBarry Smith   Notes:
526920f4b53cSBarry Smith .vb
527020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
527120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
527220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
527320f4b53cSBarry Smith .ve
527420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
527520f4b53cSBarry Smith 
52761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
527734aa8a36SMatthew G. Knepley @*/
5278d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5279d71ae5a4SJacob Faibussowitsch {
528034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
528134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
528234aa8a36SMatthew G. Knepley   if (f < 0) {
528334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
528434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
528534aa8a36SMatthew G. Knepley   } else {
528634aa8a36SMatthew G. Knepley     PetscInt Nf;
528734aa8a36SMatthew G. Knepley 
52889566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52897a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
529034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
529134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5292e5e52638SMatthew G. Knepley   }
52933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5294e5e52638SMatthew G. Knepley }
5295e5e52638SMatthew G. Knepley 
5296b0441da4SMatthew G. Knepley /*@
5297b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5298b0441da4SMatthew G. Knepley 
5299b0441da4SMatthew G. Knepley   Not collective
5300b0441da4SMatthew G. Knepley 
5301f899ff85SJose E. Roman   Input Parameter:
530220f4b53cSBarry Smith . dm - The `DM` object
5303b0441da4SMatthew G. Knepley 
5304d8d19677SJose E. Roman   Output Parameters:
5305b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5306b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5307b0441da4SMatthew G. Knepley 
5308b0441da4SMatthew G. Knepley   Level: developer
5309b0441da4SMatthew G. Knepley 
531020f4b53cSBarry Smith   Notes:
531120f4b53cSBarry Smith .vb
531220f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
531320f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
531420f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
531520f4b53cSBarry Smith .ve
531620f4b53cSBarry Smith 
53171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5318b0441da4SMatthew G. Knepley @*/
5319d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5320d71ae5a4SJacob Faibussowitsch {
5321b0441da4SMatthew G. Knepley   PetscInt Nf;
5322b0441da4SMatthew G. Knepley 
5323b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5324b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53254f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
53264f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
53279566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5328b0441da4SMatthew G. Knepley   if (!Nf) {
53299566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5330b0441da4SMatthew G. Knepley   } else {
53319566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5332b0441da4SMatthew G. Knepley   }
53333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5334b0441da4SMatthew G. Knepley }
5335b0441da4SMatthew G. Knepley 
5336b0441da4SMatthew G. Knepley /*@
5337b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5338b0441da4SMatthew G. Knepley 
533920f4b53cSBarry Smith   Not Collective
5340b0441da4SMatthew G. Knepley 
5341b0441da4SMatthew G. Knepley   Input Parameters:
534220f4b53cSBarry Smith + dm         - The `DM` object
5343b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5344b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5345b0441da4SMatthew G. Knepley 
5346b0441da4SMatthew G. Knepley   Level: developer
5347b0441da4SMatthew G. Knepley 
534820f4b53cSBarry Smith   Notes:
534920f4b53cSBarry Smith .vb
535020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
535120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
535220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
535320f4b53cSBarry Smith .ve
535420f4b53cSBarry Smith 
53551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5356b0441da4SMatthew G. Knepley @*/
5357d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5358d71ae5a4SJacob Faibussowitsch {
5359b0441da4SMatthew G. Knepley   PetscInt Nf;
5360b0441da4SMatthew G. Knepley 
5361b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5362b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53639566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5364b0441da4SMatthew G. Knepley   if (!Nf) {
53659566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5366b0441da4SMatthew G. Knepley   } else {
53679566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5368e5e52638SMatthew G. Knepley   }
53693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5370e5e52638SMatthew G. Knepley }
5371e5e52638SMatthew G. Knepley 
5372d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5373d71ae5a4SJacob Faibussowitsch {
5374799db056SMatthew G. Knepley   DM           plex;
5375799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5376799db056SMatthew G. Knepley   const char **names;
5377799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5378799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5379799db056SMatthew G. Knepley   size_t       len;
5380799db056SMatthew G. Knepley   MPI_Comm     comm;
5381799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5382783e2ec8SMatthew G. Knepley 
5383783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5384799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5385799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5386799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5387799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5388799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5389799db056SMatthew G. Knepley     PetscDS  dsBC;
5390799db056SMatthew G. Knepley     PetscInt numBd;
5391799db056SMatthew G. Knepley 
539207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5393799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5394799db056SMatthew G. Knepley     maxLabels += numBd;
5395799db056SMatthew G. Knepley   }
5396799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5397799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5398799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5399799db056SMatthew G. Knepley     PetscDS  dsBC;
5400799db056SMatthew G. Knepley     PetscInt numBd, bd;
5401799db056SMatthew G. Knepley 
540207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5403799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5404799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5405799db056SMatthew G. Knepley       DMLabel      label;
5406799db056SMatthew G. Knepley       PetscInt     field;
5407799db056SMatthew G. Knepley       PetscObject  obj;
5408799db056SMatthew G. Knepley       PetscClassId id;
5409799db056SMatthew G. Knepley 
5410799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
54119566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
54129566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5413799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
54149371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
54159371c9d4SSatish Balay         if (labels[l] == label) break;
5416799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5417783e2ec8SMatthew G. Knepley     }
5418799db056SMatthew G. Knepley   }
5419799db056SMatthew G. Knepley   /* Get label names */
5420799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5421799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
54229371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
54239371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
54249371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
54259371c9d4SSatish Balay   }
5426799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5427462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5428799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5429c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5430799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5431799db056SMatthew G. Knepley   /* Put all names on all processes */
5432799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5433799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5434799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5435799db056SMatthew G. Knepley   gNl = displs[size];
54369371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54379371c9d4SSatish Balay     counts[p] *= gmaxLen;
54389371c9d4SSatish Balay     displs[p] *= gmaxLen;
54399371c9d4SSatish Balay   }
5440799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5441799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5442799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5443799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5444799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5445799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5446799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54479371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54485d6b2bfcSJames Wright       if (glabels[m] == glabels[gl]) goto next_label;
54499566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5450799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54519566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5452799db056SMatthew G. Knepley     ++gl;
54535d6b2bfcSJames Wright   next_label:
54545d6b2bfcSJames Wright     continue;
5455783e2ec8SMatthew G. Knepley   }
5456799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5458783e2ec8SMatthew G. Knepley }
5459783e2ec8SMatthew G. Knepley 
5460d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5461d71ae5a4SJacob Faibussowitsch {
5462e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5463e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5464e5e52638SMatthew G. Knepley 
5465e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54663ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5468e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54699371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54709371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54719371c9d4SSatish Balay     tmpd[s].label  = NULL;
54729371c9d4SSatish Balay     tmpd[s].fields = NULL;
54739371c9d4SSatish Balay   }
54749566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5475e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5476e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5478e5e52638SMatthew G. Knepley }
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley /*@
548120f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5482e5e52638SMatthew G. Knepley 
548320f4b53cSBarry Smith   Not Collective
5484e5e52638SMatthew G. Knepley 
5485e5e52638SMatthew G. Knepley   Input Parameter:
548620f4b53cSBarry Smith . dm - The `DM`
5487e5e52638SMatthew G. Knepley 
5488e5e52638SMatthew G. Knepley   Output Parameter:
548920f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5490e5e52638SMatthew G. Knepley 
5491e5e52638SMatthew G. Knepley   Level: intermediate
5492e5e52638SMatthew G. Knepley 
54931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5494e5e52638SMatthew G. Knepley @*/
5495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5496d71ae5a4SJacob Faibussowitsch {
5497e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5498e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54994f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5500e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
55013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5502e5e52638SMatthew G. Knepley }
5503e5e52638SMatthew G. Knepley 
5504e5e52638SMatthew G. Knepley /*@
550520f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5506e5e52638SMatthew G. Knepley 
550720f4b53cSBarry Smith   Logically Collective
5508e5e52638SMatthew G. Knepley 
5509e5e52638SMatthew G. Knepley   Input Parameter:
551020f4b53cSBarry Smith . dm - The `DM`
5511e5e52638SMatthew G. Knepley 
5512e5e52638SMatthew G. Knepley   Level: intermediate
5513e5e52638SMatthew G. Knepley 
55141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5515e5e52638SMatthew G. Knepley @*/
5516d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5517d71ae5a4SJacob Faibussowitsch {
5518e5e52638SMatthew G. Knepley   PetscInt s;
5519e5e52638SMatthew G. Knepley 
5520e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5521e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5522e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
55239566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
552407218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
55259566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
55269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5527e5e52638SMatthew G. Knepley   }
55289566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5529e5e52638SMatthew G. Knepley   dm->probs = NULL;
5530e5e52638SMatthew G. Knepley   dm->Nds   = 0;
55313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5532e5e52638SMatthew G. Knepley }
5533e5e52638SMatthew G. Knepley 
5534e5e52638SMatthew G. Knepley /*@
553520f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5536e5e52638SMatthew G. Knepley 
553720f4b53cSBarry Smith   Not Collective
5538e5e52638SMatthew G. Knepley 
5539e5e52638SMatthew G. Knepley   Input Parameter:
554020f4b53cSBarry Smith . dm - The `DM`
5541e5e52638SMatthew G. Knepley 
5542e5e52638SMatthew G. Knepley   Output Parameter:
554307218a29SMatthew G. Knepley . ds - The default `PetscDS`
5544e5e52638SMatthew G. Knepley 
5545e5e52638SMatthew G. Knepley   Level: intermediate
5546e5e52638SMatthew G. Knepley 
55471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5548e5e52638SMatthew G. Knepley @*/
554907218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5550d71ae5a4SJacob Faibussowitsch {
5551e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5552e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55534f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
555407218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
555507218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5557e5e52638SMatthew G. Knepley }
5558e5e52638SMatthew G. Knepley 
5559e5e52638SMatthew G. Knepley /*@
556020f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5561e5e52638SMatthew G. Knepley 
556220f4b53cSBarry Smith   Not Collective
5563e5e52638SMatthew G. Knepley 
5564e5e52638SMatthew G. Knepley   Input Parameters:
556520f4b53cSBarry Smith + dm    - The `DM`
556620f4b53cSBarry Smith - point - Cell for the `PetscDS`
5567e5e52638SMatthew G. Knepley 
556807218a29SMatthew G. Knepley   Output Parameters:
556907218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
557007218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5571e5e52638SMatthew G. Knepley 
5572e5e52638SMatthew G. Knepley   Level: developer
5573e5e52638SMatthew G. Knepley 
55741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5575e5e52638SMatthew G. Knepley @*/
557607218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5577d71ae5a4SJacob Faibussowitsch {
557807218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5579e5e52638SMatthew G. Knepley   PetscInt s;
5580e5e52638SMatthew G. Knepley 
5581e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5582e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55834f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55844f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
558563a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
558607218a29SMatthew G. Knepley   if (ds) *ds = NULL;
558707218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5588e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5589e5e52638SMatthew G. Knepley     PetscInt val;
5590e5e52638SMatthew G. Knepley 
55919371c9d4SSatish Balay     if (!dm->probs[s].label) {
559207218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55939371c9d4SSatish Balay     } else {
55949566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55959371c9d4SSatish Balay       if (val >= 0) {
559607218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
559707218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55989371c9d4SSatish Balay         break;
55999371c9d4SSatish Balay       }
5600e5e52638SMatthew G. Knepley     }
5601e5e52638SMatthew G. Knepley   }
560207218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
56033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5604e5e52638SMatthew G. Knepley }
5605e5e52638SMatthew G. Knepley 
5606e5e52638SMatthew G. Knepley /*@
560720f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5608e5e52638SMatthew G. Knepley 
560920f4b53cSBarry Smith   Not Collective
5610e5e52638SMatthew G. Knepley 
5611e5e52638SMatthew G. Knepley   Input Parameters:
561220f4b53cSBarry Smith + dm    - The `DM`
561320f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5614e5e52638SMatthew G. Knepley 
5615b3cf3223SMatthew G. Knepley   Output Parameters:
561620f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
561707218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
561807218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5619e5e52638SMatthew G. Knepley 
5620e5e52638SMatthew G. Knepley   Level: advanced
5621e5e52638SMatthew G. Knepley 
562220f4b53cSBarry Smith   Note:
562320f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
562420f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
562507218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
562620f4b53cSBarry Smith 
56271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5628e5e52638SMatthew G. Knepley @*/
562907218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5630d71ae5a4SJacob Faibussowitsch {
5631e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5632e5e52638SMatthew G. Knepley 
5633e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5634e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5635e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56369371c9d4SSatish Balay   if (fields) {
56374f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56389371c9d4SSatish Balay     *fields = NULL;
56399371c9d4SSatish Balay   }
56409371c9d4SSatish Balay   if (ds) {
56414f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56429371c9d4SSatish Balay     *ds = NULL;
56439371c9d4SSatish Balay   }
564407218a29SMatthew G. Knepley   if (dsIn) {
56454f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
564607218a29SMatthew G. Knepley     *dsIn = NULL;
564707218a29SMatthew G. Knepley   }
5648e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5649154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5650b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5651b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
565207218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56533ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5654b3cf3223SMatthew G. Knepley     }
5655e5e52638SMatthew G. Knepley   }
56563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5657e5e52638SMatthew G. Knepley }
5658e5e52638SMatthew G. Knepley 
5659e5e52638SMatthew G. Knepley /*@
5660bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5661083401c6SMatthew G. Knepley 
566220f4b53cSBarry Smith   Collective
5663083401c6SMatthew G. Knepley 
5664083401c6SMatthew G. Knepley   Input Parameters:
5665bb7acecfSBarry Smith + dm     - The `DM`
566620f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
566707218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
566807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
566907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5670083401c6SMatthew G. Knepley 
567120f4b53cSBarry Smith   Level: advanced
567220f4b53cSBarry Smith 
5673bb7acecfSBarry Smith   Note:
5674bb7acecfSBarry 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,
5675083401c6SMatthew G. Knepley   the fields argument is ignored.
5676083401c6SMatthew G. Knepley 
56771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5678083401c6SMatthew G. Knepley @*/
567907218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5680d71ae5a4SJacob Faibussowitsch {
5681083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5682083401c6SMatthew G. Knepley 
5683083401c6SMatthew G. Knepley   PetscFunctionBegin;
5684083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5685083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
568607218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5687064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
568807218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5689083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5690083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56919566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
569207218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5693083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
569407218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56953ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5696083401c6SMatthew G. Knepley     }
5697083401c6SMatthew G. Knepley   }
56989566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
57019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
570207218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5703083401c6SMatthew G. Knepley   if (!label) {
5704083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5705083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5706083401c6SMatthew G. Knepley     Nds = 0;
5707083401c6SMatthew G. Knepley   }
5708083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5709083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5710083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
571107218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
57123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5713083401c6SMatthew G. Knepley }
5714083401c6SMatthew G. Knepley 
5715083401c6SMatthew G. Knepley /*@
571620f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5717e5e52638SMatthew G. Knepley 
571820f4b53cSBarry Smith   Not Collective
5719e5e52638SMatthew G. Knepley 
5720e5e52638SMatthew G. Knepley   Input Parameters:
572120f4b53cSBarry Smith + dm  - The `DM`
5722e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5723e5e52638SMatthew G. Knepley 
5724e5e52638SMatthew G. Knepley   Output Parameters:
572520f4b53cSBarry Smith + label  - The region label, or `NULL`
572620f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
572707218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
572807218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5729e5e52638SMatthew G. Knepley 
5730e5e52638SMatthew G. Knepley   Level: advanced
5731e5e52638SMatthew G. Knepley 
57321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5733e5e52638SMatthew G. Knepley @*/
573407218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5735d71ae5a4SJacob Faibussowitsch {
5736e5e52638SMatthew G. Knepley   PetscInt Nds;
5737e5e52638SMatthew G. Knepley 
5738e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5739e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57409566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
574163a3b9bcSJacob 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);
5742e5e52638SMatthew G. Knepley   if (label) {
57434f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5744e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5745e5e52638SMatthew G. Knepley   }
5746b3cf3223SMatthew G. Knepley   if (fields) {
57474f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5748b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5749b3cf3223SMatthew G. Knepley   }
5750e5e52638SMatthew G. Knepley   if (ds) {
57514f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5752e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5753e5e52638SMatthew G. Knepley   }
575407218a29SMatthew G. Knepley   if (dsIn) {
57554f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
575607218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
575707218a29SMatthew G. Knepley   }
57583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5759e5e52638SMatthew G. Knepley }
5760e5e52638SMatthew G. Knepley 
5761e5e52638SMatthew G. Knepley /*@
576220f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5763e5e52638SMatthew G. Knepley 
576420f4b53cSBarry Smith   Not Collective
5765e5e52638SMatthew G. Knepley 
5766e5e52638SMatthew G. Knepley   Input Parameters:
576720f4b53cSBarry Smith + dm     - The `DM`
5768083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
576920f4b53cSBarry Smith . label  - The region label, or `NULL`
577007218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
577107218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
577207218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5773e5e52638SMatthew G. Knepley 
5774e5e52638SMatthew G. Knepley   Level: advanced
5775e5e52638SMatthew G. Knepley 
57761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5777e5e52638SMatthew G. Knepley @*/
577807218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5779d71ae5a4SJacob Faibussowitsch {
5780083401c6SMatthew G. Knepley   PetscInt Nds;
5781e5e52638SMatthew G. Knepley 
5782e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5783e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5784ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57859566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
578663a3b9bcSJacob 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);
57879566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57889566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5789083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5790083401c6SMatthew G. Knepley   if (fields) {
5791083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57929566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57939566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5794083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5795e5e52638SMatthew G. Knepley   }
5796083401c6SMatthew G. Knepley   if (ds) {
5797083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57989566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57999566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5800083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5801083401c6SMatthew G. Knepley   }
580207218a29SMatthew G. Knepley   if (dsIn) {
580307218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
580407218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
580507218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
580607218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
580707218a29SMatthew G. Knepley   }
58083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5809e5e52638SMatthew G. Knepley }
5810e5e52638SMatthew G. Knepley 
5811e5e52638SMatthew G. Knepley /*@
581220f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
58131d3af9e0SMatthew G. Knepley 
581420f4b53cSBarry Smith   Not Collective
58151d3af9e0SMatthew G. Knepley 
58161d3af9e0SMatthew G. Knepley   Input Parameters:
581720f4b53cSBarry Smith + dm - The `DM`
581820f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
58191d3af9e0SMatthew G. Knepley 
58201d3af9e0SMatthew G. Knepley   Output Parameter:
58211d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
58221d3af9e0SMatthew G. Knepley 
58231d3af9e0SMatthew G. Knepley   Level: advanced
58241d3af9e0SMatthew G. Knepley 
58251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
58261d3af9e0SMatthew G. Knepley @*/
5827d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5828d71ae5a4SJacob Faibussowitsch {
58291d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
58301d3af9e0SMatthew G. Knepley 
58311d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
58321d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58331d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
58344f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58359566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58369371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58379371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58381d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58391d3af9e0SMatthew G. Knepley   else *num = n;
58403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58411d3af9e0SMatthew G. Knepley }
58421d3af9e0SMatthew G. Knepley 
5843cc4c1da9SBarry Smith /*@
5844bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58452df84da0SMatthew G. Knepley 
584620f4b53cSBarry Smith   Not Collective
58472df84da0SMatthew G. Knepley 
5848f1a722f8SMatthew G. Knepley   Input Parameters:
5849bb7acecfSBarry Smith + dm     - The `DM`
58502df84da0SMatthew G. Knepley . Nc     - The number of components for the field
585120f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5852bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58532df84da0SMatthew G. Knepley 
58542df84da0SMatthew G. Knepley   Output Parameter:
5855bb7acecfSBarry Smith . fem - The `PetscFE`
58562df84da0SMatthew G. Knepley 
585720f4b53cSBarry Smith   Level: intermediate
585820f4b53cSBarry Smith 
5859bb7acecfSBarry Smith   Note:
5860bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58612df84da0SMatthew G. Knepley 
58621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58632df84da0SMatthew G. Knepley @*/
5864d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5865d71ae5a4SJacob Faibussowitsch {
58662df84da0SMatthew G. Knepley   DMPolytopeType ct;
58672df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58682df84da0SMatthew G. Knepley 
58692df84da0SMatthew G. Knepley   PetscFunctionBegin;
58702df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58712df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58724f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58732df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58744f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58759566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58789566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58802df84da0SMatthew G. Knepley }
58812df84da0SMatthew G. Knepley 
58821d3af9e0SMatthew G. Knepley /*@
5883bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5884e5e52638SMatthew G. Knepley 
588520f4b53cSBarry Smith   Collective
5886e5e52638SMatthew G. Knepley 
5887e5e52638SMatthew G. Knepley   Input Parameter:
5888bb7acecfSBarry Smith . dm - The `DM`
5889e5e52638SMatthew G. Knepley 
589020f4b53cSBarry Smith   Options Database Key:
5891bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
589245480ffeSMatthew G. Knepley 
589320f4b53cSBarry Smith   Level: intermediate
589420f4b53cSBarry Smith 
58951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5896e5e52638SMatthew G. Knepley @*/
5897d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5898d71ae5a4SJacob Faibussowitsch {
5899e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5900083401c6SMatthew G. Knepley   PetscDS   dsDef;
5901083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5902f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5903f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5904e5e52638SMatthew G. Knepley 
5905e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5906e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59073ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
59089566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
59099566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5910083401c6SMatthew G. Knepley   /* Determine how many regions we have */
59119566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5912083401c6SMatthew G. Knepley   Nl   = 0;
5913083401c6SMatthew G. Knepley   Ndef = 0;
5914083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5915083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5916083401c6SMatthew G. Knepley     PetscInt l;
5917083401c6SMatthew G. Knepley 
5918f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5919f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5920f918ec44SMatthew G. Knepley     {
5921f918ec44SMatthew G. Knepley       PetscClassId id;
5922f918ec44SMatthew G. Knepley 
59239566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5924f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5925f918ec44SMatthew G. Knepley         Ceed ceed;
5926f918ec44SMatthew G. Knepley 
59279566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
59289566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5929f918ec44SMatthew G. Knepley       }
5930f918ec44SMatthew G. Knepley     }
5931f918ec44SMatthew G. Knepley #endif
59329371c9d4SSatish Balay     if (!label) {
59339371c9d4SSatish Balay       ++Ndef;
59349371c9d4SSatish Balay       continue;
59359371c9d4SSatish Balay     }
59369371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59379371c9d4SSatish Balay       if (label == labelSet[l]) break;
5938083401c6SMatthew G. Knepley     if (l < Nl) continue;
5939083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5940083401c6SMatthew G. Knepley   }
5941083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
594207218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5943083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5944b3cf3223SMatthew G. Knepley     IS        fields;
5945b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5946b3cf3223SMatthew G. Knepley 
59479371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59489371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59497a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59509566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59519371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59529371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59539566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59549566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59559566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59569566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
595788f0c812SMatthew G. Knepley 
59589566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
595907218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59609566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59619566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59622df9ee95SMatthew G. Knepley   }
596307218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59649566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5965083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5966083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59670122748bSMatthew G. Knepley     DM              plex;
5968083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5969083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5970083401c6SMatthew G. Knepley     PetscInt       *fields;
5971083401c6SMatthew G. Knepley     const PetscInt *cells;
5972083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59730122748bSMatthew G. Knepley 
59749566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59769566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59779566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59785fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5979083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5980083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5981083401c6SMatthew G. Knepley       IS      pointIS;
5982083401c6SMatthew G. Knepley 
59839566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59849566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59859566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59869566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5987083401c6SMatthew G. Knepley     }
59889566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5989083401c6SMatthew G. Knepley 
59909566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59919566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59929566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59939566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59949566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59959566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59969566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5997083401c6SMatthew G. Knepley 
59989566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59999371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
60009371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
60019566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
60029566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
60039566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
60049566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
6005083401c6SMatthew G. Knepley 
60069566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
600707218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
60089566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
60099566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
60109566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
60119566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
60129566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
6013083401c6SMatthew G. Knepley   }
6014083401c6SMatthew G. Knepley   /* Create label DSes
6015083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
6016083401c6SMatthew G. Knepley   */
6017083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
6018083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
6019083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
602007218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
6021083401c6SMatthew G. Knepley     IS        fields;
6022083401c6SMatthew G. Knepley     PetscInt *fld, nf;
6023083401c6SMatthew G. Knepley 
60249566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
60259371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60269371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
60279566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
60289371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60299371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
60309566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
60319566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
60329566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
60339566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
60349566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6035083401c6SMatthew G. Knepley     {
6036083401c6SMatthew G. Knepley       DMPolytopeType ct;
6037083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60385fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60390122748bSMatthew G. Knepley 
60409566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6041665f567fSMatthew G. Knepley       if (lStart >= 0) {
60429566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6043412e9a14SMatthew G. Knepley         switch (ct) {
6044412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6045412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6046412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6047d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6048d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6049d71ae5a4SJacob Faibussowitsch           break;
6050d71ae5a4SJacob Faibussowitsch         default:
6051d71ae5a4SJacob Faibussowitsch           break;
6052412e9a14SMatthew G. Knepley         }
6053665f567fSMatthew G. Knepley       }
6054462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
605507218a29SMatthew G. Knepley       if (isCohesive) {
605607218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
605707218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
605807218a29SMatthew G. Knepley       }
60595fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60605fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60615fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60629566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60639566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
606407218a29SMatthew G. Knepley             if (dsIn) {
606507218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
606607218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
606707218a29SMatthew G. Knepley             }
60685fedec97SMatthew G. Knepley           }
60695fedec97SMatthew G. Knepley           ++nf;
60705fedec97SMatthew G. Knepley         }
60715fedec97SMatthew G. Knepley       }
6072e5e52638SMatthew G. Knepley     }
607307218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
607407218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60759566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
607607218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6077e5e52638SMatthew G. Knepley   }
60789566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6079e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6080083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6081083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
608207218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6083083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6084083401c6SMatthew G. Knepley     const PetscInt *fld;
60855fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60865fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6087e5e52638SMatthew G. Knepley 
60889566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60899566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60909566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60919566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6092083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6093083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60945fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6095e5e52638SMatthew G. Knepley       PetscClassId id;
6096e5e52638SMatthew G. Knepley 
60975fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60989566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60995fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
610007218a29SMatthew G. Knepley       if (isCohesive) {
610107218a29SMatthew G. Knepley         if (!isCohesiveField) {
610207218a29SMatthew G. Knepley           PetscObject bdDisc;
610307218a29SMatthew G. Knepley 
610407218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
610507218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
610607218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
610707218a29SMatthew G. Knepley         } else {
61089566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
610907218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
611007218a29SMatthew G. Knepley         }
611107218a29SMatthew G. Knepley       } else {
611207218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
611307218a29SMatthew G. Knepley       }
6114083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
61159566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6116e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6117e5e52638SMatthew G. Knepley     }
61189566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6119e5e52638SMatthew G. Knepley   }
6120f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
61219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6122f9244615SMatthew G. Knepley   if (flg) {
61233b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
61243b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
612507218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
61263b4aee56SMatthew G. Knepley       PetscInt Nf, f;
61273b4aee56SMatthew G. Knepley 
61289566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
612907218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
613007218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
613107218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
613207218a29SMatthew G. Knepley       }
61333b4aee56SMatthew G. Knepley     }
6134f9244615SMatthew G. Knepley   }
6135e5e52638SMatthew G. Knepley   /* Setup DSes */
6136e5e52638SMatthew G. Knepley   if (doSetup) {
613707218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6138cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6139cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6140cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6141cf1363e4SMatthew G. Knepley       }
614207218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
614307218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
614407218a29SMatthew G. Knepley     }
6145e5e52638SMatthew G. Knepley   }
61463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6147e5e52638SMatthew G. Knepley }
6148e5e52638SMatthew G. Knepley 
6149e5e52638SMatthew G. Knepley /*@
6150d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6151d2b2dc1eSMatthew G. Knepley 
6152d2b2dc1eSMatthew G. Knepley   Input Parameters:
6153d2b2dc1eSMatthew G. Knepley + dm     - The DM
6154d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6155d2b2dc1eSMatthew G. Knepley 
6156d2b2dc1eSMatthew G. Knepley   Level: developer
6157d2b2dc1eSMatthew G. Knepley 
6158d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6159d2b2dc1eSMatthew G. Knepley @*/
6160d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6161d2b2dc1eSMatthew G. Knepley {
6162d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6163d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6164d2b2dc1eSMatthew G. Knepley 
6165d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6166d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6167d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6168d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6169d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6170d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6171d2b2dc1eSMatthew G. Knepley 
6172d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6173d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6174d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6175d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6176d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6177d2b2dc1eSMatthew G. Knepley 
6178d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6179d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6180d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6181d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6182d2b2dc1eSMatthew G. Knepley   }
6183d2b2dc1eSMatthew G. Knepley   if (tensor) {
6184d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6185d2b2dc1eSMatthew G. Knepley   } else {
6186d2b2dc1eSMatthew G. Knepley     PetscSection s;
6187d2b2dc1eSMatthew G. Knepley 
6188d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6189d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6190d2b2dc1eSMatthew G. Knepley   }
6191d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6192d2b2dc1eSMatthew G. Knepley }
6193d2b2dc1eSMatthew G. Knepley 
6194d2b2dc1eSMatthew G. Knepley /*@
6195bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61967f96f943SMatthew G. Knepley 
619720f4b53cSBarry Smith   Collective
6198f2cacb80SMatthew G. Knepley 
61997f96f943SMatthew G. Knepley   Input Parameters:
6200bb7acecfSBarry Smith + dm   - The `DM`
62017f96f943SMatthew G. Knepley - time - The time
62027f96f943SMatthew G. Knepley 
62037f96f943SMatthew G. Knepley   Output Parameters:
620420f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
620520f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
620620f4b53cSBarry Smith 
620720f4b53cSBarry Smith   Level: developer
62087f96f943SMatthew G. Knepley 
6209bb7acecfSBarry Smith   Note:
6210bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
62117f96f943SMatthew G. Knepley 
62121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
62137f96f943SMatthew G. Knepley @*/
6214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6215d71ae5a4SJacob Faibussowitsch {
62167f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
62177f96f943SMatthew G. Knepley   void   **ectxs;
6218f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
62197f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
62207f96f943SMatthew G. Knepley 
62217f96f943SMatthew G. Knepley   PetscFunctionBegin;
6222f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6223f60fa741SMatthew G. Knepley   if (u) {
6224f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6225f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6226f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6227f60fa741SMatthew G. Knepley   }
6228f60fa741SMatthew G. Knepley   if (u_t) {
6229f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6230f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6231f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6232f60fa741SMatthew G. Knepley   }
62339566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
62349566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62359566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62367f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62377f96f943SMatthew G. Knepley     PetscDS         ds;
62387f96f943SMatthew G. Knepley     DMLabel         label;
62397f96f943SMatthew G. Knepley     IS              fieldIS;
62407f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62417f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62427f96f943SMatthew G. Knepley 
624307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62449566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62459566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62469566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62479566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6248f2cacb80SMatthew G. Knepley     if (u) {
6249f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(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));
6251f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62527f96f943SMatthew G. Knepley     }
6253f2cacb80SMatthew G. Knepley     if (u_t) {
62549566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62559566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6256f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6257f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6258f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6259f2cacb80SMatthew G. Knepley     }
62609566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6261f2cacb80SMatthew G. Knepley   }
6262f2cacb80SMatthew G. Knepley   if (u) {
62639566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62649566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6265f2cacb80SMatthew G. Knepley   }
6266f2cacb80SMatthew G. Knepley   if (u_t) {
62679566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62689566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6269f2cacb80SMatthew G. Knepley   }
62709566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6271f60fa741SMatthew G. Knepley   if (u) {
6272f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6273f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6274f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6275f60fa741SMatthew G. Knepley   }
6276f60fa741SMatthew G. Knepley   if (u_t) {
6277f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6278f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6279f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6280f60fa741SMatthew G. Knepley   }
62813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62827f96f943SMatthew G. Knepley }
62837f96f943SMatthew G. Knepley 
6284bb4b53efSMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscInt minDegree, PetscInt maxDegree, PetscDS ds, PetscDS dsIn)
6285d71ae5a4SJacob Faibussowitsch {
628607218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
628745480ffeSMatthew G. Knepley 
628845480ffeSMatthew G. Knepley   PetscFunctionBegin;
62899566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
6290bb4b53efSMatthew G. Knepley   PetscCall(PetscDSCopy(ds, minDegree, maxDegree, dm, dsNew));
629107218a29SMatthew G. Knepley   if (dsIn) {
629207218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
6293bb4b53efSMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, minDegree, maxDegree, dm, dsInNew));
629445480ffeSMatthew G. Knepley   }
629507218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62969566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
629707218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
629945480ffeSMatthew G. Knepley }
630045480ffeSMatthew G. Knepley 
63017f96f943SMatthew G. Knepley /*@
6302bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6303e5e52638SMatthew G. Knepley 
630420f4b53cSBarry Smith   Collective
6305e5e52638SMatthew G. Knepley 
6306bb4b53efSMatthew G. Knepley   Input Parameters:
6307bb4b53efSMatthew G. Knepley + dm        - The `DM`
6308bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
6309bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
6310e5e52638SMatthew G. Knepley 
6311e5e52638SMatthew G. Knepley   Output Parameter:
6312bb7acecfSBarry Smith . newdm - The `DM`
6313e5e52638SMatthew G. Knepley 
6314e5e52638SMatthew G. Knepley   Level: advanced
6315e5e52638SMatthew G. Knepley 
63161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6317e5e52638SMatthew G. Knepley @*/
6318bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
6319d71ae5a4SJacob Faibussowitsch {
6320e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6321e5e52638SMatthew G. Knepley 
6322e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63233ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
63249566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
63259566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6326e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6327e5e52638SMatthew G. Knepley     DMLabel  label;
6328b3cf3223SMatthew G. Knepley     IS       fields;
632907218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6330783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6331e5e52638SMatthew G. Knepley 
633207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6333b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
6334bb4b53efSMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, minDegree, maxDegree, ds, dsIn));
6335d5b43468SJose E. Roman     /* Complete new labels in the new DS */
633607218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63379566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6338783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6339b8025e53SMatthew G. Knepley       PetscWeakForm wf;
634045480ffeSMatthew G. Knepley       DMLabel       label;
6341783e2ec8SMatthew G. Knepley       PetscInt      field;
6342783e2ec8SMatthew G. Knepley 
63439566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63449566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6345783e2ec8SMatthew G. Knepley     }
6346e5e52638SMatthew G. Knepley   }
6347799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6349e5e52638SMatthew G. Knepley }
6350e5e52638SMatthew G. Knepley 
6351e5e52638SMatthew G. Knepley /*@
6352bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6353e5e52638SMatthew G. Knepley 
635420f4b53cSBarry Smith   Collective
6355e5e52638SMatthew G. Knepley 
6356e5e52638SMatthew G. Knepley   Input Parameter:
6357bb7acecfSBarry Smith . dm - The `DM`
6358e5e52638SMatthew G. Knepley 
6359e5e52638SMatthew G. Knepley   Output Parameter:
6360bb7acecfSBarry Smith . newdm - The `DM`
6361e5e52638SMatthew G. Knepley 
6362e5e52638SMatthew G. Knepley   Level: advanced
6363e5e52638SMatthew G. Knepley 
636473ff1848SBarry Smith   Developer Note:
6365bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6366bb7acecfSBarry Smith 
63671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6368e5e52638SMatthew G. Knepley @*/
6369d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6370d71ae5a4SJacob Faibussowitsch {
6371e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6372bb4b53efSMatthew G. Knepley   PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
6373bb4b53efSMatthew G. Knepley   PetscCall(DMCopyDS(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
63743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6375e5e52638SMatthew G. Knepley }
6376e5e52638SMatthew G. Knepley 
6377c73cfb54SMatthew G. Knepley /*@
6378bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6379c73cfb54SMatthew G. Knepley 
638020f4b53cSBarry Smith   Not Collective
6381c73cfb54SMatthew G. Knepley 
6382c73cfb54SMatthew G. Knepley   Input Parameter:
6383bb7acecfSBarry Smith . dm - The `DM`
6384c73cfb54SMatthew G. Knepley 
6385c73cfb54SMatthew G. Knepley   Output Parameter:
6386c73cfb54SMatthew G. Knepley . dim - The topological dimension
6387c73cfb54SMatthew G. Knepley 
6388c73cfb54SMatthew G. Knepley   Level: beginner
6389c73cfb54SMatthew G. Knepley 
63901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6391c73cfb54SMatthew G. Knepley @*/
6392d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6393d71ae5a4SJacob Faibussowitsch {
6394c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6395c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63964f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6397c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6399c73cfb54SMatthew G. Knepley }
6400c73cfb54SMatthew G. Knepley 
6401c73cfb54SMatthew G. Knepley /*@
6402bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6403c73cfb54SMatthew G. Knepley 
640420f4b53cSBarry Smith   Collective
6405c73cfb54SMatthew G. Knepley 
6406c73cfb54SMatthew G. Knepley   Input Parameters:
6407bb7acecfSBarry Smith + dm  - The `DM`
6408c73cfb54SMatthew G. Knepley - dim - The topological dimension
6409c73cfb54SMatthew G. Knepley 
6410c73cfb54SMatthew G. Knepley   Level: beginner
6411c73cfb54SMatthew G. Knepley 
64121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6413c73cfb54SMatthew G. Knepley @*/
6414d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6415d71ae5a4SJacob Faibussowitsch {
6416e5e52638SMatthew G. Knepley   PetscDS  ds;
641745480ffeSMatthew G. Knepley   PetscInt Nds, n;
6418f17e8794SMatthew G. Knepley 
6419c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6420c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6421c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6422c73cfb54SMatthew G. Knepley   dm->dim = dim;
6423d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
64249566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
642545480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
642607218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
64279566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
642845480ffeSMatthew G. Knepley     }
6429d17bd122SMatthew G. Knepley   }
64303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6431c73cfb54SMatthew G. Knepley }
6432c73cfb54SMatthew G. Knepley 
6433793f3fe5SMatthew G. Knepley /*@
6434793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6435793f3fe5SMatthew G. Knepley 
643620f4b53cSBarry Smith   Collective
6437793f3fe5SMatthew G. Knepley 
6438793f3fe5SMatthew G. Knepley   Input Parameters:
6439bb7acecfSBarry Smith + dm  - the `DM`
6440793f3fe5SMatthew G. Knepley - dim - the dimension
6441793f3fe5SMatthew G. Knepley 
6442793f3fe5SMatthew G. Knepley   Output Parameters:
6443793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6444aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6445793f3fe5SMatthew G. Knepley 
644620f4b53cSBarry Smith   Level: intermediate
644720f4b53cSBarry Smith 
6448793f3fe5SMatthew G. Knepley   Note:
6449793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6450a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6451793f3fe5SMatthew G. Knepley   then the interval is empty.
6452793f3fe5SMatthew G. Knepley 
64531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6454793f3fe5SMatthew G. Knepley @*/
6455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6456d71ae5a4SJacob Faibussowitsch {
6457793f3fe5SMatthew G. Knepley   PetscInt d;
6458793f3fe5SMatthew G. Knepley 
6459793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6460793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64619566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64627a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6463dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6465793f3fe5SMatthew G. Knepley }
6466793f3fe5SMatthew G. Knepley 
64676636e97aSMatthew G Knepley /*@
6468bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6469f4d763aaSMatthew G. Knepley 
647020f4b53cSBarry Smith   Collective
64718f700142SStefano Zampini 
6472f4d763aaSMatthew G. Knepley   Input Parameter:
6473bb7acecfSBarry Smith . dm - The original `DM`
6474f4d763aaSMatthew G. Knepley 
6475f4d763aaSMatthew G. Knepley   Output Parameter:
6476bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6477f4d763aaSMatthew G. Knepley 
6478f4d763aaSMatthew G. Knepley   Level: intermediate
6479f4d763aaSMatthew G. Knepley 
6480bb7acecfSBarry Smith   Note:
6481bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6482bb7acecfSBarry 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
6483bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6484bb7acecfSBarry Smith 
64851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6486f4d763aaSMatthew G. Knepley @*/
6487d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6488d71ae5a4SJacob Faibussowitsch {
6489c26acbdeSMatthew G. Knepley   PetscSection section;
6490eb9d3e4dSMatthew G. Knepley   IS           perm;
6491eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
649214f150ffSMatthew G. Knepley 
649314f150ffSMatthew G. Knepley   PetscFunctionBegin;
649414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64954f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64969566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64979566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6498eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6499eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6500462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6501eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6502c26acbdeSMatthew G. Knepley     *odm = dm;
65033ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6504c26acbdeSMatthew G. Knepley   }
650514f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6506c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
650714f150ffSMatthew G. Knepley     PetscSF      sf;
6508eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
650914f150ffSMatthew G. Knepley 
65109566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
65119566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
65129566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
65139566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
65149566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
65159566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6516eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
65179566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
65189566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
651914f150ffSMatthew G. Knepley   }
652014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
65213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
652214f150ffSMatthew G. Knepley }
6523f4d763aaSMatthew G. Knepley 
6524f4d763aaSMatthew G. Knepley /*@
6525cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6526f4d763aaSMatthew G. Knepley 
6527f4d763aaSMatthew G. Knepley   Input Parameter:
6528bb7acecfSBarry Smith . dm - The original `DM`
6529f4d763aaSMatthew G. Knepley 
6530cdb7a50dSMatthew G. Knepley   Output Parameters:
6531cdb7a50dSMatthew G. Knepley + num - The output sequence number
6532cdb7a50dSMatthew G. Knepley - val - The output sequence value
6533f4d763aaSMatthew G. Knepley 
6534f4d763aaSMatthew G. Knepley   Level: intermediate
6535f4d763aaSMatthew G. Knepley 
6536bb7acecfSBarry Smith   Note:
6537bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6538bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6539bb7acecfSBarry Smith 
654073ff1848SBarry Smith   Developer Note:
6541bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6542bb7acecfSBarry Smith   not directly related to the `DM`.
6543f4d763aaSMatthew G. Knepley 
65441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6545f4d763aaSMatthew G. Knepley @*/
6546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6547d71ae5a4SJacob Faibussowitsch {
6548f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6549f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65509371c9d4SSatish Balay   if (num) {
65514f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65529371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65539371c9d4SSatish Balay   }
65549371c9d4SSatish Balay   if (val) {
65554f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65569371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65579371c9d4SSatish Balay   }
65583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6559f4d763aaSMatthew G. Knepley }
6560f4d763aaSMatthew G. Knepley 
6561f4d763aaSMatthew G. Knepley /*@
6562cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6563f4d763aaSMatthew G. Knepley 
6564f4d763aaSMatthew G. Knepley   Input Parameters:
6565bb7acecfSBarry Smith + dm  - The original `DM`
6566cdb7a50dSMatthew G. Knepley . num - The output sequence number
6567cdb7a50dSMatthew G. Knepley - val - The output sequence value
6568f4d763aaSMatthew G. Knepley 
6569f4d763aaSMatthew G. Knepley   Level: intermediate
6570f4d763aaSMatthew G. Knepley 
6571bb7acecfSBarry Smith   Note:
6572bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6573bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6574f4d763aaSMatthew G. Knepley 
65751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6576f4d763aaSMatthew G. Knepley @*/
6577d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6578d71ae5a4SJacob Faibussowitsch {
6579f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6580f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6581f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6582cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6584cdb7a50dSMatthew G. Knepley }
6585cdb7a50dSMatthew G. Knepley 
65865d83a8b1SBarry Smith /*@
6587bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6588cdb7a50dSMatthew G. Knepley 
6589cdb7a50dSMatthew G. Knepley   Input Parameters:
6590bb7acecfSBarry Smith + dm     - The original `DM`
6591b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6592cdb7a50dSMatthew G. Knepley . name   - The sequence name
6593cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6594cdb7a50dSMatthew G. Knepley 
6595cdb7a50dSMatthew G. Knepley   Output Parameter:
6596cdb7a50dSMatthew G. Knepley . val - The output sequence value
6597cdb7a50dSMatthew G. Knepley 
6598cdb7a50dSMatthew G. Knepley   Level: intermediate
6599cdb7a50dSMatthew G. Knepley 
6600bb7acecfSBarry Smith   Note:
6601bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6602bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6603bb7acecfSBarry Smith 
660473ff1848SBarry Smith   Developer Note:
6605bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6606cdb7a50dSMatthew G. Knepley 
66071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6608cdb7a50dSMatthew G. Knepley @*/
6609b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6610d71ae5a4SJacob Faibussowitsch {
6611cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6612cdb7a50dSMatthew G. Knepley 
6613cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6614cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6615cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6616b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
66174f572ea9SToby Isaac   PetscAssertPointer(val, 5);
66189566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6619cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6620cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6621cdb7a50dSMatthew G. Knepley     PetscScalar value;
6622cdb7a50dSMatthew G. Knepley 
66239566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
66244aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6625cdb7a50dSMatthew G. Knepley #endif
6626cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
66273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6628f4d763aaSMatthew G. Knepley }
66298e4ac7eaSMatthew G. Knepley 
66308e4ac7eaSMatthew G. Knepley /*@
6631b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6632b2033f5dSMatthew G. Knepley 
6633b2033f5dSMatthew G. Knepley   Input Parameters:
6634b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6635b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6636b2033f5dSMatthew G. Knepley - name   - The sequence name
6637b2033f5dSMatthew G. Knepley 
6638b2033f5dSMatthew G. Knepley   Output Parameter:
6639b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6640b2033f5dSMatthew G. Knepley 
6641b2033f5dSMatthew G. Knepley   Level: intermediate
6642b2033f5dSMatthew G. Knepley 
6643b2033f5dSMatthew G. Knepley   Note:
6644b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6645b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6646b2033f5dSMatthew G. Knepley 
6647b2033f5dSMatthew G. Knepley   Developer Note:
6648b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6649b2033f5dSMatthew G. Knepley 
6650b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6651b2033f5dSMatthew G. Knepley @*/
6652b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6653b2033f5dSMatthew G. Knepley {
6654b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6655b2033f5dSMatthew G. Knepley 
6656b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6657b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6658b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6659b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6660b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6661b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6662b2033f5dSMatthew G. Knepley   if (ishdf5) {
6663b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6664b2033f5dSMatthew G. Knepley     PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6665b2033f5dSMatthew G. Knepley #endif
6666b2033f5dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6667b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6668b2033f5dSMatthew G. Knepley }
6669b2033f5dSMatthew G. Knepley 
6670b2033f5dSMatthew G. Knepley /*@
6671bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66728e4ac7eaSMatthew G. Knepley 
667320f4b53cSBarry Smith   Not Collective
66748e4ac7eaSMatthew G. Knepley 
66758e4ac7eaSMatthew G. Knepley   Input Parameter:
6676bb7acecfSBarry Smith . dm - The `DM`
66778e4ac7eaSMatthew G. Knepley 
66788e4ac7eaSMatthew G. Knepley   Output Parameter:
6679bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66808e4ac7eaSMatthew G. Knepley 
66818e4ac7eaSMatthew G. Knepley   Level: beginner
66828e4ac7eaSMatthew G. Knepley 
66831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66848e4ac7eaSMatthew G. Knepley @*/
6685d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6686d71ae5a4SJacob Faibussowitsch {
66878e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66888e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66894f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66908e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66928e4ac7eaSMatthew G. Knepley }
66938e4ac7eaSMatthew G. Knepley 
66948e4ac7eaSMatthew G. Knepley /*@
6695bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66968e4ac7eaSMatthew G. Knepley 
669720f4b53cSBarry Smith   Collective
66988e4ac7eaSMatthew G. Knepley 
66998e4ac7eaSMatthew G. Knepley   Input Parameters:
6700bb7acecfSBarry Smith + dm         - The `DM`
6701bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
67028e4ac7eaSMatthew G. Knepley 
670373ff1848SBarry Smith   Level: beginner
670473ff1848SBarry Smith 
6705bb7acecfSBarry Smith   Note:
6706bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
67075d3b26e6SMatthew G. Knepley 
67081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
67098e4ac7eaSMatthew G. Knepley @*/
6710d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6711d71ae5a4SJacob Faibussowitsch {
67128e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
67138e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67148833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
67158e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
67163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67178e4ac7eaSMatthew G. Knepley }
6718c58f1c22SToby Isaac 
6719cc4c1da9SBarry Smith /*@
6720bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6721c58f1c22SToby Isaac 
6722c58f1c22SToby Isaac   Not Collective
6723c58f1c22SToby Isaac 
6724c58f1c22SToby Isaac   Input Parameters:
6725bb7acecfSBarry Smith + dm   - The `DM` object
6726c58f1c22SToby Isaac - name - The label name
6727c58f1c22SToby Isaac 
6728c58f1c22SToby Isaac   Level: intermediate
6729c58f1c22SToby Isaac 
67301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6731c58f1c22SToby Isaac @*/
6732d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6733d71ae5a4SJacob Faibussowitsch {
67345d80c0bfSVaclav Hapla   PetscBool flg;
67355d80c0bfSVaclav Hapla   DMLabel   label;
6736c58f1c22SToby Isaac 
6737c58f1c22SToby Isaac   PetscFunctionBegin;
6738c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67394f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67409566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6741c58f1c22SToby Isaac   if (!flg) {
67429566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67439566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67449566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6745c58f1c22SToby Isaac   }
67463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6747c58f1c22SToby Isaac }
6748c58f1c22SToby Isaac 
6749cc4c1da9SBarry Smith /*@
6750bb7acecfSBarry 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.
67510fdc7489SMatthew Knepley 
67520fdc7489SMatthew Knepley   Not Collective
67530fdc7489SMatthew Knepley 
67540fdc7489SMatthew Knepley   Input Parameters:
6755bb7acecfSBarry Smith + dm   - The `DM` object
67560fdc7489SMatthew Knepley . l    - The index for the label
67570fdc7489SMatthew Knepley - name - The label name
67580fdc7489SMatthew Knepley 
67590fdc7489SMatthew Knepley   Level: intermediate
67600fdc7489SMatthew Knepley 
67611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
67620fdc7489SMatthew Knepley @*/
6763d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6764d71ae5a4SJacob Faibussowitsch {
67650fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
67660fdc7489SMatthew Knepley   DMLabel     label;
67670fdc7489SMatthew Knepley   PetscInt    Nl, m;
67680fdc7489SMatthew Knepley   PetscBool   flg, match;
67690fdc7489SMatthew Knepley   const char *lname;
67700fdc7489SMatthew Knepley 
67710fdc7489SMatthew Knepley   PetscFunctionBegin;
67720fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67734f572ea9SToby Isaac   PetscAssertPointer(name, 3);
67749566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67750fdc7489SMatthew Knepley   if (!flg) {
67769566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67779566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67789566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67790fdc7489SMatthew Knepley   }
67809566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
678163a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67820fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67839566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67849566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67850fdc7489SMatthew Knepley     if (match) break;
67860fdc7489SMatthew Knepley   }
67873ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67880fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67890fdc7489SMatthew Knepley   else prev->next = orig->next;
67900fdc7489SMatthew Knepley   if (!l) {
67910fdc7489SMatthew Knepley     orig->next = dm->labels;
67920fdc7489SMatthew Knepley     dm->labels = orig;
67930fdc7489SMatthew Knepley   } else {
6794fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67950fdc7489SMatthew Knepley     orig->next = prev->next;
67960fdc7489SMatthew Knepley     prev->next = orig;
67970fdc7489SMatthew Knepley   }
67983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67990fdc7489SMatthew Knepley }
68000fdc7489SMatthew Knepley 
6801cc4c1da9SBarry Smith /*@
6802bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6803c58f1c22SToby Isaac 
6804c58f1c22SToby Isaac   Not Collective
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   Input Parameters:
6807bb7acecfSBarry Smith + dm    - The `DM` object
6808c58f1c22SToby Isaac . name  - The label name
6809c58f1c22SToby Isaac - point - The mesh point
6810c58f1c22SToby Isaac 
6811c58f1c22SToby Isaac   Output Parameter:
6812c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6813c58f1c22SToby Isaac 
6814c58f1c22SToby Isaac   Level: beginner
6815c58f1c22SToby Isaac 
68161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6817c58f1c22SToby Isaac @*/
6818d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6819d71ae5a4SJacob Faibussowitsch {
6820c58f1c22SToby Isaac   DMLabel label;
6821c58f1c22SToby Isaac 
6822c58f1c22SToby Isaac   PetscFunctionBegin;
6823c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68244f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68259566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68267a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
68279566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
68283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6829c58f1c22SToby Isaac }
6830c58f1c22SToby Isaac 
6831cc4c1da9SBarry Smith /*@
6832bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6833c58f1c22SToby Isaac 
6834c58f1c22SToby Isaac   Not Collective
6835c58f1c22SToby Isaac 
6836c58f1c22SToby Isaac   Input Parameters:
6837bb7acecfSBarry Smith + dm    - The `DM` object
6838c58f1c22SToby Isaac . name  - The label name
6839c58f1c22SToby Isaac . point - The mesh point
6840c58f1c22SToby Isaac - value - The label value for this point
6841c58f1c22SToby Isaac 
6842c58f1c22SToby Isaac   Output Parameter:
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Level: beginner
6845c58f1c22SToby Isaac 
68461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6847c58f1c22SToby Isaac @*/
6848d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6849d71ae5a4SJacob Faibussowitsch {
6850c58f1c22SToby Isaac   DMLabel label;
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   PetscFunctionBegin;
6853c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68544f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68559566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6856c58f1c22SToby Isaac   if (!label) {
68579566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68589566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6859c58f1c22SToby Isaac   }
68609566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
68613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6862c58f1c22SToby Isaac }
6863c58f1c22SToby Isaac 
6864cc4c1da9SBarry Smith /*@
6865bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac   Not Collective
6868c58f1c22SToby Isaac 
6869c58f1c22SToby Isaac   Input Parameters:
6870bb7acecfSBarry Smith + dm    - The `DM` object
6871c58f1c22SToby Isaac . name  - The label name
6872c58f1c22SToby Isaac . point - The mesh point
6873c58f1c22SToby Isaac - value - The label value for this point
6874c58f1c22SToby Isaac 
6875c58f1c22SToby Isaac   Level: beginner
6876c58f1c22SToby Isaac 
68771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6878c58f1c22SToby Isaac @*/
6879d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6880d71ae5a4SJacob Faibussowitsch {
6881c58f1c22SToby Isaac   DMLabel label;
6882c58f1c22SToby Isaac 
6883c58f1c22SToby Isaac   PetscFunctionBegin;
6884c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68854f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68869566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68873ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68889566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6890c58f1c22SToby Isaac }
6891c58f1c22SToby Isaac 
6892cc4c1da9SBarry Smith /*@
6893bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6894c58f1c22SToby Isaac 
6895c58f1c22SToby Isaac   Not Collective
6896c58f1c22SToby Isaac 
6897c58f1c22SToby Isaac   Input Parameters:
6898bb7acecfSBarry Smith + dm   - The `DM` object
6899c58f1c22SToby Isaac - name - The label name
6900c58f1c22SToby Isaac 
6901c58f1c22SToby Isaac   Output Parameter:
6902c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6903c58f1c22SToby Isaac 
6904c58f1c22SToby Isaac   Level: beginner
6905c58f1c22SToby Isaac 
690673ff1848SBarry Smith   Developer Note:
6907bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6908bb7acecfSBarry Smith 
69091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6910c58f1c22SToby Isaac @*/
6911d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6912d71ae5a4SJacob Faibussowitsch {
6913c58f1c22SToby Isaac   DMLabel label;
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   PetscFunctionBegin;
6916c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69174f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69184f572ea9SToby Isaac   PetscAssertPointer(size, 3);
69199566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6920c58f1c22SToby Isaac   *size = 0;
69213ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69229566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
69233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6924c58f1c22SToby Isaac }
6925c58f1c22SToby Isaac 
6926cc4c1da9SBarry Smith /*@
6927bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac   Not Collective
6930c58f1c22SToby Isaac 
6931c58f1c22SToby Isaac   Input Parameters:
693260225df5SJacob Faibussowitsch + dm   - The `DM` object
6933c58f1c22SToby Isaac - name - The label name
6934c58f1c22SToby Isaac 
6935c58f1c22SToby Isaac   Output Parameter:
693620f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac   Level: beginner
6939c58f1c22SToby Isaac 
69401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6941c58f1c22SToby Isaac @*/
6942d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6943d71ae5a4SJacob Faibussowitsch {
6944c58f1c22SToby Isaac   DMLabel label;
6945c58f1c22SToby Isaac 
6946c58f1c22SToby Isaac   PetscFunctionBegin;
6947c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69484f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69494f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
69509566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6951c58f1c22SToby Isaac   *ids = NULL;
6952dab2e251SBlaise Bourdin   if (label) {
69539566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6954dab2e251SBlaise Bourdin   } else {
6955dab2e251SBlaise Bourdin     /* returning an empty IS */
69569566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6957dab2e251SBlaise Bourdin   }
69583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6959c58f1c22SToby Isaac }
6960c58f1c22SToby Isaac 
6961cc4c1da9SBarry Smith /*@
6962c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   Not Collective
6965c58f1c22SToby Isaac 
6966c58f1c22SToby Isaac   Input Parameters:
6967bb7acecfSBarry Smith + dm    - The `DM` object
6968b6971eaeSBarry Smith . name  - The label name of the stratum
6969c58f1c22SToby Isaac - value - The stratum value
6970c58f1c22SToby Isaac 
6971c58f1c22SToby Isaac   Output Parameter:
6972bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Level: beginner
6975c58f1c22SToby Isaac 
69761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6977c58f1c22SToby Isaac @*/
6978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6979d71ae5a4SJacob Faibussowitsch {
6980c58f1c22SToby Isaac   DMLabel label;
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   PetscFunctionBegin;
6983c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69844f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69854f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69869566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6987c58f1c22SToby Isaac   *size = 0;
69883ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69899566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6991c58f1c22SToby Isaac }
6992c58f1c22SToby Isaac 
6993cc4c1da9SBarry Smith /*@
6994c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6995c58f1c22SToby Isaac 
6996c58f1c22SToby Isaac   Not Collective
6997c58f1c22SToby Isaac 
6998c58f1c22SToby Isaac   Input Parameters:
6999bb7acecfSBarry Smith + dm    - The `DM` object
7000c58f1c22SToby Isaac . name  - The label name
7001c58f1c22SToby Isaac - value - The stratum value
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   Output Parameter:
700420f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Level: beginner
7007c58f1c22SToby Isaac 
70081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
7009c58f1c22SToby Isaac @*/
7010d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7011d71ae5a4SJacob Faibussowitsch {
7012c58f1c22SToby Isaac   DMLabel label;
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   PetscFunctionBegin;
7015c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70164f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70174f572ea9SToby Isaac   PetscAssertPointer(points, 4);
70189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7019c58f1c22SToby Isaac   *points = NULL;
70203ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70219566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
70223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7023c58f1c22SToby Isaac }
7024c58f1c22SToby Isaac 
7025cc4c1da9SBarry Smith /*@
70269044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
70274de306b1SToby Isaac 
70284de306b1SToby Isaac   Not Collective
70294de306b1SToby Isaac 
70304de306b1SToby Isaac   Input Parameters:
7031bb7acecfSBarry Smith + dm     - The `DM` object
70324de306b1SToby Isaac . name   - The label name
70334de306b1SToby Isaac . value  - The stratum value
70344de306b1SToby Isaac - points - The stratum points
70354de306b1SToby Isaac 
70364de306b1SToby Isaac   Level: beginner
70374de306b1SToby Isaac 
70381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
70394de306b1SToby Isaac @*/
7040d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
7041d71ae5a4SJacob Faibussowitsch {
70424de306b1SToby Isaac   DMLabel label;
70434de306b1SToby Isaac 
70444de306b1SToby Isaac   PetscFunctionBegin;
70454de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70464f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7047292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
70489566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70493ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70509566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
70513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70524de306b1SToby Isaac }
70534de306b1SToby Isaac 
7054cc4c1da9SBarry Smith /*@
7055bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Not Collective
7058c58f1c22SToby Isaac 
7059c58f1c22SToby Isaac   Input Parameters:
7060bb7acecfSBarry Smith + dm    - The `DM` object
7061c58f1c22SToby Isaac . name  - The label name
7062c58f1c22SToby Isaac - value - The label value for this point
7063c58f1c22SToby Isaac 
7064c58f1c22SToby Isaac   Output Parameter:
7065c58f1c22SToby Isaac 
7066c58f1c22SToby Isaac   Level: beginner
7067c58f1c22SToby Isaac 
70681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7069c58f1c22SToby Isaac @*/
7070d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7071d71ae5a4SJacob Faibussowitsch {
7072c58f1c22SToby Isaac   DMLabel label;
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac   PetscFunctionBegin;
7075c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70764f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70779566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70783ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70799566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7081c58f1c22SToby Isaac }
7082c58f1c22SToby Isaac 
7083c58f1c22SToby Isaac /*@
7084bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Not Collective
7087c58f1c22SToby Isaac 
7088c58f1c22SToby Isaac   Input Parameter:
7089bb7acecfSBarry Smith . dm - The `DM` object
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac   Output Parameter:
7092c58f1c22SToby Isaac . numLabels - the number of Labels
7093c58f1c22SToby Isaac 
7094c58f1c22SToby Isaac   Level: intermediate
7095c58f1c22SToby Isaac 
70961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7097c58f1c22SToby Isaac @*/
7098d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7099d71ae5a4SJacob Faibussowitsch {
71005d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7101c58f1c22SToby Isaac   PetscInt    n    = 0;
7102c58f1c22SToby Isaac 
7103c58f1c22SToby Isaac   PetscFunctionBegin;
7104c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71054f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
71069371c9d4SSatish Balay   while (next) {
71079371c9d4SSatish Balay     ++n;
71089371c9d4SSatish Balay     next = next->next;
71099371c9d4SSatish Balay   }
7110c58f1c22SToby Isaac   *numLabels = n;
71113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7112c58f1c22SToby Isaac }
7113c58f1c22SToby Isaac 
7114cc4c1da9SBarry Smith /*@
7115c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7116c58f1c22SToby Isaac 
7117c58f1c22SToby Isaac   Not Collective
7118c58f1c22SToby Isaac 
7119c58f1c22SToby Isaac   Input Parameters:
7120bb7acecfSBarry Smith + dm - The `DM` object
7121c58f1c22SToby Isaac - n  - the label number
7122c58f1c22SToby Isaac 
7123c58f1c22SToby Isaac   Output Parameter:
7124c58f1c22SToby Isaac . name - the label name
7125c58f1c22SToby Isaac 
7126c58f1c22SToby Isaac   Level: intermediate
7127c58f1c22SToby Isaac 
712873ff1848SBarry Smith   Developer Note:
7129bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7130bb7acecfSBarry Smith 
71311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7132c58f1c22SToby Isaac @*/
7133cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7134d71ae5a4SJacob Faibussowitsch {
71355d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7136c58f1c22SToby Isaac   PetscInt    l    = 0;
7137c58f1c22SToby Isaac 
7138c58f1c22SToby Isaac   PetscFunctionBegin;
7139c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71404f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7141c58f1c22SToby Isaac   while (next) {
7142c58f1c22SToby Isaac     if (l == n) {
71439566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
71443ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7145c58f1c22SToby Isaac     }
7146c58f1c22SToby Isaac     ++l;
7147c58f1c22SToby Isaac     next = next->next;
7148c58f1c22SToby Isaac   }
714963a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7150c58f1c22SToby Isaac }
7151c58f1c22SToby Isaac 
7152cc4c1da9SBarry Smith /*@
7153bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7154c58f1c22SToby Isaac 
7155c58f1c22SToby Isaac   Not Collective
7156c58f1c22SToby Isaac 
7157c58f1c22SToby Isaac   Input Parameters:
7158bb7acecfSBarry Smith + dm   - The `DM` object
7159c58f1c22SToby Isaac - name - The label name
7160c58f1c22SToby Isaac 
7161c58f1c22SToby Isaac   Output Parameter:
7162bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac   Level: intermediate
7165c58f1c22SToby Isaac 
71661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7167c58f1c22SToby Isaac @*/
7168d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7169d71ae5a4SJacob Faibussowitsch {
71705d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7171d67d17b1SMatthew G. Knepley   const char *lname;
7172c58f1c22SToby Isaac 
7173c58f1c22SToby Isaac   PetscFunctionBegin;
7174c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71754f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71764f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7177c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7178c58f1c22SToby Isaac   while (next) {
71799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7181c58f1c22SToby Isaac     if (*hasLabel) break;
7182c58f1c22SToby Isaac     next = next->next;
7183c58f1c22SToby Isaac   }
71843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7185c58f1c22SToby Isaac }
7186c58f1c22SToby Isaac 
7187a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7188cc4c1da9SBarry Smith /*@
718920f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7190c58f1c22SToby Isaac 
7191c58f1c22SToby Isaac   Not Collective
7192c58f1c22SToby Isaac 
7193c58f1c22SToby Isaac   Input Parameters:
7194bb7acecfSBarry Smith + dm   - The `DM` object
7195c58f1c22SToby Isaac - name - The label name
7196c58f1c22SToby Isaac 
7197c58f1c22SToby Isaac   Output Parameter:
719820f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7199c58f1c22SToby Isaac 
7200bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7201bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7202bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7203bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7204bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7205bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7206bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72076d7c9049SMatthew G. Knepley 
7208c58f1c22SToby Isaac   Level: intermediate
7209c58f1c22SToby Isaac 
721060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7211c58f1c22SToby Isaac @*/
7212d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7213d71ae5a4SJacob Faibussowitsch {
72145d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7215c58f1c22SToby Isaac   PetscBool   hasLabel;
7216d67d17b1SMatthew G. Knepley   const char *lname;
7217c58f1c22SToby Isaac 
7218c58f1c22SToby Isaac   PetscFunctionBegin;
7219c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72204f572ea9SToby Isaac   PetscAssertPointer(name, 2);
72214f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7222c58f1c22SToby Isaac   *label = NULL;
7223c58f1c22SToby Isaac   while (next) {
72249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72259566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7226c58f1c22SToby Isaac     if (hasLabel) {
7227c58f1c22SToby Isaac       *label = next->label;
7228c58f1c22SToby Isaac       break;
7229c58f1c22SToby Isaac     }
7230c58f1c22SToby Isaac     next = next->next;
7231c58f1c22SToby Isaac   }
72323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7233c58f1c22SToby Isaac }
7234c58f1c22SToby Isaac 
7235cc4c1da9SBarry Smith /*@
7236bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   Not Collective
7239c58f1c22SToby Isaac 
7240c58f1c22SToby Isaac   Input Parameters:
7241bb7acecfSBarry Smith + dm - The `DM` object
7242c58f1c22SToby Isaac - n  - the label number
7243c58f1c22SToby Isaac 
7244c58f1c22SToby Isaac   Output Parameter:
7245c58f1c22SToby Isaac . label - the label
7246c58f1c22SToby Isaac 
7247c58f1c22SToby Isaac   Level: intermediate
7248c58f1c22SToby Isaac 
72491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7250c58f1c22SToby Isaac @*/
7251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7252d71ae5a4SJacob Faibussowitsch {
72535d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7254c58f1c22SToby Isaac   PetscInt    l    = 0;
7255c58f1c22SToby Isaac 
7256c58f1c22SToby Isaac   PetscFunctionBegin;
7257c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72584f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7259c58f1c22SToby Isaac   while (next) {
7260c58f1c22SToby Isaac     if (l == n) {
7261c58f1c22SToby Isaac       *label = next->label;
72623ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7263c58f1c22SToby Isaac     }
7264c58f1c22SToby Isaac     ++l;
7265c58f1c22SToby Isaac     next = next->next;
7266c58f1c22SToby Isaac   }
726763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7268c58f1c22SToby Isaac }
7269c58f1c22SToby Isaac 
7270cc4c1da9SBarry Smith /*@
7271bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7272c58f1c22SToby Isaac 
7273c58f1c22SToby Isaac   Not Collective
7274c58f1c22SToby Isaac 
7275c58f1c22SToby Isaac   Input Parameters:
7276bb7acecfSBarry Smith + dm    - The `DM` object
7277bb7acecfSBarry Smith - label - The `DMLabel`
7278c58f1c22SToby Isaac 
7279c58f1c22SToby Isaac   Level: developer
7280c58f1c22SToby Isaac 
728160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7282c58f1c22SToby Isaac @*/
7283d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7284d71ae5a4SJacob Faibussowitsch {
72855d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7286c58f1c22SToby Isaac   PetscBool   hasLabel;
7287d67d17b1SMatthew G. Knepley   const char *lname;
72885d80c0bfSVaclav Hapla   PetscBool   flg;
7289c58f1c22SToby Isaac 
7290c58f1c22SToby Isaac   PetscFunctionBegin;
7291c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72929566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72939566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72947a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72959566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7296c58f1c22SToby Isaac   tmpLabel->label  = label;
7297c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72985d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72995d80c0bfSVaclav Hapla   *p = tmpLabel;
73009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
73019566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
73025d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
73039566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7304ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
73053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7306c58f1c22SToby Isaac }
7307c58f1c22SToby Isaac 
7308a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7309cc4c1da9SBarry Smith /*@
73104a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
73114a7ee7d0SMatthew G. Knepley 
73124a7ee7d0SMatthew G. Knepley   Not Collective
73134a7ee7d0SMatthew G. Knepley 
73144a7ee7d0SMatthew G. Knepley   Input Parameters:
7315bb7acecfSBarry Smith + dm    - The `DM` object
7316bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
73174a7ee7d0SMatthew G. Knepley 
7318bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7319bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7320bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7321bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7322bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7323bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7324bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
73254a7ee7d0SMatthew G. Knepley 
73264a7ee7d0SMatthew G. Knepley   Level: intermediate
73274a7ee7d0SMatthew G. Knepley 
73281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
73294a7ee7d0SMatthew G. Knepley @*/
7330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7331d71ae5a4SJacob Faibussowitsch {
73324a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
73334a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
73344a7ee7d0SMatthew G. Knepley   const char *name, *lname;
73354a7ee7d0SMatthew G. Knepley 
73364a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
73374a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73384a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
73399566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
73404a7ee7d0SMatthew G. Knepley   while (next) {
73419566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73429566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
73434a7ee7d0SMatthew G. Knepley     if (hasLabel) {
73449566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
73459566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
73464a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
73479566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
73484a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
73499566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
73504a7ee7d0SMatthew G. Knepley       next->label = label;
73514a7ee7d0SMatthew G. Knepley       break;
73524a7ee7d0SMatthew G. Knepley     }
73534a7ee7d0SMatthew G. Knepley     next = next->next;
73544a7ee7d0SMatthew G. Knepley   }
73553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73564a7ee7d0SMatthew G. Knepley }
73574a7ee7d0SMatthew G. Knepley 
7358cc4c1da9SBarry Smith /*@
7359bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7360c58f1c22SToby Isaac 
7361c58f1c22SToby Isaac   Not Collective
7362c58f1c22SToby Isaac 
7363c58f1c22SToby Isaac   Input Parameters:
7364bb7acecfSBarry Smith + dm   - The `DM` object
7365c58f1c22SToby Isaac - name - The label name
7366c58f1c22SToby Isaac 
7367c58f1c22SToby Isaac   Output Parameter:
736820f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7369bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7370c58f1c22SToby Isaac 
7371c58f1c22SToby Isaac   Level: developer
7372c58f1c22SToby Isaac 
73731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7374c58f1c22SToby Isaac @*/
7375d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7376d71ae5a4SJacob Faibussowitsch {
737795d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7378c58f1c22SToby Isaac   PetscBool   hasLabel;
7379d67d17b1SMatthew G. Knepley   const char *lname;
7380c58f1c22SToby Isaac 
7381c58f1c22SToby Isaac   PetscFunctionBegin;
7382c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73834f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7384e5472504SVaclav Hapla   if (label) {
73854f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7386c58f1c22SToby Isaac     *label = NULL;
7387e5472504SVaclav Hapla   }
73885d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73899566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73909566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7391c58f1c22SToby Isaac     if (hasLabel) {
739295d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73939566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
739495d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73959566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7396ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
739795d578d6SVaclav Hapla       if (label) *label = link->label;
73989566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73999566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7400c58f1c22SToby Isaac       break;
7401c58f1c22SToby Isaac     }
7402c58f1c22SToby Isaac   }
74033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7404c58f1c22SToby Isaac }
7405c58f1c22SToby Isaac 
7406306894acSVaclav Hapla /*@
7407bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7408306894acSVaclav Hapla 
7409306894acSVaclav Hapla   Not Collective
7410306894acSVaclav Hapla 
7411306894acSVaclav Hapla   Input Parameters:
7412bb7acecfSBarry Smith + dm           - The `DM` object
7413bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
741420f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7415306894acSVaclav Hapla 
7416306894acSVaclav Hapla   Level: developer
7417306894acSVaclav Hapla 
7418bb7acecfSBarry Smith   Note:
7419306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7420bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7421306894acSVaclav Hapla   *label nullified.
7422306894acSVaclav Hapla 
74231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7424306894acSVaclav Hapla @*/
7425d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7426d71ae5a4SJacob Faibussowitsch {
742743e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7428306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7429306894acSVaclav Hapla 
7430306894acSVaclav Hapla   PetscFunctionBegin;
7431306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74324f572ea9SToby Isaac   PetscAssertPointer(label, 2);
74333ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7434306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7435306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
74365d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
743743e45a93SVaclav Hapla     if (*label == link->label) {
7438306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
743943e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7440306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7441ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
744243e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
74439566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
74449566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7445306894acSVaclav Hapla       break;
7446306894acSVaclav Hapla     }
7447306894acSVaclav Hapla   }
74487a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
74493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7450306894acSVaclav Hapla }
7451306894acSVaclav Hapla 
7452cc4c1da9SBarry Smith /*@
7453c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7454c58f1c22SToby Isaac 
7455c58f1c22SToby Isaac   Not Collective
7456c58f1c22SToby Isaac 
7457c58f1c22SToby Isaac   Input Parameters:
7458bb7acecfSBarry Smith + dm   - The `DM` object
7459c58f1c22SToby Isaac - name - The label name
7460c58f1c22SToby Isaac 
7461c58f1c22SToby Isaac   Output Parameter:
7462c58f1c22SToby Isaac . output - The flag for output
7463c58f1c22SToby Isaac 
7464c58f1c22SToby Isaac   Level: developer
7465c58f1c22SToby Isaac 
74661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7467c58f1c22SToby Isaac @*/
7468d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7469d71ae5a4SJacob Faibussowitsch {
74705d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7471d67d17b1SMatthew G. Knepley   const char *lname;
7472c58f1c22SToby Isaac 
7473c58f1c22SToby Isaac   PetscFunctionBegin;
7474c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74754f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74764f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7477c58f1c22SToby Isaac   while (next) {
7478c58f1c22SToby Isaac     PetscBool flg;
7479c58f1c22SToby Isaac 
74809566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74819566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74829371c9d4SSatish Balay     if (flg) {
74839371c9d4SSatish Balay       *output = next->output;
74843ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74859371c9d4SSatish Balay     }
7486c58f1c22SToby Isaac     next = next->next;
7487c58f1c22SToby Isaac   }
748898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7489c58f1c22SToby Isaac }
7490c58f1c22SToby Isaac 
7491cc4c1da9SBarry Smith /*@
7492bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7493c58f1c22SToby Isaac 
7494c58f1c22SToby Isaac   Not Collective
7495c58f1c22SToby Isaac 
7496c58f1c22SToby Isaac   Input Parameters:
7497bb7acecfSBarry Smith + dm     - The `DM` object
7498c58f1c22SToby Isaac . name   - The label name
7499bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7500c58f1c22SToby Isaac 
7501c58f1c22SToby Isaac   Level: developer
7502c58f1c22SToby Isaac 
75031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7504c58f1c22SToby Isaac @*/
7505d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7506d71ae5a4SJacob Faibussowitsch {
75075d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7508d67d17b1SMatthew G. Knepley   const char *lname;
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac   PetscFunctionBegin;
7511c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75124f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7513c58f1c22SToby Isaac   while (next) {
7514c58f1c22SToby Isaac     PetscBool flg;
7515c58f1c22SToby Isaac 
75169566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
75179566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
75189371c9d4SSatish Balay     if (flg) {
75199371c9d4SSatish Balay       next->output = output;
75203ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
75219371c9d4SSatish Balay     }
7522c58f1c22SToby Isaac     next = next->next;
7523c58f1c22SToby Isaac   }
752498921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7525c58f1c22SToby Isaac }
7526c58f1c22SToby Isaac 
7527c58f1c22SToby Isaac /*@
7528bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7529c58f1c22SToby Isaac 
753020f4b53cSBarry Smith   Collective
7531c58f1c22SToby Isaac 
7532d8d19677SJose E. Roman   Input Parameters:
7533bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7534bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7535bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7536bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7537bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7538c58f1c22SToby Isaac 
7539c58f1c22SToby Isaac   Level: intermediate
7540c58f1c22SToby Isaac 
7541bb7acecfSBarry Smith   Note:
75422cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7543c58f1c22SToby Isaac 
75441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7545c58f1c22SToby Isaac @*/
7546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7547d71ae5a4SJacob Faibussowitsch {
75482cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7549c58f1c22SToby Isaac   const char *name;
7550c58f1c22SToby Isaac   PetscBool   flg;
75515d80c0bfSVaclav Hapla   DMLabelLink link;
7552c58f1c22SToby Isaac 
75535d80c0bfSVaclav Hapla   PetscFunctionBegin;
75545d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75555d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75565d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75575d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75587a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
75593ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
75605d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
75615d80c0bfSVaclav Hapla     label = link->label;
75629566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
75635d80c0bfSVaclav Hapla     if (!all) {
75649566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7565c58f1c22SToby Isaac       if (flg) continue;
75669566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
75677d5acc75SStefano Zampini       if (flg) continue;
75689566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7569ba2698f1SMatthew G. Knepley       if (flg) continue;
75705d80c0bfSVaclav Hapla     }
75719566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
75722cbb9b06SVaclav Hapla     if (labelOld) {
75732cbb9b06SVaclav Hapla       switch (emode) {
7574d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7575d71ae5a4SJacob Faibussowitsch         continue;
7576d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7577d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7578d71ae5a4SJacob Faibussowitsch         break;
7579d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7580d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7581d71ae5a4SJacob Faibussowitsch       default:
7582d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75832cbb9b06SVaclav Hapla       }
75842cbb9b06SVaclav Hapla     }
75855d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75869566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75875d80c0bfSVaclav Hapla     } else {
75885d80c0bfSVaclav Hapla       labelNew = label;
75895d80c0bfSVaclav Hapla     }
75909566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75919566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7592c58f1c22SToby Isaac   }
75933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7594c58f1c22SToby Isaac }
7595461a15a0SLisandro Dalcin 
7596609dae6eSVaclav Hapla /*@C
7597b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7598609dae6eSVaclav Hapla 
759920f4b53cSBarry Smith   Collective; No Fortran Support
7600609dae6eSVaclav Hapla 
7601609dae6eSVaclav Hapla   Input Parameters:
7602bb7acecfSBarry Smith + dm0 - First `DM` object
7603bb7acecfSBarry Smith - dm1 - Second `DM` object
7604609dae6eSVaclav Hapla 
7605a4e35b19SJacob Faibussowitsch   Output Parameters:
76065efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
760720f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7608609dae6eSVaclav Hapla 
7609609dae6eSVaclav Hapla   Level: intermediate
7610609dae6eSVaclav Hapla 
7611609dae6eSVaclav Hapla   Notes:
7612bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7613bb7acecfSBarry Smith 
761420f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7615bb7acecfSBarry Smith 
761620f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7617609dae6eSVaclav Hapla 
76185efe38ccSVaclav Hapla   The output message is set independently on each rank.
7619bb7acecfSBarry Smith 
7620bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7621bb7acecfSBarry Smith 
762220f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7623bb7acecfSBarry Smith 
762420f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7625609dae6eSVaclav Hapla 
7626609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7627bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7628609dae6eSVaclav Hapla 
7629cc4c1da9SBarry Smith   Developer Note:
7630cc4c1da9SBarry Smith   Can automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7631cc4c1da9SBarry Smith 
76321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7633609dae6eSVaclav Hapla @*/
7634d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7635d71ae5a4SJacob Faibussowitsch {
76365efe38ccSVaclav Hapla   PetscInt    n, i;
7637609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
76385efe38ccSVaclav Hapla   PetscBool   eq;
7639609dae6eSVaclav Hapla   MPI_Comm    comm;
76405efe38ccSVaclav Hapla   PetscMPIInt rank;
7641609dae6eSVaclav Hapla 
7642609dae6eSVaclav Hapla   PetscFunctionBegin;
7643609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7644609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7645609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
76464f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
76474f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
76489566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
76499566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
76505efe38ccSVaclav Hapla   {
76515efe38ccSVaclav Hapla     PetscInt n1;
76525efe38ccSVaclav Hapla 
76539566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76549566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76555efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
765648a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7657462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
76585efe38ccSVaclav Hapla     if (!eq) goto finish;
76595efe38ccSVaclav Hapla   }
76605efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7661609dae6eSVaclav Hapla     DMLabel     l0, l1;
7662609dae6eSVaclav Hapla     const char *name;
7663609dae6eSVaclav Hapla     char       *msgInner;
7664609dae6eSVaclav Hapla 
7665609dae6eSVaclav Hapla     /* Ignore label order */
76669566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
76679566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
76689566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7669609dae6eSVaclav Hapla     if (!l1) {
767063a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
76715efe38ccSVaclav Hapla       eq = PETSC_FALSE;
76725efe38ccSVaclav Hapla       break;
7673609dae6eSVaclav Hapla     }
76749566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
76759566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
76769566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
76775efe38ccSVaclav Hapla     if (!eq) break;
7678609dae6eSVaclav Hapla   }
7679462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7680609dae6eSVaclav Hapla finish:
76815efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7682609dae6eSVaclav Hapla   if (message) {
7683609dae6eSVaclav Hapla     *message = NULL;
768448a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76855efe38ccSVaclav Hapla   } else {
768648a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76879566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76885efe38ccSVaclav Hapla   }
76895efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76905efe38ccSVaclav Hapla   if (equal) *equal = eq;
76917a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7693609dae6eSVaclav Hapla }
7694609dae6eSVaclav Hapla 
7695d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7696d71ae5a4SJacob Faibussowitsch {
7697461a15a0SLisandro Dalcin   PetscFunctionBegin;
76984f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7699461a15a0SLisandro Dalcin   if (!*label) {
77009566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
77019566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7702461a15a0SLisandro Dalcin   }
77039566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
77043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7705461a15a0SLisandro Dalcin }
7706461a15a0SLisandro Dalcin 
77070fdc7489SMatthew Knepley /*
77080fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
77090fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
77100fdc7489SMatthew Knepley   (label, id) pair in the DM.
77110fdc7489SMatthew Knepley 
77120fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
77130fdc7489SMatthew Knepley   each label.
77140fdc7489SMatthew Knepley */
7715d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7716d71ae5a4SJacob Faibussowitsch {
77170fdc7489SMatthew Knepley   DMUniversalLabel ul;
77180fdc7489SMatthew Knepley   PetscBool       *active;
77190fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
77200fdc7489SMatthew Knepley 
77210fdc7489SMatthew Knepley   PetscFunctionBegin;
77229566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
77239566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
77249566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
77259566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
77260fdc7489SMatthew Knepley   ul->Nl = 0;
77270fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77280fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
77290fdc7489SMatthew Knepley     const char *name;
77300fdc7489SMatthew Knepley 
77319566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77329566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
77339566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
77340fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
77350fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
77360fdc7489SMatthew Knepley   }
77379566063dSJacob 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));
77380fdc7489SMatthew Knepley   ul->Nv = 0;
77390fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77400fdc7489SMatthew Knepley     DMLabel     label;
77410fdc7489SMatthew Knepley     PetscInt    nv;
77420fdc7489SMatthew Knepley     const char *name;
77430fdc7489SMatthew Knepley 
77440fdc7489SMatthew Knepley     if (!active[l]) continue;
77459566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77469566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77479566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77489566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
77490fdc7489SMatthew Knepley     ul->indices[m] = l;
77500fdc7489SMatthew Knepley     ul->Nv += nv;
77510fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77520fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77530fdc7489SMatthew Knepley     ++m;
77540fdc7489SMatthew Knepley   }
77550fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77560fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77570fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77580fdc7489SMatthew Knepley   }
77590fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77600fdc7489SMatthew Knepley     PetscInt b;
77610fdc7489SMatthew Knepley 
77620fdc7489SMatthew Knepley     ul->masks[l] = 0;
77630fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
77640fdc7489SMatthew Knepley   }
77659566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
77660fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77670fdc7489SMatthew Knepley     DMLabel         label;
77680fdc7489SMatthew Knepley     IS              valueIS;
77690fdc7489SMatthew Knepley     const PetscInt *varr;
77700fdc7489SMatthew Knepley     PetscInt        nv, v;
77710fdc7489SMatthew Knepley 
77720fdc7489SMatthew Knepley     if (!active[l]) continue;
77739566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77749566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77759566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
77769566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7777ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77789566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77799566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77809566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77810fdc7489SMatthew Knepley     ++m;
77820fdc7489SMatthew Knepley   }
77839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77840fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77850fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77860fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77870fdc7489SMatthew Knepley 
77880fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77890fdc7489SMatthew Knepley       DMLabel  label;
77900649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77910fdc7489SMatthew Knepley 
77920fdc7489SMatthew Knepley       if (!active[l]) continue;
77939566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77949566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77959566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77969371c9d4SSatish Balay       if (val == defval) {
77979371c9d4SSatish Balay         ++m;
77989371c9d4SSatish Balay         continue;
77999371c9d4SSatish Balay       }
78000649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
78010fdc7489SMatthew Knepley       marked = PETSC_TRUE;
78029566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
780363a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
78040fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
78050fdc7489SMatthew Knepley       ++m;
78060fdc7489SMatthew Knepley     }
78079566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
78080fdc7489SMatthew Knepley   }
78099566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
78100fdc7489SMatthew Knepley   *universal = ul;
78113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78120fdc7489SMatthew Knepley }
78130fdc7489SMatthew Knepley 
7814d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7815d71ae5a4SJacob Faibussowitsch {
78160fdc7489SMatthew Knepley   PetscInt l;
78170fdc7489SMatthew Knepley 
78180fdc7489SMatthew Knepley   PetscFunctionBegin;
78199566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
78209566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
78219566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
78229566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
78239566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
78240fdc7489SMatthew Knepley   *universal = NULL;
78253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78260fdc7489SMatthew Knepley }
78270fdc7489SMatthew Knepley 
7828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7829d71ae5a4SJacob Faibussowitsch {
78300fdc7489SMatthew Knepley   PetscFunctionBegin;
78314f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
78320fdc7489SMatthew Knepley   *ulabel = ul->label;
78333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78340fdc7489SMatthew Knepley }
78350fdc7489SMatthew Knepley 
7836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7837d71ae5a4SJacob Faibussowitsch {
78380fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
78390fdc7489SMatthew Knepley 
78400fdc7489SMatthew Knepley   PetscFunctionBegin;
7841064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
78420fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
78439566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
78449566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
78450fdc7489SMatthew Knepley   }
78460fdc7489SMatthew Knepley   if (preserveOrder) {
78470fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
78480fdc7489SMatthew Knepley       const char *name;
78490fdc7489SMatthew Knepley       PetscBool   match;
78500fdc7489SMatthew Knepley 
78519566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78529566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
785363a3b9bcSJacob 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]);
78540fdc7489SMatthew Knepley     }
78550fdc7489SMatthew Knepley   }
78563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78570fdc7489SMatthew Knepley }
78580fdc7489SMatthew Knepley 
7859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7860d71ae5a4SJacob Faibussowitsch {
78610fdc7489SMatthew Knepley   PetscInt l;
78620fdc7489SMatthew Knepley 
78630fdc7489SMatthew Knepley   PetscFunctionBegin;
78640fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78650fdc7489SMatthew Knepley     DMLabel  label;
78660fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
78670fdc7489SMatthew Knepley 
78680fdc7489SMatthew Knepley     if (lval) {
78699566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
78709566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
78719566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
78720fdc7489SMatthew Knepley     }
78730fdc7489SMatthew Knepley   }
78743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78750fdc7489SMatthew Knepley }
7876a8fb8f29SToby Isaac 
7877a8fb8f29SToby Isaac /*@
7878bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7879bb7acecfSBarry Smith 
788020f4b53cSBarry Smith   Not Collective
7881a8fb8f29SToby Isaac 
7882a8fb8f29SToby Isaac   Input Parameter:
7883bb7acecfSBarry Smith . dm - The `DM` object
7884a8fb8f29SToby Isaac 
7885a8fb8f29SToby Isaac   Output Parameter:
7886bb7acecfSBarry Smith . cdm - The coarse `DM`
7887a8fb8f29SToby Isaac 
7888a8fb8f29SToby Isaac   Level: intermediate
7889a8fb8f29SToby Isaac 
78901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7891a8fb8f29SToby Isaac @*/
7892d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7893d71ae5a4SJacob Faibussowitsch {
7894a8fb8f29SToby Isaac   PetscFunctionBegin;
7895a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78964f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7897a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7899a8fb8f29SToby Isaac }
7900a8fb8f29SToby Isaac 
7901a8fb8f29SToby Isaac /*@
7902bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7903a8fb8f29SToby Isaac 
7904a8fb8f29SToby Isaac   Input Parameters:
7905bb7acecfSBarry Smith + dm  - The `DM` object
7906bb7acecfSBarry Smith - cdm - The coarse `DM`
7907a8fb8f29SToby Isaac 
7908a8fb8f29SToby Isaac   Level: intermediate
7909a8fb8f29SToby Isaac 
7910bb7acecfSBarry Smith   Note:
7911bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7912bb7acecfSBarry Smith 
79131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7914a8fb8f29SToby Isaac @*/
7915d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7916d71ae5a4SJacob Faibussowitsch {
7917a8fb8f29SToby Isaac   PetscFunctionBegin;
7918a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7919a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
792089d734beSBarry Smith   if (dm == cdm) cdm = NULL;
79219566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
79229566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7923a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
79243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7925a8fb8f29SToby Isaac }
7926a8fb8f29SToby Isaac 
792788bdff64SToby Isaac /*@
7928bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
792988bdff64SToby Isaac 
793088bdff64SToby Isaac   Input Parameter:
7931bb7acecfSBarry Smith . dm - The `DM` object
793288bdff64SToby Isaac 
793388bdff64SToby Isaac   Output Parameter:
7934bb7acecfSBarry Smith . fdm - The fine `DM`
793588bdff64SToby Isaac 
793688bdff64SToby Isaac   Level: intermediate
793788bdff64SToby Isaac 
79381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
793988bdff64SToby Isaac @*/
7940d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7941d71ae5a4SJacob Faibussowitsch {
794288bdff64SToby Isaac   PetscFunctionBegin;
794388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79444f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
794588bdff64SToby Isaac   *fdm = dm->fineMesh;
79463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
794788bdff64SToby Isaac }
794888bdff64SToby Isaac 
794988bdff64SToby Isaac /*@
7950bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
795188bdff64SToby Isaac 
795288bdff64SToby Isaac   Input Parameters:
7953bb7acecfSBarry Smith + dm  - The `DM` object
7954bb7acecfSBarry Smith - fdm - The fine `DM`
795588bdff64SToby Isaac 
7956bb7acecfSBarry Smith   Level: developer
795788bdff64SToby Isaac 
7958bb7acecfSBarry Smith   Note:
7959bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7960bb7acecfSBarry Smith 
79611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
796288bdff64SToby Isaac @*/
7963d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7964d71ae5a4SJacob Faibussowitsch {
796588bdff64SToby Isaac   PetscFunctionBegin;
796688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
796788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
796889d734beSBarry Smith   if (dm == fdm) fdm = NULL;
79699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
79709566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
797188bdff64SToby Isaac   dm->fineMesh = fdm;
79723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
797388bdff64SToby Isaac }
797488bdff64SToby Isaac 
7975a6ba4734SToby Isaac /*@C
7976bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7977a6ba4734SToby Isaac 
797820f4b53cSBarry Smith   Collective
7979783e2ec8SMatthew G. Knepley 
7980a6ba4734SToby Isaac   Input Parameters:
7981bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7982bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7983a6ba4734SToby Isaac . name     - The BC name
798445480ffeSMatthew G. Knepley . label    - The label defining constrained points
7985bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
798645480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7987a6ba4734SToby Isaac . field    - The field to constrain
798845480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7989a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7990a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
799156cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7992a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7993a6ba4734SToby Isaac 
799445480ffeSMatthew G. Knepley   Output Parameter:
799545480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
799645480ffeSMatthew G. Knepley 
7997a6ba4734SToby Isaac   Options Database Keys:
7998a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7999a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8000a6ba4734SToby Isaac 
800120f4b53cSBarry Smith   Level: intermediate
800220f4b53cSBarry Smith 
8003bb7acecfSBarry Smith   Notes:
8004aeebefc2SPierre 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\:
800573ff1848SBarry Smith .vb
800673ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
800773ff1848SBarry Smith .ve
800856cf3b9cSMatthew G. Knepley 
8009a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
801056cf3b9cSMatthew G. Knepley 
801120f4b53cSBarry Smith .vb
801220f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
801320f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
801420f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
801520f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
801620f4b53cSBarry Smith .ve
801756cf3b9cSMatthew G. Knepley + dim - the spatial dimension
801856cf3b9cSMatthew G. Knepley . Nf - the number of fields
801956cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
802056cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
802156cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
802256cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
802356cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
802456cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
802556cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
802656cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
802756cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
802856cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
802956cf3b9cSMatthew G. Knepley . t - current time
803056cf3b9cSMatthew G. Knepley . x - coordinates of the current point
803156cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
803256cf3b9cSMatthew G. Knepley . constants - constant parameters
803356cf3b9cSMatthew G. Knepley - bcval - output values at the current point
803456cf3b9cSMatthew G. Knepley 
80351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
8036a6ba4734SToby Isaac @*/
8037d71ae5a4SJacob 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)
8038d71ae5a4SJacob Faibussowitsch {
8039e5e52638SMatthew G. Knepley   PetscDS ds;
8040a6ba4734SToby Isaac 
8041a6ba4734SToby Isaac   PetscFunctionBegin;
8042a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8043783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
804445480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
804545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
804645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
804745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
804801a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
80499566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8050799db056SMatthew G. Knepley   /* Complete label */
8051799db056SMatthew G. Knepley   if (label) {
8052799db056SMatthew G. Knepley     PetscObject  obj;
8053799db056SMatthew G. Knepley     PetscClassId id;
8054799db056SMatthew G. Knepley 
8055799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8056799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8057799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8058799db056SMatthew G. Knepley       DM plex;
8059799db056SMatthew G. Knepley 
8060799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8061799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8062799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8063799db056SMatthew G. Knepley     }
8064799db056SMatthew G. Knepley   }
80659566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
80663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8067a6ba4734SToby Isaac }
8068a6ba4734SToby Isaac 
806945480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8070d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8071d71ae5a4SJacob Faibussowitsch {
8072e5e52638SMatthew G. Knepley   PetscDS     ds;
8073dff059c6SToby Isaac   DMBoundary *lastnext;
8074e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8075e6f8dbb6SToby Isaac 
8076e6f8dbb6SToby Isaac   PetscFunctionBegin;
80779566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8078e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
807947a1f5adSToby Isaac   if (dm->boundary) {
808047a1f5adSToby Isaac     DMBoundary next = dm->boundary;
808147a1f5adSToby Isaac 
808247a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80833ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
808447a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
808547a1f5adSToby Isaac     while (next) {
808647a1f5adSToby Isaac       DMBoundary b = next;
808747a1f5adSToby Isaac 
808847a1f5adSToby Isaac       next = b->next;
80899566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8090a6ba4734SToby Isaac     }
809147a1f5adSToby Isaac     dm->boundary = NULL;
8092a6ba4734SToby Isaac   }
809347a1f5adSToby Isaac 
8094f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8095e6f8dbb6SToby Isaac   while (dsbound) {
8096e6f8dbb6SToby Isaac     DMBoundary dmbound;
8097e6f8dbb6SToby Isaac 
80989566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8099e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
810045480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
810147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8102dff059c6SToby Isaac     *lastnext = dmbound;
8103f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8104dff059c6SToby Isaac     dsbound   = dsbound->next;
8105a6ba4734SToby Isaac   }
81063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8107a6ba4734SToby Isaac }
8108a6ba4734SToby Isaac 
8109bb7acecfSBarry Smith /* TODO: missing manual page */
8110d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8111d71ae5a4SJacob Faibussowitsch {
8112b95f2879SToby Isaac   DMBoundary b;
8113a6ba4734SToby Isaac 
8114a6ba4734SToby Isaac   PetscFunctionBegin;
8115a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81164f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8117a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
81189566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8119b95f2879SToby Isaac   b = dm->boundary;
812057508eceSPierre Jolivet   while (b && !*isBd) {
8121e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8122e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8123a6ba4734SToby Isaac     PetscInt   i;
8124a6ba4734SToby Isaac 
812545480ffeSMatthew G. Knepley     if (label) {
812657508eceSPierre Jolivet       for (i = 0; i < dsb->Nv && !*isBd; ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8127a6ba4734SToby Isaac     }
8128a6ba4734SToby Isaac     b = b->next;
8129a6ba4734SToby Isaac   }
81303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8131a6ba4734SToby Isaac }
81324d6f44ffSToby Isaac 
81334d6f44ffSToby Isaac /*@C
8134bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8135a6e0b375SMatthew G. Knepley 
813620f4b53cSBarry Smith   Collective
81374d6f44ffSToby Isaac 
81384d6f44ffSToby Isaac   Input Parameters:
8139bb7acecfSBarry Smith + dm    - The `DM`
81400709b2feSToby Isaac . time  - The time
81414d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
81424d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
81434d6f44ffSToby Isaac - mode  - The insertion mode for values
81444d6f44ffSToby Isaac 
81454d6f44ffSToby Isaac   Output Parameter:
81464d6f44ffSToby Isaac . X - vector
81474d6f44ffSToby Isaac 
814820f4b53cSBarry Smith   Calling sequence of `funcs`:
81494d6f44ffSToby Isaac + dim  - The spatial dimension
81508ec8862eSJed Brown . time - The time at which to sample
81514d6f44ffSToby Isaac . x    - The coordinates
815277b739a6SMatthew Knepley . Nc   - The number of components
81534d6f44ffSToby Isaac . u    - The output field values
81544d6f44ffSToby Isaac - ctx  - optional user-defined function context
81554d6f44ffSToby Isaac 
81564d6f44ffSToby Isaac   Level: developer
81574d6f44ffSToby Isaac 
8158bb7acecfSBarry Smith   Developer Notes:
8159bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8160bb7acecfSBarry Smith 
8161bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8162bb7acecfSBarry Smith 
81631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
81644d6f44ffSToby Isaac @*/
8165a4e35b19SJacob 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)
8166d71ae5a4SJacob Faibussowitsch {
81674d6f44ffSToby Isaac   Vec localX;
81684d6f44ffSToby Isaac 
81694d6f44ffSToby Isaac   PetscFunctionBegin;
81704d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8171708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
81729566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8173f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81749566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
81759566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81769566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81779566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8178708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
81793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81804d6f44ffSToby Isaac }
81814d6f44ffSToby Isaac 
8182a6e0b375SMatthew G. Knepley /*@C
8183bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8184a6e0b375SMatthew G. Knepley 
818520f4b53cSBarry Smith   Not Collective
8186a6e0b375SMatthew G. Knepley 
8187a6e0b375SMatthew G. Knepley   Input Parameters:
8188bb7acecfSBarry Smith + dm    - The `DM`
8189a6e0b375SMatthew G. Knepley . time  - The time
8190a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8191a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8192a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8193a6e0b375SMatthew G. Knepley 
8194a6e0b375SMatthew G. Knepley   Output Parameter:
8195a6e0b375SMatthew G. Knepley . localX - vector
8196a6e0b375SMatthew G. Knepley 
819720f4b53cSBarry Smith   Calling sequence of `funcs`:
8198a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8199a4e35b19SJacob Faibussowitsch . time - The current timestep
8200a6e0b375SMatthew G. Knepley . x    - The coordinates
820177b739a6SMatthew Knepley . Nc   - The number of components
8202a6e0b375SMatthew G. Knepley . u    - The output field values
8203a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8204a6e0b375SMatthew G. Knepley 
8205a6e0b375SMatthew G. Knepley   Level: developer
8206a6e0b375SMatthew G. Knepley 
8207bb7acecfSBarry Smith   Developer Notes:
8208bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8209bb7acecfSBarry Smith 
8210bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8211bb7acecfSBarry Smith 
82121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8213a6e0b375SMatthew G. Knepley @*/
8214a4e35b19SJacob 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)
8215d71ae5a4SJacob Faibussowitsch {
82164d6f44ffSToby Isaac   PetscFunctionBegin;
82174d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8218064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8219fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
82203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82214d6f44ffSToby Isaac }
82224d6f44ffSToby Isaac 
8223a6e0b375SMatthew G. Knepley /*@C
8224bb7acecfSBarry 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.
8225a6e0b375SMatthew G. Knepley 
822620f4b53cSBarry Smith   Collective
8227a6e0b375SMatthew G. Knepley 
8228a6e0b375SMatthew G. Knepley   Input Parameters:
8229bb7acecfSBarry Smith + dm     - The `DM`
8230a6e0b375SMatthew G. Knepley . time   - The time
8231a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8232a4e35b19SJacob Faibussowitsch . ids    - The ids
8233a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8234a4e35b19SJacob Faibussowitsch . comps  - The components
8235bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8236a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8237bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8238a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8239a6e0b375SMatthew G. Knepley 
8240a6e0b375SMatthew G. Knepley   Output Parameter:
8241a6e0b375SMatthew G. Knepley . X - vector
8242a6e0b375SMatthew G. Knepley 
824320f4b53cSBarry Smith   Calling sequence of `funcs`:
8244a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8245a4e35b19SJacob Faibussowitsch . time - The current timestep
8246a6e0b375SMatthew G. Knepley . x    - The coordinates
824777b739a6SMatthew Knepley . Nc   - The number of components
8248a6e0b375SMatthew G. Knepley . u    - The output field values
8249a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8250a6e0b375SMatthew G. Knepley 
8251a6e0b375SMatthew G. Knepley   Level: developer
8252a6e0b375SMatthew G. Knepley 
8253bb7acecfSBarry Smith   Developer Notes:
8254bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8255bb7acecfSBarry Smith 
8256bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8257bb7acecfSBarry Smith 
82581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8259a6e0b375SMatthew G. Knepley @*/
8260a4e35b19SJacob 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)
8261d71ae5a4SJacob Faibussowitsch {
82622c53366bSMatthew G. Knepley   Vec localX;
82632c53366bSMatthew G. Knepley 
82642c53366bSMatthew G. Knepley   PetscFunctionBegin;
82652c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82669566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8267f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82689566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
82699566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82709566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82719566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
82723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82732c53366bSMatthew G. Knepley }
82742c53366bSMatthew G. Knepley 
8275a6e0b375SMatthew G. Knepley /*@C
8276bb7acecfSBarry 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.
8277a6e0b375SMatthew G. Knepley 
827820f4b53cSBarry Smith   Not Collective
8279a6e0b375SMatthew G. Knepley 
8280a6e0b375SMatthew G. Knepley   Input Parameters:
8281bb7acecfSBarry Smith + dm     - The `DM`
8282a6e0b375SMatthew G. Knepley . time   - The time
8283bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8284a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8285a4e35b19SJacob Faibussowitsch . ids    - The ids
8286a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8287a4e35b19SJacob Faibussowitsch . comps  - The components
8288a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8289a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8290a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8291a6e0b375SMatthew G. Knepley 
8292a6e0b375SMatthew G. Knepley   Output Parameter:
8293a6e0b375SMatthew G. Knepley . localX - vector
8294a6e0b375SMatthew G. Knepley 
829520f4b53cSBarry Smith   Calling sequence of `funcs`:
8296a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8297a4e35b19SJacob Faibussowitsch . time - The current time
8298a6e0b375SMatthew G. Knepley . x    - The coordinates
829977b739a6SMatthew Knepley . Nc   - The number of components
8300a6e0b375SMatthew G. Knepley . u    - The output field values
8301a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8302a6e0b375SMatthew G. Knepley 
8303a6e0b375SMatthew G. Knepley   Level: developer
8304a6e0b375SMatthew G. Knepley 
8305bb7acecfSBarry Smith   Developer Notes:
8306bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8307bb7acecfSBarry Smith 
8308bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8309bb7acecfSBarry Smith 
83101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8311a6e0b375SMatthew G. Knepley @*/
8312a4e35b19SJacob 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)
8313d71ae5a4SJacob Faibussowitsch {
83144d6f44ffSToby Isaac   PetscFunctionBegin;
83154d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8316064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8317fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
83183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83194d6f44ffSToby Isaac }
83202716604bSToby Isaac 
8321a6e0b375SMatthew G. Knepley /*@C
8322bb7acecfSBarry 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.
8323a6e0b375SMatthew G. Knepley 
832420f4b53cSBarry Smith   Not Collective
8325a6e0b375SMatthew G. Knepley 
8326a6e0b375SMatthew G. Knepley   Input Parameters:
8327bb7acecfSBarry Smith + dm     - The `DM`
8328a6e0b375SMatthew G. Knepley . time   - The time
832920f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8330a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8331a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8332a6e0b375SMatthew G. Knepley 
8333a6e0b375SMatthew G. Knepley   Output Parameter:
8334a6e0b375SMatthew G. Knepley . localX - The output vector
8335a6e0b375SMatthew G. Knepley 
833620f4b53cSBarry Smith   Calling sequence of `funcs`:
8337a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8338a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8339a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8340a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8341a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8342a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8343a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8344a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8345a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8346a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8347a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8348a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8349a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8350a6e0b375SMatthew G. Knepley . t            - The current time
8351a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8352a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8353a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8354a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8355a6e0b375SMatthew G. Knepley 
835673ff1848SBarry Smith   Level: intermediate
835773ff1848SBarry Smith 
8358bb7acecfSBarry Smith   Note:
8359bb7acecfSBarry 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.
8360bb7acecfSBarry 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
8361bb7acecfSBarry 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
8362a6e0b375SMatthew 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.
8363a6e0b375SMatthew G. Knepley 
8364bb7acecfSBarry Smith   Developer Notes:
8365bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8366bb7acecfSBarry Smith 
8367bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8368bb7acecfSBarry Smith 
8369a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8370a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8371a6e0b375SMatthew G. Knepley @*/
8372a4e35b19SJacob 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)
8373d71ae5a4SJacob Faibussowitsch {
83748c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83758c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8376eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
83778c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8378fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
83793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83808c6c5593SMatthew G. Knepley }
83818c6c5593SMatthew G. Knepley 
8382a6e0b375SMatthew G. Knepley /*@C
8383a6e0b375SMatthew 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.
8384a6e0b375SMatthew G. Knepley 
838520f4b53cSBarry Smith   Not Collective
8386a6e0b375SMatthew G. Knepley 
8387a6e0b375SMatthew G. Knepley   Input Parameters:
8388bb7acecfSBarry Smith + dm     - The `DM`
8389a6e0b375SMatthew G. Knepley . time   - The time
8390bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8391a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8392a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8393bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
839420f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8395a6e0b375SMatthew G. Knepley . localU - The input field vector
8396a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8397a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8398a6e0b375SMatthew G. Knepley 
8399a6e0b375SMatthew G. Knepley   Output Parameter:
8400a6e0b375SMatthew G. Knepley . localX - The output vector
8401a6e0b375SMatthew G. Knepley 
840220f4b53cSBarry Smith   Calling sequence of `funcs`:
8403a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8404a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8405a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8406a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8407a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8408a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8409a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8410a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8411a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8412a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8413a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8414a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8415a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8416a6e0b375SMatthew G. Knepley . t            - The current time
8417a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8418a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8419a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8420a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8421a6e0b375SMatthew G. Knepley 
842273ff1848SBarry Smith   Level: intermediate
842373ff1848SBarry Smith 
8424bb7acecfSBarry Smith   Note:
8425bb7acecfSBarry 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.
8426bb7acecfSBarry 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
8427bb7acecfSBarry 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
8428a6e0b375SMatthew 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.
8429a6e0b375SMatthew G. Knepley 
8430bb7acecfSBarry Smith   Developer Notes:
8431bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8432bb7acecfSBarry Smith 
8433bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8434bb7acecfSBarry Smith 
84351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8436a6e0b375SMatthew G. Knepley @*/
8437a4e35b19SJacob 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)
8438d71ae5a4SJacob Faibussowitsch {
84398c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8441064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8442064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8443fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84458c6c5593SMatthew G. Knepley }
84468c6c5593SMatthew G. Knepley 
84472716604bSToby Isaac /*@C
8448d29d7c6eSMatthew 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.
8449d29d7c6eSMatthew G. Knepley 
845020f4b53cSBarry Smith   Not Collective
8451d29d7c6eSMatthew G. Knepley 
8452d29d7c6eSMatthew G. Knepley   Input Parameters:
8453bb7acecfSBarry Smith + dm     - The `DM`
8454d29d7c6eSMatthew G. Knepley . time   - The time
8455bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8456d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8457d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8458bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
845920f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8460d29d7c6eSMatthew G. Knepley . U      - The input field vector
8461d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8462d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8463d29d7c6eSMatthew G. Knepley 
8464d29d7c6eSMatthew G. Knepley   Output Parameter:
8465d29d7c6eSMatthew G. Knepley . X - The output vector
8466d29d7c6eSMatthew G. Knepley 
846720f4b53cSBarry Smith   Calling sequence of `funcs`:
8468d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8469d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8470d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8471d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8472d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8473d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8474d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8475d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8476d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8477d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8478d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8479d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8480d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8481d29d7c6eSMatthew G. Knepley . t            - The current time
8482d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8483d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8484d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8485d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8486d29d7c6eSMatthew G. Knepley 
848773ff1848SBarry Smith   Level: intermediate
848873ff1848SBarry Smith 
8489bb7acecfSBarry Smith   Note:
8490bb7acecfSBarry 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.
8491bb7acecfSBarry 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
8492bb7acecfSBarry 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
8493d29d7c6eSMatthew 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.
8494d29d7c6eSMatthew G. Knepley 
8495bb7acecfSBarry Smith   Developer Notes:
8496bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8497bb7acecfSBarry Smith 
8498bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8499bb7acecfSBarry Smith 
85001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8501d29d7c6eSMatthew G. Knepley @*/
8502a4e35b19SJacob 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)
8503d71ae5a4SJacob Faibussowitsch {
8504d29d7c6eSMatthew G. Knepley   DM  dmIn;
8505d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8506d29d7c6eSMatthew G. Knepley 
8507d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8508d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8509d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8510d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8511d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8512f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
851372fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
851472fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8515d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8516d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8517d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8518d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8519d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
85203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8521d29d7c6eSMatthew G. Knepley }
8522d29d7c6eSMatthew G. Knepley 
8523d29d7c6eSMatthew G. Knepley /*@C
8524ece3a9fcSMatthew 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.
8525ece3a9fcSMatthew G. Knepley 
852620f4b53cSBarry Smith   Not Collective
8527ece3a9fcSMatthew G. Knepley 
8528ece3a9fcSMatthew G. Knepley   Input Parameters:
8529bb7acecfSBarry Smith + dm     - The `DM`
8530ece3a9fcSMatthew G. Knepley . time   - The time
8531bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8532ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8533ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8534bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
853520f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8536ece3a9fcSMatthew G. Knepley . localU - The input field vector
8537ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8538ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8539ece3a9fcSMatthew G. Knepley 
8540ece3a9fcSMatthew G. Knepley   Output Parameter:
8541ece3a9fcSMatthew G. Knepley . localX - The output vector
8542ece3a9fcSMatthew G. Knepley 
854320f4b53cSBarry Smith   Calling sequence of `funcs`:
8544ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8545ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8546ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8547ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8548ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8549ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8550ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8551ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8552ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8553ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8554ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8555ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8556ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8557ece3a9fcSMatthew G. Knepley . t            - The current time
8558ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8559ece3a9fcSMatthew G. Knepley . n            - The face normal
8560ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8561ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8562ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8563ece3a9fcSMatthew G. Knepley 
856473ff1848SBarry Smith   Level: intermediate
856573ff1848SBarry Smith 
8566ece3a9fcSMatthew G. Knepley   Note:
8567bb7acecfSBarry 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.
8568bb7acecfSBarry 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
8569bb7acecfSBarry 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
8570ece3a9fcSMatthew 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.
8571ece3a9fcSMatthew G. Knepley 
8572bb7acecfSBarry Smith   Developer Notes:
8573bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8574bb7acecfSBarry Smith 
8575bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8576bb7acecfSBarry Smith 
85771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8578ece3a9fcSMatthew G. Knepley @*/
8579a4e35b19SJacob 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)
8580d71ae5a4SJacob Faibussowitsch {
8581ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8582ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8583064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8584064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8585fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8587ece3a9fcSMatthew G. Knepley }
8588ece3a9fcSMatthew G. Knepley 
8589ece3a9fcSMatthew G. Knepley /*@C
85902716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85912716604bSToby Isaac 
859220f4b53cSBarry Smith   Collective
8593bb7acecfSBarry Smith 
85942716604bSToby Isaac   Input Parameters:
8595bb7acecfSBarry Smith + dm    - The `DM`
85960709b2feSToby Isaac . time  - The time
85972716604bSToby Isaac . funcs - The functions to evaluate for each field component
85982716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8599574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86002716604bSToby Isaac 
86012716604bSToby Isaac   Output Parameter:
86022716604bSToby Isaac . diff - The diff ||u - u_h||_2
86032716604bSToby Isaac 
86042716604bSToby Isaac   Level: developer
86052716604bSToby Isaac 
8606bb7acecfSBarry Smith   Developer Notes:
8607bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8608bb7acecfSBarry Smith 
8609bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8610bb7acecfSBarry Smith 
86111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
86122716604bSToby Isaac @*/
8613d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8614d71ae5a4SJacob Faibussowitsch {
86152716604bSToby Isaac   PetscFunctionBegin;
86162716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8617b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8618fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
86193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86202716604bSToby Isaac }
8621b698f381SToby Isaac 
8622b698f381SToby Isaac /*@C
8623b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8624b698f381SToby Isaac 
862520f4b53cSBarry Smith   Collective
8626d083f849SBarry Smith 
8627b698f381SToby Isaac   Input Parameters:
8628bb7acecfSBarry Smith + dm    - The `DM`
8629a4e35b19SJacob Faibussowitsch . time  - The time
8630b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8631b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8632574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8633b698f381SToby Isaac - n     - The vector to project along
8634b698f381SToby Isaac 
8635b698f381SToby Isaac   Output Parameter:
8636b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8637b698f381SToby Isaac 
8638b698f381SToby Isaac   Level: developer
8639b698f381SToby Isaac 
8640bb7acecfSBarry Smith   Developer Notes:
8641bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8642bb7acecfSBarry Smith 
8643bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8644bb7acecfSBarry Smith 
86451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8646b698f381SToby Isaac @*/
8647d71ae5a4SJacob 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)
8648d71ae5a4SJacob Faibussowitsch {
8649b698f381SToby Isaac   PetscFunctionBegin;
8650b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8651b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8652fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
86533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8654b698f381SToby Isaac }
8655b698f381SToby Isaac 
86562a16baeaSToby Isaac /*@C
86572a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86582a16baeaSToby Isaac 
865920f4b53cSBarry Smith   Collective
8660d083f849SBarry Smith 
86612a16baeaSToby Isaac   Input Parameters:
8662bb7acecfSBarry Smith + dm    - The `DM`
86632a16baeaSToby Isaac . time  - The time
86642a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86652a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8666574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86672a16baeaSToby Isaac 
86682a16baeaSToby Isaac   Output Parameter:
86692a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86702a16baeaSToby Isaac 
86712a16baeaSToby Isaac   Level: developer
86722a16baeaSToby Isaac 
8673bb7acecfSBarry Smith   Developer Notes:
8674bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8675bb7acecfSBarry Smith 
8676bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8677bb7acecfSBarry Smith 
867842747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
86792a16baeaSToby Isaac @*/
8680d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8681d71ae5a4SJacob Faibussowitsch {
86822a16baeaSToby Isaac   PetscFunctionBegin;
86832a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86842a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8685fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
86863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86872a16baeaSToby Isaac }
86882a16baeaSToby Isaac 
8689df0b854cSToby Isaac /*@C
8690bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8691502a2867SDave May 
8692502a2867SDave May   Not Collective
8693502a2867SDave May 
8694502a2867SDave May   Input Parameter:
8695bb7acecfSBarry Smith . dm - The `DM`
8696502a2867SDave May 
86970a19bb7dSprj-   Output Parameters:
86980a19bb7dSprj- + nranks - the number of neighbours
86990a19bb7dSprj- - ranks  - the neighbors ranks
8700502a2867SDave May 
87019bdbcad8SBarry Smith   Level: beginner
87029bdbcad8SBarry Smith 
8703bb7acecfSBarry Smith   Note:
8704bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8705502a2867SDave May 
87061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8707502a2867SDave May @*/
8708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8709d71ae5a4SJacob Faibussowitsch {
8710502a2867SDave May   PetscFunctionBegin;
8711502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8712fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
87133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8714502a2867SDave May }
8715502a2867SDave May 
8716531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8717531c7667SBarry Smith 
8718531c7667SBarry Smith /*
8719531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
87202b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8721531c7667SBarry Smith */
872266976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8723d71ae5a4SJacob Faibussowitsch {
8724531c7667SBarry Smith   PetscFunctionBegin;
8725531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8726531c7667SBarry Smith     Vec x1local;
8727531c7667SBarry Smith     DM  dm;
87289566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87297a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
87309566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
87319566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
87329566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8733531c7667SBarry Smith     x1 = x1local;
8734531c7667SBarry Smith   }
87359566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8736531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8737531c7667SBarry Smith     DM dm;
87389566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87399566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8740531c7667SBarry Smith   }
87413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8742531c7667SBarry Smith }
8743531c7667SBarry Smith 
8744531c7667SBarry Smith /*@
8745bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8746531c7667SBarry Smith 
8747a4e35b19SJacob Faibussowitsch   Input Parameters:
8748a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8749a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8750531c7667SBarry Smith 
87519bdbcad8SBarry Smith   Level: advanced
87529bdbcad8SBarry Smith 
875373ff1848SBarry Smith   Developer Note:
875473ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8755531c7667SBarry Smith 
87561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8757531c7667SBarry Smith @*/
8758d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8759d71ae5a4SJacob Faibussowitsch {
8760531c7667SBarry Smith   PetscFunctionBegin;
8761531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
87623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8763531c7667SBarry Smith }
87648320bc6fSPatrick Sanan 
87658320bc6fSPatrick Sanan /*@
8766bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
87678320bc6fSPatrick Sanan 
87688320bc6fSPatrick Sanan   Collective
87698320bc6fSPatrick Sanan 
87708320bc6fSPatrick Sanan   Input Parameters:
8771bb7acecfSBarry Smith + dm1 - the first `DM`
8772bb7acecfSBarry Smith - dm2 - the second `DM`
87738320bc6fSPatrick Sanan 
87748320bc6fSPatrick Sanan   Output Parameters:
8775bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8776bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
87778320bc6fSPatrick Sanan 
877820f4b53cSBarry Smith   Level: advanced
877920f4b53cSBarry Smith 
87808320bc6fSPatrick Sanan   Notes:
8781bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
87823d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
87838320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8784bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
87853d862458SPatrick Sanan   decomposition, but hold different data.
87868320bc6fSPatrick Sanan 
87878320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8788bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87898320bc6fSPatrick Sanan 
8790bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87918320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87928320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8793bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87948320bc6fSPatrick Sanan 
8795bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8796bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87978320bc6fSPatrick Sanan .vb
87988320bc6fSPatrick Sanan   ...
87999566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
88008320bc6fSPatrick Sanan   if (set && compatible)  {
88019566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
88029566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
88039566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
88048320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
88058320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
88068320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88078320bc6fSPatrick Sanan       }
88088320bc6fSPatrick Sanan     }
88099566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
88109566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
88118320bc6fSPatrick Sanan   } else {
88128320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88138320bc6fSPatrick Sanan   }
88148320bc6fSPatrick Sanan   ...
88158320bc6fSPatrick Sanan .ve
88168320bc6fSPatrick Sanan 
8817bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
88188320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
88198320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
88208320bc6fSPatrick Sanan   always check the "set" output parameter.
88218320bc6fSPatrick Sanan 
8822bb7acecfSBarry Smith   A `DM` is always compatible with itself.
88238320bc6fSPatrick Sanan 
8824bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
88258320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
88268320bc6fSPatrick Sanan   incompatible.
88278320bc6fSPatrick Sanan 
88288320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8829bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
88308320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
88318320bc6fSPatrick Sanan 
883273ff1848SBarry Smith   Developer Note:
8833bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
88343d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8835a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8836bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
88378320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
88383d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8839bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
88408320bc6fSPatrick Sanan 
88411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
88428320bc6fSPatrick Sanan @*/
8843d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8844d71ae5a4SJacob Faibussowitsch {
88458320bc6fSPatrick Sanan   PetscMPIInt compareResult;
88468320bc6fSPatrick Sanan   DMType      type, type2;
88478320bc6fSPatrick Sanan   PetscBool   sameType;
88488320bc6fSPatrick Sanan 
88498320bc6fSPatrick Sanan   PetscFunctionBegin;
8850a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
88518320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
88528320bc6fSPatrick Sanan 
88538320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8854a5bc1bf3SBarry Smith   if (dm1 == dm2) {
88558320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88568320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
88573ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88588320bc6fSPatrick Sanan   }
88598320bc6fSPatrick Sanan 
88608320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
88618320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
88628320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
88638320bc6fSPatrick Sanan      determined by the implementation-specific logic */
88649566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
88658320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
88668320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88678320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
88683ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88698320bc6fSPatrick Sanan   }
88708320bc6fSPatrick Sanan 
88718320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8872a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8873dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
88743ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
88758320bc6fSPatrick Sanan   }
88768320bc6fSPatrick Sanan 
8877a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
88788320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
88799566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
88809566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
88819566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
88828320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8883dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
88848320bc6fSPatrick Sanan   } else {
88858320bc6fSPatrick Sanan     *set = PETSC_FALSE;
88868320bc6fSPatrick Sanan   }
88873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88888320bc6fSPatrick Sanan }
8889c0f0dcc3SMatthew G. Knepley 
8890c0f0dcc3SMatthew G. Knepley /*@C
8891bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8892c0f0dcc3SMatthew G. Knepley 
889320f4b53cSBarry Smith   Logically Collective
8894c0f0dcc3SMatthew G. Knepley 
8895c0f0dcc3SMatthew G. Knepley   Input Parameters:
889660225df5SJacob Faibussowitsch + dm             - the `DM`
8897c0f0dcc3SMatthew G. Knepley . f              - the monitor function
889820f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
889920f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8900c0f0dcc3SMatthew G. Knepley 
890120f4b53cSBarry Smith   Options Database Key:
890260225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8903c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8904c0f0dcc3SMatthew G. Knepley 
89059bdbcad8SBarry Smith   Level: intermediate
89069bdbcad8SBarry Smith 
8907bb7acecfSBarry Smith   Note:
8908c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8909bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8910c0f0dcc3SMatthew G. Knepley   order in which they were set.
8911c0f0dcc3SMatthew G. Knepley 
891273ff1848SBarry Smith   Fortran Note:
8913bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8914bb7acecfSBarry Smith 
891573ff1848SBarry Smith   Developer Note:
8916bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8917c0f0dcc3SMatthew G. Knepley 
89181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8919c0f0dcc3SMatthew G. Knepley @*/
8920d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8921d71ae5a4SJacob Faibussowitsch {
8922c0f0dcc3SMatthew G. Knepley   PetscInt m;
8923c0f0dcc3SMatthew G. Knepley 
8924c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8925c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8926c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8927c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8928c0f0dcc3SMatthew G. Knepley 
89299566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f, mctx, monitordestroy, (PetscErrorCode (*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
89303ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8931c0f0dcc3SMatthew G. Knepley   }
89327a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8933c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8934c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8935c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
89363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8937c0f0dcc3SMatthew G. Knepley }
8938c0f0dcc3SMatthew G. Knepley 
8939c0f0dcc3SMatthew G. Knepley /*@
8940bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8941c0f0dcc3SMatthew G. Knepley 
894220f4b53cSBarry Smith   Logically Collective
8943c0f0dcc3SMatthew G. Knepley 
8944c0f0dcc3SMatthew G. Knepley   Input Parameter:
8945c0f0dcc3SMatthew G. Knepley . dm - the DM
8946c0f0dcc3SMatthew G. Knepley 
8947c0f0dcc3SMatthew G. Knepley   Options Database Key:
8948c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8949bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8950c0f0dcc3SMatthew G. Knepley   set via the options database
8951c0f0dcc3SMatthew G. Knepley 
89529bdbcad8SBarry Smith   Level: intermediate
89539bdbcad8SBarry Smith 
8954bb7acecfSBarry Smith   Note:
8955bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8956c0f0dcc3SMatthew G. Knepley 
89571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8958c0f0dcc3SMatthew G. Knepley @*/
8959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8960d71ae5a4SJacob Faibussowitsch {
8961c0f0dcc3SMatthew G. Knepley   PetscInt m;
8962c0f0dcc3SMatthew G. Knepley 
8963c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8964c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8965c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
89669566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8967c0f0dcc3SMatthew G. Knepley   }
8968c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
89693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8970c0f0dcc3SMatthew G. Knepley }
8971c0f0dcc3SMatthew G. Knepley 
8972c0f0dcc3SMatthew G. Knepley /*@C
8973c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8974c0f0dcc3SMatthew G. Knepley 
897520f4b53cSBarry Smith   Collective
8976c0f0dcc3SMatthew G. Knepley 
8977c0f0dcc3SMatthew G. Knepley   Input Parameters:
8978bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8979c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8980c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8981c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8982c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8983bb7acecfSBarry 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
8984c0f0dcc3SMatthew G. Knepley 
8985c0f0dcc3SMatthew G. Knepley   Output Parameter:
8986c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8987c0f0dcc3SMatthew G. Knepley 
8988c0f0dcc3SMatthew G. Knepley   Level: developer
8989c0f0dcc3SMatthew G. Knepley 
8990648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8991db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
899260225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8993db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8994c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8995db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8996bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8997c0f0dcc3SMatthew G. Knepley @*/
8998d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8999d71ae5a4SJacob Faibussowitsch {
9000c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9001c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9002c0f0dcc3SMatthew G. Knepley 
9003c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9004c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9005648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
9006c0f0dcc3SMatthew G. Knepley   if (*flg) {
9007c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9008c0f0dcc3SMatthew G. Knepley 
90099566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
9010648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
90119566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
90129566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode (*)(DM, void *))monitor, vf, (PetscErrorCode (*)(void **))PetscViewerAndFormatDestroy));
9013c0f0dcc3SMatthew G. Knepley   }
90143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9015c0f0dcc3SMatthew G. Knepley }
9016c0f0dcc3SMatthew G. Knepley 
9017c0f0dcc3SMatthew G. Knepley /*@
9018c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
9019c0f0dcc3SMatthew G. Knepley 
902020f4b53cSBarry Smith   Collective
9021c0f0dcc3SMatthew G. Knepley 
90222fe279fdSBarry Smith   Input Parameter:
9023bb7acecfSBarry Smith . dm - The `DM`
9024c0f0dcc3SMatthew G. Knepley 
9025c0f0dcc3SMatthew G. Knepley   Level: developer
9026c0f0dcc3SMatthew G. Knepley 
902773ff1848SBarry Smith   Developer Note:
9028a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
9029a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
9030a4e35b19SJacob Faibussowitsch   concept of discretization.
9031bb7acecfSBarry Smith 
90321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
9033c0f0dcc3SMatthew G. Knepley @*/
9034d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
9035d71ae5a4SJacob Faibussowitsch {
9036c0f0dcc3SMatthew G. Knepley   PetscInt m;
9037c0f0dcc3SMatthew G. Knepley 
9038c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
90393ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
9040c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
904148a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
90423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9043c0f0dcc3SMatthew G. Knepley }
90442e4af2aeSMatthew G. Knepley 
90452e4af2aeSMatthew G. Knepley /*@
9046bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
90472e4af2aeSMatthew G. Knepley 
904820f4b53cSBarry Smith   Collective
90492e4af2aeSMatthew G. Knepley 
90502e4af2aeSMatthew G. Knepley   Input Parameters:
9051bb7acecfSBarry Smith + dm  - The `DM`
90526b867d5aSJose E. Roman - sol - The solution vector
90532e4af2aeSMatthew G. Knepley 
90546b867d5aSJose E. Roman   Input/Output Parameter:
905520f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
90566b867d5aSJose E. Roman            contains the error in each field
90576b867d5aSJose E. Roman 
90586b867d5aSJose E. Roman   Output Parameter:
905920f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
906020f4b53cSBarry Smith 
906120f4b53cSBarry Smith   Level: developer
90622e4af2aeSMatthew G. Knepley 
9063bb7acecfSBarry Smith   Note:
9064bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
90652e4af2aeSMatthew G. Knepley 
90661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
90672e4af2aeSMatthew G. Knepley @*/
9068d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9069d71ae5a4SJacob Faibussowitsch {
90702e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
90712e4af2aeSMatthew G. Knepley   void    **ctxs;
90722e4af2aeSMatthew G. Knepley   PetscReal time;
90732e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
90742e4af2aeSMatthew G. Knepley 
90752e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
90769566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
90779566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
90789566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
90792e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
90802e4af2aeSMatthew G. Knepley     PetscDS         ds;
90812e4af2aeSMatthew G. Knepley     DMLabel         label;
90822e4af2aeSMatthew G. Knepley     IS              fieldIS;
90832e4af2aeSMatthew G. Knepley     const PetscInt *fields;
90842e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
90852e4af2aeSMatthew G. Knepley 
908607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
90879566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90889566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90892e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90902e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90919566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90922e4af2aeSMatthew G. Knepley     }
90939566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90942e4af2aeSMatthew G. Knepley   }
9095ad540459SPierre 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);
90969566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90979566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90982e4af2aeSMatthew G. Knepley   if (errorVec) {
90992e4af2aeSMatthew G. Knepley     DM             edm;
91002e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
91012e4af2aeSMatthew G. Knepley     PetscBool      simplex;
91022e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
91032e4af2aeSMatthew G. Knepley 
91049566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
91059566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
91069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
91079566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
91082e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
91099566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
91102e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
91112e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
91122e4af2aeSMatthew G. Knepley       PetscQuadrature q;
91132e4af2aeSMatthew G. Knepley       const char     *name;
91142e4af2aeSMatthew G. Knepley 
91159566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
91169566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
91179566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
91189566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
91199566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
91209566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
91219566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
91229566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
91232e4af2aeSMatthew G. Knepley     }
91249566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
91252e4af2aeSMatthew G. Knepley 
91269566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
91279566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
91289566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
91299566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
91302e4af2aeSMatthew G. Knepley   }
91319566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
91323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91332e4af2aeSMatthew G. Knepley }
91349a2a23afSMatthew G. Knepley 
91359a2a23afSMatthew G. Knepley /*@
9136bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
91379a2a23afSMatthew G. Knepley 
913820f4b53cSBarry Smith   Not Collective
91399a2a23afSMatthew G. Knepley 
91409a2a23afSMatthew G. Knepley   Input Parameter:
9141bb7acecfSBarry Smith . dm - The `DM`
91429a2a23afSMatthew G. Knepley 
91439a2a23afSMatthew G. Knepley   Output Parameter:
9144a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
91459a2a23afSMatthew G. Knepley 
91469a2a23afSMatthew G. Knepley   Level: advanced
91479a2a23afSMatthew G. Knepley 
9148e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
91499a2a23afSMatthew G. Knepley @*/
9150d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9151d71ae5a4SJacob Faibussowitsch {
91529a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91539a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91549566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
91553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91569a2a23afSMatthew G. Knepley }
91579a2a23afSMatthew G. Knepley 
91589a2a23afSMatthew G. Knepley /*@
9159ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
91609a2a23afSMatthew G. Knepley 
916120f4b53cSBarry Smith   Not Collective
91629a2a23afSMatthew G. Knepley 
91639a2a23afSMatthew G. Knepley   Input Parameters:
9164bb7acecfSBarry Smith + dm    - The `DM`
9165bb7acecfSBarry Smith . label - The `DMLabel`
9166ac17215fSMatthew G. Knepley . value - The label value indicating the region
9167ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
91689a2a23afSMatthew G. Knepley 
91699a2a23afSMatthew G. Knepley   Output Parameter:
9170bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
91719a2a23afSMatthew G. Knepley 
91729bdbcad8SBarry Smith   Level: advanced
91739bdbcad8SBarry Smith 
9174bb7acecfSBarry Smith   Note:
9175bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
917604c51a94SMatthew G. Knepley 
9177e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
91789a2a23afSMatthew G. Knepley @*/
9179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9180d71ae5a4SJacob Faibussowitsch {
9181ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
918204c51a94SMatthew G. Knepley   PetscBool       has;
91839a2a23afSMatthew G. Knepley 
91849a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91859a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91869a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91879a2a23afSMatthew G. Knepley   key.label = label;
91889a2a23afSMatthew G. Knepley   key.value = value;
9189ac17215fSMatthew G. Knepley   key.part  = part;
91909566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91919566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91929566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91949a2a23afSMatthew G. Knepley }
91959a2a23afSMatthew G. Knepley 
91969a2a23afSMatthew G. Knepley /*@
9197bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91989a2a23afSMatthew G. Knepley 
919920f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
92009a2a23afSMatthew G. Knepley 
92019a2a23afSMatthew G. Knepley   Input Parameters:
9202bb7acecfSBarry Smith + dm    - The `DM`
9203bb7acecfSBarry Smith . label - The `DMLabel`
92049a2a23afSMatthew G. Knepley . value - The label value indicating the region
9205ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9206bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
92079a2a23afSMatthew G. Knepley 
92089a2a23afSMatthew G. Knepley   Level: advanced
92099a2a23afSMatthew G. Knepley 
9210e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
92119a2a23afSMatthew G. Knepley @*/
9212d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9213d71ae5a4SJacob Faibussowitsch {
92149a2a23afSMatthew G. Knepley   Vec             old;
92159a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
92169a2a23afSMatthew G. Knepley 
92179a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92189a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92199a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
92209a2a23afSMatthew G. Knepley   key.label = label;
92219a2a23afSMatthew G. Knepley   key.value = value;
9222ac17215fSMatthew G. Knepley   key.part  = part;
92239566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
92249566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
92259566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
92269566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9227d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
92283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92299a2a23afSMatthew G. Knepley }
92309a2a23afSMatthew G. Knepley 
9231cc4c1da9SBarry Smith /*@
9232bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
92339a2a23afSMatthew G. Knepley 
923420f4b53cSBarry Smith   Not Collective
92359a2a23afSMatthew G. Knepley 
92369a2a23afSMatthew G. Knepley   Input Parameter:
9237bb7acecfSBarry Smith . dm - The `DM`
92389a2a23afSMatthew G. Knepley 
92399a2a23afSMatthew G. Knepley   Output Parameters:
9240bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9241bb7acecfSBarry Smith . values - The label values for each `Vec`
9242bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
92439a2a23afSMatthew G. Knepley 
92449bdbcad8SBarry Smith   Level: advanced
92459bdbcad8SBarry Smith 
9246bb7acecfSBarry Smith   Note:
9247bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
92489a2a23afSMatthew G. Knepley 
9249e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
92509a2a23afSMatthew G. Knepley @*/
9251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9252d71ae5a4SJacob Faibussowitsch {
92539a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
92549a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
92559a2a23afSMatthew G. Knepley 
92569a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92579a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92584f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
92594f572ea9SToby Isaac   PetscAssertPointer(values, 3);
92604f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
92619566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
92629566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
92639566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
92649371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
92659371c9d4SSatish Balay     labels[i] = keys[i].label;
92669371c9d4SSatish Balay     values[i] = keys[i].value;
92679371c9d4SSatish Balay     parts[i]  = keys[i].part;
92689371c9d4SSatish Balay   }
92699566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
92703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92719a2a23afSMatthew G. Knepley }
92729a2a23afSMatthew G. Knepley 
92739a2a23afSMatthew G. Knepley /*@
9274bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
92759a2a23afSMatthew G. Knepley 
927620f4b53cSBarry Smith   Not Collective
92779a2a23afSMatthew G. Knepley 
92789a2a23afSMatthew G. Knepley   Input Parameter:
9279bb7acecfSBarry Smith . dm - The `DM`
92809a2a23afSMatthew G. Knepley 
92819a2a23afSMatthew G. Knepley   Output Parameter:
9282bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
92839a2a23afSMatthew G. Knepley 
92849a2a23afSMatthew G. Knepley   Level: advanced
92859a2a23afSMatthew G. Knepley 
9286bb7acecfSBarry Smith   Note:
9287bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9288bb7acecfSBarry Smith 
9289e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92909a2a23afSMatthew G. Knepley @*/
9291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9292d71ae5a4SJacob Faibussowitsch {
92939a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92949a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9295d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9296d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9297e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9298e4d5475eSStefano Zampini 
9299e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
93009566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9301d705d0eaSStefano Zampini   {
9302d705d0eaSStefano Zampini     Vec     *auxData;
9303d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9304d705d0eaSStefano Zampini 
9305d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9306d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9307d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9308d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9309d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9310e4d5475eSStefano Zampini   }
9311e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9312e4d5475eSStefano Zampini }
9313e4d5475eSStefano Zampini 
9314e4d5475eSStefano Zampini /*@
9315e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9316e4d5475eSStefano Zampini 
9317e4d5475eSStefano Zampini   Not Collective
9318e4d5475eSStefano Zampini 
9319e4d5475eSStefano Zampini   Input Parameter:
9320e4d5475eSStefano Zampini . dm - The `DM`
9321e4d5475eSStefano Zampini 
9322e4d5475eSStefano Zampini   Level: advanced
9323e4d5475eSStefano Zampini 
9324e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9325e4d5475eSStefano Zampini @*/
9326e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9327e4d5475eSStefano Zampini {
9328e4d5475eSStefano Zampini   Vec     *auxData;
9329e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9330e4d5475eSStefano Zampini 
9331e4d5475eSStefano Zampini   PetscFunctionBegin;
9332e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9333d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9334e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9335d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9336d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9337e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9338e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
93393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93409a2a23afSMatthew G. Knepley }
9341b5a892a1SMatthew G. Knepley 
9342cc4c1da9SBarry Smith /*@
9343bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9344b5a892a1SMatthew G. Knepley 
934520f4b53cSBarry Smith   Not Collective
9346b5a892a1SMatthew G. Knepley 
9347b5a892a1SMatthew G. Knepley   Input Parameters:
9348bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9349b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9350b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9351b5a892a1SMatthew G. Knepley 
9352b5a892a1SMatthew G. Knepley   Output Parameters:
9353bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9354b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9355b5a892a1SMatthew G. Knepley 
9356b5a892a1SMatthew G. Knepley   Level: advanced
9357b5a892a1SMatthew G. Knepley 
9358bb7acecfSBarry Smith   Note:
9359bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9360bb7acecfSBarry Smith 
936185036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9362bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9363bb7acecfSBarry Smith 
9364bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9365bb7acecfSBarry Smith 
93661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9367b5a892a1SMatthew G. Knepley @*/
9368d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9369d71ae5a4SJacob Faibussowitsch {
9370b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
937185036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9372b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9373b5a892a1SMatthew G. Knepley 
9374b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93759371c9d4SSatish Balay   if (!nO) {
93769371c9d4SSatish Balay     *ornt  = 0;
93779371c9d4SSatish Balay     *found = PETSC_TRUE;
93783ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93799371c9d4SSatish Balay   }
9380b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
938185036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9382b5a892a1SMatthew G. Knepley 
93839371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93849371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
93859371c9d4SSatish Balay     if (c == cS) {
93869371c9d4SSatish Balay       *ornt = o;
93879371c9d4SSatish Balay       break;
93889371c9d4SSatish Balay     }
9389b5a892a1SMatthew G. Knepley   }
9390b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9392b5a892a1SMatthew G. Knepley }
9393b5a892a1SMatthew G. Knepley 
9394cc4c1da9SBarry Smith /*@
9395bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9396b5a892a1SMatthew G. Knepley 
939720f4b53cSBarry Smith   Not Collective
9398b5a892a1SMatthew G. Knepley 
9399b5a892a1SMatthew G. Knepley   Input Parameters:
9400bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9401b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9402b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9403b5a892a1SMatthew G. Knepley 
94042fe279fdSBarry Smith   Output Parameter:
9405bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9406b5a892a1SMatthew G. Knepley 
9407b5a892a1SMatthew G. Knepley   Level: advanced
9408b5a892a1SMatthew G. Knepley 
9409bb7acecfSBarry Smith   Note:
9410bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9411bb7acecfSBarry Smith 
941273ff1848SBarry Smith   Developer Note:
9413bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9414bb7acecfSBarry Smith 
94151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9416b5a892a1SMatthew G. Knepley @*/
9417d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9418d71ae5a4SJacob Faibussowitsch {
9419b5a892a1SMatthew G. Knepley   PetscBool found;
9420b5a892a1SMatthew G. Knepley 
9421b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94229566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
94237a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9425b5a892a1SMatthew G. Knepley }
9426b5a892a1SMatthew G. Knepley 
9427cc4c1da9SBarry Smith /*@
9428bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9429b5a892a1SMatthew G. Knepley 
943020f4b53cSBarry Smith   Not Collective
9431b5a892a1SMatthew G. Knepley 
9432b5a892a1SMatthew G. Knepley   Input Parameters:
9433bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9434b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9435b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9436b5a892a1SMatthew G. Knepley 
9437b5a892a1SMatthew G. Knepley   Output Parameters:
9438bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9439b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9440b5a892a1SMatthew G. Knepley 
9441b5a892a1SMatthew G. Knepley   Level: advanced
9442b5a892a1SMatthew G. Knepley 
944373ff1848SBarry Smith   Notes:
9444bb7acecfSBarry Smith   An arrangement is a vertex order
9445bb7acecfSBarry Smith 
944685036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9447bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9448bb7acecfSBarry Smith 
9449bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9450bb7acecfSBarry Smith 
945185036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9452b5a892a1SMatthew G. Knepley @*/
9453d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9454d71ae5a4SJacob Faibussowitsch {
9455b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
945685036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9457b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9458b5a892a1SMatthew G. Knepley 
9459b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94609371c9d4SSatish Balay   if (!nO) {
94619371c9d4SSatish Balay     *ornt  = 0;
94629371c9d4SSatish Balay     *found = PETSC_TRUE;
94633ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94649371c9d4SSatish Balay   }
9465b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
946685036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9467b5a892a1SMatthew G. Knepley 
94689371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94699371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
94709371c9d4SSatish Balay     if (c == cS) {
94719371c9d4SSatish Balay       *ornt = o;
94729371c9d4SSatish Balay       break;
94739371c9d4SSatish Balay     }
9474b5a892a1SMatthew G. Knepley   }
9475b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9477b5a892a1SMatthew G. Knepley }
9478b5a892a1SMatthew G. Knepley 
9479cc4c1da9SBarry Smith /*@
9480bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9481b5a892a1SMatthew G. Knepley 
948220f4b53cSBarry Smith   Not Collective
9483b5a892a1SMatthew G. Knepley 
9484b5a892a1SMatthew G. Knepley   Input Parameters:
9485bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9486b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9487b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9488b5a892a1SMatthew G. Knepley 
94892fe279fdSBarry Smith   Output Parameter:
9490bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9491b5a892a1SMatthew G. Knepley 
9492b5a892a1SMatthew G. Knepley   Level: advanced
9493b5a892a1SMatthew G. Knepley 
9494bb7acecfSBarry Smith   Note:
9495bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9496bb7acecfSBarry Smith 
949773ff1848SBarry Smith   Developer Note:
9498bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9499bb7acecfSBarry Smith 
95001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9501b5a892a1SMatthew G. Knepley @*/
9502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9503d71ae5a4SJacob Faibussowitsch {
9504b5a892a1SMatthew G. Knepley   PetscBool found;
9505b5a892a1SMatthew G. Knepley 
9506b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
95079566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
95087a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
95093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9510b5a892a1SMatthew G. Knepley }
9511012bc364SMatthew G. Knepley 
9512cc4c1da9SBarry Smith /*@
9513012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9514012bc364SMatthew G. Knepley 
951520f4b53cSBarry Smith   Not Collective
9516012bc364SMatthew G. Knepley 
9517012bc364SMatthew G. Knepley   Input Parameters:
9518bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9519012bc364SMatthew G. Knepley - point - Coordinates of the point
9520012bc364SMatthew G. Knepley 
95212fe279fdSBarry Smith   Output Parameter:
9522012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9523012bc364SMatthew G. Knepley 
9524012bc364SMatthew G. Knepley   Level: advanced
9525012bc364SMatthew G. Knepley 
95261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9527012bc364SMatthew G. Knepley @*/
9528d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9529d71ae5a4SJacob Faibussowitsch {
9530012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9531012bc364SMatthew G. Knepley   PetscInt  d;
9532012bc364SMatthew G. Knepley 
9533012bc364SMatthew G. Knepley   PetscFunctionBegin;
9534012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9535012bc364SMatthew G. Knepley   switch (ct) {
9536012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9537012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9538012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
95399371c9d4SSatish Balay       if (point[d] < -1.0) {
95409371c9d4SSatish Balay         *inside = PETSC_FALSE;
95419371c9d4SSatish Balay         break;
95429371c9d4SSatish Balay       }
9543012bc364SMatthew G. Knepley       sum += point[d];
9544012bc364SMatthew G. Knepley     }
95459371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
95469371c9d4SSatish Balay       *inside = PETSC_FALSE;
95479371c9d4SSatish Balay       break;
95489371c9d4SSatish Balay     }
9549012bc364SMatthew G. Knepley     break;
9550012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9551012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9552012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
95539371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
95549371c9d4SSatish Balay         *inside = PETSC_FALSE;
9555012bc364SMatthew G. Knepley         break;
95569371c9d4SSatish Balay       }
95579371c9d4SSatish Balay     break;
9558d71ae5a4SJacob Faibussowitsch   default:
9559d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9560012bc364SMatthew G. Knepley   }
95613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9562012bc364SMatthew G. Knepley }
9563adc21957SMatthew G. Knepley 
9564adc21957SMatthew G. Knepley /*@
9565adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9566adc21957SMatthew G. Knepley 
9567adc21957SMatthew G. Knepley   Logically collective
9568adc21957SMatthew G. Knepley 
9569adc21957SMatthew G. Knepley   Input Parameters:
9570adc21957SMatthew G. Knepley + dm      - The DM
9571adc21957SMatthew G. Knepley - reorder - Flag for reordering
9572adc21957SMatthew G. Knepley 
9573adc21957SMatthew G. Knepley   Level: intermediate
9574adc21957SMatthew G. Knepley 
9575adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9576adc21957SMatthew G. Knepley @*/
9577adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9578adc21957SMatthew G. Knepley {
9579adc21957SMatthew G. Knepley   PetscFunctionBegin;
9580adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9581adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9582adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9583adc21957SMatthew G. Knepley }
9584adc21957SMatthew G. Knepley 
9585adc21957SMatthew G. Knepley /*@
9586adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9587adc21957SMatthew G. Knepley 
9588adc21957SMatthew G. Knepley   Not collective
9589adc21957SMatthew G. Knepley 
9590adc21957SMatthew G. Knepley   Input Parameter:
9591adc21957SMatthew G. Knepley . dm - The DM
9592adc21957SMatthew G. Knepley 
9593adc21957SMatthew G. Knepley   Output Parameter:
9594adc21957SMatthew G. Knepley . reorder - Flag for reordering
9595adc21957SMatthew G. Knepley 
9596adc21957SMatthew G. Knepley   Level: intermediate
9597adc21957SMatthew G. Knepley 
9598adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9599adc21957SMatthew G. Knepley @*/
9600adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9601adc21957SMatthew G. Knepley {
9602adc21957SMatthew G. Knepley   PetscFunctionBegin;
9603adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9604adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9605adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9606adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9607adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9608adc21957SMatthew G. Knepley }
9609adc21957SMatthew G. Knepley 
9610cc4c1da9SBarry Smith /*@
9611adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9612adc21957SMatthew G. Knepley 
9613adc21957SMatthew G. Knepley   Logically collective
9614adc21957SMatthew G. Knepley 
9615adc21957SMatthew G. Knepley   Input Parameters:
9616adc21957SMatthew G. Knepley + dm      - The DM
9617adc21957SMatthew G. Knepley - reorder - The reordering method
9618adc21957SMatthew G. Knepley 
9619adc21957SMatthew G. Knepley   Level: intermediate
9620adc21957SMatthew G. Knepley 
9621adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9622adc21957SMatthew G. Knepley @*/
9623adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9624adc21957SMatthew G. Knepley {
9625adc21957SMatthew G. Knepley   PetscFunctionBegin;
9626adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9627adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9628adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9629adc21957SMatthew G. Knepley }
9630adc21957SMatthew G. Knepley 
9631cc4c1da9SBarry Smith /*@
9632adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9633adc21957SMatthew G. Knepley 
9634adc21957SMatthew G. Knepley   Not collective
9635adc21957SMatthew G. Knepley 
9636adc21957SMatthew G. Knepley   Input Parameter:
9637adc21957SMatthew G. Knepley . dm - The DM
9638adc21957SMatthew G. Knepley 
9639adc21957SMatthew G. Knepley   Output Parameter:
9640adc21957SMatthew G. Knepley . reorder - The reordering method
9641adc21957SMatthew G. Knepley 
9642adc21957SMatthew G. Knepley   Level: intermediate
9643adc21957SMatthew G. Knepley 
9644adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9645adc21957SMatthew G. Knepley @*/
9646adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9647adc21957SMatthew G. Knepley {
9648adc21957SMatthew G. Knepley   PetscFunctionBegin;
9649adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9650adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9651adc21957SMatthew G. Knepley   *reorder = NULL;
9652adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9653adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9654adc21957SMatthew G. Knepley }
9655