xref: /petsc/src/dm/interface/dm.c (revision 4e8208cbcbc709572b8abe32f33c78b69c819375)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>      /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h> /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6d2b2dc1eSMatthew G. Knepley #include <petscdmceed.h>
7f19dbd58SToby Isaac #include <petscdmfield.h>
80c312b8eSJed Brown #include <petscsf.h>
92764a2aaSMatthew G. Knepley #include <petscds.h>
1047c6ae99SBarry Smith 
11f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
12f918ec44SMatthew G. Knepley   #include <petscfeceed.h>
13f918ec44SMatthew G. Knepley #endif
14f918ec44SMatthew G. Knepley 
15732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
16d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
1742d0c57aSJames Wright PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_CreateMassMatrix, DM_Load, DM_View, DM_AdaptInterpolator, DM_ProjectFunction;
1867a56275SMatthew G Knepley 
19ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
2078e542f3SJed Brown const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "LOWER_BOUND", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "UPPER_BOUND", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType",
2178e542f3SJed Brown                                                 "DM_BC_",  NULL};
220e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
23476787b7SMatthew G. Knepley const char *const DMPolytopeTypes[] =
24476787b7SMatthew 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",
25476787b7SMatthew G. Knepley    "unknown", "unknown_cell", "unknown_face",   "invalid",  "DMPolytopeType", "DM_POLYTOPE_", NULL};
262cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2760c22052SBarry Smith 
28a4121054SBarry Smith /*@
29bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
300b4b7b1cSBarry Smith   algebraic solvers, time integrators, and optimization algorithms in PETSc.
31a4121054SBarry Smith 
32d083f849SBarry Smith   Collective
33a4121054SBarry Smith 
34a4121054SBarry Smith   Input Parameter:
35bb7acecfSBarry Smith . comm - The communicator for the `DM` object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Output Parameter:
38bb7acecfSBarry Smith . dm - The `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Level: beginner
41a4121054SBarry Smith 
42bb7acecfSBarry Smith   Notes:
43bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
44bb7acecfSBarry Smith 
45bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
460b4b7b1cSBarry Smith   error when you try to use the `dm`.
470b4b7b1cSBarry Smith 
480b4b7b1cSBarry Smith   `DM` is an orphan initialism or orphan acronym, the letters have no meaning and never did.
49bb7acecfSBarry Smith 
501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
51a4121054SBarry Smith @*/
DMCreate(MPI_Comm comm,DM * dm)52d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
53d71ae5a4SJacob Faibussowitsch {
54a4121054SBarry Smith   DM      v;
55e5e52638SMatthew G. Knepley   PetscDS ds;
56a4121054SBarry Smith 
57a4121054SBarry Smith   PetscFunctionBegin;
584f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
59377f809aSBarry Smith 
609566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
6262e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6349be4549SMatthew G. Knepley   v->setupcalled                          = PETSC_FALSE;
6449be4549SMatthew G. Knepley   v->setfromoptionscalled                 = PETSC_FALSE;
650298fd71SBarry Smith   v->ltogmap                              = NULL;
66a4ea9b21SRichard Tran Mills   v->bind_below                           = 0;
671411c6eeSJed Brown   v->bs                                   = 1;
68171400e9SBarry Smith   v->coloringtype                         = IS_COLORING_GLOBAL;
699566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
709566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
71c58f1c22SToby Isaac   v->labels                    = NULL;
7234aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7334aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
74c58f1c22SToby Isaac   v->depthLabel                = NULL;
75ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
761bb6d2a8SBarry Smith   v->localSection              = NULL;
771bb6d2a8SBarry Smith   v->globalSection             = NULL;
783b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
793b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8079769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
816858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
826858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
836858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8496173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
859566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
8607218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
879566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
889566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
8914f150ffSMatthew G. Knepley   v->dmBC              = NULL;
90a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
91f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
92cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
939566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
949566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
954a7a4c06SLawrence Mitchell 
961411c6eeSJed Brown   *dm = v;
973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
98a4121054SBarry Smith }
99a4121054SBarry Smith 
10038221697SMatthew G. Knepley /*@
101bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
10238221697SMatthew G. Knepley 
103d083f849SBarry Smith   Collective
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley   Input Parameter:
106bb7acecfSBarry Smith . dm - The original `DM` object
10738221697SMatthew G. Knepley 
10838221697SMatthew G. Knepley   Output Parameter:
109bb7acecfSBarry Smith . newdm - The new `DM` object
11038221697SMatthew G. Knepley 
11138221697SMatthew G. Knepley   Level: beginner
11238221697SMatthew G. Knepley 
1131cb8cacdSPatrick Sanan   Notes:
114bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
115bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
116bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1171bb6d2a8SBarry Smith 
118bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
11989706ed2SPatrick Sanan 
120bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
121bb7acecfSBarry Smith 
12260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
12338221697SMatthew G. Knepley @*/
DMClone(DM dm,DM * newdm)124d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
125d71ae5a4SJacob Faibussowitsch {
12638221697SMatthew G. Knepley   PetscSF              sf;
12738221697SMatthew G. Knepley   Vec                  coords;
12838221697SMatthew G. Knepley   void                *ctx;
129ec196627SMatthew G. Knepley   MatOrderingType      otype;
130ec196627SMatthew G. Knepley   DMReorderDefaultFlag flg;
1316858538eSMatthew G. Knepley   PetscInt             dim, cdim, i;
13238221697SMatthew G. Knepley 
13338221697SMatthew G. Knepley   PetscFunctionBegin;
13438221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1354f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1369566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1379566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
138ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
139ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
140c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
141fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1429566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1439566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1449566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1459566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1469566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1479566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
148dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1493f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1509566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1519566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1529566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1539566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
154ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetDefault(dm, &flg));
155ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetDefault(*newdm, flg));
156ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetType(dm, &otype));
157ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetType(*newdm, otype));
1586858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1596858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
160be4c1c3eSMatthew G. Knepley       DM           ncdm;
161be4c1c3eSMatthew G. Knepley       PetscSection cs;
1625a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
163be4c1c3eSMatthew G. Knepley 
1646858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1659566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
166462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1675a0206caSToby Isaac       if (pEndMax >= 0) {
1686858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1696858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1709566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1715e50ca95SJames Wright         if (dm->coordinates[i].dm->periodic.setup) {
1725e50ca95SJames Wright           ncdm->periodic.setup = dm->coordinates[i].dm->periodic.setup;
1735e50ca95SJames Wright           PetscCall(ncdm->periodic.setup(ncdm));
1745e50ca95SJames Wright         }
1756858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1766858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1779566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
178be4c1c3eSMatthew G. Knepley       }
179be4c1c3eSMatthew G. Knepley     }
1806858538eSMatthew G. Knepley   }
1819566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1829566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1839566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18438221697SMatthew G. Knepley   if (coords) {
1859566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18638221697SMatthew G. Knepley   } else {
1879566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1889566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18938221697SMatthew G. Knepley   }
1906858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1916858538eSMatthew G. Knepley   if (coords) {
1926858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1936858538eSMatthew G. Knepley   } else {
1946858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1956858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1966858538eSMatthew G. Knepley   }
19790b157c4SStefano Zampini   {
1984fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1996858538eSMatthew G. Knepley 
2004fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2014fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
202c6b900c6SMatthew G. Knepley   }
20334aa8a36SMatthew G. Knepley   {
20434aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20534aa8a36SMatthew G. Knepley 
2069566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2079566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20834aa8a36SMatthew G. Knepley   }
2093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21038221697SMatthew G. Knepley }
21138221697SMatthew G. Knepley 
2125d83a8b1SBarry Smith /*@
2135d83a8b1SBarry Smith   DMSetVecType - Sets the type of vector to be created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2149a42bb27SBarry Smith 
21520f4b53cSBarry Smith   Logically Collective
2169a42bb27SBarry Smith 
217147403d9SBarry Smith   Input Parameters:
21832546409SMatthew G. Knepley + dm    - initial distributed array
219bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2209a42bb27SBarry Smith 
22120f4b53cSBarry Smith   Options Database Key:
222147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2239a42bb27SBarry Smith 
2249a42bb27SBarry Smith   Level: intermediate
2259a42bb27SBarry Smith 
22660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
227bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2289a42bb27SBarry Smith @*/
DMSetVecType(DM dm,VecType ctype)22932546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
230d71ae5a4SJacob Faibussowitsch {
23132546409SMatthew G. Knepley   char *tmp;
23232546409SMatthew G. Knepley 
2339a42bb27SBarry Smith   PetscFunctionBegin;
23432546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23532546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
23632546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
23732546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
23832546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2409a42bb27SBarry Smith }
2419a42bb27SBarry Smith 
2425d83a8b1SBarry Smith /*@
243bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
244c0dedaeaSBarry Smith 
24520f4b53cSBarry Smith   Logically Collective
246c0dedaeaSBarry Smith 
247c0dedaeaSBarry Smith   Input Parameter:
248c0dedaeaSBarry Smith . da - initial distributed array
249c0dedaeaSBarry Smith 
250c0dedaeaSBarry Smith   Output Parameter:
251c0dedaeaSBarry Smith . ctype - the vector type
252c0dedaeaSBarry Smith 
253c0dedaeaSBarry Smith   Level: intermediate
254c0dedaeaSBarry Smith 
25560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
256c0dedaeaSBarry Smith @*/
DMGetVecType(DM da,VecType * ctype)257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
258d71ae5a4SJacob Faibussowitsch {
259c0dedaeaSBarry Smith   PetscFunctionBegin;
260c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
261c0dedaeaSBarry Smith   *ctype = da->vectype;
2623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
263c0dedaeaSBarry Smith }
264c0dedaeaSBarry Smith 
2655f1ad066SMatthew G Knepley /*@
266bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2675f1ad066SMatthew G Knepley 
26820f4b53cSBarry Smith   Not Collective
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Input Parameter:
271bb7acecfSBarry Smith . v - The `Vec`
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley   Output Parameter:
274bb7acecfSBarry Smith . dm - The `DM`
2755f1ad066SMatthew G Knepley 
2765f1ad066SMatthew G Knepley   Level: intermediate
2775f1ad066SMatthew G Knepley 
278bb7acecfSBarry Smith   Note:
279bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
280bb7acecfSBarry Smith 
2811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2825f1ad066SMatthew G Knepley @*/
VecGetDM(Vec v,DM * dm)283d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
284d71ae5a4SJacob Faibussowitsch {
2855f1ad066SMatthew G Knepley   PetscFunctionBegin;
2865f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2874f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2889566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2905f1ad066SMatthew G Knepley }
2915f1ad066SMatthew G Knepley 
2925f1ad066SMatthew G Knepley /*@
293bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2945f1ad066SMatthew G Knepley 
29520f4b53cSBarry Smith   Not Collective
2965f1ad066SMatthew G Knepley 
2975f1ad066SMatthew G Knepley   Input Parameters:
298bb7acecfSBarry Smith + v  - The `Vec`
299bb7acecfSBarry Smith - dm - The `DM`
3005f1ad066SMatthew G Knepley 
30120f4b53cSBarry Smith   Level: developer
30220f4b53cSBarry Smith 
30373ff1848SBarry Smith   Notes:
304bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
305d9805387SMatthew G. Knepley 
306bb7acecfSBarry 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.
307bb7acecfSBarry Smith 
3081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3095f1ad066SMatthew G Knepley @*/
VecSetDM(Vec v,DM dm)310d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
311d71ae5a4SJacob Faibussowitsch {
3125f1ad066SMatthew G Knepley   PetscFunctionBegin;
3135f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
314d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3159566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3175f1ad066SMatthew G Knepley }
3185f1ad066SMatthew G Knepley 
319cc4c1da9SBarry Smith /*@
320bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3218f1509bcSBarry Smith 
32220f4b53cSBarry Smith   Logically Collective
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith   Input Parameters:
325bb7acecfSBarry Smith + dm    - the `DM` context
3268f1509bcSBarry Smith - ctype - the matrix type
3278f1509bcSBarry Smith 
32820f4b53cSBarry Smith   Options Database Key:
3298f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3308f1509bcSBarry Smith 
3318f1509bcSBarry Smith   Level: intermediate
3328f1509bcSBarry Smith 
3331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
334bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3358f1509bcSBarry Smith @*/
DMSetISColoringType(DM dm,ISColoringType ctype)336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
337d71ae5a4SJacob Faibussowitsch {
3388f1509bcSBarry Smith   PetscFunctionBegin;
3398f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3408f1509bcSBarry Smith   dm->coloringtype = ctype;
3413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3428f1509bcSBarry Smith }
3438f1509bcSBarry Smith 
344cc4c1da9SBarry Smith /*@
345bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
346521d9a4cSLisandro Dalcin 
34720f4b53cSBarry Smith   Logically Collective
348521d9a4cSLisandro Dalcin 
349521d9a4cSLisandro Dalcin   Input Parameter:
350bb7acecfSBarry Smith . dm - the `DM` context
3518f1509bcSBarry Smith 
3528f1509bcSBarry Smith   Output Parameter:
3538f1509bcSBarry Smith . ctype - the matrix type
3548f1509bcSBarry Smith 
35520f4b53cSBarry Smith   Options Database Key:
3568f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3578f1509bcSBarry Smith 
3588f1509bcSBarry Smith   Level: intermediate
3598f1509bcSBarry Smith 
3601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
36142747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3628f1509bcSBarry Smith @*/
DMGetISColoringType(DM dm,ISColoringType * ctype)363d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
364d71ae5a4SJacob Faibussowitsch {
3658f1509bcSBarry Smith   PetscFunctionBegin;
3668f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3678f1509bcSBarry Smith   *ctype = dm->coloringtype;
3683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3698f1509bcSBarry Smith }
3708f1509bcSBarry Smith 
371cc4c1da9SBarry Smith /*@
372bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3738f1509bcSBarry Smith 
37420f4b53cSBarry Smith   Logically Collective
3758f1509bcSBarry Smith 
3768f1509bcSBarry Smith   Input Parameters:
377bb7acecfSBarry Smith + dm    - the `DM` context
378bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
379521d9a4cSLisandro Dalcin 
38020f4b53cSBarry Smith   Options Database Key:
381bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
382521d9a4cSLisandro Dalcin 
383521d9a4cSLisandro Dalcin   Level: intermediate
384521d9a4cSLisandro Dalcin 
38542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
386521d9a4cSLisandro Dalcin @*/
DMSetMatType(DM dm,MatType ctype)387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
388d71ae5a4SJacob Faibussowitsch {
38932546409SMatthew G. Knepley   char *tmp;
39032546409SMatthew G. Knepley 
391521d9a4cSLisandro Dalcin   PetscFunctionBegin;
392521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39332546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
39432546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
3959566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
39632546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
3973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
398521d9a4cSLisandro Dalcin }
399521d9a4cSLisandro Dalcin 
400cc4c1da9SBarry Smith /*@
40120f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
402c0dedaeaSBarry Smith 
40320f4b53cSBarry Smith   Logically Collective
404c0dedaeaSBarry Smith 
405c0dedaeaSBarry Smith   Input Parameter:
406bb7acecfSBarry Smith . dm - the `DM` context
407c0dedaeaSBarry Smith 
408c0dedaeaSBarry Smith   Output Parameter:
409c0dedaeaSBarry Smith . ctype - the matrix type
410c0dedaeaSBarry Smith 
411c0dedaeaSBarry Smith   Level: intermediate
412c0dedaeaSBarry Smith 
41342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
414c0dedaeaSBarry Smith @*/
DMGetMatType(DM dm,MatType * ctype)415d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
416d71ae5a4SJacob Faibussowitsch {
417c0dedaeaSBarry Smith   PetscFunctionBegin;
418c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
419c0dedaeaSBarry Smith   *ctype = dm->mattype;
4203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
421c0dedaeaSBarry Smith }
422c0dedaeaSBarry Smith 
423c688c046SMatthew G Knepley /*@
424bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
425c688c046SMatthew G Knepley 
42620f4b53cSBarry Smith   Not Collective
427c688c046SMatthew G Knepley 
428c688c046SMatthew G Knepley   Input Parameter:
429bb7acecfSBarry Smith . A - The `Mat`
430c688c046SMatthew G Knepley 
431c688c046SMatthew G Knepley   Output Parameter:
432bb7acecfSBarry Smith . dm - The `DM`
433c688c046SMatthew G Knepley 
434c688c046SMatthew G Knepley   Level: intermediate
435c688c046SMatthew G Knepley 
436bb7acecfSBarry Smith   Note:
437bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
438bb7acecfSBarry Smith 
43973ff1848SBarry Smith   Developer Note:
440bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4418f1509bcSBarry Smith 
4421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
443c688c046SMatthew G Knepley @*/
MatGetDM(Mat A,DM * dm)444d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
445d71ae5a4SJacob Faibussowitsch {
446c688c046SMatthew G Knepley   PetscFunctionBegin;
447c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4484f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4499566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
451c688c046SMatthew G Knepley }
452c688c046SMatthew G Knepley 
453c688c046SMatthew G Knepley /*@
454bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
455c688c046SMatthew G Knepley 
45620f4b53cSBarry Smith   Not Collective
457c688c046SMatthew G Knepley 
458c688c046SMatthew G Knepley   Input Parameters:
45920f4b53cSBarry Smith + A  - The `Mat`
46020f4b53cSBarry Smith - dm - The `DM`
461c688c046SMatthew G Knepley 
462bb7acecfSBarry Smith   Level: developer
463c688c046SMatthew G Knepley 
464bb7acecfSBarry Smith   Note:
465bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
466bb7acecfSBarry Smith 
46773ff1848SBarry Smith   Developer Note:
468bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
469bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4708f1509bcSBarry Smith 
4711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
472c688c046SMatthew G Knepley @*/
MatSetDM(Mat A,DM dm)473d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
474d71ae5a4SJacob Faibussowitsch {
475c688c046SMatthew G Knepley   PetscFunctionBegin;
476c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4778865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4789566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
480c688c046SMatthew G Knepley }
481c688c046SMatthew G Knepley 
482cc4c1da9SBarry Smith /*@
483bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4849a42bb27SBarry Smith 
48520f4b53cSBarry Smith   Logically Collective
4869a42bb27SBarry Smith 
487d8d19677SJose E. Roman   Input Parameters:
48860225df5SJacob Faibussowitsch + dm     - the `DM` context
489bb7acecfSBarry Smith - prefix - the prefix to prepend
4909a42bb27SBarry Smith 
49120f4b53cSBarry Smith   Level: advanced
49220f4b53cSBarry Smith 
49320f4b53cSBarry Smith   Note:
4949a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
4959a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
4969a42bb27SBarry Smith 
4971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4989a42bb27SBarry Smith @*/
DMSetOptionsPrefix(DM dm,const char prefix[])499d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
500d71ae5a4SJacob Faibussowitsch {
5019a42bb27SBarry Smith   PetscFunctionBegin;
5029a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5039566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5041baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5051baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5079a42bb27SBarry Smith }
5089a42bb27SBarry Smith 
509cc4c1da9SBarry Smith /*@
510da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
511bb7acecfSBarry Smith   `DM` options in the options database.
51231697293SDave May 
51320f4b53cSBarry Smith   Logically Collective
51431697293SDave May 
51531697293SDave May   Input Parameters:
516bb7acecfSBarry Smith + dm     - the `DM` context
517bb7acecfSBarry Smith - prefix - the string to append to the current prefix
51831697293SDave May 
51920f4b53cSBarry Smith   Level: advanced
52020f4b53cSBarry Smith 
52120f4b53cSBarry Smith   Note:
522bb7acecfSBarry 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.
52331697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
52431697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
52531697293SDave May 
5261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
52731697293SDave May @*/
DMAppendOptionsPrefix(DM dm,const char prefix[])528d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
529d71ae5a4SJacob Faibussowitsch {
53031697293SDave May   PetscFunctionBegin;
53131697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5329566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
53431697293SDave May }
53531697293SDave May 
536cc4c1da9SBarry Smith /*@
53731697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
538bb7acecfSBarry Smith   DM options in the options database.
53931697293SDave May 
54031697293SDave May   Not Collective
54131697293SDave May 
5422fe279fdSBarry Smith   Input Parameter:
543bb7acecfSBarry Smith . dm - the `DM` context
54431697293SDave May 
5452fe279fdSBarry Smith   Output Parameter:
54631697293SDave May . prefix - pointer to the prefix string used is returned
54731697293SDave May 
54831697293SDave May   Level: advanced
54931697293SDave May 
5501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
55131697293SDave May @*/
DMGetOptionsPrefix(DM dm,const char * prefix[])552d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
553d71ae5a4SJacob Faibussowitsch {
55431697293SDave May   PetscFunctionBegin;
55531697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5569566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55831697293SDave May }
55931697293SDave May 
DMCountNonCyclicReferences_Internal(DM dm,PetscBool recurseCoarse,PetscBool recurseFine,PetscInt * ncrefct)56062e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
561d71ae5a4SJacob Faibussowitsch {
5626eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
56388bdff64SToby Isaac 
56488bdff64SToby Isaac   PetscFunctionBegin;
565aab5bcd8SJed Brown   *ncrefct = 0;
56688bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56788bdff64SToby Isaac     refct--;
56888bdff64SToby Isaac     if (recurseCoarse) {
56988bdff64SToby Isaac       PetscInt coarseCount;
57088bdff64SToby Isaac 
57162e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
57288bdff64SToby Isaac       refct += coarseCount;
57388bdff64SToby Isaac     }
57488bdff64SToby Isaac   }
57588bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
57688bdff64SToby Isaac     refct--;
57788bdff64SToby Isaac     if (recurseFine) {
57888bdff64SToby Isaac       PetscInt fineCount;
57988bdff64SToby Isaac 
58062e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
58188bdff64SToby Isaac       refct += fineCount;
58288bdff64SToby Isaac     }
58388bdff64SToby Isaac   }
58488bdff64SToby Isaac   *ncrefct = refct;
5853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58688bdff64SToby Isaac }
58788bdff64SToby Isaac 
58862e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
DMCountNonCyclicReferences(PetscObject dm,PetscInt * ncrefct)58962e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
59062e5d2d2SJDBetteridge {
59162e5d2d2SJDBetteridge   PetscFunctionBegin;
59262e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
5933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59462e5d2d2SJDBetteridge }
59562e5d2d2SJDBetteridge 
DMDestroyLabelLinkList_Internal(DM dm)596d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
597d71ae5a4SJacob Faibussowitsch {
5985d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
599354557abSToby Isaac 
600354557abSToby Isaac   PetscFunctionBegin;
601354557abSToby Isaac   /* destroy the labels */
602354557abSToby Isaac   while (next) {
603354557abSToby Isaac     DMLabelLink tmp = next->next;
604354557abSToby Isaac 
6055d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
606ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6079566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6089566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
609354557abSToby Isaac     next = tmp;
610354557abSToby Isaac   }
6115d80c0bfSVaclav Hapla   dm->labels = NULL;
6123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
613354557abSToby Isaac }
614354557abSToby Isaac 
DMDestroyCoordinates_Private(DMCoordinates * c)61566976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
616d71ae5a4SJacob Faibussowitsch {
6176858538eSMatthew G. Knepley   PetscFunctionBegin;
6186858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6196858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6206858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6216858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6226858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6246858538eSMatthew G. Knepley }
6256858538eSMatthew G. Knepley 
6260764c050SBarry Smith /*@
627bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
62847c6ae99SBarry Smith 
62920f4b53cSBarry Smith   Collective
63047c6ae99SBarry Smith 
63147c6ae99SBarry Smith   Input Parameter:
632bb7acecfSBarry Smith . dm - the `DM` object to destroy
63347c6ae99SBarry Smith 
63447c6ae99SBarry Smith   Level: developer
63547c6ae99SBarry Smith 
6361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
63747c6ae99SBarry Smith @*/
DMDestroy(DM * dm)638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
639d71ae5a4SJacob Faibussowitsch {
6406eb26441SStefano Zampini   PetscInt cnt;
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   PetscFunctionBegin;
6433ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
644f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*dm, DM_CLASSID, 1);
64587e657c6SBarry Smith 
64688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
64762e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
648f4f49eeaSPierre Jolivet   --((PetscObject)*dm)->refct;
6499371c9d4SSatish Balay   if (--cnt > 0) {
6509371c9d4SSatish Balay     *dm = NULL;
6513ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6529371c9d4SSatish Balay   }
653f4f49eeaSPierre Jolivet   if (((PetscObject)*dm)->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
654f4f49eeaSPierre Jolivet   ((PetscObject)*dm)->refct = 0;
6556eb26441SStefano Zampini 
6569566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6579566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
658974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
659974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6602348bcf4SPeter Brune 
661b17ce1afSJed Brown   /* Destroy the list of hooks */
662c833c3b5SJed Brown   {
663c833c3b5SJed Brown     DMCoarsenHookLink link, next;
664b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
665b17ce1afSJed Brown       next = link->next;
6669566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
667b17ce1afSJed Brown     }
6680298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
669c833c3b5SJed Brown   }
670c833c3b5SJed Brown   {
671c833c3b5SJed Brown     DMRefineHookLink link, next;
672c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
673c833c3b5SJed Brown       next = link->next;
6749566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
675c833c3b5SJed Brown     }
6760298fd71SBarry Smith     (*dm)->refinehook = NULL;
677c833c3b5SJed Brown   }
678be081cd6SPeter Brune   {
679be081cd6SPeter Brune     DMSubDomainHookLink link, next;
680be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
681be081cd6SPeter Brune       next = link->next;
6829566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
683be081cd6SPeter Brune     }
6840298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
685be081cd6SPeter Brune   }
686baf369e7SPeter Brune   {
687baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
688baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
689baf369e7SPeter Brune       next = link->next;
6909566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
691baf369e7SPeter Brune     }
6920298fd71SBarry Smith     (*dm)->gtolhook = NULL;
693baf369e7SPeter Brune   }
694d4d07f1eSToby Isaac   {
695d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
696d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
697d4d07f1eSToby Isaac       next = link->next;
6989566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
699d4d07f1eSToby Isaac     }
700d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
701d4d07f1eSToby Isaac   }
702aa1993deSMatthew G Knepley   /* Destroy the work arrays */
703aa1993deSMatthew G Knepley   {
704aa1993deSMatthew G Knepley     DMWorkLink link, next;
705835f2295SStefano Zampini     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (*dm)->workout->mem);
706aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
707aa1993deSMatthew G Knepley       next = link->next;
7089566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7099566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
710aa1993deSMatthew G Knepley     }
7110298fd71SBarry Smith     (*dm)->workin = NULL;
712aa1993deSMatthew G Knepley   }
713c58f1c22SToby Isaac   /* destroy the labels */
7149566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
715f4cdcedcSVaclav Hapla   /* destroy the fields */
7169566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
717f4cdcedcSVaclav Hapla   /* destroy the boundaries */
718e6f8dbb6SToby Isaac   {
719e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
720e6f8dbb6SToby Isaac     while (next) {
721e6f8dbb6SToby Isaac       DMBoundary b = next;
722e6f8dbb6SToby Isaac 
723e6f8dbb6SToby Isaac       next = b->next;
7249566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
725e6f8dbb6SToby Isaac     }
726e6f8dbb6SToby Isaac   }
727b17ce1afSJed Brown 
7289566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7299566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7309566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
73152536dc3SBarry Smith 
73248a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7339566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7349566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7359566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7369566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
73788ed4aceSMatthew G Knepley 
7389566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7399566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
740adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7419566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7429566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7439566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7449566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7459566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
74648a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7479566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
748e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7499566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
75048a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7516eb26441SStefano Zampini 
7529566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
75348a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7549566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7554fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7569566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7579566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7584758e3ceSMatthew G. Knepley   PetscCall(PetscFree2((*dm)->nullspaceConstructors, (*dm)->nearnullspaceConstructors));
7596858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7606858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7619566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7629566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
764ddedc8f6SJames Wright   for (PetscInt i = 0; i < (*dm)->periodic.num_affines; i++) {
765ddedc8f6SJames Wright     PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local[i]));
766ddedc8f6SJames Wright     PetscCall(VecDestroy(&(*dm)->periodic.affine[i]));
767ddedc8f6SJames Wright   }
768ddedc8f6SJames Wright   if ((*dm)->periodic.num_affines > 0) PetscCall(PetscFree2((*dm)->periodic.affine_to_local, (*dm)->periodic.affine));
7696636e97aSMatthew G Knepley 
7709566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7719566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
772e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7739566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
774732e2eb9SMatthew G Knepley 
775213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7769566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
777d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
778f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7799566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7809566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
781f918ec44SMatthew G. Knepley #endif
782435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7839566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78547c6ae99SBarry Smith }
78647c6ae99SBarry Smith 
787d7bf68aeSBarry Smith /*@
788bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
789d7bf68aeSBarry Smith 
79020f4b53cSBarry Smith   Collective
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith   Input Parameter:
793bb7acecfSBarry Smith . dm - the `DM` object to setup
794d7bf68aeSBarry Smith 
795bb7acecfSBarry Smith   Level: intermediate
796d7bf68aeSBarry Smith 
797bb7acecfSBarry Smith   Note:
798bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
799bb7acecfSBarry Smith 
8001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
801d7bf68aeSBarry Smith @*/
DMSetUp(DM dm)802d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
803d71ae5a4SJacob Faibussowitsch {
804d7bf68aeSBarry Smith   PetscFunctionBegin;
805171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8063ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
807dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8088387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
810d7bf68aeSBarry Smith }
811d7bf68aeSBarry Smith 
812d7bf68aeSBarry Smith /*@
813bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
814d7bf68aeSBarry Smith 
81520f4b53cSBarry Smith   Collective
816d7bf68aeSBarry Smith 
817d7bf68aeSBarry Smith   Input Parameter:
818bb7acecfSBarry Smith . dm - the `DM` object to set options for
819d7bf68aeSBarry Smith 
82020f4b53cSBarry Smith   Options Database Keys:
821bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
822bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
823bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
824a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
82520f4b53cSBarry Smith . -dm_bind_below <n>                                 - bind (force execution on CPU) for `Vec` and `Mat` objects with local size (number of vector entries or matrix rows) below n; currently only supported for `DMDA`
82622d6dc08SStefano Zampini . -dm_plex_option_phases <ph0_, ph1_, ...>           - List of prefixes for option processing phases
82720f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8289318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
829cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8305dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8319318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8329318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8339318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
834bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
835bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
836b9da1bb3SMatthew G. Knepley . -dm_plex_orient <bool>                             - `PETSC_TRUE` turns on topological orientation (flipping edges and faces)
8379318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
838be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
839ac9d17c7SMatthew G. Knepley . -dm_plex_coordinate_dim <dim>                      - Change the coordinate dimension of a mesh (usually given with cdm_ prefix)
840be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
841be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8429318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8439318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8449318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
845bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8469318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8479318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8489318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8499318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
850bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8519318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8529318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8539318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8549318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
855bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
85661f058f9SMatthew G. Knepley . -dm_plex_save_transform <bool>                     - Save the `DMPlexTransform` that produced this mesh
857d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
858d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
859d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
860d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
861d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
862909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
863909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8649318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8659318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8669318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
86720f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
868d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
86920f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
870bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
871bb7acecfSBarry 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()`
872bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
873bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
874bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
875384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
876d7bf68aeSBarry Smith 
87795eb5ee5SVaclav Hapla   Level: intermediate
87895eb5ee5SVaclav Hapla 
8798e704042SBarry Smith   Note:
8808e704042SBarry Smith   For some `DMType` such as `DMDA` this cannot be called after `DMSetUp()` has been called.
8818e704042SBarry Smith 
8821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
883bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
8848e704042SBarry Smith          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`, `DMSetUp()`
885d7bf68aeSBarry Smith @*/
DMSetFromOptions(DM dm)886d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
887d71ae5a4SJacob Faibussowitsch {
8887781c08eSBarry Smith   char      typeName[256];
889ca266f36SBarry Smith   PetscBool flg;
890d7bf68aeSBarry Smith 
891d7bf68aeSBarry Smith   PetscFunctionBegin;
892171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8949566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8959566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
896dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
897d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
8999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9001baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9021baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
903863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9059566063dSJacob 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));
906eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
907dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
908f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
909dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
910d0609cedSBarry Smith   PetscOptionsEnd();
9113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
912d7bf68aeSBarry Smith }
913d7bf68aeSBarry Smith 
914ffeef943SBarry Smith /*@
915bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
916fe2efc57SMark 
91720f4b53cSBarry Smith   Collective
918fe2efc57SMark 
919fe2efc57SMark   Input Parameters:
920bb7acecfSBarry Smith + dm   - the `DM` object
921ce78bad3SBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in `obj` is used)
92260225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
923fe2efc57SMark 
924fe2efc57SMark   Level: intermediate
925bb7acecfSBarry Smith 
926bb7acecfSBarry Smith   Note:
927bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
928bb7acecfSBarry Smith 
92960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
930fe2efc57SMark @*/
DMViewFromOptions(DM dm,PeOp PetscObject obj,const char name[])931ce78bad3SBarry Smith PetscErrorCode DMViewFromOptions(DM dm, PeOp PetscObject obj, const char name[])
932d71ae5a4SJacob Faibussowitsch {
933fe2efc57SMark   PetscFunctionBegin;
934fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9359566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
937fe2efc57SMark }
938fe2efc57SMark 
939ffeef943SBarry Smith /*@
940bb7acecfSBarry 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
941bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94247c6ae99SBarry Smith 
94320f4b53cSBarry Smith   Collective
94447c6ae99SBarry Smith 
945d8d19677SJose E. Roman   Input Parameters:
946bb7acecfSBarry Smith + dm - the `DM` object to view
94747c6ae99SBarry Smith - v  - the viewer
94847c6ae99SBarry Smith 
949c8a418c4SMatthew G. Knepley   Options Database Keys:
950c8a418c4SMatthew G. Knepley + -view_pyvista_warp <f>                 - Warps the mesh by the active scalar with factor f
951a36b0f0fSMatthew G. Knepley . -view_pyvista_clip <xl,xu,yl,yu,zl,zu> - Defines the clipping box
952a36b0f0fSMatthew G. Knepley . -dm_view_draw_line_color <int>         - Specify the X-window color for cell borders
953a36b0f0fSMatthew G. Knepley . -dm_view_draw_cell_color <int>         - Specify the X-window color for cells
954a36b0f0fSMatthew G. Knepley - -dm_view_draw_affine <bool>            - Flag to ignore high-order edges
955c8a418c4SMatthew G. Knepley 
95620f4b53cSBarry Smith   Level: beginner
95720f4b53cSBarry Smith 
95849c89c76SBlaise Bourdin   Notes:
95949c89c76SBlaise Bourdin 
96049c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWERHDF5` i.e. HDF5 format can be used with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` to save multiple `DMPLEX`
961bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
962bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
963cd7e8a5eSksagiyam 
96449c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWEREXODUSII` i.e. ExodusII format assumes that element blocks (mapped to "Cell sets" labels)
96549c89c76SBlaise Bourdin   consists of sequentially numbered cells.
96649c89c76SBlaise Bourdin 
96749c89c76SBlaise Bourdin   If `dm` has been distributed, only the part of the `DM` on MPI rank 0 (including "ghost" cells and vertices) will be written.
96849c89c76SBlaise Bourdin 
969c8a418c4SMatthew G. Knepley   Only TRI, TET, QUAD, and HEX cells are supported in ExodusII.
97049c89c76SBlaise Bourdin 
97149c89c76SBlaise 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.
97249c89c76SBlaise Bourdin   The order of the mesh shall be set using `PetscViewerExodusIISetOrder()`
97349c89c76SBlaise Bourdin 
974d7c1f440SPierre Jolivet   Variable names can be set and queried using `PetscViewerExodusII[Set/Get][Nodal/Zonal]VariableNames[s]`.
97549c89c76SBlaise Bourdin 
9761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
97747c6ae99SBarry Smith @*/
DMView(DM dm,PetscViewer v)978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
979d71ae5a4SJacob Faibussowitsch {
98032c0f0efSBarry Smith   PetscBool         isbinary;
98176a8abe0SBarry Smith   PetscMPIInt       size;
98276a8abe0SBarry Smith   PetscViewerFormat format;
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith   PetscFunctionBegin;
985171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98648a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
987b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
98874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
98974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
99074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
99174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
99274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
99374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
99474903a4fSStefano Zampini      in an error here */
99574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9969566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
997b1b135c8SBarry Smith 
99842d0c57aSJames Wright   PetscCall(PetscLogEventBegin(DM_View, v, 0, 0, 0));
9999566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
10009566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
10013ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
10029566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
10039566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
100432c0f0efSBarry Smith   if (isbinary) {
100555849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
100632c0f0efSBarry Smith     char     type[256];
100732c0f0efSBarry Smith 
10089566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
1009c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
10109566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
101132c0f0efSBarry Smith   }
1012dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
101342d0c57aSJames Wright   PetscCall(PetscLogEventEnd(DM_View, v, 0, 0, 0));
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 @*/
DMCreateGlobalVector(DM dm,Vec * vec)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 @*/
DMCreateLocalVector(DM dm,Vec * vec)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 @*/
DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping * ltog)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 @*/
DMGetBlockSize(DM dm,PetscInt * bs)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 @*/
DMCreateInterpolation(DM dmc,DM dmf,Mat * mat,Vec * vec)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 @*/
DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec * scale)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 @*/
DMCreateRestriction(DM dmc,DM dmf,Mat * mat)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 @*/
DMCreateInjection(DM dac,DM daf,Mat * mat)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 @*/
DMCreateMassMatrix(DM dmc,DM dmf,Mat * mat)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 @*/
DMCreateMassMatrixLumped(DM dm,Vec * llm,Vec * lm)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 /*@
14321898fd5cSMatthew G. Knepley   DMCreateGradientMatrix - Gets the gradient matrix between two `DM` objects, M_(ic)j = \int \partial_c \phi_i \psi_j where the \phi are Galerkin basis functions for a Galerkin finite element model on the `DM`
14331898fd5cSMatthew G. Knepley 
14341898fd5cSMatthew G. Knepley   Collective
14351898fd5cSMatthew G. Knepley 
14361898fd5cSMatthew G. Knepley   Input Parameters:
14371898fd5cSMatthew G. Knepley + dmc - the target `DM` object
14381898fd5cSMatthew G. Knepley - dmf - the source `DM` object, can be `NULL`
14391898fd5cSMatthew G. Knepley 
14401898fd5cSMatthew G. Knepley   Output Parameter:
14411898fd5cSMatthew G. Knepley . mat - the gradient matrix
14421898fd5cSMatthew G. Knepley 
14431898fd5cSMatthew G. Knepley   Level: developer
14441898fd5cSMatthew G. Knepley 
14451898fd5cSMatthew G. Knepley   Notes:
14461898fd5cSMatthew G. Knepley   For `DMPLEX` the finite element model for the `DM` must have been already provided.
14471898fd5cSMatthew G. Knepley 
14481898fd5cSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
14491898fd5cSMatthew G. Knepley @*/
DMCreateGradientMatrix(DM dmc,DM dmf,Mat * mat)14501898fd5cSMatthew G. Knepley PetscErrorCode DMCreateGradientMatrix(DM dmc, DM dmf, Mat *mat)
14511898fd5cSMatthew G. Knepley {
14521898fd5cSMatthew G. Knepley   PetscFunctionBegin;
14531898fd5cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
14541898fd5cSMatthew G. Knepley   if (!dmf) dmf = dmc;
14551898fd5cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
14561898fd5cSMatthew G. Knepley   PetscAssertPointer(mat, 3);
14571898fd5cSMatthew G. Knepley   PetscUseTypeMethod(dmc, creategradientmatrix, dmf, mat);
14581898fd5cSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
14591898fd5cSMatthew G. Knepley }
14601898fd5cSMatthew G. Knepley 
14611898fd5cSMatthew G. Knepley /*@
1462bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1463bb7acecfSBarry Smith   of a PDE on the `DM`.
146447c6ae99SBarry Smith 
146520f4b53cSBarry Smith   Collective
146647c6ae99SBarry Smith 
1467d8d19677SJose E. Roman   Input Parameters:
1468bb7acecfSBarry Smith + dm    - the `DM` object
1469bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
147047c6ae99SBarry Smith 
147147c6ae99SBarry Smith   Output Parameter:
147247c6ae99SBarry Smith . coloring - the coloring
147347c6ae99SBarry Smith 
14741bf8429eSBarry Smith   Level: developer
14751bf8429eSBarry Smith 
1476ec5066bdSBarry Smith   Notes:
1477bb7acecfSBarry 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
1478bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1479ec5066bdSBarry Smith 
1480bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14811bf8429eSBarry 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,
14821bf8429eSBarry Smith   otherwise an error will be generated.
1483ec5066bdSBarry Smith 
14841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1485aab9d709SJed Brown @*/
DMCreateColoring(DM dm,ISColoringType ctype,ISColoring * coloring)1486d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1487d71ae5a4SJacob Faibussowitsch {
148847c6ae99SBarry Smith   PetscFunctionBegin;
1489171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14904f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1491dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
149347c6ae99SBarry Smith }
149447c6ae99SBarry Smith 
1495b412c318SBarry Smith /*@
1496bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
149747c6ae99SBarry Smith 
149820f4b53cSBarry Smith   Collective
149947c6ae99SBarry Smith 
150047c6ae99SBarry Smith   Input Parameter:
1501bb7acecfSBarry Smith . dm - the `DM` object
150247c6ae99SBarry Smith 
150347c6ae99SBarry Smith   Output Parameter:
150447c6ae99SBarry Smith . mat - the empty Jacobian
150547c6ae99SBarry Smith 
150620f4b53cSBarry Smith   Options Database Key:
1507bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1508f27dd7c6SMatthew G. Knepley 
150920f4b53cSBarry Smith   Level: beginner
151020f4b53cSBarry Smith 
151195452b02SPatrick Sanan   Notes:
151295452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
151394013140SBarry Smith   do not need to do it yourself.
151494013140SBarry Smith 
151594013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1516bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
151794013140SBarry Smith 
1518bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
151994013140SBarry Smith   internally by PETSc.
152094013140SBarry Smith 
1521bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1522bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
152394013140SBarry Smith 
15241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1525aab9d709SJed Brown @*/
DMCreateMatrix(DM dm,Mat * mat)1526d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1527d71ae5a4SJacob Faibussowitsch {
152847c6ae99SBarry Smith   PetscFunctionBegin;
1529171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15304f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
15319566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
15329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1533dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
153476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1535c6b011d8SStefano Zampini     DM mdm;
1536c6b011d8SStefano Zampini 
15379566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
15387a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1539c6b011d8SStefano Zampini   }
1540e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1541e5e52638SMatthew G. Knepley   if (dm->Nf) {
1542e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1543649ef022SMatthew Knepley     PetscInt     Nf, f;
1544e571a35bSMatthew G. Knepley 
15459566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1546649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
15474758e3ceSMatthew G. Knepley       if (dm->nullspaceConstructors && dm->nullspaceConstructors[f]) {
15489566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15499566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15509566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1551649ef022SMatthew Knepley         break;
1552e571a35bSMatthew G. Knepley       }
1553649ef022SMatthew Knepley     }
1554649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
15554758e3ceSMatthew G. Knepley       if (dm->nearnullspaceConstructors && dm->nearnullspaceConstructors[f]) {
15569566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15579566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15589566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1559e571a35bSMatthew G. Knepley       }
1560e571a35bSMatthew G. Knepley     }
1561e571a35bSMatthew G. Knepley   }
15629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
156447c6ae99SBarry Smith }
156547c6ae99SBarry Smith 
1566732e2eb9SMatthew G Knepley /*@
1567a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1568a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1569a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1570aa0f6e3cSJed Brown 
157120f4b53cSBarry Smith   Logically Collective
1572aa0f6e3cSJed Brown 
1573aa0f6e3cSJed Brown   Input Parameters:
1574bb7acecfSBarry Smith + dm   - the `DM`
1575bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1576aa0f6e3cSJed Brown 
1577aa0f6e3cSJed Brown   Level: developer
1578aa0f6e3cSJed Brown 
157973ff1848SBarry Smith   Note:
1580a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1581a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1582a4e35b19SJacob Faibussowitsch 
15831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1584aa0f6e3cSJed Brown @*/
DMSetMatrixPreallocateSkip(DM dm,PetscBool skip)1585d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1586d71ae5a4SJacob Faibussowitsch {
1587aa0f6e3cSJed Brown   PetscFunctionBegin;
1588aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1589aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1591aa0f6e3cSJed Brown }
1592aa0f6e3cSJed Brown 
1593aa0f6e3cSJed Brown /*@
1594bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1595732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1596732e2eb9SMatthew G Knepley 
159720f4b53cSBarry Smith   Logically Collective
1598732e2eb9SMatthew G Knepley 
1599d8d19677SJose E. Roman   Input Parameters:
1600bb7acecfSBarry Smith + dm   - the `DM`
1601bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1602732e2eb9SMatthew G Knepley 
160320f4b53cSBarry Smith   Options Database Key:
1604bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1605f27dd7c6SMatthew G. Knepley 
160620f4b53cSBarry Smith   Level: developer
160720f4b53cSBarry Smith 
16081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1609732e2eb9SMatthew G Knepley @*/
DMSetMatrixPreallocateOnly(DM dm,PetscBool only)1610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1611d71ae5a4SJacob Faibussowitsch {
1612732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1613732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1614732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
16153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1616732e2eb9SMatthew G Knepley }
1617732e2eb9SMatthew G Knepley 
1618b06ff27eSHong Zhang /*@
16190b4b7b1cSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix nonzero structure will be created
1620bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1621b06ff27eSHong Zhang 
162220f4b53cSBarry Smith   Logically Collective
1623b06ff27eSHong Zhang 
1624d8d19677SJose E. Roman   Input Parameters:
1625bb7acecfSBarry Smith + dm   - the `DM`
16260b4b7b1cSBarry Smith - only - `PETSC_TRUE` if you only want matrix nonzero structure
1627b06ff27eSHong Zhang 
1628b06ff27eSHong Zhang   Level: developer
1629bb7acecfSBarry Smith 
16301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1631b06ff27eSHong Zhang @*/
DMSetMatrixStructureOnly(DM dm,PetscBool only)1632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1633d71ae5a4SJacob Faibussowitsch {
1634b06ff27eSHong Zhang   PetscFunctionBegin;
1635b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1636b06ff27eSHong Zhang   dm->structure_only = only;
16373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1638b06ff27eSHong Zhang }
1639b06ff27eSHong Zhang 
1640863027abSJed Brown /*@
1641863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1642863027abSJed Brown 
164320f4b53cSBarry Smith   Logically Collective
1644863027abSJed Brown 
1645863027abSJed Brown   Input Parameters:
1646863027abSJed Brown + dm    - the `DM`
1647863027abSJed Brown - btype - block by topological point or field node
1648863027abSJed Brown 
164920f4b53cSBarry Smith   Options Database Key:
16500e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1651863027abSJed Brown 
165220f4b53cSBarry Smith   Level: advanced
165320f4b53cSBarry Smith 
16541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1655863027abSJed Brown @*/
DMSetBlockingType(DM dm,DMBlockingType btype)1656863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1657863027abSJed Brown {
1658863027abSJed Brown   PetscFunctionBegin;
1659863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1660863027abSJed Brown   dm->blocking_type = btype;
16613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1662863027abSJed Brown }
1663863027abSJed Brown 
1664863027abSJed Brown /*@
1665863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1666863027abSJed Brown 
1667863027abSJed Brown   Not Collective
1668863027abSJed Brown 
16692fe279fdSBarry Smith   Input Parameter:
1670863027abSJed Brown . dm - the `DM`
1671863027abSJed Brown 
16722fe279fdSBarry Smith   Output Parameter:
1673863027abSJed Brown . btype - block by topological point or field node
1674863027abSJed Brown 
1675863027abSJed Brown   Level: advanced
1676863027abSJed Brown 
16771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1678863027abSJed Brown @*/
DMGetBlockingType(DM dm,DMBlockingType * btype)1679863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1680863027abSJed Brown {
1681863027abSJed Brown   PetscFunctionBegin;
1682863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16834f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1684863027abSJed Brown   *btype = dm->blocking_type;
16853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1686863027abSJed Brown }
1687863027abSJed Brown 
1688a89ea682SMatthew G Knepley /*@C
1689bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1690a89ea682SMatthew G Knepley 
1691a89ea682SMatthew G Knepley   Not Collective
1692a89ea682SMatthew G Knepley 
1693a89ea682SMatthew G Knepley   Input Parameters:
1694bb7acecfSBarry Smith + dm    - the `DM` object
1695a5b23f4aSJose E. Roman . count - The minimum size
169620f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1697a89ea682SMatthew G Knepley 
1698a89ea682SMatthew G Knepley   Output Parameter:
169960225df5SJacob Faibussowitsch . mem - the work array
1700a89ea682SMatthew G Knepley 
1701a89ea682SMatthew G Knepley   Level: developer
1702a89ea682SMatthew G Knepley 
170373ff1848SBarry Smith   Notes:
1704da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1705bb7acecfSBarry Smith 
1706bb7acecfSBarry Smith   The array may contain nonzero values
1707bb7acecfSBarry Smith 
17081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1709a89ea682SMatthew G Knepley @*/
DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void * mem)1710d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1711d71ae5a4SJacob Faibussowitsch {
1712aa1993deSMatthew G Knepley   DMWorkLink  link;
171369291d52SBarry Smith   PetscMPIInt dsize;
1714a89ea682SMatthew G Knepley 
1715a89ea682SMatthew G Knepley   PetscFunctionBegin;
1716a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17174f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1718442f3b32SStefano Zampini   if (!count) {
1719442f3b32SStefano Zampini     *(void **)mem = NULL;
1720442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1721442f3b32SStefano Zampini   }
1722aa1993deSMatthew G Knepley   if (dm->workin) {
1723aa1993deSMatthew G Knepley     link       = dm->workin;
1724aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1725aa1993deSMatthew G Knepley   } else {
17264dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1727a89ea682SMatthew G Knepley   }
1728b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1729b77fa653SStefano Zampini      Get size directly */
1730b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1731b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1732b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1733b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1734b77fa653SStefano Zampini #endif
1735b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1736b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1737b77fa653SStefano Zampini #endif
1738b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1739b77fa653SStefano Zampini 
17405056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
17419566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
17429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1743854ce69bSBarry Smith     link->bytes = dsize * count;
1744aa1993deSMatthew G Knepley   }
1745aa1993deSMatthew G Knepley   link->next    = dm->workout;
1746aa1993deSMatthew G Knepley   dm->workout   = link;
1747aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1749a89ea682SMatthew G Knepley }
1750a89ea682SMatthew G Knepley 
1751aa1993deSMatthew G Knepley /*@C
1752bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1753aa1993deSMatthew G Knepley 
1754aa1993deSMatthew G Knepley   Not Collective
1755aa1993deSMatthew G Knepley 
1756aa1993deSMatthew G Knepley   Input Parameters:
1757bb7acecfSBarry Smith + dm    - the `DM` object
1758a5b23f4aSJose E. Roman . count - The minimum size
175920f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1760aa1993deSMatthew G Knepley 
1761aa1993deSMatthew G Knepley   Output Parameter:
176260225df5SJacob Faibussowitsch . mem - the work array
1763aa1993deSMatthew G Knepley 
1764aa1993deSMatthew G Knepley   Level: developer
1765aa1993deSMatthew G Knepley 
176673ff1848SBarry Smith   Developer Note:
1767bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1768147403d9SBarry Smith 
17691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1770aa1993deSMatthew G Knepley @*/
DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void * mem)1771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1772d71ae5a4SJacob Faibussowitsch {
1773aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1774aa1993deSMatthew G Knepley 
1775aa1993deSMatthew G Knepley   PetscFunctionBegin;
17764f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1777a4e35b19SJacob Faibussowitsch   (void)count;
1778a4e35b19SJacob Faibussowitsch   (void)dtype;
1779442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1780aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1781aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1782aa1993deSMatthew G Knepley       *p            = link->next;
1783aa1993deSMatthew G Knepley       link->next    = dm->workin;
1784aa1993deSMatthew G Knepley       dm->workin    = link;
17850298fd71SBarry Smith       *(void **)mem = NULL;
17863ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1787aa1993deSMatthew G Knepley     }
1788aa1993deSMatthew G Knepley   }
1789aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1790aa1993deSMatthew G Knepley }
1791e7c4fc90SDmitry Karpeev 
17928cda7954SMatthew G. Knepley /*@C
1793bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1794bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17958cda7954SMatthew G. Knepley 
179620f4b53cSBarry Smith   Logically Collective; No Fortran Support
17978cda7954SMatthew G. Knepley 
17988cda7954SMatthew G. Knepley   Input Parameters:
1799bb7acecfSBarry Smith + dm     - The `DM`
18008cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18018cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
18028cda7954SMatthew G. Knepley 
180320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1804bb7acecfSBarry Smith + dm        - The present `DM`
1805bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1806147403d9SBarry Smith . field     - The field number in dm
1807147403d9SBarry Smith - nullSpace - The nullspace for the given field
18088cda7954SMatthew G. Knepley 
180949762cbcSSatish Balay   Level: intermediate
181049762cbcSSatish Balay 
18111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1812147403d9SBarry Smith @*/
DMSetNullSpaceConstructor(DM dm,PetscInt field,PetscErrorCode (* nullsp)(DM dm,PetscInt origField,PetscInt field,MatNullSpace * nullSpace))1813a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1814d71ae5a4SJacob Faibussowitsch {
1815435a35e8SMatthew G Knepley   PetscFunctionBegin;
1816435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18174758e3ceSMatthew G. Knepley   PetscCheck(field < dm->Nf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= %" PetscInt_FMT " fields", field, dm->Nf);
18184758e3ceSMatthew G. Knepley   PetscCheck(dm->nullspaceConstructors, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must call DMCreateDS() to setup nullspaces");
1819435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
18203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1821435a35e8SMatthew G Knepley }
1822435a35e8SMatthew G Knepley 
18238cda7954SMatthew G. Knepley /*@C
1824bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
18258cda7954SMatthew G. Knepley 
182620f4b53cSBarry Smith   Not Collective; No Fortran Support
18278cda7954SMatthew G. Knepley 
18288cda7954SMatthew G. Knepley   Input Parameters:
1829bb7acecfSBarry Smith + dm    - The `DM`
18308cda7954SMatthew G. Knepley - field - The field number for the nullspace
18318cda7954SMatthew G. Knepley 
18328cda7954SMatthew G. Knepley   Output Parameter:
18338cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
18348cda7954SMatthew G. Knepley 
183520f4b53cSBarry Smith   Calling sequence of `nullsp`:
1836147403d9SBarry Smith + dm        - The present DM
1837147403d9SBarry Smith . origField - The field number given above, in the original DM
1838147403d9SBarry Smith . field     - The field number in dm
1839147403d9SBarry Smith - nullSpace - The nullspace for the given field
18408cda7954SMatthew G. Knepley 
184149762cbcSSatish Balay   Level: intermediate
184249762cbcSSatish Balay 
18431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1844147403d9SBarry Smith @*/
DMGetNullSpaceConstructor(DM dm,PetscInt field,PetscErrorCode (** nullsp)(DM dm,PetscInt origField,PetscInt field,MatNullSpace * nullSpace))1845a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1846d71ae5a4SJacob Faibussowitsch {
18470a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18480a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18494f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18504758e3ceSMatthew G. Knepley   PetscCheck(field < dm->Nf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= %" PetscInt_FMT " fields", field, dm->Nf);
18514758e3ceSMatthew G. Knepley   PetscCheck(dm->nullspaceConstructors, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must call DMCreateDS() to setup nullspaces");
18520a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18540a50eb56SMatthew G. Knepley }
18550a50eb56SMatthew G. Knepley 
18568cda7954SMatthew G. Knepley /*@C
1857bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18588cda7954SMatthew G. Knepley 
185920f4b53cSBarry Smith   Logically Collective; No Fortran Support
18608cda7954SMatthew G. Knepley 
18618cda7954SMatthew G. Knepley   Input Parameters:
1862bb7acecfSBarry Smith + dm     - The `DM`
18638cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18648cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18658cda7954SMatthew G. Knepley 
186620f4b53cSBarry Smith   Calling sequence of `nullsp`:
1867bb7acecfSBarry Smith + dm        - The present `DM`
1868bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1869147403d9SBarry Smith . field     - The field number in dm
1870147403d9SBarry Smith - nullSpace - The nullspace for the given field
18718cda7954SMatthew G. Knepley 
187249762cbcSSatish Balay   Level: intermediate
187349762cbcSSatish Balay 
18741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1875bb7acecfSBarry Smith           `MatNullSpace`
1876147403d9SBarry Smith @*/
DMSetNearNullSpaceConstructor(DM dm,PetscInt field,PetscErrorCode (* nullsp)(DM dm,PetscInt origField,PetscInt field,MatNullSpace * nullSpace))1877a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1878d71ae5a4SJacob Faibussowitsch {
1879f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1880f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18814758e3ceSMatthew G. Knepley   PetscCheck(field < dm->Nf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= %" PetscInt_FMT " fields", field, dm->Nf);
18824758e3ceSMatthew G. Knepley   PetscCheck(dm->nearnullspaceConstructors, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must call DMCreateDS() to setup nullspaces");
1883f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1885f9d4088aSMatthew G. Knepley }
1886f9d4088aSMatthew G. Knepley 
18878cda7954SMatthew G. Knepley /*@C
1888bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18898cda7954SMatthew G. Knepley 
189020f4b53cSBarry Smith   Not Collective; No Fortran Support
18918cda7954SMatthew G. Knepley 
18928cda7954SMatthew G. Knepley   Input Parameters:
1893bb7acecfSBarry Smith + dm    - The `DM`
18948cda7954SMatthew G. Knepley - field - The field number for the nullspace
18958cda7954SMatthew G. Knepley 
18968cda7954SMatthew G. Knepley   Output Parameter:
18978cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18988cda7954SMatthew G. Knepley 
189920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1900bb7acecfSBarry Smith + dm        - The present `DM`
1901bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1902147403d9SBarry Smith . field     - The field number in dm
1903147403d9SBarry Smith - nullSpace - The nullspace for the given field
19048cda7954SMatthew G. Knepley 
190549762cbcSSatish Balay   Level: intermediate
190649762cbcSSatish Balay 
19071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1908bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1909147403d9SBarry Smith @*/
DMGetNearNullSpaceConstructor(DM dm,PetscInt field,PetscErrorCode (** nullsp)(DM dm,PetscInt origField,PetscInt field,MatNullSpace * nullSpace))1910a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1911d71ae5a4SJacob Faibussowitsch {
1912f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1913f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19144f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
19154758e3ceSMatthew G. Knepley   PetscCheck(field < dm->Nf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= %" PetscInt_FMT " fields", field, dm->Nf);
19164758e3ceSMatthew G. Knepley   PetscCheck(dm->nearnullspaceConstructors, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must call DMCreateDS() to setup nullspaces");
1917f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
19183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1919f9d4088aSMatthew G. Knepley }
1920f9d4088aSMatthew G. Knepley 
19214f3b5142SJed Brown /*@C
1922bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
19234d343eeaSMatthew G Knepley 
192420f4b53cSBarry Smith   Not Collective; No Fortran Support
19254d343eeaSMatthew G Knepley 
19264d343eeaSMatthew G Knepley   Input Parameter:
1927bb7acecfSBarry Smith . dm - the `DM` object
19284d343eeaSMatthew G Knepley 
19294d343eeaSMatthew G Knepley   Output Parameters:
193020f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
1931ce78bad3SBarry Smith . fieldNames - The name of each field (or `NULL` if not requested)
193220f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
19334d343eeaSMatthew G Knepley 
19344d343eeaSMatthew G Knepley   Level: intermediate
19354d343eeaSMatthew G Knepley 
1936bb7acecfSBarry Smith   Note:
193773ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
193873ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1939bb7acecfSBarry Smith   `PetscFree()`.
194021c9b008SJed Brown 
194173ff1848SBarry Smith   Developer Note:
1942bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1943bb7acecfSBarry Smith   likely be removed.
1944bb7acecfSBarry Smith 
19451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1946bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19474d343eeaSMatthew G Knepley @*/
DMCreateFieldIS(DM dm,PetscInt * numFields,char *** fieldNames,IS * fields[])1948ce78bad3SBarry Smith PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS *fields[])
1949d71ae5a4SJacob Faibussowitsch {
195037d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19514d343eeaSMatthew G Knepley 
19524d343eeaSMatthew G Knepley   PetscFunctionBegin;
19534d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
195469ca1f37SDmitry Karpeev   if (numFields) {
19554f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
195669ca1f37SDmitry Karpeev     *numFields = 0;
195769ca1f37SDmitry Karpeev   }
195837d0c07bSMatthew G Knepley   if (fieldNames) {
19594f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19600298fd71SBarry Smith     *fieldNames = NULL;
196169ca1f37SDmitry Karpeev   }
196269ca1f37SDmitry Karpeev   if (fields) {
19634f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19640298fd71SBarry Smith     *fields = NULL;
196569ca1f37SDmitry Karpeev   }
19669566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
196737d0c07bSMatthew G Knepley   if (section) {
19683a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
196937d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
197037d0c07bSMatthew G Knepley 
19719566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19739566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
197537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
197637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
197837d0c07bSMatthew G Knepley     }
197937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
198037d0c07bSMatthew G Knepley       PetscInt gdof;
198137d0c07bSMatthew G Knepley 
19829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
198337d0c07bSMatthew G Knepley       if (gdof > 0) {
198437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19853a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
198637d0c07bSMatthew G Knepley 
19879566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19889566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19893a544194SStefano Zampini           fpdof = fdof - fcdof;
19903a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19913a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19923a544194SStefano Zampini             fieldNc[f] = 1;
19933a544194SStefano Zampini           }
19943a544194SStefano Zampini           fieldSizes[f] += fpdof;
199537d0c07bSMatthew G Knepley         }
199637d0c07bSMatthew G Knepley       }
199737d0c07bSMatthew G Knepley     }
199837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19999566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
200037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
200137d0c07bSMatthew G Knepley     }
200237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
200337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
200437d0c07bSMatthew G Knepley 
20059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
200637d0c07bSMatthew G Knepley       if (gdof > 0) {
20079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
200837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
200937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
201037d0c07bSMatthew G Knepley 
20119566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
20129566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
2013ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
201437d0c07bSMatthew G Knepley         }
201537d0c07bSMatthew G Knepley       }
201637d0c07bSMatthew G Knepley     }
20178865f1eaSKarl Rupp     if (numFields) *numFields = nF;
201837d0c07bSMatthew G Knepley     if (fieldNames) {
20199566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
202037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
202137d0c07bSMatthew G Knepley         const char *fieldName;
202237d0c07bSMatthew G Knepley 
20239566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
2024835f2295SStefano Zampini         PetscCall(PetscStrallocpy(fieldName, &(*fieldNames)[f]));
202537d0c07bSMatthew G Knepley       }
202637d0c07bSMatthew G Knepley     }
202737d0c07bSMatthew G Knepley     if (fields) {
20289566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
202937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
20303a544194SStefano Zampini         PetscInt bs, in[2], out[2];
20313a544194SStefano Zampini 
20329566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
20333a544194SStefano Zampini         in[0] = -fieldNc[f];
20343a544194SStefano Zampini         in[1] = fieldNc[f];
2035462c564dSBarry Smith         PetscCallMPI(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
20363a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
20379566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
203837d0c07bSMatthew G Knepley       }
203937d0c07bSMatthew G Knepley     }
20409566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
2041dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
20423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20434d343eeaSMatthew G Knepley }
20444d343eeaSMatthew G Knepley 
204516621825SDmitry Karpeev /*@C
2046bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2047a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2048e7c4fc90SDmitry Karpeev 
204920f4b53cSBarry Smith   Not Collective; No Fortran Support
2050e7c4fc90SDmitry Karpeev 
2051e7c4fc90SDmitry Karpeev   Input Parameter:
2052bb7acecfSBarry Smith . dm - the `DM` object
2053e7c4fc90SDmitry Karpeev 
2054e7c4fc90SDmitry Karpeev   Output Parameters:
205520f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
205620f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
205720f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
205820f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2059e7c4fc90SDmitry Karpeev 
2060e7c4fc90SDmitry Karpeev   Level: intermediate
2061e7c4fc90SDmitry Karpeev 
2062a4e35b19SJacob Faibussowitsch   Notes:
2063a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2064a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2065a4e35b19SJacob Faibussowitsch 
2066a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2067a4e35b19SJacob Faibussowitsch 
206873ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
206973ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2070bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2071e7c4fc90SDmitry Karpeev 
20728d9ecca5SBarry Smith   Fortran Notes:
20738d9ecca5SBarry Smith   Use the declarations
20748d9ecca5SBarry Smith .vb
20758d9ecca5SBarry Smith   character(80), pointer :: namelist(:)
20768d9ecca5SBarry Smith   IS, pointer :: islist(:)
20778d9ecca5SBarry Smith   DM, pointer :: dmlist(:)
20788d9ecca5SBarry Smith .ve
20798d9ecca5SBarry Smith 
20808d9ecca5SBarry Smith   `namelist` must be provided, `islist` may be `PETSC_NULL_IS_POINTER` and `dmlist` may be `PETSC_NULL_DM_POINTER`
20818d9ecca5SBarry Smith 
20828d9ecca5SBarry Smith   Use `DMDestroyFieldDecomposition()` to free the returned objects
20838d9ecca5SBarry Smith 
208460225df5SJacob Faibussowitsch   Developer Notes:
2085bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2086bb7acecfSBarry Smith 
208773ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
208873ff1848SBarry Smith   decomposition is computed.
208973ff1848SBarry Smith 
209073ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2091e7c4fc90SDmitry Karpeev @*/
DMCreateFieldDecomposition(DM dm,PetscInt * len,char *** namelist,IS * islist[],DM * dmlist[])2092ce78bad3SBarry Smith PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS *islist[], DM *dmlist[])
2093d71ae5a4SJacob Faibussowitsch {
2094e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2095e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20968865f1eaSKarl Rupp   if (len) {
20974f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20988865f1eaSKarl Rupp     *len = 0;
20998865f1eaSKarl Rupp   }
21008865f1eaSKarl Rupp   if (namelist) {
21014f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2102ea78f98cSLisandro Dalcin     *namelist = NULL;
21038865f1eaSKarl Rupp   }
21048865f1eaSKarl Rupp   if (islist) {
21054f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2106ea78f98cSLisandro Dalcin     *islist = NULL;
21078865f1eaSKarl Rupp   }
21088865f1eaSKarl Rupp   if (dmlist) {
21094f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2110ea78f98cSLisandro Dalcin     *dmlist = NULL;
21118865f1eaSKarl Rupp   }
2112f3f0edfdSDmitry Karpeev   /*
2113f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2114f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2115f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2116f3f0edfdSDmitry Karpeev    */
21177a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
211816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2119435a35e8SMatthew G Knepley     PetscSection section;
2120435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2121435a35e8SMatthew G Knepley 
21229566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
21239566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2124435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2125f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
21269566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
21279566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
21289566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2129435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2130435a35e8SMatthew G Knepley         const char *fieldName;
2131435a35e8SMatthew G Knepley 
21329566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
213303dc3394SMatthew G. Knepley         if (namelist) {
21349566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
2135835f2295SStefano Zampini           PetscCall(PetscStrallocpy(fieldName, &(*namelist)[f]));
2136435a35e8SMatthew G Knepley         }
213703dc3394SMatthew G. Knepley       }
2138435a35e8SMatthew G Knepley     } else {
21399566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2140e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
21410298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2142e7c4fc90SDmitry Karpeev     }
2143dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
21443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
214516621825SDmitry Karpeev }
214616621825SDmitry Karpeev 
21475d83a8b1SBarry Smith /*@
214820f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
214920f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2150435a35e8SMatthew G Knepley 
2151435a35e8SMatthew G Knepley   Not collective
2152435a35e8SMatthew G Knepley 
2153435a35e8SMatthew G Knepley   Input Parameters:
2154bb7acecfSBarry Smith + dm        - The `DM` object
2155bb7acecfSBarry Smith . numFields - The number of fields to select
21562adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2157435a35e8SMatthew G Knepley 
2158435a35e8SMatthew G Knepley   Output Parameters:
2159b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2160b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2161435a35e8SMatthew G Knepley 
216220f4b53cSBarry Smith   Level: intermediate
216320f4b53cSBarry Smith 
2164bb7acecfSBarry Smith   Note:
2165bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21665d3b26e6SMatthew G. Knepley 
216729861759SMatthew Knepley .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `VecISCopy()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2168435a35e8SMatthew G Knepley @*/
DMCreateSubDM(DM dm,PetscInt numFields,const PetscInt fields[],IS * is,DM * subdm)2169d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2170d71ae5a4SJacob Faibussowitsch {
2171435a35e8SMatthew G Knepley   PetscFunctionBegin;
2172435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21734f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21744f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21754f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2176dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2178435a35e8SMatthew G Knepley }
2179435a35e8SMatthew G Knepley 
21802adcc780SMatthew G. Knepley /*@C
2181ce78bad3SBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and a single `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21822adcc780SMatthew G. Knepley 
21832adcc780SMatthew G. Knepley   Not collective
21842adcc780SMatthew G. Knepley 
2185d8d19677SJose E. Roman   Input Parameters:
2186bb7acecfSBarry Smith + dms - The `DM` objects
2187bb7acecfSBarry Smith - n   - The number of `DM`s
21882adcc780SMatthew G. Knepley 
21892adcc780SMatthew G. Knepley   Output Parameters:
2190ce78bad3SBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or `NULL`, its length is `n`
2191bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21922adcc780SMatthew G. Knepley 
219320f4b53cSBarry Smith   Level: intermediate
219420f4b53cSBarry Smith 
2195bb7acecfSBarry Smith   Note:
2196bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21975d3b26e6SMatthew G. Knepley 
219873ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21992adcc780SMatthew G. Knepley @*/
DMCreateSuperDM(DM dms[],PetscInt n,IS * is[],DM * superdm)22005d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2201d71ae5a4SJacob Faibussowitsch {
22022adcc780SMatthew G. Knepley   PetscInt i;
22032adcc780SMatthew G. Knepley 
22042adcc780SMatthew G. Knepley   PetscFunctionBegin;
22054f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2206ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
22074f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
22084f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2209bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2210bb7acecfSBarry Smith   if (n) {
2211b9d85ea2SLisandro Dalcin     DM dm = dms[0];
221200045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2213dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
22142adcc780SMatthew G. Knepley   }
22153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22162adcc780SMatthew G. Knepley }
22172adcc780SMatthew G. Knepley 
221816621825SDmitry Karpeev /*@C
2219a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2220a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
222116621825SDmitry Karpeev 
222220f4b53cSBarry Smith   Not Collective
222316621825SDmitry Karpeev 
222416621825SDmitry Karpeev   Input Parameter:
2225bb7acecfSBarry Smith . dm - the `DM` object
222616621825SDmitry Karpeev 
222716621825SDmitry Karpeev   Output Parameters:
2228ce78bad3SBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested), also the length of the four arrays below
222920f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
223073ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
223173ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
223273ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
223316621825SDmitry Karpeev 
223416621825SDmitry Karpeev   Level: intermediate
223516621825SDmitry Karpeev 
223673ff1848SBarry Smith   Notes:
2237a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2238a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2239a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2240a4e35b19SJacob Faibussowitsch 
2241a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2242a4e35b19SJacob Faibussowitsch 
224373ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
224473ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2245bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
224616621825SDmitry Karpeev 
2247a4e35b19SJacob Faibussowitsch   Developer Notes:
224820f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2249bb7acecfSBarry Smith 
225073ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
225173ff1848SBarry Smith 
225273ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
225373ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
225416621825SDmitry Karpeev @*/
DMCreateDomainDecomposition(DM dm,PetscInt * n,char *** namelist,IS * innerislist[],IS * outerislist[],DM * dmlist[])2255ce78bad3SBarry Smith PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS *innerislist[], IS *outerislist[], DM *dmlist[])
2256d71ae5a4SJacob Faibussowitsch {
2257be081cd6SPeter Brune   DMSubDomainHookLink link;
2258be081cd6SPeter Brune   PetscInt            i, l;
225916621825SDmitry Karpeev 
226016621825SDmitry Karpeev   PetscFunctionBegin;
226116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22629371c9d4SSatish Balay   if (n) {
22634f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22649371c9d4SSatish Balay     *n = 0;
22659371c9d4SSatish Balay   }
22669371c9d4SSatish Balay   if (namelist) {
22674f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22689371c9d4SSatish Balay     *namelist = NULL;
22699371c9d4SSatish Balay   }
22709371c9d4SSatish Balay   if (innerislist) {
22714f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22729371c9d4SSatish Balay     *innerislist = NULL;
22739371c9d4SSatish Balay   }
22749371c9d4SSatish Balay   if (outerislist) {
22754f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22769371c9d4SSatish Balay     *outerislist = NULL;
22779371c9d4SSatish Balay   }
22789371c9d4SSatish Balay   if (dmlist) {
22794f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22809371c9d4SSatish Balay     *dmlist = NULL;
22819371c9d4SSatish Balay   }
2282f3f0edfdSDmitry Karpeev   /*
2283f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2284f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2285f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2286f3f0edfdSDmitry Karpeev    */
22877a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
228816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2289dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
229014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2291f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2292be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2293be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22949566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2295be081cd6SPeter Brune         }
2296648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2297e7c4fc90SDmitry Karpeev       }
229814a18fd3SPeter Brune     }
2299bb7acecfSBarry Smith     if (n) *n = l;
230014a18fd3SPeter Brune   }
23013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2302e30e807fSPeter Brune }
2303e30e807fSPeter Brune 
2304e30e807fSPeter Brune /*@C
230573ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
230673ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2307e30e807fSPeter Brune 
230820f4b53cSBarry Smith   Not Collective
2309e30e807fSPeter Brune 
2310e30e807fSPeter Brune   Input Parameters:
2311bb7acecfSBarry Smith + dm     - the `DM` object
231273ff1848SBarry Smith . n      - the number of subdomains
2313e30e807fSPeter Brune - subdms - the local subdomains
2314e30e807fSPeter Brune 
2315e30e807fSPeter Brune   Output Parameters:
23166b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2317e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2318e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2319e30e807fSPeter Brune 
232020f4b53cSBarry Smith   Level: developer
232120f4b53cSBarry Smith 
2322bb7acecfSBarry Smith   Note:
2323bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2324e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2325e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2326e30e807fSPeter Brune   solution and residual data.
2327e30e807fSPeter Brune 
232873ff1848SBarry Smith   Developer Note:
2329a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2330a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2331bb7acecfSBarry Smith 
23321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2333e30e807fSPeter Brune @*/
DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM * subdms,VecScatter * iscat[],VecScatter * oscat[],VecScatter * gscat[])23345d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2335d71ae5a4SJacob Faibussowitsch {
2336e30e807fSPeter Brune   PetscFunctionBegin;
2337e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23384f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2339dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
23403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2341e7c4fc90SDmitry Karpeev }
2342e7c4fc90SDmitry Karpeev 
234347c6ae99SBarry Smith /*@
2344bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
234547c6ae99SBarry Smith 
234620f4b53cSBarry Smith   Collective
234747c6ae99SBarry Smith 
2348d8d19677SJose E. Roman   Input Parameters:
2349bb7acecfSBarry Smith + dm   - the `DM` object
2350bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
235147c6ae99SBarry Smith 
235247c6ae99SBarry Smith   Output Parameter:
235320f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2354ae0a1c52SMatthew G Knepley 
235520f4b53cSBarry Smith   Options Database Key:
2356412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2357412e9a14SMatthew G. Knepley 
235847c6ae99SBarry Smith   Level: developer
235947c6ae99SBarry Smith 
236020f4b53cSBarry Smith   Note:
236120f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
236220f4b53cSBarry Smith 
236373ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
236473ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
236547c6ae99SBarry Smith @*/
DMRefine(DM dm,MPI_Comm comm,DM * dmf)2366d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2367d71ae5a4SJacob Faibussowitsch {
2368c833c3b5SJed Brown   DMRefineHookLink link;
236947c6ae99SBarry Smith 
237047c6ae99SBarry Smith   PetscFunctionBegin;
2371732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2373dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23744057135bSMatthew G Knepley   if (*dmf) {
237543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23768865f1eaSKarl Rupp 
23779566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23788865f1eaSKarl Rupp 
2379644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23800598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2381656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23828865f1eaSKarl Rupp 
23839566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2384c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23851baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2386c833c3b5SJed Brown     }
2387c833c3b5SJed Brown   }
23889566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2390c833c3b5SJed Brown }
2391c833c3b5SJed Brown 
2392bb9467b5SJed Brown /*@C
2393c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2394c833c3b5SJed Brown 
239520f4b53cSBarry Smith   Logically Collective; No Fortran Support
2396c833c3b5SJed Brown 
23974165533cSJose E. Roman   Input Parameters:
2398bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2399bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2400f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
240120f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2402c833c3b5SJed Brown 
240320f4b53cSBarry Smith   Calling sequence of `refinehook`:
2404bb7acecfSBarry Smith + coarse - coarse level `DM`
2405bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2406c833c3b5SJed Brown - ctx    - optional user-defined function context
2407c833c3b5SJed Brown 
240820f4b53cSBarry Smith   Calling sequence of `interphook`:
2409bb7acecfSBarry Smith + coarse - coarse level `DM`
2410c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2411bb7acecfSBarry Smith . fine   - fine level `DM` to update
2412c833c3b5SJed Brown - ctx    - optional user-defined function context
2413c833c3b5SJed Brown 
2414c833c3b5SJed Brown   Level: advanced
2415c833c3b5SJed Brown 
2416c833c3b5SJed Brown   Notes:
2417bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2418bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2419bb7acecfSBarry Smith 
2420bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2421c833c3b5SJed Brown 
2422c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2423c833c3b5SJed Brown 
24241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2425c833c3b5SJed Brown @*/
DMRefineHookAdd(DM coarse,PetscErrorCode (* refinehook)(DM coarse,DM fine,PetscCtx ctx),PetscErrorCode (* interphook)(DM coarse,Mat interp,DM fine,PetscCtx ctx),PetscCtx ctx)2426*2a8381b2SBarry Smith PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM coarse, DM fine, PetscCtx ctx), PetscErrorCode (*interphook)(DM coarse, Mat interp, DM fine, PetscCtx ctx), PetscCtx ctx)
2427d71ae5a4SJacob Faibussowitsch {
2428c833c3b5SJed Brown   DMRefineHookLink link, *p;
2429c833c3b5SJed Brown 
2430c833c3b5SJed Brown   PetscFunctionBegin;
2431c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24323d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
24333ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
24343d8e3701SJed Brown   }
24359566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2436c833c3b5SJed Brown   link->refinehook = refinehook;
2437c833c3b5SJed Brown   link->interphook = interphook;
2438c833c3b5SJed Brown   link->ctx        = ctx;
24390298fd71SBarry Smith   link->next       = NULL;
2440c833c3b5SJed Brown   *p               = link;
24413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2442c833c3b5SJed Brown }
2443c833c3b5SJed Brown 
24443d8e3701SJed Brown /*@C
2445bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2446bb7acecfSBarry Smith   a nonlinear problem to a finer grid
24473d8e3701SJed Brown 
244820f4b53cSBarry Smith   Logically Collective; No Fortran Support
24493d8e3701SJed Brown 
24504165533cSJose E. Roman   Input Parameters:
2451bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2452bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2453bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
245420f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
24553d8e3701SJed Brown 
24563d8e3701SJed Brown   Level: advanced
24573d8e3701SJed Brown 
2458bb7acecfSBarry Smith   Note:
24593d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24603d8e3701SJed Brown 
24611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24623d8e3701SJed Brown @*/
DMRefineHookRemove(DM coarse,PetscErrorCode (* refinehook)(DM,DM,void *),PetscErrorCode (* interphook)(DM,Mat,DM,void *),PetscCtx ctx)2463*2a8381b2SBarry Smith PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), PetscCtx ctx)
2464d71ae5a4SJacob Faibussowitsch {
24653d8e3701SJed Brown   DMRefineHookLink link, *p;
24663d8e3701SJed Brown 
24673d8e3701SJed Brown   PetscFunctionBegin;
24683d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24693d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24703d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24713d8e3701SJed Brown       link = *p;
24723d8e3701SJed Brown       *p   = link->next;
24739566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24743d8e3701SJed Brown       break;
24753d8e3701SJed Brown     }
24763d8e3701SJed Brown   }
24773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24783d8e3701SJed Brown }
24793d8e3701SJed Brown 
2480c833c3b5SJed Brown /*@
2481bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2482c833c3b5SJed Brown 
2483c833c3b5SJed Brown   Collective if any hooks are
2484c833c3b5SJed Brown 
24854165533cSJose E. Roman   Input Parameters:
2486bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2487bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2488bb7acecfSBarry Smith - fine   - finer `DM` to update
2489c833c3b5SJed Brown 
2490c833c3b5SJed Brown   Level: developer
2491c833c3b5SJed Brown 
249273ff1848SBarry Smith   Developer Note:
2493bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2494bb7acecfSBarry Smith   an API with consistent terminology.
2495bb7acecfSBarry Smith 
24961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2497c833c3b5SJed Brown @*/
DMInterpolate(DM coarse,Mat interp,DM fine)2498d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2499d71ae5a4SJacob Faibussowitsch {
2500c833c3b5SJed Brown   DMRefineHookLink link;
2501c833c3b5SJed Brown 
2502c833c3b5SJed Brown   PetscFunctionBegin;
2503c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
25041baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
25054057135bSMatthew G Knepley   }
25063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
250747c6ae99SBarry Smith }
250847c6ae99SBarry Smith 
2509eb3f98d2SBarry Smith /*@
25101f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
25111f3379b2SToby Isaac 
251220f4b53cSBarry Smith   Collective
25131f3379b2SToby Isaac 
25144165533cSJose E. Roman   Input Parameters:
2515bb7acecfSBarry Smith + coarse    - coarse `DM`
2516bb7acecfSBarry Smith . fine      - fine `DM`
2517bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2518bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2519bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
25201f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
25211f3379b2SToby Isaac 
25224165533cSJose E. Roman   Output Parameter:
25231f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
25241f3379b2SToby Isaac 
25251f3379b2SToby Isaac   Level: developer
25261f3379b2SToby Isaac 
2527bb7acecfSBarry Smith   Note:
2528bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
25291f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
25301f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
25311f3379b2SToby Isaac   slope-limiting reconstruction.
25321f3379b2SToby Isaac 
253373ff1848SBarry Smith   Developer Note:
2534bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2535bb7acecfSBarry Smith 
25361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
25371f3379b2SToby Isaac @*/
DMInterpolateSolution(DM coarse,DM fine,Mat interp,Vec coarseSol,Vec fineSol)2538d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2539d71ae5a4SJacob Faibussowitsch {
25401f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
25411f3379b2SToby Isaac 
25421f3379b2SToby Isaac   PetscFunctionBegin;
25431f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
25441f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
25451f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
25461f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
25471f3379b2SToby Isaac 
25489566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
25491f3379b2SToby Isaac   if (interpsol) {
25509566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
25511f3379b2SToby Isaac   } else if (interp) {
25529566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
255398921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
25543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25551f3379b2SToby Isaac }
25561f3379b2SToby Isaac 
25571f3379b2SToby Isaac /*@
2558bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2559eb3f98d2SBarry Smith 
2560eb3f98d2SBarry Smith   Not Collective
2561eb3f98d2SBarry Smith 
2562eb3f98d2SBarry Smith   Input Parameter:
2563bb7acecfSBarry Smith . dm - the `DM` object
2564eb3f98d2SBarry Smith 
2565eb3f98d2SBarry Smith   Output Parameter:
2566eb3f98d2SBarry Smith . level - number of refinements
2567eb3f98d2SBarry Smith 
2568eb3f98d2SBarry Smith   Level: developer
2569eb3f98d2SBarry Smith 
2570bb7acecfSBarry Smith   Note:
2571bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2572bb7acecfSBarry Smith 
25731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2574eb3f98d2SBarry Smith @*/
DMGetRefineLevel(DM dm,PetscInt * level)2575d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2576d71ae5a4SJacob Faibussowitsch {
2577eb3f98d2SBarry Smith   PetscFunctionBegin;
2578eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2579eb3f98d2SBarry Smith   *level = dm->levelup;
25803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2581eb3f98d2SBarry Smith }
2582eb3f98d2SBarry Smith 
2583fef3a512SBarry Smith /*@
2584bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2585fef3a512SBarry Smith 
2586fef3a512SBarry Smith   Not Collective
2587fef3a512SBarry Smith 
2588d8d19677SJose E. Roman   Input Parameters:
2589bb7acecfSBarry Smith + dm    - the `DM` object
2590fef3a512SBarry Smith - level - number of refinements
2591fef3a512SBarry Smith 
2592fef3a512SBarry Smith   Level: advanced
2593fef3a512SBarry Smith 
259495452b02SPatrick Sanan   Notes:
2595bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2596fef3a512SBarry Smith 
2597bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2598bb7acecfSBarry Smith 
25991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2600fef3a512SBarry Smith @*/
DMSetRefineLevel(DM dm,PetscInt level)2601d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2602d71ae5a4SJacob Faibussowitsch {
2603fef3a512SBarry Smith   PetscFunctionBegin;
2604fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2605fef3a512SBarry Smith   dm->levelup = level;
26063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2607fef3a512SBarry Smith }
2608fef3a512SBarry Smith 
2609d410b0cfSMatthew G. Knepley /*@
2610bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2611d410b0cfSMatthew G. Knepley 
261220f4b53cSBarry Smith   Collective
2613d410b0cfSMatthew G. Knepley 
2614f1a722f8SMatthew G. Knepley   Input Parameters:
2615bb7acecfSBarry Smith + dm     - the `DM` object
2616d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2617d410b0cfSMatthew G. Knepley 
2618d410b0cfSMatthew G. Knepley   Output Parameter:
261920f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2620d410b0cfSMatthew G. Knepley 
2621d410b0cfSMatthew G. Knepley   Level: developer
2622d410b0cfSMatthew G. Knepley 
262320f4b53cSBarry Smith   Note:
262420f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
262520f4b53cSBarry Smith 
26261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2627d410b0cfSMatthew G. Knepley @*/
DMExtrude(DM dm,PetscInt layers,DM * dme)2628d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2629d71ae5a4SJacob Faibussowitsch {
2630d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2631d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2632dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2633d410b0cfSMatthew G. Knepley   if (*dme) {
2634d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
26359566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2636d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
26379566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2638d410b0cfSMatthew G. Knepley   }
26393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2640d410b0cfSMatthew G. Knepley }
2641d410b0cfSMatthew G. Knepley 
DMGetBasisTransformDM_Internal(DM dm,DM * tdm)2642d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2643d71ae5a4SJacob Faibussowitsch {
2644ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2645ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26464f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2647ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
26483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2649ca3d3a14SMatthew G. Knepley }
2650ca3d3a14SMatthew G. Knepley 
DMGetBasisTransformVec_Internal(DM dm,Vec * tv)2651d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2652d71ae5a4SJacob Faibussowitsch {
2653ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2654ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26554f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2656ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
26573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2658ca3d3a14SMatthew G. Knepley }
2659ca3d3a14SMatthew G. Knepley 
2660ca3d3a14SMatthew G. Knepley /*@
2661bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2662ca3d3a14SMatthew G. Knepley 
2663ca3d3a14SMatthew G. Knepley   Input Parameter:
266420f4b53cSBarry Smith . dm - The `DM`
2665ca3d3a14SMatthew G. Knepley 
2666ca3d3a14SMatthew G. Knepley   Output Parameter:
266720f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2668ca3d3a14SMatthew G. Knepley 
2669ca3d3a14SMatthew G. Knepley   Level: developer
2670ca3d3a14SMatthew G. Knepley 
26711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2672ca3d3a14SMatthew G. Knepley @*/
DMHasBasisTransform(DM dm,PetscBool * flg)2673d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2674d71ae5a4SJacob Faibussowitsch {
2675ca3d3a14SMatthew G. Knepley   Vec tv;
2676ca3d3a14SMatthew G. Knepley 
2677ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2678ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26794f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26809566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2681ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2683ca3d3a14SMatthew G. Knepley }
2684ca3d3a14SMatthew G. Knepley 
DMConstructBasisTransform_Internal(DM dm)2685d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2686d71ae5a4SJacob Faibussowitsch {
2687ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2688ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2689ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2690ca3d3a14SMatthew G. Knepley 
2691ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26929566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26939566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26949566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26959566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26969566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26979566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26989566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2700ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
27019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2702ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2703ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2704ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
27059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2706ca3d3a14SMatthew G. Knepley       if (!dof) continue;
27079566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
27089566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2709ca3d3a14SMatthew G. Knepley     }
2710ca3d3a14SMatthew G. Knepley   }
27119566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
27129566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
27139566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2714ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2715ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
27169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2717ca3d3a14SMatthew G. Knepley       if (dof) {
2718ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2719ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2720ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2721ca3d3a14SMatthew G. Knepley 
2722ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
27239566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
27249566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
27259566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2726ca3d3a14SMatthew G. Knepley       }
2727ca3d3a14SMatthew G. Knepley     }
2728ca3d3a14SMatthew G. Knepley   }
27299566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
27303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2731ca3d3a14SMatthew G. Knepley }
2732ca3d3a14SMatthew G. Knepley 
DMCopyTransform(DM dm,DM newdm)2733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2734d71ae5a4SJacob Faibussowitsch {
2735ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2736ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2737ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2738ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2739ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2740ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2741ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
27429566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
27433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2744ca3d3a14SMatthew G. Knepley }
2745ca3d3a14SMatthew G. Knepley 
2746bb9467b5SJed Brown /*@C
2747bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2748baf369e7SPeter Brune 
274920f4b53cSBarry Smith   Logically Collective
2750baf369e7SPeter Brune 
27514165533cSJose E. Roman   Input Parameters:
2752bb7acecfSBarry Smith + dm        - the `DM`
2753bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2754bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
275520f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2756baf369e7SPeter Brune 
275720f4b53cSBarry Smith   Calling sequence of `beginhook`:
2758a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2759baf369e7SPeter Brune . g    - global vector
2760baf369e7SPeter Brune . mode - mode
2761baf369e7SPeter Brune . l    - local vector
2762baf369e7SPeter Brune - ctx  - optional user-defined function context
2763baf369e7SPeter Brune 
276420f4b53cSBarry Smith   Calling sequence of `endhook`:
2765a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2766a4e35b19SJacob Faibussowitsch . g    - global vector
2767a4e35b19SJacob Faibussowitsch . mode - mode
2768a4e35b19SJacob Faibussowitsch . l    - local vector
2769baf369e7SPeter Brune - ctx  - optional user-defined function context
2770baf369e7SPeter Brune 
2771baf369e7SPeter Brune   Level: advanced
2772baf369e7SPeter Brune 
2773bb7acecfSBarry Smith   Note:
2774bb7acecfSBarry 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.
2775bb7acecfSBarry Smith 
27761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2777baf369e7SPeter Brune @*/
DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (* beginhook)(DM dm,Vec g,InsertMode mode,Vec l,PetscCtx ctx),PetscErrorCode (* endhook)(DM dm,Vec g,InsertMode mode,Vec l,PetscCtx ctx),PetscCtx ctx)2778*2a8381b2SBarry Smith PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM dm, Vec g, InsertMode mode, Vec l, PetscCtx ctx), PetscErrorCode (*endhook)(DM dm, Vec g, InsertMode mode, Vec l, PetscCtx ctx), PetscCtx ctx)
2779d71ae5a4SJacob Faibussowitsch {
2780baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2781baf369e7SPeter Brune 
2782baf369e7SPeter Brune   PetscFunctionBegin;
2783baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2784baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27859566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2786baf369e7SPeter Brune   link->beginhook = beginhook;
2787baf369e7SPeter Brune   link->endhook   = endhook;
2788baf369e7SPeter Brune   link->ctx       = ctx;
27890298fd71SBarry Smith   link->next      = NULL;
2790baf369e7SPeter Brune   *p              = link;
27913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2792baf369e7SPeter Brune }
2793baf369e7SPeter Brune 
DMGlobalToLocalHook_Constraints(DM dm,Vec g,InsertMode mode,Vec l,PetscCtx ctx)2794*2a8381b2SBarry Smith static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, PetscCtx ctx)
2795d71ae5a4SJacob Faibussowitsch {
27964c274da1SToby Isaac   Mat          cMat;
279779769bd5SJed Brown   Vec          cVec, cBias;
27984c274da1SToby Isaac   PetscSection section, cSec;
27994c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
28004c274da1SToby Isaac 
28014c274da1SToby Isaac   PetscFunctionBegin;
2802a4e35b19SJacob Faibussowitsch   (void)g;
2803a4e35b19SJacob Faibussowitsch   (void)ctx;
28044c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28059566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
28064c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
28075db9a05bSToby Isaac     PetscInt nRows;
28085db9a05bSToby Isaac 
28099566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
28103ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
28119566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
28129566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
28139566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
28149566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
28159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
28164c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
28179566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
28184c274da1SToby Isaac       if (dof) {
28194c274da1SToby Isaac         PetscScalar *vals;
28209566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
28219566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
28224c274da1SToby Isaac       }
28234c274da1SToby Isaac     }
28249566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
28254c274da1SToby Isaac   }
28263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28274c274da1SToby Isaac }
28284c274da1SToby Isaac 
282947c6ae99SBarry Smith /*@
283001729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
283101729b5cSPatrick Sanan 
283220f4b53cSBarry Smith   Neighbor-wise Collective
283301729b5cSPatrick Sanan 
283401729b5cSPatrick Sanan   Input Parameters:
2835bb7acecfSBarry Smith + dm   - the `DM` object
283601729b5cSPatrick Sanan . g    - the global vector
2837bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
283801729b5cSPatrick Sanan - l    - the local vector
283901729b5cSPatrick Sanan 
284020f4b53cSBarry Smith   Level: beginner
284120f4b53cSBarry Smith 
284201729b5cSPatrick Sanan   Notes:
2843bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2844bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2845bb7acecfSBarry Smith 
2846bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
284701729b5cSPatrick Sanan 
28481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
284960225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2850bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
285101729b5cSPatrick Sanan @*/
DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)2852d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2853d71ae5a4SJacob Faibussowitsch {
285401729b5cSPatrick Sanan   PetscFunctionBegin;
28559566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
28569566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
28573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
285801729b5cSPatrick Sanan }
285901729b5cSPatrick Sanan 
286001729b5cSPatrick Sanan /*@
286147c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
286247c6ae99SBarry Smith 
286320f4b53cSBarry Smith   Neighbor-wise Collective
286447c6ae99SBarry Smith 
286547c6ae99SBarry Smith   Input Parameters:
2866bb7acecfSBarry Smith + dm   - the `DM` object
286747c6ae99SBarry Smith . g    - the global vector
2868bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
286947c6ae99SBarry Smith - l    - the local vector
287047c6ae99SBarry Smith 
287101729b5cSPatrick Sanan   Level: intermediate
287247c6ae99SBarry Smith 
2873bb7acecfSBarry Smith   Notes:
2874bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2875bb7acecfSBarry Smith 
2876bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2877bb7acecfSBarry Smith 
2878bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2879bb7acecfSBarry Smith 
2880bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2881bb7acecfSBarry Smith 
288260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
288347c6ae99SBarry Smith @*/
DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)2884d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2885d71ae5a4SJacob Faibussowitsch {
28867128ae9fSMatthew G Knepley   PetscSF                 sf;
2887baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
288847c6ae99SBarry Smith 
288947c6ae99SBarry Smith   PetscFunctionBegin;
2890171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2891baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28921baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2893baf369e7SPeter Brune   }
28949566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28957128ae9fSMatthew G Knepley   if (sf) {
2896ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2897ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2898d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28997128ae9fSMatthew G Knepley 
29007a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
29019566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
29029566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
29039566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
29049566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
29059566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
29067128ae9fSMatthew G Knepley   } else {
2907fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
29087128ae9fSMatthew G Knepley   }
29093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
291047c6ae99SBarry Smith }
291147c6ae99SBarry Smith 
291247c6ae99SBarry Smith /*@
291347c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
291447c6ae99SBarry Smith 
291520f4b53cSBarry Smith   Neighbor-wise Collective
291647c6ae99SBarry Smith 
291747c6ae99SBarry Smith   Input Parameters:
2918bb7acecfSBarry Smith + dm   - the `DM` object
291947c6ae99SBarry Smith . g    - the global vector
2920bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
292147c6ae99SBarry Smith - l    - the local vector
292247c6ae99SBarry Smith 
292301729b5cSPatrick Sanan   Level: intermediate
292447c6ae99SBarry Smith 
2925bb7acecfSBarry Smith   Note:
2926bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2927bb7acecfSBarry Smith 
292860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
292947c6ae99SBarry Smith @*/
DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)2930d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2931d71ae5a4SJacob Faibussowitsch {
29327128ae9fSMatthew G Knepley   PetscSF                 sf;
2933ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2934ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2935ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2936baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2937d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
293847c6ae99SBarry Smith 
293947c6ae99SBarry Smith   PetscFunctionBegin;
2940171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29419566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
29429566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
29437128ae9fSMatthew G Knepley   if (sf) {
29447a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
29457128ae9fSMatthew G Knepley 
29469566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
29479566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
29489566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
29499566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
29509566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
29519566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
29527128ae9fSMatthew G Knepley   } else {
2953fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
29547128ae9fSMatthew G Knepley   }
29559566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2956baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
29579566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2958baf369e7SPeter Brune   }
29593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
296047c6ae99SBarry Smith }
296147c6ae99SBarry Smith 
2962d4d07f1eSToby Isaac /*@C
2963d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2964d4d07f1eSToby Isaac 
296520f4b53cSBarry Smith   Logically Collective
2966d4d07f1eSToby Isaac 
29674165533cSJose E. Roman   Input Parameters:
2968bb7acecfSBarry Smith + dm        - the `DM`
2969bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2970bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
297120f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2972d4d07f1eSToby Isaac 
297320f4b53cSBarry Smith   Calling sequence of `beginhook`:
2974a4e35b19SJacob Faibussowitsch + global - global `DM`
2975d4d07f1eSToby Isaac . l      - local vector
2976d4d07f1eSToby Isaac . mode   - mode
2977d4d07f1eSToby Isaac . g      - global vector
2978d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2979d4d07f1eSToby Isaac 
298020f4b53cSBarry Smith   Calling sequence of `endhook`:
2981bb7acecfSBarry Smith + global - global `DM`
2982d4d07f1eSToby Isaac . l      - local vector
2983d4d07f1eSToby Isaac . mode   - mode
2984d4d07f1eSToby Isaac . g      - global vector
2985d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2986d4d07f1eSToby Isaac 
2987d4d07f1eSToby Isaac   Level: advanced
2988d4d07f1eSToby Isaac 
29891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2990d4d07f1eSToby Isaac @*/
DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (* beginhook)(DM global,Vec l,InsertMode mode,Vec g,PetscCtx ctx),PetscErrorCode (* endhook)(DM global,Vec l,InsertMode mode,Vec g,PetscCtx ctx),PetscCtx ctx)2991*2a8381b2SBarry Smith PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM global, Vec l, InsertMode mode, Vec g, PetscCtx ctx), PetscErrorCode (*endhook)(DM global, Vec l, InsertMode mode, Vec g, PetscCtx ctx), PetscCtx ctx)
2992d71ae5a4SJacob Faibussowitsch {
2993d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2994d4d07f1eSToby Isaac 
2995d4d07f1eSToby Isaac   PetscFunctionBegin;
2996d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2997d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29989566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2999d4d07f1eSToby Isaac   link->beginhook = beginhook;
3000d4d07f1eSToby Isaac   link->endhook   = endhook;
3001d4d07f1eSToby Isaac   link->ctx       = ctx;
3002d4d07f1eSToby Isaac   link->next      = NULL;
3003d4d07f1eSToby Isaac   *p              = link;
30043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3005d4d07f1eSToby Isaac }
3006d4d07f1eSToby Isaac 
DMLocalToGlobalHook_Constraints(DM dm,Vec l,InsertMode mode,Vec g,PetscCtx ctx)3007*2a8381b2SBarry Smith static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, PetscCtx ctx)
3008d71ae5a4SJacob Faibussowitsch {
30094c274da1SToby Isaac   PetscFunctionBegin;
3010a4e35b19SJacob Faibussowitsch   (void)g;
3011a4e35b19SJacob Faibussowitsch   (void)ctx;
30124c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30132b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
30142b6b7d09SToby Isaac     Mat          cMat;
30152b6b7d09SToby Isaac     Vec          cVec;
30165db9a05bSToby Isaac     PetscInt     nRows;
30172b6b7d09SToby Isaac     PetscSection section, cSec;
30182b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
30192b6b7d09SToby Isaac 
30202b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
30212b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
30225db9a05bSToby Isaac 
30239566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
30243ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
30259566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
30269566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
30279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
30284c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
30299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
30304c274da1SToby Isaac       if (dof) {
30314c274da1SToby Isaac         PetscInt     d;
30324c274da1SToby Isaac         PetscScalar *vals;
30339566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
30349566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
30354c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
30364c274da1SToby Isaac          * we just extracted */
3037ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
30384c274da1SToby Isaac       }
30394c274da1SToby Isaac     }
30409566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
30419566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
30424c274da1SToby Isaac   }
30433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30444c274da1SToby Isaac }
304501729b5cSPatrick Sanan /*@
304601729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
304701729b5cSPatrick Sanan 
304820f4b53cSBarry Smith   Neighbor-wise Collective
304901729b5cSPatrick Sanan 
305001729b5cSPatrick Sanan   Input Parameters:
3051bb7acecfSBarry Smith + dm   - the `DM` object
305201729b5cSPatrick Sanan . l    - the local vector
3053bb7acecfSBarry 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.
305401729b5cSPatrick Sanan - g    - the global vector
305501729b5cSPatrick Sanan 
305620f4b53cSBarry Smith   Level: beginner
305720f4b53cSBarry Smith 
305801729b5cSPatrick Sanan   Notes:
305901729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3060bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
306101729b5cSPatrick Sanan 
3062bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3063bb7acecfSBarry Smith 
3064bb7acecfSBarry 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.
3065bb7acecfSBarry Smith 
3066bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
306701729b5cSPatrick Sanan 
30681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
306901729b5cSPatrick Sanan @*/
DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)3070d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3071d71ae5a4SJacob Faibussowitsch {
307201729b5cSPatrick Sanan   PetscFunctionBegin;
30739566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30749566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
307601729b5cSPatrick Sanan }
30774c274da1SToby Isaac 
307847c6ae99SBarry Smith /*@
307901729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30809a42bb27SBarry Smith 
308120f4b53cSBarry Smith   Neighbor-wise Collective
30829a42bb27SBarry Smith 
30839a42bb27SBarry Smith   Input Parameters:
3084bb7acecfSBarry Smith + dm   - the `DM` object
3085f6813fd5SJed Brown . l    - the local vector
3086aa624791SPierre 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.
30871eb28f2eSBarry Smith - g    - the global vector
30889a42bb27SBarry Smith 
308920f4b53cSBarry Smith   Level: intermediate
309020f4b53cSBarry Smith 
309195452b02SPatrick Sanan   Notes:
3092bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3093bb7acecfSBarry Smith 
3094bb7acecfSBarry 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.
3095bb7acecfSBarry Smith 
3096bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3097bb7acecfSBarry Smith 
3098bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3099bb7acecfSBarry Smith 
3100bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
31019a42bb27SBarry Smith 
31021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
31039a42bb27SBarry Smith @*/
DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)3104d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3105d71ae5a4SJacob Faibussowitsch {
31067128ae9fSMatthew G Knepley   PetscSF                 sf;
310784330215SMatthew G. Knepley   PetscSection            s, gs;
3108d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3109ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3110ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3111ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3112fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3113d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
31149a42bb27SBarry Smith 
31159a42bb27SBarry Smith   PetscFunctionBegin;
3116171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3117d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
31181baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3119d4d07f1eSToby Isaac   }
31209566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
31219566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31229566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31237128ae9fSMatthew G Knepley   switch (mode) {
31247128ae9fSMatthew G Knepley   case INSERT_VALUES:
31257128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3126d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3127d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3128d71ae5a4SJacob Faibussowitsch     break;
31297128ae9fSMatthew G Knepley   case ADD_VALUES:
31307128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3131d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3132d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3133d71ae5a4SJacob Faibussowitsch     break;
3134d71ae5a4SJacob Faibussowitsch   default:
3135d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31367128ae9fSMatthew G Knepley   }
3137ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
31389566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3139ca3d3a14SMatthew G. Knepley     if (transform) {
31409566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31419566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
31429566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
31439566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3144fa88e482SJed Brown     } else if (isInsert) {
31459566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3146fa88e482SJed Brown     } else {
31479566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3148fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3149ca3d3a14SMatthew G. Knepley     }
3150fa88e482SJed Brown     if (s && isInsert) {
31519566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3152fa88e482SJed Brown     } else {
31539566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3154fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3155fa88e482SJed Brown     }
3156ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
31579566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
315884330215SMatthew G. Knepley     } else if (s && isInsert) {
315984330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
316084330215SMatthew G. Knepley 
31619566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31629566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31639566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
316484330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3165b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
316684330215SMatthew G. Knepley 
31679566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31689566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31699566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31709566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31719566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31729566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3173b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
317403442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31757a8be351SBarry 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);
3176b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3177b3b16f48SMatthew G. Knepley         if (!gcdof) {
317884330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3179b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3180b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
318184330215SMatthew G. Knepley           const PetscInt *cdofs;
318284330215SMatthew G. Knepley           PetscInt        cind = 0;
318384330215SMatthew G. Knepley 
31849566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3185b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31869371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31879371c9d4SSatish Balay               ++cind;
31889371c9d4SSatish Balay               continue;
31899371c9d4SSatish Balay             }
3190b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
319184330215SMatthew G. Knepley           }
31927a8be351SBarry 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);
319384330215SMatthew G. Knepley       }
3194ca3d3a14SMatthew G. Knepley     }
3195fa88e482SJed Brown     if (g_inplace) {
31969566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3197fa88e482SJed Brown     } else {
31989566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3199fa88e482SJed Brown     }
3200ca3d3a14SMatthew G. Knepley     if (transform) {
32019566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32029566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3203fa88e482SJed Brown     } else if (l_inplace) {
32049566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3205ca3d3a14SMatthew G. Knepley     } else {
32069566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3207ca3d3a14SMatthew G. Knepley     }
32087128ae9fSMatthew G Knepley   } else {
3209fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32107128ae9fSMatthew G Knepley   }
32113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32129a42bb27SBarry Smith }
32139a42bb27SBarry Smith 
32149a42bb27SBarry Smith /*@
32159a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
321647c6ae99SBarry Smith 
321720f4b53cSBarry Smith   Neighbor-wise Collective
321847c6ae99SBarry Smith 
321947c6ae99SBarry Smith   Input Parameters:
3220bb7acecfSBarry Smith + dm   - the `DM` object
3221f6813fd5SJed Brown . l    - the local vector
3222bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3223f6813fd5SJed Brown - g    - the global vector
322447c6ae99SBarry Smith 
322501729b5cSPatrick Sanan   Level: intermediate
322647c6ae99SBarry Smith 
3227bb7acecfSBarry Smith   Note:
3228bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3229bb7acecfSBarry Smith 
323060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
323147c6ae99SBarry Smith @*/
DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)3232d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3233d71ae5a4SJacob Faibussowitsch {
32347128ae9fSMatthew G Knepley   PetscSF                 sf;
323584330215SMatthew G. Knepley   PetscSection            s;
3236d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3237ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
323847c6ae99SBarry Smith 
323947c6ae99SBarry Smith   PetscFunctionBegin;
3240171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32419566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
32429566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
32437128ae9fSMatthew G Knepley   switch (mode) {
32447128ae9fSMatthew G Knepley   case INSERT_VALUES:
3245d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3246d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3247d71ae5a4SJacob Faibussowitsch     break;
32487128ae9fSMatthew G Knepley   case ADD_VALUES:
3249d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3250d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3251d71ae5a4SJacob Faibussowitsch     break;
3252d71ae5a4SJacob Faibussowitsch   default:
3253d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
32547128ae9fSMatthew G Knepley   }
325584330215SMatthew G. Knepley   if (sf && !isInsert) {
3256ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3257ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3258ca3d3a14SMatthew G. Knepley     Vec                tmpl;
325984330215SMatthew G. Knepley 
32609566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3261ca3d3a14SMatthew G. Knepley     if (transform) {
32629566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32639566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3264ca3d3a14SMatthew G. Knepley     } else {
32659566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3266ca3d3a14SMatthew G. Knepley     }
32679566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32689566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3269ca3d3a14SMatthew G. Knepley     if (transform) {
32709566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32719566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3272ca3d3a14SMatthew G. Knepley     } else {
32739566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3274ca3d3a14SMatthew G. Knepley     }
32759566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
327684330215SMatthew G. Knepley   } else if (s && isInsert) {
32777128ae9fSMatthew G Knepley   } else {
3278fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32797128ae9fSMatthew G Knepley   }
3280d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32819566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3282d4d07f1eSToby Isaac   }
32833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
328447c6ae99SBarry Smith }
328547c6ae99SBarry Smith 
3286f089877aSRichard Tran Mills /*@
3287a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3288a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3289a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3290f089877aSRichard Tran Mills 
329120f4b53cSBarry Smith   Neighbor-wise Collective
3292f089877aSRichard Tran Mills 
3293f089877aSRichard Tran Mills   Input Parameters:
3294bb7acecfSBarry Smith + dm   - the `DM` object
3295bc0a1609SRichard Tran Mills . g    - the original local vector
3296bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3297f089877aSRichard Tran Mills 
3298bc0a1609SRichard Tran Mills   Output Parameter:
3299bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3300f089877aSRichard Tran Mills 
3301f089877aSRichard Tran Mills   Level: intermediate
3302f089877aSRichard Tran Mills 
330373ff1848SBarry Smith   Note:
3304a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3305a4e35b19SJacob Faibussowitsch 
330660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3307f089877aSRichard Tran Mills @*/
DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)3308d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3309d71ae5a4SJacob Faibussowitsch {
3310f089877aSRichard Tran Mills   PetscFunctionBegin;
3311f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33129f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
33139f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
33149f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
33153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3316f089877aSRichard Tran Mills }
3317f089877aSRichard Tran Mills 
3318f089877aSRichard Tran Mills /*@
3319bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3320bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3321f089877aSRichard Tran Mills 
332220f4b53cSBarry Smith   Neighbor-wise Collective
3323f089877aSRichard Tran Mills 
3324f089877aSRichard Tran Mills   Input Parameters:
332560225df5SJacob Faibussowitsch + dm   - the `DM` object
3326bc0a1609SRichard Tran Mills . g    - the original local vector
3327bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3328f089877aSRichard Tran Mills 
3329bc0a1609SRichard Tran Mills   Output Parameter:
3330bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3331f089877aSRichard Tran Mills 
3332f089877aSRichard Tran Mills   Level: intermediate
3333f089877aSRichard Tran Mills 
333460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3335f089877aSRichard Tran Mills @*/
DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)3336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3337d71ae5a4SJacob Faibussowitsch {
3338f089877aSRichard Tran Mills   PetscFunctionBegin;
3339f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33409f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
33419f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
33429f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
33433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3344f089877aSRichard Tran Mills }
3345f089877aSRichard Tran Mills 
334647c6ae99SBarry Smith /*@
3347bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
334847c6ae99SBarry Smith 
334920f4b53cSBarry Smith   Collective
335047c6ae99SBarry Smith 
3351d8d19677SJose E. Roman   Input Parameters:
3352bb7acecfSBarry Smith + dm   - the `DM` object
335320f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
335447c6ae99SBarry Smith 
335547c6ae99SBarry Smith   Output Parameter:
3356bb7acecfSBarry Smith . dmc - the coarsened `DM`
335747c6ae99SBarry Smith 
335847c6ae99SBarry Smith   Level: developer
335947c6ae99SBarry Smith 
336073ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
336173ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
336247c6ae99SBarry Smith @*/
DMCoarsen(DM dm,MPI_Comm comm,DM * dmc)3363d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3364d71ae5a4SJacob Faibussowitsch {
3365b17ce1afSJed Brown   DMCoarsenHookLink link;
336647c6ae99SBarry Smith 
336747c6ae99SBarry Smith   PetscFunctionBegin;
3368171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33699566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3370dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3371b9d85ea2SLisandro Dalcin   if (*dmc) {
3372a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33739566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
337443842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33759566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3376644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33770598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3378656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33799566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3380b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33819566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3382b17ce1afSJed Brown     }
3383b9d85ea2SLisandro Dalcin   }
33849566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33857a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3387b17ce1afSJed Brown }
3388b17ce1afSJed Brown 
3389bb9467b5SJed Brown /*@C
3390b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3391b17ce1afSJed Brown 
339220f4b53cSBarry Smith   Logically Collective; No Fortran Support
3393b17ce1afSJed Brown 
33944165533cSJose E. Roman   Input Parameters:
3395bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3396b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3397bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
339820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3399b17ce1afSJed Brown 
340020f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3401bb7acecfSBarry Smith + fine   - fine level `DM`
3402bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3403b17ce1afSJed Brown - ctx    - optional user-defined function context
3404b17ce1afSJed Brown 
340520f4b53cSBarry Smith   Calling sequence of `restricthook`:
3406bb7acecfSBarry Smith + fine      - fine level `DM`
3407bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3408c833c3b5SJed Brown . rscale    - scaling vector for restriction
3409c833c3b5SJed Brown . inject    - matrix restricting by injection
3410b17ce1afSJed Brown . coarse    - coarse level DM to update
3411b17ce1afSJed Brown - ctx       - optional user-defined function context
3412b17ce1afSJed Brown 
3413b17ce1afSJed Brown   Level: advanced
3414b17ce1afSJed Brown 
3415b17ce1afSJed Brown   Notes:
3416bb7acecfSBarry 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`.
3417b17ce1afSJed Brown 
3418b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3419b17ce1afSJed Brown 
3420b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3421bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3422b17ce1afSJed Brown 
3423bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3424bb7acecfSBarry Smith 
34251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3426b17ce1afSJed Brown @*/
DMCoarsenHookAdd(DM fine,PetscErrorCode (* coarsenhook)(DM fine,DM coarse,PetscCtx ctx),PetscErrorCode (* restricthook)(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,PetscCtx ctx),PetscCtx ctx)3427*2a8381b2SBarry Smith PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM fine, DM coarse, PetscCtx ctx), PetscErrorCode (*restricthook)(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, PetscCtx ctx), PetscCtx ctx)
3428d71ae5a4SJacob Faibussowitsch {
3429b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3430b17ce1afSJed Brown 
3431b17ce1afSJed Brown   PetscFunctionBegin;
3432b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
34331e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34343ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
34351e3d8eccSJed Brown   }
34369566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3437b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3438b17ce1afSJed Brown   link->restricthook = restricthook;
3439b17ce1afSJed Brown   link->ctx          = ctx;
34400298fd71SBarry Smith   link->next         = NULL;
3441b17ce1afSJed Brown   *p                 = link;
34423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3443b17ce1afSJed Brown }
3444b17ce1afSJed Brown 
3445dc822a44SJed Brown /*@C
3446bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3447dc822a44SJed Brown 
344820f4b53cSBarry Smith   Logically Collective; No Fortran Support
3449dc822a44SJed Brown 
34504165533cSJose E. Roman   Input Parameters:
3451bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3452dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3453bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
345420f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3455dc822a44SJed Brown 
3456dc822a44SJed Brown   Level: advanced
3457dc822a44SJed Brown 
345873ff1848SBarry Smith   Notes:
345973ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
346073ff1848SBarry Smith 
346173ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3462dc822a44SJed Brown 
34631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3464dc822a44SJed Brown @*/
DMCoarsenHookRemove(DM fine,PetscErrorCode (* coarsenhook)(DM,DM,void *),PetscErrorCode (* restricthook)(DM,Mat,Vec,Mat,DM,void *),PetscCtx ctx)3465*2a8381b2SBarry Smith PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), PetscCtx ctx)
3466d71ae5a4SJacob Faibussowitsch {
3467dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3468dc822a44SJed Brown 
3469dc822a44SJed Brown   PetscFunctionBegin;
3470dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3471dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3472dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3473dc822a44SJed Brown       link = *p;
3474dc822a44SJed Brown       *p   = link->next;
34759566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3476dc822a44SJed Brown       break;
3477dc822a44SJed Brown     }
3478dc822a44SJed Brown   }
34793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3480dc822a44SJed Brown }
3481dc822a44SJed Brown 
3482b17ce1afSJed Brown /*@
3483bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3484b17ce1afSJed Brown 
3485b17ce1afSJed Brown   Collective if any hooks are
3486b17ce1afSJed Brown 
34874165533cSJose E. Roman   Input Parameters:
3488bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3489bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3490e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3491bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
349220f4b53cSBarry Smith - coarse  - coarser `DM` to update
3493b17ce1afSJed Brown 
3494b17ce1afSJed Brown   Level: developer
3495b17ce1afSJed Brown 
349673ff1848SBarry Smith   Developer Note:
3497bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3498bb7acecfSBarry Smith 
34991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3500b17ce1afSJed Brown @*/
DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)3501d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3502d71ae5a4SJacob Faibussowitsch {
3503b17ce1afSJed Brown   DMCoarsenHookLink link;
3504b17ce1afSJed Brown 
3505b17ce1afSJed Brown   PetscFunctionBegin;
3506b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
35071baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3508b17ce1afSJed Brown   }
35093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
351047c6ae99SBarry Smith }
351147c6ae99SBarry Smith 
3512bb9467b5SJed Brown /*@C
351373ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
35145dbd56e3SPeter Brune 
351520f4b53cSBarry Smith   Logically Collective; No Fortran Support
35165dbd56e3SPeter Brune 
35174165533cSJose E. Roman   Input Parameters:
3518bb7acecfSBarry Smith + global       - global `DM`
3519bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
35205dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
352120f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
35225dbd56e3SPeter Brune 
352320f4b53cSBarry Smith   Calling sequence of `ddhook`:
3524bb7acecfSBarry Smith + global - global `DM`
352573ff1848SBarry Smith . block  - subdomain `DM`
3526ec4806b8SPeter Brune - ctx    - optional user-defined function context
3527ec4806b8SPeter Brune 
352820f4b53cSBarry Smith   Calling sequence of `restricthook`:
3529bb7acecfSBarry Smith + global - global `DM`
353073ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
353173ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
353273ff1848SBarry Smith . block  - subdomain `DM`
35335dbd56e3SPeter Brune - ctx    - optional user-defined function context
35345dbd56e3SPeter Brune 
35355dbd56e3SPeter Brune   Level: advanced
35365dbd56e3SPeter Brune 
35375dbd56e3SPeter Brune   Notes:
353873ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
35395dbd56e3SPeter Brune 
35405dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
35415dbd56e3SPeter Brune 
35425dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3543bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
35445dbd56e3SPeter Brune 
354573ff1848SBarry Smith   Developer Note:
354673ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
354773ff1848SBarry Smith 
354873ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
35495dbd56e3SPeter Brune @*/
DMSubDomainHookAdd(DM global,PetscErrorCode (* ddhook)(DM global,DM block,PetscCtx ctx),PetscErrorCode (* restricthook)(DM global,VecScatter out,VecScatter in,DM block,PetscCtx ctx),PetscCtx ctx)3550*2a8381b2SBarry Smith PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM global, DM block, PetscCtx ctx), PetscErrorCode (*restricthook)(DM global, VecScatter out, VecScatter in, DM block, PetscCtx ctx), PetscCtx ctx)
3551d71ae5a4SJacob Faibussowitsch {
3552be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
35535dbd56e3SPeter Brune 
35545dbd56e3SPeter Brune   PetscFunctionBegin;
35555dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3556b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35573ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3558b3a6b972SJed Brown   }
35599566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35605dbd56e3SPeter Brune   link->restricthook = restricthook;
3561be081cd6SPeter Brune   link->ddhook       = ddhook;
35625dbd56e3SPeter Brune   link->ctx          = ctx;
35630298fd71SBarry Smith   link->next         = NULL;
35645dbd56e3SPeter Brune   *p                 = link;
35653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35665dbd56e3SPeter Brune }
35675dbd56e3SPeter Brune 
3568b3a6b972SJed Brown /*@C
356973ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3570b3a6b972SJed Brown 
357120f4b53cSBarry Smith   Logically Collective; No Fortran Support
3572b3a6b972SJed Brown 
35734165533cSJose E. Roman   Input Parameters:
3574bb7acecfSBarry Smith + global       - global `DM`
3575bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3576b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
357720f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3578b3a6b972SJed Brown 
3579b3a6b972SJed Brown   Level: advanced
3580b3a6b972SJed Brown 
358173ff1848SBarry Smith   Note:
358273ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
358373ff1848SBarry Smith 
358473ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
358573ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3586b3a6b972SJed Brown @*/
DMSubDomainHookRemove(DM global,PetscErrorCode (* ddhook)(DM,DM,void *),PetscErrorCode (* restricthook)(DM,VecScatter,VecScatter,DM,void *),PetscCtx ctx)3587*2a8381b2SBarry Smith PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), PetscCtx ctx)
3588d71ae5a4SJacob Faibussowitsch {
3589b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3590b3a6b972SJed Brown 
3591b3a6b972SJed Brown   PetscFunctionBegin;
3592b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3593b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3594b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3595b3a6b972SJed Brown       link = *p;
3596b3a6b972SJed Brown       *p   = link->next;
35979566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3598b3a6b972SJed Brown       break;
3599b3a6b972SJed Brown     }
3600b3a6b972SJed Brown   }
36013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3602b3a6b972SJed Brown }
3603b3a6b972SJed Brown 
36045dbd56e3SPeter Brune /*@
360573ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
36065dbd56e3SPeter Brune 
36075dbd56e3SPeter Brune   Collective if any hooks are
36085dbd56e3SPeter Brune 
36094165533cSJose E. Roman   Input Parameters:
3610a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3611a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3612a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3613a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
36145dbd56e3SPeter Brune 
36155dbd56e3SPeter Brune   Level: developer
36165dbd56e3SPeter Brune 
361773ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
36185dbd56e3SPeter Brune @*/
DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)3619d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3620d71ae5a4SJacob Faibussowitsch {
3621be081cd6SPeter Brune   DMSubDomainHookLink link;
36225dbd56e3SPeter Brune 
36235dbd56e3SPeter Brune   PetscFunctionBegin;
3624be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
36251baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
36265dbd56e3SPeter Brune   }
36273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36285dbd56e3SPeter Brune }
36295dbd56e3SPeter Brune 
36305fe1f584SPeter Brune /*@
3631bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
36325fe1f584SPeter Brune 
36335fe1f584SPeter Brune   Not Collective
36345fe1f584SPeter Brune 
36355fe1f584SPeter Brune   Input Parameter:
3636bb7acecfSBarry Smith . dm - the `DM` object
36375fe1f584SPeter Brune 
36385fe1f584SPeter Brune   Output Parameter:
36396a7d9d85SPeter Brune . level - number of coarsenings
36405fe1f584SPeter Brune 
36415fe1f584SPeter Brune   Level: developer
36425fe1f584SPeter Brune 
36431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36445fe1f584SPeter Brune @*/
DMGetCoarsenLevel(DM dm,PetscInt * level)3645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3646d71ae5a4SJacob Faibussowitsch {
36475fe1f584SPeter Brune   PetscFunctionBegin;
36485fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36494f572ea9SToby Isaac   PetscAssertPointer(level, 2);
36505fe1f584SPeter Brune   *level = dm->leveldown;
36513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36525fe1f584SPeter Brune }
36535fe1f584SPeter Brune 
36549a64c4a8SMatthew G. Knepley /*@
3655bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36569a64c4a8SMatthew G. Knepley 
365720f4b53cSBarry Smith   Collective
36589a64c4a8SMatthew G. Knepley 
36599a64c4a8SMatthew G. Knepley   Input Parameters:
3660bb7acecfSBarry Smith + dm    - the `DM` object
36619a64c4a8SMatthew G. Knepley - level - number of coarsenings
36629a64c4a8SMatthew G. Knepley 
36639a64c4a8SMatthew G. Knepley   Level: developer
36649a64c4a8SMatthew G. Knepley 
3665bb7acecfSBarry Smith   Note:
3666bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3667bb7acecfSBarry Smith 
366842747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36699a64c4a8SMatthew G. Knepley @*/
DMSetCoarsenLevel(DM dm,PetscInt level)3670d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3671d71ae5a4SJacob Faibussowitsch {
36729a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36739a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36749a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36769a64c4a8SMatthew G. Knepley }
36779a64c4a8SMatthew G. Knepley 
3678cc4c1da9SBarry Smith /*@
3679bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
368047c6ae99SBarry Smith 
368120f4b53cSBarry Smith   Collective
368247c6ae99SBarry Smith 
3683d8d19677SJose E. Roman   Input Parameters:
3684bb7acecfSBarry Smith + dm      - the `DM` object
368547c6ae99SBarry Smith - nlevels - the number of levels of refinement
368647c6ae99SBarry Smith 
368747c6ae99SBarry Smith   Output Parameter:
3688bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
368947c6ae99SBarry Smith 
369047c6ae99SBarry Smith   Level: developer
369147c6ae99SBarry Smith 
36921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
369347c6ae99SBarry Smith @*/
DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])3694d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3695d71ae5a4SJacob Faibussowitsch {
369647c6ae99SBarry Smith   PetscFunctionBegin;
3697171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36987a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36993ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
37004f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3701213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
370247c6ae99SBarry Smith     PetscInt i;
370347c6ae99SBarry Smith 
37049566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
370548a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3706213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
37073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
370847c6ae99SBarry Smith }
370947c6ae99SBarry Smith 
3710cc4c1da9SBarry Smith /*@
3711bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
371247c6ae99SBarry Smith 
371320f4b53cSBarry Smith   Collective
371447c6ae99SBarry Smith 
3715d8d19677SJose E. Roman   Input Parameters:
3716bb7acecfSBarry Smith + dm      - the `DM` object
371747c6ae99SBarry Smith - nlevels - the number of levels of coarsening
371847c6ae99SBarry Smith 
371947c6ae99SBarry Smith   Output Parameter:
3720bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
372147c6ae99SBarry Smith 
372247c6ae99SBarry Smith   Level: developer
372347c6ae99SBarry Smith 
37241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
372547c6ae99SBarry Smith @*/
DMCoarsenHierarchy(DM dm,PetscInt nlevels,DM dmc[])3726d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3727d71ae5a4SJacob Faibussowitsch {
372847c6ae99SBarry Smith   PetscFunctionBegin;
3729171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37307a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
37313ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
37324f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3733213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
373447c6ae99SBarry Smith     PetscInt i;
373547c6ae99SBarry Smith 
37369566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
373748a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3738213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
37393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374047c6ae99SBarry Smith }
374147c6ae99SBarry Smith 
37421a266240SBarry Smith /*@C
3743bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
37441a266240SBarry Smith 
3745bb7acecfSBarry Smith   Logically Collective if the function is collective
37461a266240SBarry Smith 
37471a266240SBarry Smith   Input Parameters:
3748bb7acecfSBarry Smith + dm      - the `DM` object
374949abdd8aSBarry Smith - destroy - the destroy function, see `PetscCtxDestroyFn` for the calling sequence
37501a266240SBarry Smith 
37511a266240SBarry Smith   Level: intermediate
37521a266240SBarry Smith 
375349abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`,
375449abdd8aSBarry Smith           `DMGetApplicationContext()`, `PetscCtxDestroyFn`
3755f07f9ceaSJed Brown @*/
DMSetApplicationContextDestroy(DM dm,PetscCtxDestroyFn * destroy)375649abdd8aSBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscCtxDestroyFn *destroy)
3757d71ae5a4SJacob Faibussowitsch {
37581a266240SBarry Smith   PetscFunctionBegin;
3759171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37601a266240SBarry Smith   dm->ctxdestroy = destroy;
37613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37621a266240SBarry Smith }
37631a266240SBarry Smith 
3764b07ff414SBarry Smith /*@
3765bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
376647c6ae99SBarry Smith 
376747c6ae99SBarry Smith   Not Collective
376847c6ae99SBarry Smith 
376947c6ae99SBarry Smith   Input Parameters:
3770bb7acecfSBarry Smith + dm  - the `DM` object
377147c6ae99SBarry Smith - ctx - the user context
377247c6ae99SBarry Smith 
377347c6ae99SBarry Smith   Level: intermediate
377447c6ae99SBarry Smith 
3775ce78bad3SBarry Smith   Note:
377635cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3777e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3778e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3779bb7acecfSBarry Smith 
3780*2a8381b2SBarry Smith   Fortran Notes:
3781*2a8381b2SBarry Smith   This only works when the context is a Fortran derived type or a `PetscObject`. Declare `ctx` with
3782*2a8381b2SBarry Smith .vb
3783*2a8381b2SBarry Smith   type(tUsertype), pointer :: ctx
3784*2a8381b2SBarry Smith .ve
3785ce78bad3SBarry Smith 
378660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
378747c6ae99SBarry Smith @*/
DMSetApplicationContext(DM dm,PetscCtx ctx)3788*2a8381b2SBarry Smith PetscErrorCode DMSetApplicationContext(DM dm, PetscCtx ctx)
3789d71ae5a4SJacob Faibussowitsch {
379047c6ae99SBarry Smith   PetscFunctionBegin;
3791171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379247c6ae99SBarry Smith   dm->ctx = ctx;
37933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379447c6ae99SBarry Smith }
379547c6ae99SBarry Smith 
379647c6ae99SBarry Smith /*@
3797ce78bad3SBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object provided with `DMSetApplicationContext()`
379847c6ae99SBarry Smith 
379947c6ae99SBarry Smith   Not Collective
380047c6ae99SBarry Smith 
380147c6ae99SBarry Smith   Input Parameter:
3802bb7acecfSBarry Smith . dm - the `DM` object
380347c6ae99SBarry Smith 
380447c6ae99SBarry Smith   Output Parameter:
3805ce78bad3SBarry Smith . ctx - a pointer to the user context
380647c6ae99SBarry Smith 
380747c6ae99SBarry Smith   Level: intermediate
380847c6ae99SBarry Smith 
3809bb7acecfSBarry Smith   Note:
381035cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3811bb7acecfSBarry Smith 
3812ce78bad3SBarry Smith   Fortran Notes:
3813ce78bad3SBarry Smith   This only works when the context is a Fortran derived type (it cannot be a `PetscObject`) and you **must** write a Fortran interface definition for this
3814ce78bad3SBarry Smith   function that tells the Fortran compiler the derived data type that is returned as the `ctx` argument. For example,
3815ce78bad3SBarry Smith .vb
3816ce78bad3SBarry Smith   Interface DMGetApplicationContext
3817ce78bad3SBarry Smith     Subroutine DMGetApplicationContext(dm,ctx,ierr)
3818ce78bad3SBarry Smith   #include <petsc/finclude/petscdm.h>
3819ce78bad3SBarry Smith       use petscdm
3820ce78bad3SBarry Smith       DM dm
3821ce78bad3SBarry Smith       type(tUsertype), pointer :: ctx
3822ce78bad3SBarry Smith       PetscErrorCode ierr
3823ce78bad3SBarry Smith     End Subroutine
3824ce78bad3SBarry Smith   End Interface DMGetApplicationContext
3825ce78bad3SBarry Smith .ve
3826ce78bad3SBarry Smith 
3827bfe80ac4SPierre Jolivet   The prototype for `ctx` must be
3828ce78bad3SBarry Smith .vb
3829ce78bad3SBarry Smith   type(tUsertype), pointer :: ctx
3830ce78bad3SBarry Smith .ve
3831ce78bad3SBarry Smith 
383242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
383347c6ae99SBarry Smith @*/
DMGetApplicationContext(DM dm,PetscCtxRt ctx)3834*2a8381b2SBarry Smith PetscErrorCode DMGetApplicationContext(DM dm, PetscCtxRt ctx)
3835d71ae5a4SJacob Faibussowitsch {
383647c6ae99SBarry Smith   PetscFunctionBegin;
3837171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38381b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
38393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
384047c6ae99SBarry Smith }
384147c6ae99SBarry Smith 
384208da532bSDmitry Karpeev /*@C
3843bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
384408da532bSDmitry Karpeev 
384520f4b53cSBarry Smith   Logically Collective
384608da532bSDmitry Karpeev 
3847d8d19677SJose E. Roman   Input Parameters:
384808da532bSDmitry Karpeev + dm - the DM object
3849ce78bad3SBarry Smith - f  - the function that computes variable bounds used by `SNESVI` (use `NULL` to cancel a previous function that was set)
385008da532bSDmitry Karpeev 
385108da532bSDmitry Karpeev   Level: intermediate
385208da532bSDmitry Karpeev 
3853ce78bad3SBarry Smith   Developer Note:
3854ce78bad3SBarry Smith   Should be called `DMSetComputeVIBounds()` or something similar
3855ce78bad3SBarry Smith 
38561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3857db781477SPatrick Sanan          `DMSetJacobian()`
385808da532bSDmitry Karpeev @*/
DMSetVariableBounds(DM dm,PetscErrorCode (* f)(DM,Vec,Vec))3859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3860d71ae5a4SJacob Faibussowitsch {
386108da532bSDmitry Karpeev   PetscFunctionBegin;
38625a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
386308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
38643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
386508da532bSDmitry Karpeev }
386608da532bSDmitry Karpeev 
386708da532bSDmitry Karpeev /*@
3868bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
386908da532bSDmitry Karpeev 
387008da532bSDmitry Karpeev   Not Collective
387108da532bSDmitry Karpeev 
387208da532bSDmitry Karpeev   Input Parameter:
3873bb7acecfSBarry Smith . dm - the `DM` object to destroy
387408da532bSDmitry Karpeev 
387508da532bSDmitry Karpeev   Output Parameter:
3876bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
387708da532bSDmitry Karpeev 
387808da532bSDmitry Karpeev   Level: developer
387908da532bSDmitry Karpeev 
38801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
388108da532bSDmitry Karpeev @*/
DMHasVariableBounds(DM dm,PetscBool * flg)3882d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3883d71ae5a4SJacob Faibussowitsch {
388408da532bSDmitry Karpeev   PetscFunctionBegin;
38855a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38864f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
388708da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
388908da532bSDmitry Karpeev }
389008da532bSDmitry Karpeev 
3891cc4c1da9SBarry Smith /*@
3892bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
389308da532bSDmitry Karpeev 
389420f4b53cSBarry Smith   Logically Collective
389508da532bSDmitry Karpeev 
3896f899ff85SJose E. Roman   Input Parameter:
3897bb7acecfSBarry Smith . dm - the `DM` object
389808da532bSDmitry Karpeev 
389960225df5SJacob Faibussowitsch   Output Parameters:
390008da532bSDmitry Karpeev + xl - lower bound
390108da532bSDmitry Karpeev - xu - upper bound
390208da532bSDmitry Karpeev 
3903907376e6SBarry Smith   Level: advanced
3904907376e6SBarry Smith 
390520f4b53cSBarry Smith   Note:
390695452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
390708da532bSDmitry Karpeev 
39081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
390908da532bSDmitry Karpeev @*/
DMComputeVariableBounds(DM dm,Vec xl,Vec xu)3910d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3911d71ae5a4SJacob Faibussowitsch {
391208da532bSDmitry Karpeev   PetscFunctionBegin;
39135a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
391408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
39155a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3916dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
39173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
391808da532bSDmitry Karpeev }
391908da532bSDmitry Karpeev 
3920b0ae01b7SPeter Brune /*@
3921bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3922b0ae01b7SPeter Brune 
3923b0ae01b7SPeter Brune   Not Collective
3924b0ae01b7SPeter Brune 
3925b0ae01b7SPeter Brune   Input Parameter:
3926b0ae01b7SPeter Brune . dm - the DM object
3927b0ae01b7SPeter Brune 
3928b0ae01b7SPeter Brune   Output Parameter:
3929bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3930b0ae01b7SPeter Brune 
3931b0ae01b7SPeter Brune   Level: developer
3932b0ae01b7SPeter Brune 
39331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3934b0ae01b7SPeter Brune @*/
DMHasColoring(DM dm,PetscBool * flg)3935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3936d71ae5a4SJacob Faibussowitsch {
3937b0ae01b7SPeter Brune   PetscFunctionBegin;
39385a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39394f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3940b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
39413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3942b0ae01b7SPeter Brune }
3943b0ae01b7SPeter Brune 
39443ad4599aSBarry Smith /*@
3945bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
39463ad4599aSBarry Smith 
39473ad4599aSBarry Smith   Not Collective
39483ad4599aSBarry Smith 
39493ad4599aSBarry Smith   Input Parameter:
3950bb7acecfSBarry Smith . dm - the `DM` object
39513ad4599aSBarry Smith 
39523ad4599aSBarry Smith   Output Parameter:
3953bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
39543ad4599aSBarry Smith 
39553ad4599aSBarry Smith   Level: developer
39563ad4599aSBarry Smith 
39571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
39583ad4599aSBarry Smith @*/
DMHasCreateRestriction(DM dm,PetscBool * flg)3959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3960d71ae5a4SJacob Faibussowitsch {
39613ad4599aSBarry Smith   PetscFunctionBegin;
39625a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39634f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
39643ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
39653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39663ad4599aSBarry Smith }
39673ad4599aSBarry Smith 
3968a7058e45SLawrence Mitchell /*@
3969bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3970a7058e45SLawrence Mitchell 
3971a7058e45SLawrence Mitchell   Not Collective
3972a7058e45SLawrence Mitchell 
3973a7058e45SLawrence Mitchell   Input Parameter:
3974bb7acecfSBarry Smith . dm - the `DM` object
3975a7058e45SLawrence Mitchell 
3976a7058e45SLawrence Mitchell   Output Parameter:
3977bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3978a7058e45SLawrence Mitchell 
3979a7058e45SLawrence Mitchell   Level: developer
3980a7058e45SLawrence Mitchell 
39811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3982a7058e45SLawrence Mitchell @*/
DMHasCreateInjection(DM dm,PetscBool * flg)3983d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3984d71ae5a4SJacob Faibussowitsch {
3985a7058e45SLawrence Mitchell   PetscFunctionBegin;
39865a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39874f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3988dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3989ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3991a7058e45SLawrence Mitchell }
3992a7058e45SLawrence Mitchell 
39930298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3994264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3995264ace61SBarry Smith 
3996cc4c1da9SBarry Smith /*@
3997bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3998264ace61SBarry Smith 
399920f4b53cSBarry Smith   Collective
4000264ace61SBarry Smith 
4001264ace61SBarry Smith   Input Parameters:
4002bb7acecfSBarry Smith + dm     - The `DM` object
4003bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
4004264ace61SBarry Smith 
4005264ace61SBarry Smith   Options Database Key:
4006bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
4007264ace61SBarry Smith 
4008264ace61SBarry Smith   Level: intermediate
4009264ace61SBarry Smith 
4010bb7acecfSBarry Smith   Note:
40110462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
4012bb7acecfSBarry Smith 
40131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
4014264ace61SBarry Smith @*/
DMSetType(DM dm,DMType method)4015d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
4016d71ae5a4SJacob Faibussowitsch {
4017264ace61SBarry Smith   PetscErrorCode (*r)(DM);
4018264ace61SBarry Smith   PetscBool match;
4019264ace61SBarry Smith 
4020264ace61SBarry Smith   PetscFunctionBegin;
4021264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40229566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
40233ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
4024264ace61SBarry Smith 
40259566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
40269566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
40277a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
4028264ace61SBarry Smith 
4029dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
40309566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
40319566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
40329566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
40333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4034264ace61SBarry Smith }
4035264ace61SBarry Smith 
4036cc4c1da9SBarry Smith /*@
4037bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
4038264ace61SBarry Smith 
4039264ace61SBarry Smith   Not Collective
4040264ace61SBarry Smith 
4041264ace61SBarry Smith   Input Parameter:
4042bb7acecfSBarry Smith . dm - The `DM`
4043264ace61SBarry Smith 
4044264ace61SBarry Smith   Output Parameter:
4045bb7acecfSBarry Smith . type - The `DMType` name
4046264ace61SBarry Smith 
4047264ace61SBarry Smith   Level: intermediate
4048264ace61SBarry Smith 
40491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
4050264ace61SBarry Smith @*/
DMGetType(DM dm,DMType * type)4051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
4052d71ae5a4SJacob Faibussowitsch {
4053264ace61SBarry Smith   PetscFunctionBegin;
4054264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40554f572ea9SToby Isaac   PetscAssertPointer(type, 2);
40569566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
4057264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
40583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4059264ace61SBarry Smith }
4060264ace61SBarry Smith 
40615d83a8b1SBarry Smith /*@
4062bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
406367a56275SMatthew G Knepley 
406420f4b53cSBarry Smith   Collective
406567a56275SMatthew G Knepley 
406667a56275SMatthew G Knepley   Input Parameters:
4067bb7acecfSBarry Smith + dm      - the `DM`
4068bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
406967a56275SMatthew G Knepley 
407067a56275SMatthew G Knepley   Output Parameter:
4071bb7acecfSBarry Smith . M - pointer to new `DM`
407267a56275SMatthew G Knepley 
407320f4b53cSBarry Smith   Level: intermediate
407420f4b53cSBarry Smith 
407573ff1848SBarry Smith   Note:
4076bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
4077bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
4078bb7acecfSBarry Smith   of the input `DM`.
407967a56275SMatthew G Knepley 
40801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
408167a56275SMatthew G Knepley @*/
DMConvert(DM dm,DMType newtype,DM * M)4082d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
4083d71ae5a4SJacob Faibussowitsch {
408467a56275SMatthew G Knepley   DM        B;
408567a56275SMatthew G Knepley   char      convname[256];
4086c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
408767a56275SMatthew G Knepley 
408867a56275SMatthew G Knepley   PetscFunctionBegin;
408967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409067a56275SMatthew G Knepley   PetscValidType(dm, 1);
40914f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40929566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40939566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4094c067b6caSMatthew G. Knepley   if (sametype) {
4095c067b6caSMatthew G. Knepley     *M = dm;
40969566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40973ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4098c067b6caSMatthew G. Knepley   } else {
40990298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
410067a56275SMatthew G Knepley 
410167a56275SMatthew G Knepley     /*
410267a56275SMatthew G Knepley        Order of precedence:
410367a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
410467a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
410567a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
410667a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
410767a56275SMatthew G Knepley        5) Use a really basic converter.
410867a56275SMatthew G Knepley     */
410967a56275SMatthew G Knepley 
411067a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
41119566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
41129566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
41139566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
41149566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
41159566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
41169566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
411767a56275SMatthew G Knepley     if (conv) goto foundconv;
411867a56275SMatthew G Knepley 
411967a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
41209566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
41219566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
41229566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
41239566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
41249566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
41259566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
41269566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
41279566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
412867a56275SMatthew G Knepley     if (conv) {
41299566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
413067a56275SMatthew G Knepley       goto foundconv;
413167a56275SMatthew G Knepley     }
413267a56275SMatthew G Knepley 
413367a56275SMatthew G Knepley #if 0
413467a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
413567a56275SMatthew G Knepley     conv = B->ops->convertfrom;
41369566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
413767a56275SMatthew G Knepley     if (conv) goto foundconv;
413867a56275SMatthew G Knepley 
413967a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
41403a7d0413SPierre Jolivet     if (dm->ops->convert) conv = dm->ops->convert;
414167a56275SMatthew G Knepley     if (conv) goto foundconv;
414267a56275SMatthew G Knepley #endif
414367a56275SMatthew G Knepley 
414467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
414598921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
414667a56275SMatthew G Knepley 
414767a56275SMatthew G Knepley   foundconv:
41489566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
41499566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
415012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
415190b157c4SStefano Zampini     {
41524fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
41536858538eSMatthew G. Knepley 
41544fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
41554fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4156c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
41579566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
41589566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
41599566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
41609566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
416112fa691eSMatthew G. Knepley     }
41629566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
416367a56275SMatthew G Knepley   }
41649566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
41653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
416667a56275SMatthew G Knepley }
4167264ace61SBarry Smith 
4168264ace61SBarry Smith /*@C
4169bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
41701c84c290SBarry Smith 
4171cc4c1da9SBarry Smith   Not Collective, No Fortran Support
41721c84c290SBarry Smith 
41731c84c290SBarry Smith   Input Parameters:
417420f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
417520f4b53cSBarry Smith - function - The creation routine itself
417620f4b53cSBarry Smith 
417720f4b53cSBarry Smith   Level: advanced
41781c84c290SBarry Smith 
417973ff1848SBarry Smith   Note:
418020f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
41811c84c290SBarry Smith 
418260225df5SJacob Faibussowitsch   Example Usage:
41831c84c290SBarry Smith .vb
4184bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41851c84c290SBarry Smith .ve
41861c84c290SBarry Smith 
418720f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41881c84c290SBarry Smith .vb
41891c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41901c84c290SBarry Smith     DMSetType(DM,"my_da");
41911c84c290SBarry Smith .ve
41921c84c290SBarry Smith   or at runtime via the option
41931c84c290SBarry Smith .vb
41941c84c290SBarry Smith     -da_type my_da
41951c84c290SBarry Smith .ve
4196264ace61SBarry Smith 
41971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4198264ace61SBarry Smith @*/
DMRegister(const char sname[],PetscErrorCode (* function)(DM))4199d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4200d71ae5a4SJacob Faibussowitsch {
4201264ace61SBarry Smith   PetscFunctionBegin;
42029566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
42039566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
42043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4205264ace61SBarry Smith }
4206264ace61SBarry Smith 
4207ffeef943SBarry Smith /*@
4208bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4209b859378eSBarry Smith 
421020f4b53cSBarry Smith   Collective
4211b859378eSBarry Smith 
4212b859378eSBarry Smith   Input Parameters:
4213bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4214bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4215bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4216bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4217b859378eSBarry Smith 
4218b859378eSBarry Smith   Level: intermediate
4219b859378eSBarry Smith 
4220b859378eSBarry Smith   Notes:
422155849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4222b859378eSBarry Smith 
4223bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4224bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4225bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4226cd7e8a5eSksagiyam 
42271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4228b859378eSBarry Smith @*/
DMLoad(DM newdm,PetscViewer viewer)4229d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4230d71ae5a4SJacob Faibussowitsch {
42319331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4232b859378eSBarry Smith 
4233b859378eSBarry Smith   PetscFunctionBegin;
4234b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4235b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
42369566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
42379566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
42389566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
42399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
42409331c7a4SMatthew G. Knepley   if (isbinary) {
42419331c7a4SMatthew G. Knepley     PetscInt classid;
42429331c7a4SMatthew G. Knepley     char     type[256];
4243b859378eSBarry Smith 
42449566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
4245835f2295SStefano Zampini     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %" PetscInt_FMT, classid);
42469566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
42479566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4248dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
42499331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4250dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
42519331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
42529566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
42533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4254b859378eSBarry Smith }
4255b859378eSBarry Smith 
42564ee01570SBarry Smith /* FEM Support */
42577da65231SMatthew G Knepley 
DMPrintCellIndices(PetscInt c,const char name[],PetscInt len,const PetscInt x[])42582ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
42592ecf6ec3SMatthew G. Knepley {
42602ecf6ec3SMatthew G. Knepley   PetscInt f;
42612ecf6ec3SMatthew G. Knepley 
42622ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
42632ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42642ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
42652ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42662ecf6ec3SMatthew G. Knepley }
42672ecf6ec3SMatthew G. Knepley 
DMPrintCellVector(PetscInt c,const char name[],PetscInt len,const PetscScalar x[])4268d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4269d71ae5a4SJacob Faibussowitsch {
42701d47ebbbSSatish Balay   PetscInt f;
42711b30c384SMatthew G Knepley 
42727da65231SMatthew G Knepley   PetscFunctionBegin;
427363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
427448a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
42753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42767da65231SMatthew G Knepley }
42777da65231SMatthew G Knepley 
DMPrintCellVectorReal(PetscInt c,const char name[],PetscInt len,const PetscReal x[])42785962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
42795962854dSMatthew G. Knepley {
42805962854dSMatthew G. Knepley   PetscInt f;
42815962854dSMatthew G. Knepley 
42825962854dSMatthew G. Knepley   PetscFunctionBegin;
42835962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42845962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42855962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42865962854dSMatthew G. Knepley }
42875962854dSMatthew G. Knepley 
DMPrintCellMatrix(PetscInt c,const char name[],PetscInt rows,PetscInt cols,const PetscScalar A[])4288d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4289d71ae5a4SJacob Faibussowitsch {
42901b30c384SMatthew G Knepley   PetscInt f, g;
42917da65231SMatthew G Knepley 
42927da65231SMatthew G Knepley   PetscFunctionBegin;
429363a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42941d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42959566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
429648a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42979566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42987da65231SMatthew G Knepley   }
42993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43007da65231SMatthew G Knepley }
4301e7c4fc90SDmitry Karpeev 
DMPrintLocalVec(DM dm,const char name[],PetscReal tol,Vec X)4302d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4303d71ae5a4SJacob Faibussowitsch {
43040c5b8624SToby Isaac   PetscInt           localSize, bs;
43050c5b8624SToby Isaac   PetscMPIInt        size;
43060c5b8624SToby Isaac   Vec                x, xglob;
43070c5b8624SToby Isaac   const PetscScalar *xarray;
4308e759306cSMatthew G. Knepley 
4309e759306cSMatthew G. Knepley   PetscFunctionBegin;
43109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
43119566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
43129566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
431393ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
43149566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
43150c5b8624SToby Isaac   if (size > 1) {
43169566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
43179566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
43189566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
43199566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
43200c5b8624SToby Isaac   } else {
43210c5b8624SToby Isaac     xglob = x;
43220c5b8624SToby Isaac   }
43239566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
43240c5b8624SToby Isaac   if (size > 1) {
43259566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
43269566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
43270c5b8624SToby Isaac   }
43289566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
43293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4330e759306cSMatthew G. Knepley }
4331e759306cSMatthew G. Knepley 
433288ed4aceSMatthew G Knepley /*@
4333bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
433488ed4aceSMatthew G Knepley 
433588ed4aceSMatthew G Knepley   Input Parameter:
4336bb7acecfSBarry Smith . dm - The `DM`
433788ed4aceSMatthew G Knepley 
433888ed4aceSMatthew G Knepley   Output Parameter:
4339bb7acecfSBarry Smith . section - The `PetscSection`
434088ed4aceSMatthew G Knepley 
434120f4b53cSBarry Smith   Options Database Key:
4342bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4343e5893cccSMatthew G. Knepley 
434488ed4aceSMatthew G Knepley   Level: intermediate
434588ed4aceSMatthew G Knepley 
4346bb7acecfSBarry Smith   Note:
4347bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
434888ed4aceSMatthew G Knepley 
43491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
435088ed4aceSMatthew G Knepley @*/
DMGetLocalSection(DM dm,PetscSection * section)4351d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4352d71ae5a4SJacob Faibussowitsch {
435388ed4aceSMatthew G Knepley   PetscFunctionBegin;
435488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43554f572ea9SToby Isaac   PetscAssertPointer(section, 2);
43561bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4357e5e52638SMatthew G. Knepley     PetscInt d;
4358e5e52638SMatthew G. Knepley 
435945480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
436045480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
436145480ffeSMatthew G. Knepley       PetscViewer       viewer;
436245480ffeSMatthew G. Knepley       PetscViewerFormat format;
436345480ffeSMatthew G. Knepley       PetscBool         flg;
436445480ffeSMatthew G. Knepley 
4365648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43669566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
436745480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43689566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43699566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
437045480ffeSMatthew G. Knepley       }
437145480ffeSMatthew G. Knepley       if (flg) {
43729566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43739566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4374648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
437545480ffeSMatthew G. Knepley       }
437645480ffeSMatthew G. Knepley     }
4377dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43789566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43792f0f8703SMatthew G. Knepley   }
43801bb6d2a8SBarry Smith   *section = dm->localSection;
43813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
438288ed4aceSMatthew G Knepley }
438388ed4aceSMatthew G Knepley 
438488ed4aceSMatthew G Knepley /*@
4385bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
438688ed4aceSMatthew G Knepley 
438788ed4aceSMatthew G Knepley   Input Parameters:
4388bb7acecfSBarry Smith + dm      - The `DM`
4389bb7acecfSBarry Smith - section - The `PetscSection`
439088ed4aceSMatthew G Knepley 
439188ed4aceSMatthew G Knepley   Level: intermediate
439288ed4aceSMatthew G Knepley 
4393bb7acecfSBarry Smith   Note:
4394bb7acecfSBarry Smith   Any existing Section will be destroyed
439588ed4aceSMatthew G Knepley 
43961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
439788ed4aceSMatthew G Knepley @*/
DMSetLocalSection(DM dm,PetscSection section)4398d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4399d71ae5a4SJacob Faibussowitsch {
4400c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4401af122d2aSMatthew G Knepley   PetscInt f;
440288ed4aceSMatthew G Knepley 
440388ed4aceSMatthew G Knepley   PetscFunctionBegin;
440488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4405b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44079566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
44081bb6d2a8SBarry Smith   dm->localSection = section;
44099566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4410af122d2aSMatthew G Knepley   if (numFields) {
44119566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4412af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
44130f21e855SMatthew G. Knepley       PetscObject disc;
4414af122d2aSMatthew G Knepley       const char *name;
4415af122d2aSMatthew G Knepley 
44169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
44179566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
44189566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4419af122d2aSMatthew G Knepley     }
4420af122d2aSMatthew G Knepley   }
442150350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
442250350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
44239566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
442450350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
442552947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
442650350c8eSStefano Zampini 
442750350c8eSStefano Zampini   /* Clear scratch vectors */
442850350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
442950350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
443050350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
443150350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
44323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
443388ed4aceSMatthew G Knepley }
443488ed4aceSMatthew G Knepley 
4435adc21957SMatthew G. Knepley /*@C
4436d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4437adc21957SMatthew G. Knepley 
4438adc21957SMatthew G. Knepley   Input Parameter:
4439adc21957SMatthew G. Knepley . dm - The `DM`
4440adc21957SMatthew G. Knepley 
44419cde84edSJose E. Roman   Output Parameters:
4442adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4443b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4444adc21957SMatthew G. Knepley 
4445adc21957SMatthew G. Knepley   Level: developer
4446adc21957SMatthew G. Knepley 
4447adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4448adc21957SMatthew G. Knepley @*/
DMCreateSectionPermutation(DM dm,IS * perm,PetscBT * blockStarts)4449adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4450adc21957SMatthew G. Knepley {
4451adc21957SMatthew G. Knepley   PetscFunctionBegin;
4452adc21957SMatthew G. Knepley   *perm        = NULL;
4453adc21957SMatthew G. Knepley   *blockStarts = NULL;
4454adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4455adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4456adc21957SMatthew G. Knepley }
4457adc21957SMatthew G. Knepley 
44589435951eSToby Isaac /*@
4459bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44609435951eSToby Isaac 
446120f4b53cSBarry Smith   not Collective
4462e228b242SToby Isaac 
44639435951eSToby Isaac   Input Parameter:
4464bb7acecfSBarry Smith . dm - The `DM`
44659435951eSToby Isaac 
4466d8d19677SJose E. Roman   Output Parameters:
446720f4b53cSBarry 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.
446820f4b53cSBarry 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.
446979769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44709435951eSToby Isaac 
44719435951eSToby Isaac   Level: advanced
44729435951eSToby Isaac 
4473bb7acecfSBarry Smith   Note:
4474bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44759435951eSToby Isaac 
44761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44779435951eSToby Isaac @*/
DMGetDefaultConstraints(DM dm,PetscSection * section,Mat * mat,Vec * bias)4478d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4479d71ae5a4SJacob Faibussowitsch {
44809435951eSToby Isaac   PetscFunctionBegin;
44819435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4482dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44833b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44843b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
448579769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44879435951eSToby Isaac }
44889435951eSToby Isaac 
44899435951eSToby Isaac /*@
4490bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44919435951eSToby Isaac 
449220f4b53cSBarry Smith   Collective
4493e228b242SToby Isaac 
44949435951eSToby Isaac   Input Parameters:
4495bb7acecfSBarry Smith + dm      - The `DM`
4496bb7acecfSBarry 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).
449720f4b53cSBarry 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).
449820f4b53cSBarry 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).
44999435951eSToby Isaac 
45009435951eSToby Isaac   Level: advanced
45019435951eSToby Isaac 
450220f4b53cSBarry Smith   Notes:
450320f4b53cSBarry 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()`.
450420f4b53cSBarry Smith 
450520f4b53cSBarry 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.
450620f4b53cSBarry Smith 
4507bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
45089435951eSToby Isaac 
45091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
45109435951eSToby Isaac @*/
DMSetDefaultConstraints(DM dm,PetscSection section,Mat mat,Vec bias)4511d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4512d71ae5a4SJacob Faibussowitsch {
4513e228b242SToby Isaac   PetscMPIInt result;
45149435951eSToby Isaac 
45159435951eSToby Isaac   PetscFunctionBegin;
45169435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4517e228b242SToby Isaac   if (section) {
4518e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45199566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
45207a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4521e228b242SToby Isaac   }
4522e228b242SToby Isaac   if (mat) {
4523e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
45249566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
45257a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4526e228b242SToby Isaac   }
452779769bd5SJed Brown   if (bias) {
452879769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
45299566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
453079769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
453179769bd5SJed Brown   }
45329566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45339566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
45343b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45359566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
45369566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45373b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45399566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
454079769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45429435951eSToby Isaac }
45439435951eSToby Isaac 
4544497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4545507e4973SMatthew G. Knepley /*
4546bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4547507e4973SMatthew G. Knepley 
4548507e4973SMatthew G. Knepley   Input Parameters:
4549bb7acecfSBarry Smith + dm - The `DM`
4550bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4551bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4552507e4973SMatthew G. Knepley 
4553507e4973SMatthew G. Knepley   Level: intermediate
4554507e4973SMatthew G. Knepley 
45551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4556507e4973SMatthew G. Knepley */
DMDefaultSectionCheckConsistency_Internal(DM dm,PetscSection localSection,PetscSection globalSection)4557d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4558d71ae5a4SJacob Faibussowitsch {
4559507e4973SMatthew G. Knepley   MPI_Comm        comm;
4560507e4973SMatthew G. Knepley   PetscLayout     layout;
4561507e4973SMatthew G. Knepley   const PetscInt *ranges;
4562507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4563507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4564507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4565507e4973SMatthew G. Knepley 
4566507e4973SMatthew G. Knepley   PetscFunctionBegin;
45679566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4568507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45699566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45709566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45719566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45729566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45739566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45749566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45759566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45769566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45779566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4578507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4579f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4580507e4973SMatthew G. Knepley 
45819566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45829566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45839566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45849566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45859566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45869566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4587507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45889371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45899371c9d4SSatish 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));
45909371c9d4SSatish Balay       valid = PETSC_FALSE;
45919371c9d4SSatish Balay     }
45929371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45939371c9d4SSatish 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));
45949371c9d4SSatish Balay       valid = PETSC_FALSE;
45959371c9d4SSatish Balay     }
4596507e4973SMatthew G. Knepley     if (gdof < 0) {
4597507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4598507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4599507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4600507e4973SMatthew G. Knepley 
46019566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4602507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
46039371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
46049371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
46059371c9d4SSatish Balay           valid = PETSC_FALSE;
46069371c9d4SSatish Balay           break;
46079371c9d4SSatish Balay         }
4608507e4973SMatthew G. Knepley       }
4609507e4973SMatthew G. Knepley     }
4610507e4973SMatthew G. Knepley   }
46119566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
46129566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
46135440e5dcSBarry Smith   PetscCallMPI(MPIU_Allreduce(&valid, &gvalid, 1, MPI_C_BOOL, MPI_LAND, comm));
4614507e4973SMatthew G. Knepley   if (!gvalid) {
46159566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4616507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4617507e4973SMatthew G. Knepley   }
46183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4619507e4973SMatthew G. Knepley }
4620f741bcd2SMatthew G. Knepley #endif
4621507e4973SMatthew G. Knepley 
DMGetIsoperiodicPointSF_Internal(DM dm,PetscSF * sf)462290df3356SJames Wright PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
46236725e60dSJed Brown {
46246725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
46254d86920dSPierre Jolivet 
46266725e60dSJed Brown   PetscFunctionBegin;
46276725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46284f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
46295f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
46306725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
46316725e60dSJed Brown   else *sf = dm->sf;
46323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46336725e60dSJed Brown }
46346725e60dSJed Brown 
463588ed4aceSMatthew G Knepley /*@
4636bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
463788ed4aceSMatthew G Knepley 
463820f4b53cSBarry Smith   Collective
46398b1ab98fSJed Brown 
464088ed4aceSMatthew G Knepley   Input Parameter:
4641bb7acecfSBarry Smith . dm - The `DM`
464288ed4aceSMatthew G Knepley 
464388ed4aceSMatthew G Knepley   Output Parameter:
4644bb7acecfSBarry Smith . section - The `PetscSection`
464588ed4aceSMatthew G Knepley 
464688ed4aceSMatthew G Knepley   Level: intermediate
464788ed4aceSMatthew G Knepley 
4648bb7acecfSBarry Smith   Note:
4649bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
465088ed4aceSMatthew G Knepley 
46511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
465288ed4aceSMatthew G Knepley @*/
DMGetGlobalSection(DM dm,PetscSection * section)4653d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4654d71ae5a4SJacob Faibussowitsch {
465588ed4aceSMatthew G Knepley   PetscFunctionBegin;
465688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46574f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46581bb6d2a8SBarry Smith   if (!dm->globalSection) {
4659fd59a867SMatthew G. Knepley     PetscSection s;
46606725e60dSJed Brown     PetscSF      sf;
4661fd59a867SMatthew G. Knepley 
46629566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46637a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46647a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46655f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4666eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46679566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46699566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
467088ed4aceSMatthew G Knepley   }
46711bb6d2a8SBarry Smith   *section = dm->globalSection;
46723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
467388ed4aceSMatthew G Knepley }
467488ed4aceSMatthew G Knepley 
4675b21d0597SMatthew G Knepley /*@
4676bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4677b21d0597SMatthew G Knepley 
4678b21d0597SMatthew G Knepley   Input Parameters:
4679bb7acecfSBarry Smith + dm      - The `DM`
46802fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4681b21d0597SMatthew G Knepley 
4682b21d0597SMatthew G Knepley   Level: intermediate
4683b21d0597SMatthew G Knepley 
4684bb7acecfSBarry Smith   Note:
4685bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4686b21d0597SMatthew G Knepley 
46871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4688b21d0597SMatthew G Knepley @*/
DMSetGlobalSection(DM dm,PetscSection section)4689d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4690d71ae5a4SJacob Faibussowitsch {
4691b21d0597SMatthew G Knepley   PetscFunctionBegin;
4692b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46935080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46949566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46959566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46961bb6d2a8SBarry Smith   dm->globalSection = section;
4697497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46989566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4699507e4973SMatthew G. Knepley #endif
470050350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
470150350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
470252947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
470350350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
470450350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
47053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4706b21d0597SMatthew G Knepley }
4707b21d0597SMatthew G Knepley 
470888ed4aceSMatthew G Knepley /*@
4709bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4710bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
471188ed4aceSMatthew G Knepley 
471288ed4aceSMatthew G Knepley   Input Parameter:
4713bb7acecfSBarry Smith . dm - The `DM`
471488ed4aceSMatthew G Knepley 
471588ed4aceSMatthew G Knepley   Output Parameter:
4716bb7acecfSBarry Smith . sf - The `PetscSF`
471788ed4aceSMatthew G Knepley 
471888ed4aceSMatthew G Knepley   Level: intermediate
471988ed4aceSMatthew G Knepley 
4720bb7acecfSBarry Smith   Note:
4721bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
472288ed4aceSMatthew G Knepley 
47231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
472488ed4aceSMatthew G Knepley @*/
DMGetSectionSF(DM dm,PetscSF * sf)4725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4726d71ae5a4SJacob Faibussowitsch {
472788ed4aceSMatthew G Knepley   PetscInt nroots;
472888ed4aceSMatthew G Knepley 
472988ed4aceSMatthew G Knepley   PetscFunctionBegin;
473088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47314f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
473248a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
47339566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
473488ed4aceSMatthew G Knepley   if (nroots < 0) {
473588ed4aceSMatthew G Knepley     PetscSection section, gSection;
473688ed4aceSMatthew G Knepley 
47379566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
473831ea6d37SMatthew G Knepley     if (section) {
47399566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47409566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
474131ea6d37SMatthew G Knepley     } else {
47420298fd71SBarry Smith       *sf = NULL;
47433ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
474431ea6d37SMatthew G Knepley     }
474588ed4aceSMatthew G Knepley   }
47461bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
474888ed4aceSMatthew G Knepley }
474988ed4aceSMatthew G Knepley 
475088ed4aceSMatthew G Knepley /*@
4751bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
475288ed4aceSMatthew G Knepley 
475388ed4aceSMatthew G Knepley   Input Parameters:
4754bb7acecfSBarry Smith + dm - The `DM`
4755bb7acecfSBarry Smith - sf - The `PetscSF`
475688ed4aceSMatthew G Knepley 
475788ed4aceSMatthew G Knepley   Level: intermediate
475888ed4aceSMatthew G Knepley 
4759bb7acecfSBarry Smith   Note:
4760bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
476188ed4aceSMatthew G Knepley 
47621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
476388ed4aceSMatthew G Knepley @*/
DMSetSectionSF(DM dm,PetscSF sf)4764d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4765d71ae5a4SJacob Faibussowitsch {
476688ed4aceSMatthew G Knepley   PetscFunctionBegin;
476788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4768b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47709566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47711bb6d2a8SBarry Smith   dm->sectionSF = sf;
47723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
477388ed4aceSMatthew G Knepley }
477488ed4aceSMatthew G Knepley 
4775cc4c1da9SBarry Smith /*@
4776bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
477788ed4aceSMatthew G Knepley   describing the data layout.
477888ed4aceSMatthew G Knepley 
477988ed4aceSMatthew G Knepley   Input Parameters:
4780bb7acecfSBarry Smith + dm            - The `DM`
4781bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4782bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
478388ed4aceSMatthew G Knepley 
47841bb6d2a8SBarry Smith   Level: developer
47851bb6d2a8SBarry Smith 
4786bb7acecfSBarry Smith   Note:
4787bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4788bb7acecfSBarry Smith 
478973ff1848SBarry Smith   Developer Note:
4790bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4791bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4792b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4793b6971eaeSBarry Smith   they create, this returns nothing
47941bb6d2a8SBarry Smith 
47951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
479688ed4aceSMatthew G Knepley @*/
DMCreateSectionSF(DM dm,PetscSection localSection,PetscSection globalSection)4797d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4798d71ae5a4SJacob Faibussowitsch {
479988ed4aceSMatthew G Knepley   PetscFunctionBegin;
480088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48019566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
48023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
480388ed4aceSMatthew G Knepley }
4804af122d2aSMatthew G Knepley 
4805b21d0597SMatthew G Knepley /*@
4806bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4807bb7acecfSBarry Smith 
4808bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4809b21d0597SMatthew G Knepley 
4810b21d0597SMatthew G Knepley   Input Parameter:
4811bb7acecfSBarry Smith . dm - The `DM`
4812b21d0597SMatthew G Knepley 
4813b21d0597SMatthew G Knepley   Output Parameter:
4814bb7acecfSBarry Smith . sf - The `PetscSF`
4815b21d0597SMatthew G Knepley 
4816b21d0597SMatthew G Knepley   Level: intermediate
4817b21d0597SMatthew G Knepley 
4818bb7acecfSBarry Smith   Note:
4819bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4820b21d0597SMatthew G Knepley 
48211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4822b21d0597SMatthew G Knepley @*/
DMGetPointSF(DM dm,PetscSF * sf)4823d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4824d71ae5a4SJacob Faibussowitsch {
4825b21d0597SMatthew G Knepley   PetscFunctionBegin;
4826b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48274f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4828b21d0597SMatthew G Knepley   *sf = dm->sf;
48293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4830b21d0597SMatthew G Knepley }
4831b21d0597SMatthew G Knepley 
4832057b4bcdSMatthew G Knepley /*@
4833bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4834bb7acecfSBarry Smith 
483520f4b53cSBarry Smith   Collective
4836057b4bcdSMatthew G Knepley 
4837057b4bcdSMatthew G Knepley   Input Parameters:
4838bb7acecfSBarry Smith + dm - The `DM`
4839bb7acecfSBarry Smith - sf - The `PetscSF`
4840057b4bcdSMatthew G Knepley 
4841057b4bcdSMatthew G Knepley   Level: intermediate
4842057b4bcdSMatthew G Knepley 
48431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4844057b4bcdSMatthew G Knepley @*/
DMSetPointSF(DM dm,PetscSF sf)4845d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4846d71ae5a4SJacob Faibussowitsch {
4847057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4848057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4849b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48509566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48519566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4852057b4bcdSMatthew G Knepley   dm->sf = sf;
48533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4854057b4bcdSMatthew G Knepley }
4855057b4bcdSMatthew G Knepley 
48564f37162bSMatthew G. Knepley /*@
4857bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48584f37162bSMatthew G. Knepley 
48594f37162bSMatthew G. Knepley   Input Parameter:
4860bb7acecfSBarry Smith . dm - The `DM`
48614f37162bSMatthew G. Knepley 
48624f37162bSMatthew G. Knepley   Output Parameter:
4863bb7acecfSBarry Smith . sf - The `PetscSF`
48644f37162bSMatthew G. Knepley 
48654f37162bSMatthew G. Knepley   Level: intermediate
48664f37162bSMatthew G. Knepley 
4867bb7acecfSBarry Smith   Note:
4868bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48694f37162bSMatthew G. Knepley 
48701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48714f37162bSMatthew G. Knepley @*/
DMGetNaturalSF(DM dm,PetscSF * sf)4872d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4873d71ae5a4SJacob Faibussowitsch {
48744f37162bSMatthew G. Knepley   PetscFunctionBegin;
48754f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48764f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48774f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48794f37162bSMatthew G. Knepley }
48804f37162bSMatthew G. Knepley 
48814f37162bSMatthew G. Knepley /*@
48824f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48834f37162bSMatthew G. Knepley 
48844f37162bSMatthew G. Knepley   Input Parameters:
48854f37162bSMatthew G. Knepley + dm - The DM
48864f37162bSMatthew G. Knepley - sf - The PetscSF
48874f37162bSMatthew G. Knepley 
48884f37162bSMatthew G. Knepley   Level: intermediate
48894f37162bSMatthew G. Knepley 
48901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48914f37162bSMatthew G. Knepley @*/
DMSetNaturalSF(DM dm,PetscSF sf)4892d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4893d71ae5a4SJacob Faibussowitsch {
48944f37162bSMatthew G. Knepley   PetscFunctionBegin;
48954f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48964f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48979566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48989566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48994f37162bSMatthew G. Knepley   dm->sfNatural = sf;
49003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
49014f37162bSMatthew G. Knepley }
49024f37162bSMatthew G. Knepley 
DMSetDefaultAdjacency_Private(DM dm,PetscInt f,PetscObject disc)4903d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4904d71ae5a4SJacob Faibussowitsch {
490534aa8a36SMatthew G. Knepley   PetscClassId id;
490634aa8a36SMatthew G. Knepley 
490734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
49089566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
490934aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
49109566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
491134aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
49129566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
491317c1d62eSMatthew G. Knepley   } else {
49149566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
491534aa8a36SMatthew G. Knepley   }
49163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
491734aa8a36SMatthew G. Knepley }
491834aa8a36SMatthew G. Knepley 
DMFieldEnlarge_Static(DM dm,PetscInt NfNew)4919d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4920d71ae5a4SJacob Faibussowitsch {
492144a7f3ddSMatthew G. Knepley   RegionField *tmpr;
492244a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
492344a7f3ddSMatthew G. Knepley 
492444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
49253ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
49269566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
492744a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
49289371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
49299371c9d4SSatish Balay     tmpr[f].disc        = NULL;
49309371c9d4SSatish Balay     tmpr[f].label       = NULL;
49319371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
49329371c9d4SSatish Balay   }
49339566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
493444a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
493544a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
49363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
493744a7f3ddSMatthew G. Knepley }
493844a7f3ddSMatthew G. Knepley 
493944a7f3ddSMatthew G. Knepley /*@
494020f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
494144a7f3ddSMatthew G. Knepley 
494220f4b53cSBarry Smith   Logically Collective
494344a7f3ddSMatthew G. Knepley 
494444a7f3ddSMatthew G. Knepley   Input Parameter:
494520f4b53cSBarry Smith . dm - The `DM`
494644a7f3ddSMatthew G. Knepley 
494744a7f3ddSMatthew G. Knepley   Level: intermediate
494844a7f3ddSMatthew G. Knepley 
49491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
495044a7f3ddSMatthew G. Knepley @*/
DMClearFields(DM dm)4951d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4952d71ae5a4SJacob Faibussowitsch {
495344a7f3ddSMatthew G. Knepley   PetscInt f;
495444a7f3ddSMatthew G. Knepley 
495544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
495644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49574c4edb6fSBarry Smith   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS); // DMDA does not use fields field in DM
495844a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49599566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49609566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
496144a7f3ddSMatthew G. Knepley   }
49629566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
496344a7f3ddSMatthew G. Knepley   dm->fields = NULL;
496444a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
496644a7f3ddSMatthew G. Knepley }
496744a7f3ddSMatthew G. Knepley 
4968689b5837SMatthew G. Knepley /*@
496920f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4970689b5837SMatthew G. Knepley 
497120f4b53cSBarry Smith   Not Collective
4972689b5837SMatthew G. Knepley 
4973689b5837SMatthew G. Knepley   Input Parameter:
497420f4b53cSBarry Smith . dm - The `DM`
4975689b5837SMatthew G. Knepley 
4976689b5837SMatthew G. Knepley   Output Parameter:
497760225df5SJacob Faibussowitsch . numFields - The number of fields
4978689b5837SMatthew G. Knepley 
4979689b5837SMatthew G. Knepley   Level: intermediate
4980689b5837SMatthew G. Knepley 
49811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4982689b5837SMatthew G. Knepley @*/
DMGetNumFields(DM dm,PetscInt * numFields)4983d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4984d71ae5a4SJacob Faibussowitsch {
49850f21e855SMatthew G. Knepley   PetscFunctionBegin;
49860f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49874f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
498844a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4990af122d2aSMatthew G Knepley }
4991af122d2aSMatthew G Knepley 
4992689b5837SMatthew G. Knepley /*@
499320f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4994689b5837SMatthew G. Knepley 
499520f4b53cSBarry Smith   Logically Collective
4996689b5837SMatthew G. Knepley 
4997689b5837SMatthew G. Knepley   Input Parameters:
499820f4b53cSBarry Smith + dm        - The `DM`
499960225df5SJacob Faibussowitsch - numFields - The number of fields
5000689b5837SMatthew G. Knepley 
5001689b5837SMatthew G. Knepley   Level: intermediate
5002689b5837SMatthew G. Knepley 
50031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
5004689b5837SMatthew G. Knepley @*/
DMSetNumFields(DM dm,PetscInt numFields)5005d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
5006d71ae5a4SJacob Faibussowitsch {
50070f21e855SMatthew G. Knepley   PetscInt Nf, f;
5008af122d2aSMatthew G Knepley 
5009af122d2aSMatthew G Knepley   PetscFunctionBegin;
5010af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50119566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50120f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
50130f21e855SMatthew G. Knepley     PetscContainer obj;
50140f21e855SMatthew G. Knepley 
50159566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
50169566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
50179566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
5018af122d2aSMatthew G Knepley   }
50193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5020af122d2aSMatthew G Knepley }
5021af122d2aSMatthew G Knepley 
5022c1929be8SMatthew G. Knepley /*@
5023bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
5024c1929be8SMatthew G. Knepley 
502520f4b53cSBarry Smith   Not Collective
5026c1929be8SMatthew G. Knepley 
5027c1929be8SMatthew G. Knepley   Input Parameters:
5028bb7acecfSBarry Smith + dm - The `DM`
5029c1929be8SMatthew G. Knepley - f  - The field number
5030c1929be8SMatthew G. Knepley 
503144a7f3ddSMatthew G. Knepley   Output Parameters:
503220f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
503320f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
5034c1929be8SMatthew G. Knepley 
503544a7f3ddSMatthew G. Knepley   Level: intermediate
5036c1929be8SMatthew G. Knepley 
50371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5038c1929be8SMatthew G. Knepley @*/
DMGetField(DM dm,PetscInt f,DMLabel * label,PetscObject * disc)5039d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5040d71ae5a4SJacob Faibussowitsch {
5041af122d2aSMatthew G Knepley   PetscFunctionBegin;
5042af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50434f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50447a8be351SBarry 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);
50454c4edb6fSBarry Smith   if (!dm->fields) {
50464c4edb6fSBarry Smith     if (label) *label = NULL;
50474c4edb6fSBarry Smith     if (disc) *disc = NULL;
50484c4edb6fSBarry Smith   } else { // some DM such as DMDA do not have dm->fields
504944a7f3ddSMatthew G. Knepley     if (label) *label = dm->fields[f].label;
5050bb7acecfSBarry Smith     if (disc) *disc = dm->fields[f].disc;
50514c4edb6fSBarry Smith   }
50523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5053decb47aaSMatthew G. Knepley }
5054decb47aaSMatthew G. Knepley 
5055083401c6SMatthew G. Knepley /* Does not clear the DS */
DMSetField_Internal(DM dm,PetscInt f,DMLabel label,PetscObject disc)5056d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5057d71ae5a4SJacob Faibussowitsch {
5058083401c6SMatthew G. Knepley   PetscFunctionBegin;
50599566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50609566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50619566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5062083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5063bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50649566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5065835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
50663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5067083401c6SMatthew G. Knepley }
5068083401c6SMatthew G. Knepley 
5069ffeef943SBarry Smith /*@
5070bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5071bb7acecfSBarry Smith   the field numbering.
5072c1929be8SMatthew G. Knepley 
507320f4b53cSBarry Smith   Logically Collective
5074c1929be8SMatthew G. Knepley 
5075c1929be8SMatthew G. Knepley   Input Parameters:
5076bb7acecfSBarry Smith + dm    - The `DM`
5077c1929be8SMatthew G. Knepley . f     - The field number
507820f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5079bb7acecfSBarry Smith - disc  - The discretization object
5080c1929be8SMatthew G. Knepley 
508144a7f3ddSMatthew G. Knepley   Level: intermediate
5082c1929be8SMatthew G. Knepley 
50831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5084c1929be8SMatthew G. Knepley @*/
DMSetField(DM dm,PetscInt f,DMLabel label,PetscObject disc)5085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5086d71ae5a4SJacob Faibussowitsch {
5087decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5088decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5089e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5090bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50917a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5092bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5093bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50949566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
509644a7f3ddSMatthew G. Knepley }
509744a7f3ddSMatthew G. Knepley 
5098ffeef943SBarry Smith /*@
5099bb7acecfSBarry 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)
5100bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
510144a7f3ddSMatthew G. Knepley 
510220f4b53cSBarry Smith   Logically Collective
510344a7f3ddSMatthew G. Knepley 
510444a7f3ddSMatthew G. Knepley   Input Parameters:
5105bb7acecfSBarry Smith + dm    - The `DM`
510620f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5107bb7acecfSBarry Smith - disc  - The discretization object
510844a7f3ddSMatthew G. Knepley 
510944a7f3ddSMatthew G. Knepley   Level: intermediate
511044a7f3ddSMatthew G. Knepley 
5111bb7acecfSBarry Smith   Notes:
5112bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5113bb7acecfSBarry Smith 
5114da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5115bb7acecfSBarry 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
5116bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5117bb7acecfSBarry Smith 
5118ce78bad3SBarry Smith   Fortran Note:
5119ce78bad3SBarry Smith   Use the argument `PetscObjectCast(disc)` as the second argument
5120ce78bad3SBarry Smith 
51211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
512244a7f3ddSMatthew G. Knepley @*/
DMAddField(DM dm,DMLabel label,PetscObject disc)5123d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5124d71ae5a4SJacob Faibussowitsch {
512544a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
512644a7f3ddSMatthew G. Knepley 
512744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
512844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5129064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5130bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
51319566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
513244a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5133bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
51349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5135835f2295SStefano Zampini   PetscCall(PetscObjectReference(disc));
5136bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
51379566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
51383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5139af122d2aSMatthew G Knepley }
51406636e97aSMatthew G Knepley 
5141e5e52638SMatthew G. Knepley /*@
5142e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5143e0b68406SMatthew Knepley 
514420f4b53cSBarry Smith   Logically Collective
5145e0b68406SMatthew Knepley 
5146e0b68406SMatthew Knepley   Input Parameters:
5147bb7acecfSBarry Smith + dm          - The `DM`
5148e0b68406SMatthew Knepley . f           - The field index
5149bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5150e0b68406SMatthew Knepley 
5151e0b68406SMatthew Knepley   Level: intermediate
5152e0b68406SMatthew Knepley 
51531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5154e0b68406SMatthew Knepley @*/
DMSetFieldAvoidTensor(DM dm,PetscInt f,PetscBool avoidTensor)5155d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5156d71ae5a4SJacob Faibussowitsch {
5157e0b68406SMatthew Knepley   PetscFunctionBegin;
515863a3b9bcSJacob 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);
5159e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5161e0b68406SMatthew Knepley }
5162e0b68406SMatthew Knepley 
5163e0b68406SMatthew Knepley /*@
5164e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5165e0b68406SMatthew Knepley 
516620f4b53cSBarry Smith   Not Collective
5167e0b68406SMatthew Knepley 
5168e0b68406SMatthew Knepley   Input Parameters:
5169bb7acecfSBarry Smith + dm - The `DM`
5170e0b68406SMatthew Knepley - f  - The field index
5171e0b68406SMatthew Knepley 
5172e0b68406SMatthew Knepley   Output Parameter:
5173e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5174e0b68406SMatthew Knepley 
5175e0b68406SMatthew Knepley   Level: intermediate
5176e0b68406SMatthew Knepley 
517760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5178e0b68406SMatthew Knepley @*/
DMGetFieldAvoidTensor(DM dm,PetscInt f,PetscBool * avoidTensor)5179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5180d71ae5a4SJacob Faibussowitsch {
5181e0b68406SMatthew Knepley   PetscFunctionBegin;
518263a3b9bcSJacob 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);
5183e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5185e0b68406SMatthew Knepley }
5186e0b68406SMatthew Knepley 
5187e0b68406SMatthew Knepley /*@
5188bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5189e5e52638SMatthew G. Knepley 
519020f4b53cSBarry Smith   Collective
5191e5e52638SMatthew G. Knepley 
5192bb4b53efSMatthew G. Knepley   Input Parameters:
5193bb4b53efSMatthew G. Knepley + dm        - The `DM`
5194bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
5195bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
5196e5e52638SMatthew G. Knepley 
5197e5e52638SMatthew G. Knepley   Output Parameter:
5198bb7acecfSBarry Smith . newdm - The `DM`
5199e5e52638SMatthew G. Knepley 
5200e5e52638SMatthew G. Knepley   Level: advanced
5201e5e52638SMatthew G. Knepley 
52021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5203e5e52638SMatthew G. Knepley @*/
DMCopyFields(DM dm,PetscInt minDegree,PetscInt maxDegree,DM newdm)5204bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
5205d71ae5a4SJacob Faibussowitsch {
5206e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5207e5e52638SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley   PetscFunctionBegin;
52093ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
52109566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
52119566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5212e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5213e5e52638SMatthew G. Knepley     DMLabel      label;
5214e5e52638SMatthew G. Knepley     PetscObject  field;
5215bb4b53efSMatthew G. Knepley     PetscClassId id;
521634aa8a36SMatthew G. Knepley     PetscBool    useCone, useClosure;
5217e5e52638SMatthew G. Knepley 
52189566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
5219bb4b53efSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(field, &id));
5220bb4b53efSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
5221bb4b53efSMatthew G. Knepley       PetscFE newfe;
5222bb4b53efSMatthew G. Knepley 
5223bb4b53efSMatthew G. Knepley       PetscCall(PetscFELimitDegree((PetscFE)field, minDegree, maxDegree, &newfe));
5224bb4b53efSMatthew G. Knepley       PetscCall(DMSetField(newdm, f, label, (PetscObject)newfe));
5225bb4b53efSMatthew G. Knepley       PetscCall(PetscFEDestroy(&newfe));
5226bb4b53efSMatthew G. Knepley     } else {
52279566063dSJacob Faibussowitsch       PetscCall(DMSetField(newdm, f, label, field));
5228bb4b53efSMatthew G. Knepley     }
52299566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
52309566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
523134aa8a36SMatthew G. Knepley   }
52324758e3ceSMatthew G. Knepley   // Create nullspace constructor slots
52334758e3ceSMatthew G. Knepley   if (dm->nullspaceConstructors) {
52344758e3ceSMatthew G. Knepley     PetscCall(PetscFree2(newdm->nullspaceConstructors, newdm->nearnullspaceConstructors));
52354758e3ceSMatthew G. Knepley     PetscCall(PetscCalloc2(Nf, &newdm->nullspaceConstructors, Nf, &newdm->nearnullspaceConstructors));
52364758e3ceSMatthew G. Knepley   }
52373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
523834aa8a36SMatthew G. Knepley }
523934aa8a36SMatthew G. Knepley 
524034aa8a36SMatthew G. Knepley /*@
524134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
524234aa8a36SMatthew G. Knepley 
524320f4b53cSBarry Smith   Not Collective
524434aa8a36SMatthew G. Knepley 
524534aa8a36SMatthew G. Knepley   Input Parameters:
524620f4b53cSBarry Smith + dm - The `DM` object
524720f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
524834aa8a36SMatthew G. Knepley 
5249d8d19677SJose E. Roman   Output Parameters:
525034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
525134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
525234aa8a36SMatthew G. Knepley 
525334aa8a36SMatthew G. Knepley   Level: developer
525434aa8a36SMatthew G. Knepley 
525520f4b53cSBarry Smith   Notes:
525620f4b53cSBarry Smith .vb
525720f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
525820f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
525920f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
526020f4b53cSBarry Smith .ve
526120f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
526220f4b53cSBarry Smith 
52631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
526434aa8a36SMatthew G. Knepley @*/
DMGetAdjacency(DM dm,PetscInt f,PetscBool * useCone,PetscBool * useClosure)5265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5266d71ae5a4SJacob Faibussowitsch {
526734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
526834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52694f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52704f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
527134aa8a36SMatthew G. Knepley   if (f < 0) {
527234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
527334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
527434aa8a36SMatthew G. Knepley   } else {
527534aa8a36SMatthew G. Knepley     PetscInt Nf;
527634aa8a36SMatthew G. Knepley 
52779566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52787a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
527934aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
528034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
528134aa8a36SMatthew G. Knepley   }
52823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
528334aa8a36SMatthew G. Knepley }
528434aa8a36SMatthew G. Knepley 
528534aa8a36SMatthew G. Knepley /*@
528634aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
528734aa8a36SMatthew G. Knepley 
528820f4b53cSBarry Smith   Not Collective
528934aa8a36SMatthew G. Knepley 
529034aa8a36SMatthew G. Knepley   Input Parameters:
529120f4b53cSBarry Smith + dm         - The `DM` object
529234aa8a36SMatthew G. Knepley . f          - The field number
529334aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
529434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
529534aa8a36SMatthew G. Knepley 
529634aa8a36SMatthew G. Knepley   Level: developer
529734aa8a36SMatthew G. Knepley 
529820f4b53cSBarry Smith   Notes:
529920f4b53cSBarry Smith .vb
530020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
530120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
530220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
530320f4b53cSBarry Smith .ve
530420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
530520f4b53cSBarry Smith 
53061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
530734aa8a36SMatthew G. Knepley @*/
DMSetAdjacency(DM dm,PetscInt f,PetscBool useCone,PetscBool useClosure)5308d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5309d71ae5a4SJacob Faibussowitsch {
531034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
531134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
531234aa8a36SMatthew G. Knepley   if (f < 0) {
531334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
531434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
531534aa8a36SMatthew G. Knepley   } else {
531634aa8a36SMatthew G. Knepley     PetscInt Nf;
531734aa8a36SMatthew G. Knepley 
53189566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
53197a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
532034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
532134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5322e5e52638SMatthew G. Knepley   }
53233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5324e5e52638SMatthew G. Knepley }
5325e5e52638SMatthew G. Knepley 
5326b0441da4SMatthew G. Knepley /*@
5327b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5328b0441da4SMatthew G. Knepley 
5329b0441da4SMatthew G. Knepley   Not collective
5330b0441da4SMatthew G. Knepley 
5331f899ff85SJose E. Roman   Input Parameter:
533220f4b53cSBarry Smith . dm - The `DM` object
5333b0441da4SMatthew G. Knepley 
5334d8d19677SJose E. Roman   Output Parameters:
5335b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5336b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5337b0441da4SMatthew G. Knepley 
5338b0441da4SMatthew G. Knepley   Level: developer
5339b0441da4SMatthew G. Knepley 
534020f4b53cSBarry Smith   Notes:
534120f4b53cSBarry Smith .vb
534220f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
534320f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
534420f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
534520f4b53cSBarry Smith .ve
534620f4b53cSBarry Smith 
53471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5348b0441da4SMatthew G. Knepley @*/
DMGetBasicAdjacency(DM dm,PetscBool * useCone,PetscBool * useClosure)5349d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5350d71ae5a4SJacob Faibussowitsch {
5351b0441da4SMatthew G. Knepley   PetscInt Nf;
5352b0441da4SMatthew G. Knepley 
5353b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5354b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53554f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
53564f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
53579566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5358b0441da4SMatthew G. Knepley   if (!Nf) {
53599566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5360b0441da4SMatthew G. Knepley   } else {
53619566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5362b0441da4SMatthew G. Knepley   }
53633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5364b0441da4SMatthew G. Knepley }
5365b0441da4SMatthew G. Knepley 
5366b0441da4SMatthew G. Knepley /*@
5367b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5368b0441da4SMatthew G. Knepley 
536920f4b53cSBarry Smith   Not Collective
5370b0441da4SMatthew G. Knepley 
5371b0441da4SMatthew G. Knepley   Input Parameters:
537220f4b53cSBarry Smith + dm         - The `DM` object
5373b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5374b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5375b0441da4SMatthew G. Knepley 
5376b0441da4SMatthew G. Knepley   Level: developer
5377b0441da4SMatthew G. Knepley 
537820f4b53cSBarry Smith   Notes:
537920f4b53cSBarry Smith .vb
538020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
538120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
538220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
538320f4b53cSBarry Smith .ve
538420f4b53cSBarry Smith 
53851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5386b0441da4SMatthew G. Knepley @*/
DMSetBasicAdjacency(DM dm,PetscBool useCone,PetscBool useClosure)5387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5388d71ae5a4SJacob Faibussowitsch {
5389b0441da4SMatthew G. Knepley   PetscInt Nf;
5390b0441da4SMatthew G. Knepley 
5391b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5392b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53939566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5394b0441da4SMatthew G. Knepley   if (!Nf) {
53959566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5396b0441da4SMatthew G. Knepley   } else {
53979566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5398e5e52638SMatthew G. Knepley   }
53993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5400e5e52638SMatthew G. Knepley }
5401e5e52638SMatthew G. Knepley 
DMCompleteBCLabels_Internal(DM dm)5402d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5403d71ae5a4SJacob Faibussowitsch {
5404799db056SMatthew G. Knepley   DM           plex;
5405799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5406799db056SMatthew G. Knepley   const char **names;
5407799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5408799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5409799db056SMatthew G. Knepley   size_t       len;
5410799db056SMatthew G. Knepley   MPI_Comm     comm;
5411799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5412783e2ec8SMatthew G. Knepley 
5413783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5414799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5415799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5416799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5417799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5418799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5419799db056SMatthew G. Knepley     PetscDS  dsBC;
5420799db056SMatthew G. Knepley     PetscInt numBd;
5421799db056SMatthew G. Knepley 
542207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5423799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5424799db056SMatthew G. Knepley     maxLabels += numBd;
5425799db056SMatthew G. Knepley   }
5426799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5427799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5428799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5429799db056SMatthew G. Knepley     PetscDS  dsBC;
5430799db056SMatthew G. Knepley     PetscInt numBd, bd;
5431799db056SMatthew G. Knepley 
543207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5433799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5434799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5435799db056SMatthew G. Knepley       DMLabel      label;
5436799db056SMatthew G. Knepley       PetscInt     field;
5437799db056SMatthew G. Knepley       PetscObject  obj;
5438799db056SMatthew G. Knepley       PetscClassId id;
5439799db056SMatthew G. Knepley 
5440799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
54419566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
54429566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5443fa74d514SMatthew G. Knepley       if (id != PETSCFE_CLASSID || !label) continue;
54449371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
54459371c9d4SSatish Balay         if (labels[l] == label) break;
5446799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5447783e2ec8SMatthew G. Knepley     }
5448799db056SMatthew G. Knepley   }
5449799db056SMatthew G. Knepley   /* Get label names */
5450799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5451799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
54529371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
54539371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
54549371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
54559371c9d4SSatish Balay   }
5456799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5457462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5458799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5459c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5460799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5461799db056SMatthew G. Knepley   /* Put all names on all processes */
5462799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5463799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5464799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5465799db056SMatthew G. Knepley   gNl = displs[size];
54669371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54679371c9d4SSatish Balay     counts[p] *= gmaxLen;
54689371c9d4SSatish Balay     displs[p] *= gmaxLen;
54699371c9d4SSatish Balay   }
5470799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5471799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5472799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5473799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5474799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5475799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5476799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54779371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54785d6b2bfcSJames Wright       if (glabels[m] == glabels[gl]) goto next_label;
54799566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5480799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54819566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5482799db056SMatthew G. Knepley     ++gl;
54835d6b2bfcSJames Wright   next_label:
54845d6b2bfcSJames Wright     continue;
5485783e2ec8SMatthew G. Knepley   }
5486799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5488783e2ec8SMatthew G. Knepley }
5489783e2ec8SMatthew G. Knepley 
DMDSEnlarge_Static(DM dm,PetscInt NdsNew)5490d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5491d71ae5a4SJacob Faibussowitsch {
5492e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5493e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5494e5e52638SMatthew G. Knepley 
5495e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54963ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54979566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5498e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54999371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
55009371c9d4SSatish Balay     tmpd[s].ds     = NULL;
55019371c9d4SSatish Balay     tmpd[s].label  = NULL;
55029371c9d4SSatish Balay     tmpd[s].fields = NULL;
55039371c9d4SSatish Balay   }
55049566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5505e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5506e5e52638SMatthew G. Knepley   dm->probs = tmpd;
55073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5508e5e52638SMatthew G. Knepley }
5509e5e52638SMatthew G. Knepley 
5510e5e52638SMatthew G. Knepley /*@
551120f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5512e5e52638SMatthew G. Knepley 
551320f4b53cSBarry Smith   Not Collective
5514e5e52638SMatthew G. Knepley 
5515e5e52638SMatthew G. Knepley   Input Parameter:
551620f4b53cSBarry Smith . dm - The `DM`
5517e5e52638SMatthew G. Knepley 
5518e5e52638SMatthew G. Knepley   Output Parameter:
551920f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5520e5e52638SMatthew G. Knepley 
5521e5e52638SMatthew G. Knepley   Level: intermediate
5522e5e52638SMatthew G. Knepley 
55231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5524e5e52638SMatthew G. Knepley @*/
DMGetNumDS(DM dm,PetscInt * Nds)5525d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5526d71ae5a4SJacob Faibussowitsch {
5527e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5528e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55294f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5530e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
55313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5532e5e52638SMatthew G. Knepley }
5533e5e52638SMatthew G. Knepley 
5534e5e52638SMatthew G. Knepley /*@
553520f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5536e5e52638SMatthew G. Knepley 
553720f4b53cSBarry Smith   Logically Collective
5538e5e52638SMatthew G. Knepley 
5539e5e52638SMatthew G. Knepley   Input Parameter:
554020f4b53cSBarry Smith . dm - The `DM`
5541e5e52638SMatthew G. Knepley 
5542e5e52638SMatthew G. Knepley   Level: intermediate
5543e5e52638SMatthew G. Knepley 
55441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5545e5e52638SMatthew G. Knepley @*/
DMClearDS(DM dm)5546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5547d71ae5a4SJacob Faibussowitsch {
5548e5e52638SMatthew G. Knepley   PetscInt s;
5549e5e52638SMatthew G. Knepley 
5550e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5551e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5552e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
55539566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
555407218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
55559566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
55569566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5557e5e52638SMatthew G. Knepley   }
55589566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5559e5e52638SMatthew G. Knepley   dm->probs = NULL;
5560e5e52638SMatthew G. Knepley   dm->Nds   = 0;
55613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5562e5e52638SMatthew G. Knepley }
5563e5e52638SMatthew G. Knepley 
5564e5e52638SMatthew G. Knepley /*@
556520f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5566e5e52638SMatthew G. Knepley 
556720f4b53cSBarry Smith   Not Collective
5568e5e52638SMatthew G. Knepley 
5569e5e52638SMatthew G. Knepley   Input Parameter:
557020f4b53cSBarry Smith . dm - The `DM`
5571e5e52638SMatthew G. Knepley 
5572e5e52638SMatthew G. Knepley   Output Parameter:
557307218a29SMatthew G. Knepley . ds - The default `PetscDS`
5574e5e52638SMatthew G. Knepley 
5575e5e52638SMatthew G. Knepley   Level: intermediate
5576e5e52638SMatthew G. Knepley 
5577ce78bad3SBarry Smith   Note:
5578ce78bad3SBarry Smith   The `ds` is owned by the `dm` and should not be destroyed directly.
5579ce78bad3SBarry Smith 
55801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5581e5e52638SMatthew G. Knepley @*/
DMGetDS(DM dm,PetscDS * ds)558207218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5583d71ae5a4SJacob Faibussowitsch {
5584e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5585e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55864f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
558707218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
558807218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5590e5e52638SMatthew G. Knepley }
5591e5e52638SMatthew G. Knepley 
5592e5e52638SMatthew G. Knepley /*@
559320f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5594e5e52638SMatthew G. Knepley 
559520f4b53cSBarry Smith   Not Collective
5596e5e52638SMatthew G. Knepley 
5597e5e52638SMatthew G. Knepley   Input Parameters:
559820f4b53cSBarry Smith + dm    - The `DM`
559920f4b53cSBarry Smith - point - Cell for the `PetscDS`
5600e5e52638SMatthew G. Knepley 
560107218a29SMatthew G. Knepley   Output Parameters:
560207218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
56035d550117SStefano Zampini - dsIn - The `PetscDS` for input on the given cell, or `NULL` if the same ds
5604e5e52638SMatthew G. Knepley 
5605e5e52638SMatthew G. Knepley   Level: developer
5606e5e52638SMatthew G. Knepley 
56071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5608e5e52638SMatthew G. Knepley @*/
DMGetCellDS(DM dm,PetscInt point,PetscDS * ds,PetscDS * dsIn)560907218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5610d71ae5a4SJacob Faibussowitsch {
561107218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5612e5e52638SMatthew G. Knepley   PetscInt s;
5613e5e52638SMatthew G. Knepley 
5614e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5615e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56164f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
56174f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
561863a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
561907218a29SMatthew G. Knepley   if (ds) *ds = NULL;
562007218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5621e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5622e5e52638SMatthew G. Knepley     PetscInt val;
5623e5e52638SMatthew G. Knepley 
56249371c9d4SSatish Balay     if (!dm->probs[s].label) {
562507218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
56269371c9d4SSatish Balay     } else {
56279566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
56289371c9d4SSatish Balay       if (val >= 0) {
562907218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
563007218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
56319371c9d4SSatish Balay         break;
56329371c9d4SSatish Balay       }
5633e5e52638SMatthew G. Knepley     }
5634e5e52638SMatthew G. Knepley   }
563507218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
56363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5637e5e52638SMatthew G. Knepley }
5638e5e52638SMatthew G. Knepley 
5639e5e52638SMatthew G. Knepley /*@
564020f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5641e5e52638SMatthew G. Knepley 
564220f4b53cSBarry Smith   Not Collective
5643e5e52638SMatthew G. Knepley 
5644e5e52638SMatthew G. Knepley   Input Parameters:
564520f4b53cSBarry Smith + dm    - The `DM`
564620f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5647e5e52638SMatthew G. Knepley 
5648b3cf3223SMatthew G. Knepley   Output Parameters:
564920f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
565007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
565107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5652e5e52638SMatthew G. Knepley 
5653e5e52638SMatthew G. Knepley   Level: advanced
5654e5e52638SMatthew G. Knepley 
565520f4b53cSBarry Smith   Note:
565620f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
565720f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
565807218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
565920f4b53cSBarry Smith 
56601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5661e5e52638SMatthew G. Knepley @*/
DMGetRegionDS(DM dm,DMLabel label,IS * fields,PetscDS * ds,PetscDS * dsIn)566207218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5663d71ae5a4SJacob Faibussowitsch {
5664e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5665e5e52638SMatthew G. Knepley 
5666e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5667e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5668e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56699371c9d4SSatish Balay   if (fields) {
56704f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56719371c9d4SSatish Balay     *fields = NULL;
56729371c9d4SSatish Balay   }
56739371c9d4SSatish Balay   if (ds) {
56744f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56759371c9d4SSatish Balay     *ds = NULL;
56769371c9d4SSatish Balay   }
567707218a29SMatthew G. Knepley   if (dsIn) {
56784f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
567907218a29SMatthew G. Knepley     *dsIn = NULL;
568007218a29SMatthew G. Knepley   }
5681e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5682154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5683b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5684b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
568507218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56863ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5687b3cf3223SMatthew G. Knepley     }
5688e5e52638SMatthew G. Knepley   }
56893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5690e5e52638SMatthew G. Knepley }
5691e5e52638SMatthew G. Knepley 
5692e5e52638SMatthew G. Knepley /*@
5693bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5694083401c6SMatthew G. Knepley 
569520f4b53cSBarry Smith   Collective
5696083401c6SMatthew G. Knepley 
5697083401c6SMatthew G. Knepley   Input Parameters:
5698bb7acecfSBarry Smith + dm     - The `DM`
569920f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
570007218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
570107218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
570207218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5703083401c6SMatthew G. Knepley 
570420f4b53cSBarry Smith   Level: advanced
570520f4b53cSBarry Smith 
5706bb7acecfSBarry Smith   Note:
5707bb7acecfSBarry 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,
5708083401c6SMatthew G. Knepley   the fields argument is ignored.
5709083401c6SMatthew G. Knepley 
57101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5711083401c6SMatthew G. Knepley @*/
DMSetRegionDS(DM dm,DMLabel label,IS fields,PetscDS ds,PetscDS dsIn)571207218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5713d71ae5a4SJacob Faibussowitsch {
5714083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5715083401c6SMatthew G. Knepley 
5716083401c6SMatthew G. Knepley   PetscFunctionBegin;
5717083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5718083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
571907218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5720064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
572107218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5722083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5723083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
57249566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
572507218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5726083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
572707218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
57283ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5729083401c6SMatthew G. Knepley     }
5730083401c6SMatthew G. Knepley   }
57319566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
57329566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57339566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
57349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
573507218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5736083401c6SMatthew G. Knepley   if (!label) {
5737083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5738083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5739083401c6SMatthew G. Knepley     Nds = 0;
5740083401c6SMatthew G. Knepley   }
5741083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5742083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5743083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
574407218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
57453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5746083401c6SMatthew G. Knepley }
5747083401c6SMatthew G. Knepley 
5748083401c6SMatthew G. Knepley /*@
574920f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5750e5e52638SMatthew G. Knepley 
575120f4b53cSBarry Smith   Not Collective
5752e5e52638SMatthew G. Knepley 
5753e5e52638SMatthew G. Knepley   Input Parameters:
575420f4b53cSBarry Smith + dm  - The `DM`
5755e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5756e5e52638SMatthew G. Knepley 
5757e5e52638SMatthew G. Knepley   Output Parameters:
575820f4b53cSBarry Smith + label  - The region label, or `NULL`
575920f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
576007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
576107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5762e5e52638SMatthew G. Knepley 
5763e5e52638SMatthew G. Knepley   Level: advanced
5764e5e52638SMatthew G. Knepley 
57651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5766e5e52638SMatthew G. Knepley @*/
DMGetRegionNumDS(DM dm,PetscInt num,DMLabel * label,IS * fields,PetscDS * ds,PetscDS * dsIn)576707218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5768d71ae5a4SJacob Faibussowitsch {
5769e5e52638SMatthew G. Knepley   PetscInt Nds;
5770e5e52638SMatthew G. Knepley 
5771e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5772e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57739566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
577463a3b9bcSJacob 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);
5775e5e52638SMatthew G. Knepley   if (label) {
57764f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5777e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5778e5e52638SMatthew G. Knepley   }
5779b3cf3223SMatthew G. Knepley   if (fields) {
57804f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5781b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5782b3cf3223SMatthew G. Knepley   }
5783e5e52638SMatthew G. Knepley   if (ds) {
57844f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5785e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5786e5e52638SMatthew G. Knepley   }
578707218a29SMatthew G. Knepley   if (dsIn) {
57884f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
578907218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
579007218a29SMatthew G. Knepley   }
57913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5792e5e52638SMatthew G. Knepley }
5793e5e52638SMatthew G. Knepley 
5794e5e52638SMatthew G. Knepley /*@
579520f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5796e5e52638SMatthew G. Knepley 
579720f4b53cSBarry Smith   Not Collective
5798e5e52638SMatthew G. Knepley 
5799e5e52638SMatthew G. Knepley   Input Parameters:
580020f4b53cSBarry Smith + dm     - The `DM`
5801083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
580220f4b53cSBarry Smith . label  - The region label, or `NULL`
580307218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
580407218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
580507218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5806e5e52638SMatthew G. Knepley 
5807e5e52638SMatthew G. Knepley   Level: advanced
5808e5e52638SMatthew G. Knepley 
58091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5810e5e52638SMatthew G. Knepley @*/
DMSetRegionNumDS(DM dm,PetscInt num,DMLabel label,IS fields,PetscDS ds,PetscDS dsIn)581107218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5812d71ae5a4SJacob Faibussowitsch {
5813083401c6SMatthew G. Knepley   PetscInt Nds;
5814e5e52638SMatthew G. Knepley 
5815e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5816e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5817ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
58189566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
581963a3b9bcSJacob 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);
58209566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
58219566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5822083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5823083401c6SMatthew G. Knepley   if (fields) {
5824083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
58259566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
58269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5827083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5828e5e52638SMatthew G. Knepley   }
5829083401c6SMatthew G. Knepley   if (ds) {
5830083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
58319566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
58329566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5833083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5834083401c6SMatthew G. Knepley   }
583507218a29SMatthew G. Knepley   if (dsIn) {
583607218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
583707218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
583807218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
583907218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
584007218a29SMatthew G. Knepley   }
58413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5842e5e52638SMatthew G. Knepley }
5843e5e52638SMatthew G. Knepley 
5844e5e52638SMatthew G. Knepley /*@
584520f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
58461d3af9e0SMatthew G. Knepley 
584720f4b53cSBarry Smith   Not Collective
58481d3af9e0SMatthew G. Knepley 
58491d3af9e0SMatthew G. Knepley   Input Parameters:
585020f4b53cSBarry Smith + dm - The `DM`
585120f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
58521d3af9e0SMatthew G. Knepley 
58531d3af9e0SMatthew G. Knepley   Output Parameter:
58541d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
58551d3af9e0SMatthew G. Knepley 
58561d3af9e0SMatthew G. Knepley   Level: advanced
58571d3af9e0SMatthew G. Knepley 
58581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
58591d3af9e0SMatthew G. Knepley @*/
DMFindRegionNum(DM dm,PetscDS ds,PetscInt * num)5860d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5861d71ae5a4SJacob Faibussowitsch {
58621d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
58631d3af9e0SMatthew G. Knepley 
58641d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
58651d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58661d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
58674f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58689566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58699371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58709371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58711d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58721d3af9e0SMatthew G. Knepley   else *num = n;
58733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58741d3af9e0SMatthew G. Knepley }
58751d3af9e0SMatthew G. Knepley 
5876cc4c1da9SBarry Smith /*@
5877bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58782df84da0SMatthew G. Knepley 
587920f4b53cSBarry Smith   Not Collective
58802df84da0SMatthew G. Knepley 
5881f1a722f8SMatthew G. Knepley   Input Parameters:
5882bb7acecfSBarry Smith + dm     - The `DM`
58832df84da0SMatthew G. Knepley . Nc     - The number of components for the field
588420f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5885bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58862df84da0SMatthew G. Knepley 
58872df84da0SMatthew G. Knepley   Output Parameter:
5888bb7acecfSBarry Smith . fem - The `PetscFE`
58892df84da0SMatthew G. Knepley 
589020f4b53cSBarry Smith   Level: intermediate
589120f4b53cSBarry Smith 
5892bb7acecfSBarry Smith   Note:
5893bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58942df84da0SMatthew G. Knepley 
58951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58962df84da0SMatthew G. Knepley @*/
DMCreateFEDefault(DM dm,PetscInt Nc,const char prefix[],PetscInt qorder,PetscFE * fem)5897d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5898d71ae5a4SJacob Faibussowitsch {
58992df84da0SMatthew G. Knepley   DMPolytopeType ct;
59002df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
59012df84da0SMatthew G. Knepley 
59022df84da0SMatthew G. Knepley   PetscFunctionBegin;
59032df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59042df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
59054f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
59062df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
59074f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
59089566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
59099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
59109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
59119566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
59123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59132df84da0SMatthew G. Knepley }
59142df84da0SMatthew G. Knepley 
59151d3af9e0SMatthew G. Knepley /*@
5916bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5917e5e52638SMatthew G. Knepley 
591820f4b53cSBarry Smith   Collective
5919e5e52638SMatthew G. Knepley 
5920e5e52638SMatthew G. Knepley   Input Parameter:
5921bb7acecfSBarry Smith . dm - The `DM`
5922e5e52638SMatthew G. Knepley 
592320f4b53cSBarry Smith   Options Database Key:
5924bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
592545480ffeSMatthew G. Knepley 
592620f4b53cSBarry Smith   Level: intermediate
592720f4b53cSBarry Smith 
5928ce78bad3SBarry Smith   Developer Note:
5929ce78bad3SBarry Smith   The name of this function is wrong. Create functions always return the created object as one of the arguments.
5930ce78bad3SBarry Smith 
59311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5932e5e52638SMatthew G. Knepley @*/
DMCreateDS(DM dm)5933d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5934d71ae5a4SJacob Faibussowitsch {
5935e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5936083401c6SMatthew G. Knepley   PetscDS   dsDef;
5937083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5938f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5939f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5940e5e52638SMatthew G. Knepley 
5941e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5942e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59433ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
59449566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
59459566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
59464758e3ceSMatthew G. Knepley   // Create nullspace constructor slots
59474758e3ceSMatthew G. Knepley   PetscCall(PetscFree2(dm->nullspaceConstructors, dm->nearnullspaceConstructors));
59484758e3ceSMatthew G. Knepley   PetscCall(PetscCalloc2(Nf, &dm->nullspaceConstructors, Nf, &dm->nearnullspaceConstructors));
5949083401c6SMatthew G. Knepley   /* Determine how many regions we have */
59509566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5951083401c6SMatthew G. Knepley   Nl   = 0;
5952083401c6SMatthew G. Knepley   Ndef = 0;
5953083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5954083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5955083401c6SMatthew G. Knepley     PetscInt l;
5956083401c6SMatthew G. Knepley 
5957f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5958f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5959f918ec44SMatthew G. Knepley     {
5960f918ec44SMatthew G. Knepley       PetscClassId id;
5961f918ec44SMatthew G. Knepley 
59629566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5963f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5964f918ec44SMatthew G. Knepley         Ceed ceed;
5965f918ec44SMatthew G. Knepley 
59669566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
59679566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5968f918ec44SMatthew G. Knepley       }
5969f918ec44SMatthew G. Knepley     }
5970f918ec44SMatthew G. Knepley #endif
59719371c9d4SSatish Balay     if (!label) {
59729371c9d4SSatish Balay       ++Ndef;
59739371c9d4SSatish Balay       continue;
59749371c9d4SSatish Balay     }
59759371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59769371c9d4SSatish Balay       if (label == labelSet[l]) break;
5977083401c6SMatthew G. Knepley     if (l < Nl) continue;
5978083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5979083401c6SMatthew G. Knepley   }
5980083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
598107218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5982083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5983b3cf3223SMatthew G. Knepley     IS        fields;
5984b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5985b3cf3223SMatthew G. Knepley 
59869371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59879371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59887a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59899566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59909371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59919371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59929566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59939566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59949566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59959566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
599688f0c812SMatthew G. Knepley 
59979566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
599807218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59999566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
60009566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
60012df9ee95SMatthew G. Knepley   }
600207218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
60039566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
6004083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
6005083401c6SMatthew G. Knepley   if (Ndef && Nl) {
60060122748bSMatthew G. Knepley     DM              plex;
6007083401c6SMatthew G. Knepley     DMLabel         cellLabel;
6008083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
6009083401c6SMatthew G. Knepley     PetscInt       *fields;
6010083401c6SMatthew G. Knepley     const PetscInt *cells;
6011083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
60120122748bSMatthew G. Knepley 
60139566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
60149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
60159566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
60169566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
60175fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
6018083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
6019083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
6020083401c6SMatthew G. Knepley       IS      pointIS;
6021083401c6SMatthew G. Knepley 
60229566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
60239566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
60249566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
60259566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
6026083401c6SMatthew G. Knepley     }
60279566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
6028083401c6SMatthew G. Knepley 
60299566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
60309566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
60319566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
60329566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
60339566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
60349566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
60359566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
6036083401c6SMatthew G. Knepley 
60379566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
60389371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
60399371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
60409566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
60419566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
60429566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
60439566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
6044083401c6SMatthew G. Knepley 
60459566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
604607218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
60479566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
60489566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
60499566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
60509566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
60519566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
6052083401c6SMatthew G. Knepley   }
6053083401c6SMatthew G. Knepley   /* Create label DSes
6054083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
6055083401c6SMatthew G. Knepley   */
6056083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
6057083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
6058083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
605907218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
6060083401c6SMatthew G. Knepley     IS        fields;
6061083401c6SMatthew G. Knepley     PetscInt *fld, nf;
6062083401c6SMatthew G. Knepley 
60639566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
60649371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60659371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
60669566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
60679371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60689371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
60699566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
60709566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
60719566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
60729566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
60739566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6074083401c6SMatthew G. Knepley     {
6075083401c6SMatthew G. Knepley       DMPolytopeType ct;
6076083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60775fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60780122748bSMatthew G. Knepley 
60799566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6080665f567fSMatthew G. Knepley       if (lStart >= 0) {
60819566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6082412e9a14SMatthew G. Knepley         switch (ct) {
6083412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6084412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6085412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6086d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6087d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6088d71ae5a4SJacob Faibussowitsch           break;
6089d71ae5a4SJacob Faibussowitsch         default:
6090d71ae5a4SJacob Faibussowitsch           break;
6091412e9a14SMatthew G. Knepley         }
6092665f567fSMatthew G. Knepley       }
60935440e5dcSBarry Smith       PetscCallMPI(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPI_C_BOOL, MPI_LOR, comm));
609407218a29SMatthew G. Knepley       if (isCohesive) {
609507218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
609607218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
609707218a29SMatthew G. Knepley       }
60985fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60995fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
61005fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
61019566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
61029566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
610307218a29SMatthew G. Knepley             if (dsIn) {
610407218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
610507218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
610607218a29SMatthew G. Knepley             }
61075fedec97SMatthew G. Knepley           }
61085fedec97SMatthew G. Knepley           ++nf;
61095fedec97SMatthew G. Knepley         }
61105fedec97SMatthew G. Knepley       }
6111e5e52638SMatthew G. Knepley     }
611207218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
611307218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
61149566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
611507218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6116e5e52638SMatthew G. Knepley   }
61179566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6118e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6119083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6120083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
612107218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6122083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6123083401c6SMatthew G. Knepley     const PetscInt *fld;
61245fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
61255fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6126e5e52638SMatthew G. Knepley 
61279566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
61289566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
61299566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
61309566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6131083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6132083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
61335fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6134e5e52638SMatthew G. Knepley       PetscClassId id;
6135e5e52638SMatthew G. Knepley 
61365fedec97SMatthew G. Knepley       /* Handle DS with no fields */
61379566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
61385fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
613907218a29SMatthew G. Knepley       if (isCohesive) {
614007218a29SMatthew G. Knepley         if (!isCohesiveField) {
614107218a29SMatthew G. Knepley           PetscObject bdDisc;
614207218a29SMatthew G. Knepley 
614307218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
614407218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
614507218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
614607218a29SMatthew G. Knepley         } else {
61479566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
614807218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
614907218a29SMatthew G. Knepley         }
615007218a29SMatthew G. Knepley       } else {
615107218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
615207218a29SMatthew G. Knepley       }
6153083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
61549566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6155e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6156e5e52638SMatthew G. Knepley     }
61579566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6158e5e52638SMatthew G. Knepley   }
6159f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
61609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6161f9244615SMatthew G. Knepley   if (flg) {
61623b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
61633b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
616407218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
61653b4aee56SMatthew G. Knepley       PetscInt Nf, f;
61663b4aee56SMatthew G. Knepley 
61679566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
616807218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
616907218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
617007218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
617107218a29SMatthew G. Knepley       }
61723b4aee56SMatthew G. Knepley     }
6173f9244615SMatthew G. Knepley   }
6174e5e52638SMatthew G. Knepley   /* Setup DSes */
6175e5e52638SMatthew G. Knepley   if (doSetup) {
617607218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6177cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6178cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6179cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6180cf1363e4SMatthew G. Knepley       }
618107218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
618207218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
618307218a29SMatthew G. Knepley     }
6184e5e52638SMatthew G. Knepley   }
61853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6186e5e52638SMatthew G. Knepley }
6187e5e52638SMatthew G. Knepley 
6188e5e52638SMatthew G. Knepley /*@
6189d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6190d2b2dc1eSMatthew G. Knepley 
6191d2b2dc1eSMatthew G. Knepley   Input Parameters:
6192d2b2dc1eSMatthew G. Knepley + dm     - The DM
6193d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6194d2b2dc1eSMatthew G. Knepley 
6195d2b2dc1eSMatthew G. Knepley   Level: developer
6196d2b2dc1eSMatthew G. Knepley 
6197d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6198d2b2dc1eSMatthew G. Knepley @*/
DMUseTensorOrder(DM dm,PetscBool tensor)6199d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6200d2b2dc1eSMatthew G. Knepley {
6201d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6202d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6203d2b2dc1eSMatthew G. Knepley 
6204d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6205d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6206d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6207d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6208d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6209d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6210d2b2dc1eSMatthew G. Knepley 
6211d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6212d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6213d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6214d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6215d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6216d2b2dc1eSMatthew G. Knepley 
6217d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6218d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6219d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6220d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6221d2b2dc1eSMatthew G. Knepley   }
6222d2b2dc1eSMatthew G. Knepley   if (tensor) {
6223d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6224d2b2dc1eSMatthew G. Knepley   } else {
6225d2b2dc1eSMatthew G. Knepley     PetscSection s;
6226d2b2dc1eSMatthew G. Knepley 
6227d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6228d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6229d2b2dc1eSMatthew G. Knepley   }
6230d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6231d2b2dc1eSMatthew G. Knepley }
6232d2b2dc1eSMatthew G. Knepley 
6233d2b2dc1eSMatthew G. Knepley /*@
6234bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
62357f96f943SMatthew G. Knepley 
623620f4b53cSBarry Smith   Collective
6237f2cacb80SMatthew G. Knepley 
62387f96f943SMatthew G. Knepley   Input Parameters:
6239bb7acecfSBarry Smith + dm   - The `DM`
62407f96f943SMatthew G. Knepley - time - The time
62417f96f943SMatthew G. Knepley 
62427f96f943SMatthew G. Knepley   Output Parameters:
624320f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
624420f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
624520f4b53cSBarry Smith 
624620f4b53cSBarry Smith   Level: developer
62477f96f943SMatthew G. Knepley 
6248bb7acecfSBarry Smith   Note:
6249bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
62507f96f943SMatthew G. Knepley 
62511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
62527f96f943SMatthew G. Knepley @*/
DMComputeExactSolution(DM dm,PetscReal time,Vec u,Vec u_t)6253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6254d71ae5a4SJacob Faibussowitsch {
6255*2a8381b2SBarry Smith   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, PetscCtx ctx);
62567f96f943SMatthew G. Knepley   void   **ectxs;
6257f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
62587f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
62597f96f943SMatthew G. Knepley 
62607f96f943SMatthew G. Knepley   PetscFunctionBegin;
6261f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6262f60fa741SMatthew G. Knepley   if (u) {
6263f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6264f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6265f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6266f60fa741SMatthew G. Knepley   }
6267f60fa741SMatthew G. Knepley   if (u_t) {
6268f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6269f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6270f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6271f60fa741SMatthew G. Knepley   }
62729566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
62739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62749566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62757f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62767f96f943SMatthew G. Knepley     PetscDS         ds;
62777f96f943SMatthew G. Knepley     DMLabel         label;
62787f96f943SMatthew G. Knepley     IS              fieldIS;
62797f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62807f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62817f96f943SMatthew G. Knepley 
628207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62839566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62849566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62859566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62869566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6287f2cacb80SMatthew G. Knepley     if (u) {
6288f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6289f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6290f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62917f96f943SMatthew G. Knepley     }
6292f2cacb80SMatthew G. Knepley     if (u_t) {
62939566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62949566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6295f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6296f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6297f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6298f2cacb80SMatthew G. Knepley     }
62999566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6300f2cacb80SMatthew G. Knepley   }
6301f2cacb80SMatthew G. Knepley   if (u) {
63029566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
63039566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6304f2cacb80SMatthew G. Knepley   }
6305f2cacb80SMatthew G. Knepley   if (u_t) {
63069566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
63079566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6308f2cacb80SMatthew G. Knepley   }
63099566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6310f60fa741SMatthew G. Knepley   if (u) {
6311f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6312f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6313f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6314f60fa741SMatthew G. Knepley   }
6315f60fa741SMatthew G. Knepley   if (u_t) {
6316f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6317f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6318f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6319f60fa741SMatthew G. Knepley   }
63203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
63217f96f943SMatthew G. Knepley }
63227f96f943SMatthew G. Knepley 
DMTransferDS_Internal(DM dm,DMLabel label,IS fields,PetscInt minDegree,PetscInt maxDegree,PetscDS ds,PetscDS dsIn)6323bb4b53efSMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscInt minDegree, PetscInt maxDegree, PetscDS ds, PetscDS dsIn)
6324d71ae5a4SJacob Faibussowitsch {
632507218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
632645480ffeSMatthew G. Knepley 
632745480ffeSMatthew G. Knepley   PetscFunctionBegin;
63289566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
6329bb4b53efSMatthew G. Knepley   PetscCall(PetscDSCopy(ds, minDegree, maxDegree, dm, dsNew));
633007218a29SMatthew G. Knepley   if (dsIn) {
633107218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
6332bb4b53efSMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, minDegree, maxDegree, dm, dsInNew));
633345480ffeSMatthew G. Knepley   }
633407218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
63359566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
633607218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
63373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
633845480ffeSMatthew G. Knepley }
633945480ffeSMatthew G. Knepley 
63407f96f943SMatthew G. Knepley /*@
6341bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6342e5e52638SMatthew G. Knepley 
634320f4b53cSBarry Smith   Collective
6344e5e52638SMatthew G. Knepley 
6345bb4b53efSMatthew G. Knepley   Input Parameters:
6346bb4b53efSMatthew G. Knepley + dm        - The `DM`
6347bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
6348bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
6349e5e52638SMatthew G. Knepley 
6350e5e52638SMatthew G. Knepley   Output Parameter:
6351bb7acecfSBarry Smith . newdm - The `DM`
6352e5e52638SMatthew G. Knepley 
6353e5e52638SMatthew G. Knepley   Level: advanced
6354e5e52638SMatthew G. Knepley 
63551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6356e5e52638SMatthew G. Knepley @*/
DMCopyDS(DM dm,PetscInt minDegree,PetscInt maxDegree,DM newdm)6357bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
6358d71ae5a4SJacob Faibussowitsch {
6359e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6360e5e52638SMatthew G. Knepley 
6361e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63623ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
63639566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
63649566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6365e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6366e5e52638SMatthew G. Knepley     DMLabel  label;
6367b3cf3223SMatthew G. Knepley     IS       fields;
636807218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6369783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6370e5e52638SMatthew G. Knepley 
637107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6372b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
6373bb4b53efSMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, minDegree, maxDegree, ds, dsIn));
6374d5b43468SJose E. Roman     /* Complete new labels in the new DS */
637507218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63769566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6377783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6378b8025e53SMatthew G. Knepley       PetscWeakForm wf;
637945480ffeSMatthew G. Knepley       DMLabel       label;
6380783e2ec8SMatthew G. Knepley       PetscInt      field;
6381783e2ec8SMatthew G. Knepley 
63829566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63839566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6384783e2ec8SMatthew G. Knepley     }
6385e5e52638SMatthew G. Knepley   }
6386799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6388e5e52638SMatthew G. Knepley }
6389e5e52638SMatthew G. Knepley 
6390e5e52638SMatthew G. Knepley /*@
6391bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6392e5e52638SMatthew G. Knepley 
639320f4b53cSBarry Smith   Collective
6394e5e52638SMatthew G. Knepley 
6395e5e52638SMatthew G. Knepley   Input Parameter:
6396bb7acecfSBarry Smith . dm - The `DM`
6397e5e52638SMatthew G. Knepley 
6398e5e52638SMatthew G. Knepley   Output Parameter:
6399bb7acecfSBarry Smith . newdm - The `DM`
6400e5e52638SMatthew G. Knepley 
6401e5e52638SMatthew G. Knepley   Level: advanced
6402e5e52638SMatthew G. Knepley 
640373ff1848SBarry Smith   Developer Note:
6404bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6405bb7acecfSBarry Smith 
64061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6407e5e52638SMatthew G. Knepley @*/
DMCopyDisc(DM dm,DM newdm)6408d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6409d71ae5a4SJacob Faibussowitsch {
6410e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6411bb4b53efSMatthew G. Knepley   PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
6412bb4b53efSMatthew G. Knepley   PetscCall(DMCopyDS(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
64133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6414e5e52638SMatthew G. Knepley }
6415e5e52638SMatthew G. Knepley 
6416c73cfb54SMatthew G. Knepley /*@
6417bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6418c73cfb54SMatthew G. Knepley 
641920f4b53cSBarry Smith   Not Collective
6420c73cfb54SMatthew G. Knepley 
6421c73cfb54SMatthew G. Knepley   Input Parameter:
6422bb7acecfSBarry Smith . dm - The `DM`
6423c73cfb54SMatthew G. Knepley 
6424c73cfb54SMatthew G. Knepley   Output Parameter:
6425c73cfb54SMatthew G. Knepley . dim - The topological dimension
6426c73cfb54SMatthew G. Knepley 
6427c73cfb54SMatthew G. Knepley   Level: beginner
6428c73cfb54SMatthew G. Knepley 
64291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6430c73cfb54SMatthew G. Knepley @*/
DMGetDimension(DM dm,PetscInt * dim)6431d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6432d71ae5a4SJacob Faibussowitsch {
6433c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6434c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64354f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6436c73cfb54SMatthew G. Knepley   *dim = dm->dim;
64373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6438c73cfb54SMatthew G. Knepley }
6439c73cfb54SMatthew G. Knepley 
6440c73cfb54SMatthew G. Knepley /*@
6441bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6442c73cfb54SMatthew G. Knepley 
644320f4b53cSBarry Smith   Collective
6444c73cfb54SMatthew G. Knepley 
6445c73cfb54SMatthew G. Knepley   Input Parameters:
6446bb7acecfSBarry Smith + dm  - The `DM`
6447c73cfb54SMatthew G. Knepley - dim - The topological dimension
6448c73cfb54SMatthew G. Knepley 
6449c73cfb54SMatthew G. Knepley   Level: beginner
6450c73cfb54SMatthew G. Knepley 
64511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6452c73cfb54SMatthew G. Knepley @*/
DMSetDimension(DM dm,PetscInt dim)6453d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6454d71ae5a4SJacob Faibussowitsch {
6455e5e52638SMatthew G. Knepley   PetscDS  ds;
645645480ffeSMatthew G. Knepley   PetscInt Nds, n;
6457f17e8794SMatthew G. Knepley 
6458c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6459c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6460c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6461c73cfb54SMatthew G. Knepley   dm->dim = dim;
6462d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
64639566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
646445480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
646507218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
64669566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
646745480ffeSMatthew G. Knepley     }
6468d17bd122SMatthew G. Knepley   }
64693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6470c73cfb54SMatthew G. Knepley }
6471c73cfb54SMatthew G. Knepley 
6472793f3fe5SMatthew G. Knepley /*@
6473793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6474793f3fe5SMatthew G. Knepley 
647520f4b53cSBarry Smith   Collective
6476793f3fe5SMatthew G. Knepley 
6477793f3fe5SMatthew G. Knepley   Input Parameters:
6478bb7acecfSBarry Smith + dm  - the `DM`
6479793f3fe5SMatthew G. Knepley - dim - the dimension
6480793f3fe5SMatthew G. Knepley 
6481793f3fe5SMatthew G. Knepley   Output Parameters:
6482793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6483aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6484793f3fe5SMatthew G. Knepley 
648520f4b53cSBarry Smith   Level: intermediate
648620f4b53cSBarry Smith 
6487793f3fe5SMatthew G. Knepley   Note:
6488793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6489a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6490793f3fe5SMatthew G. Knepley   then the interval is empty.
6491793f3fe5SMatthew G. Knepley 
64921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6493793f3fe5SMatthew G. Knepley @*/
DMGetDimPoints(DM dm,PetscInt dim,PetscInt * pStart,PetscInt * pEnd)6494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6495d71ae5a4SJacob Faibussowitsch {
6496793f3fe5SMatthew G. Knepley   PetscInt d;
6497793f3fe5SMatthew G. Knepley 
6498793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6499793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65009566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
65017a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6502dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
65033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6504793f3fe5SMatthew G. Knepley }
6505793f3fe5SMatthew G. Knepley 
65066636e97aSMatthew G Knepley /*@
6507bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6508f4d763aaSMatthew G. Knepley 
650920f4b53cSBarry Smith   Collective
65108f700142SStefano Zampini 
6511f4d763aaSMatthew G. Knepley   Input Parameter:
6512bb7acecfSBarry Smith . dm - The original `DM`
6513f4d763aaSMatthew G. Knepley 
6514f4d763aaSMatthew G. Knepley   Output Parameter:
6515bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6516f4d763aaSMatthew G. Knepley 
6517f4d763aaSMatthew G. Knepley   Level: intermediate
6518f4d763aaSMatthew G. Knepley 
6519bb7acecfSBarry Smith   Note:
6520bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6521bb7acecfSBarry 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
6522bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6523bb7acecfSBarry Smith 
65241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6525f4d763aaSMatthew G. Knepley @*/
DMGetOutputDM(DM dm,DM * odm)6526d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6527d71ae5a4SJacob Faibussowitsch {
6528c26acbdeSMatthew G. Knepley   PetscSection section;
6529eb9d3e4dSMatthew G. Knepley   IS           perm;
6530eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
653192a26154SJames Wright   PetscInt     num_face_sfs = 0;
653214f150ffSMatthew G. Knepley 
653314f150ffSMatthew G. Knepley   PetscFunctionBegin;
653414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65354f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
65369566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
65379566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6538eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
653992a26154SJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, NULL));
654092a26154SJames Wright   newDM = hasConstraints || perm || (num_face_sfs > 0) ? PETSC_TRUE : PETSC_FALSE;
65415440e5dcSBarry Smith   PetscCallMPI(MPIU_Allreduce(&newDM, &gnewDM, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6542eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6543c26acbdeSMatthew G. Knepley     *odm = dm;
65443ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6545c26acbdeSMatthew G. Knepley   }
654614f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6547c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
654892a26154SJames Wright     PetscSF      sf, sfNatural;
6549eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
655014f150ffSMatthew G. Knepley 
65519566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
65529566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
65539566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
65549566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
65559566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
655692a26154SJames Wright     PetscCall(DMGetNaturalSF(dm, &sfNatural));
655792a26154SJames Wright     PetscCall(DMSetNaturalSF(dm->dmBC, sfNatural));
65589566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6559eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
65609566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
65619566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
656214f150ffSMatthew G. Knepley   }
656314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
65643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
656514f150ffSMatthew G. Knepley }
6566f4d763aaSMatthew G. Knepley 
6567f4d763aaSMatthew G. Knepley /*@
6568cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6569f4d763aaSMatthew G. Knepley 
6570f4d763aaSMatthew G. Knepley   Input Parameter:
6571bb7acecfSBarry Smith . dm - The original `DM`
6572f4d763aaSMatthew G. Knepley 
6573cdb7a50dSMatthew G. Knepley   Output Parameters:
6574cdb7a50dSMatthew G. Knepley + num - The output sequence number
6575cdb7a50dSMatthew G. Knepley - val - The output sequence value
6576f4d763aaSMatthew G. Knepley 
6577f4d763aaSMatthew G. Knepley   Level: intermediate
6578f4d763aaSMatthew G. Knepley 
6579bb7acecfSBarry Smith   Note:
6580bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6581bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6582bb7acecfSBarry Smith 
658373ff1848SBarry Smith   Developer Note:
6584bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6585bb7acecfSBarry Smith   not directly related to the `DM`.
6586f4d763aaSMatthew G. Knepley 
65871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6588f4d763aaSMatthew G. Knepley @*/
DMGetOutputSequenceNumber(DM dm,PetscInt * num,PetscReal * val)6589d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6590d71ae5a4SJacob Faibussowitsch {
6591f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6592f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65939371c9d4SSatish Balay   if (num) {
65944f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65959371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65969371c9d4SSatish Balay   }
65979371c9d4SSatish Balay   if (val) {
65984f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65999371c9d4SSatish Balay     *val = dm->outputSequenceVal;
66009371c9d4SSatish Balay   }
66013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6602f4d763aaSMatthew G. Knepley }
6603f4d763aaSMatthew G. Knepley 
6604f4d763aaSMatthew G. Knepley /*@
6605cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6606f4d763aaSMatthew G. Knepley 
6607f4d763aaSMatthew G. Knepley   Input Parameters:
6608bb7acecfSBarry Smith + dm  - The original `DM`
6609cdb7a50dSMatthew G. Knepley . num - The output sequence number
6610cdb7a50dSMatthew G. Knepley - val - The output sequence value
6611f4d763aaSMatthew G. Knepley 
6612f4d763aaSMatthew G. Knepley   Level: intermediate
6613f4d763aaSMatthew G. Knepley 
6614bb7acecfSBarry Smith   Note:
6615bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6616bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6617f4d763aaSMatthew G. Knepley 
66181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6619f4d763aaSMatthew G. Knepley @*/
DMSetOutputSequenceNumber(DM dm,PetscInt num,PetscReal val)6620d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6621d71ae5a4SJacob Faibussowitsch {
6622f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6623f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6624f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6625cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
66263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6627cdb7a50dSMatthew G. Knepley }
6628cdb7a50dSMatthew G. Knepley 
66295d83a8b1SBarry Smith /*@
6630bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6631cdb7a50dSMatthew G. Knepley 
6632cdb7a50dSMatthew G. Knepley   Input Parameters:
6633bb7acecfSBarry Smith + dm     - The original `DM`
6634b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6635cdb7a50dSMatthew G. Knepley . name   - The sequence name
6636cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6637cdb7a50dSMatthew G. Knepley 
6638cdb7a50dSMatthew G. Knepley   Output Parameter:
6639cdb7a50dSMatthew G. Knepley . val - The output sequence value
6640cdb7a50dSMatthew G. Knepley 
6641cdb7a50dSMatthew G. Knepley   Level: intermediate
6642cdb7a50dSMatthew G. Knepley 
6643bb7acecfSBarry Smith   Note:
6644bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6645bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6646bb7acecfSBarry Smith 
664773ff1848SBarry Smith   Developer Note:
6648bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6649cdb7a50dSMatthew G. Knepley 
66501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6651cdb7a50dSMatthew G. Knepley @*/
DMOutputSequenceLoad(DM dm,PetscViewer viewer,const char name[],PetscInt num,PetscReal * val)6652b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6653d71ae5a4SJacob Faibussowitsch {
6654cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6655cdb7a50dSMatthew G. Knepley 
6656cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6657cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6658cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6659b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
66604f572ea9SToby Isaac   PetscAssertPointer(val, 5);
66619566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6662966bd95aSPierre Jolivet   PetscCheck(ishdf5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6663cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6664cdb7a50dSMatthew G. Knepley   PetscScalar value;
6665cdb7a50dSMatthew G. Knepley 
66669566063dSJacob Faibussowitsch   PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
66674aeb217fSMatthew G. Knepley   *val = PetscRealPart(value);
6668cdb7a50dSMatthew G. Knepley #endif
66693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6670f4d763aaSMatthew G. Knepley }
66718e4ac7eaSMatthew G. Knepley 
66728e4ac7eaSMatthew G. Knepley /*@
6673b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6674b2033f5dSMatthew G. Knepley 
6675b2033f5dSMatthew G. Knepley   Input Parameters:
6676b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6677b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6678b2033f5dSMatthew G. Knepley - name   - The sequence name
6679b2033f5dSMatthew G. Knepley 
6680b2033f5dSMatthew G. Knepley   Output Parameter:
6681b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6682b2033f5dSMatthew G. Knepley 
6683b2033f5dSMatthew G. Knepley   Level: intermediate
6684b2033f5dSMatthew G. Knepley 
6685b2033f5dSMatthew G. Knepley   Note:
6686b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6687b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6688b2033f5dSMatthew G. Knepley 
6689b2033f5dSMatthew G. Knepley   Developer Note:
6690b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6691b2033f5dSMatthew G. Knepley 
6692b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6693b2033f5dSMatthew G. Knepley @*/
DMGetOutputSequenceLength(DM dm,PetscViewer viewer,const char name[],PetscInt * len)6694b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6695b2033f5dSMatthew G. Knepley {
6696b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6697b2033f5dSMatthew G. Knepley 
6698b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6699b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6700b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6701b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6702b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6703b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6704966bd95aSPierre Jolivet   PetscCheck(ishdf5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6705b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6706b2033f5dSMatthew G. Knepley   PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6707b2033f5dSMatthew G. Knepley #endif
6708b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6709b2033f5dSMatthew G. Knepley }
6710b2033f5dSMatthew G. Knepley 
6711b2033f5dSMatthew G. Knepley /*@
6712bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
67138e4ac7eaSMatthew G. Knepley 
671420f4b53cSBarry Smith   Not Collective
67158e4ac7eaSMatthew G. Knepley 
67168e4ac7eaSMatthew G. Knepley   Input Parameter:
6717bb7acecfSBarry Smith . dm - The `DM`
67188e4ac7eaSMatthew G. Knepley 
67198e4ac7eaSMatthew G. Knepley   Output Parameter:
6720bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
67218e4ac7eaSMatthew G. Knepley 
67228e4ac7eaSMatthew G. Knepley   Level: beginner
67238e4ac7eaSMatthew G. Knepley 
67241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
67258e4ac7eaSMatthew G. Knepley @*/
DMGetUseNatural(DM dm,PetscBool * useNatural)6726d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6727d71ae5a4SJacob Faibussowitsch {
67288e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
67298e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67304f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
67318e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
67323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67338e4ac7eaSMatthew G. Knepley }
67348e4ac7eaSMatthew G. Knepley 
67358e4ac7eaSMatthew G. Knepley /*@
6736bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
67378e4ac7eaSMatthew G. Knepley 
673820f4b53cSBarry Smith   Collective
67398e4ac7eaSMatthew G. Knepley 
67408e4ac7eaSMatthew G. Knepley   Input Parameters:
6741bb7acecfSBarry Smith + dm         - The `DM`
6742bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
67438e4ac7eaSMatthew G. Knepley 
674473ff1848SBarry Smith   Level: beginner
674573ff1848SBarry Smith 
6746bb7acecfSBarry Smith   Note:
6747bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
67485d3b26e6SMatthew G. Knepley 
67491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
67508e4ac7eaSMatthew G. Knepley @*/
DMSetUseNatural(DM dm,PetscBool useNatural)6751d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6752d71ae5a4SJacob Faibussowitsch {
67538e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
67548e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67558833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
67568e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
67573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67588e4ac7eaSMatthew G. Knepley }
6759c58f1c22SToby Isaac 
6760cc4c1da9SBarry Smith /*@
6761bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac   Not Collective
6764c58f1c22SToby Isaac 
6765c58f1c22SToby Isaac   Input Parameters:
6766bb7acecfSBarry Smith + dm   - The `DM` object
6767c58f1c22SToby Isaac - name - The label name
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   Level: intermediate
6770c58f1c22SToby Isaac 
67711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6772c58f1c22SToby Isaac @*/
DMCreateLabel(DM dm,const char name[])6773d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6774d71ae5a4SJacob Faibussowitsch {
67755d80c0bfSVaclav Hapla   PetscBool flg;
67765d80c0bfSVaclav Hapla   DMLabel   label;
6777c58f1c22SToby Isaac 
6778c58f1c22SToby Isaac   PetscFunctionBegin;
6779c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67804f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67819566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6782c58f1c22SToby Isaac   if (!flg) {
67839566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67849566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67859566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6786c58f1c22SToby Isaac   }
67873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6788c58f1c22SToby Isaac }
6789c58f1c22SToby Isaac 
6790cc4c1da9SBarry Smith /*@
6791bb7acecfSBarry 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.
67920fdc7489SMatthew Knepley 
67930fdc7489SMatthew Knepley   Not Collective
67940fdc7489SMatthew Knepley 
67950fdc7489SMatthew Knepley   Input Parameters:
6796bb7acecfSBarry Smith + dm   - The `DM` object
67970fdc7489SMatthew Knepley . l    - The index for the label
67980fdc7489SMatthew Knepley - name - The label name
67990fdc7489SMatthew Knepley 
68000fdc7489SMatthew Knepley   Level: intermediate
68010fdc7489SMatthew Knepley 
68021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
68030fdc7489SMatthew Knepley @*/
DMCreateLabelAtIndex(DM dm,PetscInt l,const char name[])6804d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6805d71ae5a4SJacob Faibussowitsch {
68060fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
68070fdc7489SMatthew Knepley   DMLabel     label;
68080fdc7489SMatthew Knepley   PetscInt    Nl, m;
68090fdc7489SMatthew Knepley   PetscBool   flg, match;
68100fdc7489SMatthew Knepley   const char *lname;
68110fdc7489SMatthew Knepley 
68120fdc7489SMatthew Knepley   PetscFunctionBegin;
68130fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68144f572ea9SToby Isaac   PetscAssertPointer(name, 3);
68159566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
68160fdc7489SMatthew Knepley   if (!flg) {
68179566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
68189566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
68199566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
68200fdc7489SMatthew Knepley   }
68219566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
682263a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
68230fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
68249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
68259566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
68260fdc7489SMatthew Knepley     if (match) break;
68270fdc7489SMatthew Knepley   }
68283ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
68290fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
68300fdc7489SMatthew Knepley   else prev->next = orig->next;
68310fdc7489SMatthew Knepley   if (!l) {
68320fdc7489SMatthew Knepley     orig->next = dm->labels;
68330fdc7489SMatthew Knepley     dm->labels = orig;
68340fdc7489SMatthew Knepley   } else {
6835fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
68360fdc7489SMatthew Knepley     orig->next = prev->next;
68370fdc7489SMatthew Knepley     prev->next = orig;
68380fdc7489SMatthew Knepley   }
68393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
68400fdc7489SMatthew Knepley }
68410fdc7489SMatthew Knepley 
6842cc4c1da9SBarry Smith /*@
6843bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   Not Collective
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   Input Parameters:
6848bb7acecfSBarry Smith + dm    - The `DM` object
6849c58f1c22SToby Isaac . name  - The label name
6850c58f1c22SToby Isaac - point - The mesh point
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   Output Parameter:
6853c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6854c58f1c22SToby Isaac 
6855c58f1c22SToby Isaac   Level: beginner
6856c58f1c22SToby Isaac 
68571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6858c58f1c22SToby Isaac @*/
DMGetLabelValue(DM dm,const char name[],PetscInt point,PetscInt * value)6859d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6860d71ae5a4SJacob Faibussowitsch {
6861c58f1c22SToby Isaac   DMLabel label;
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   PetscFunctionBegin;
6864c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68654f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68669566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68677a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
68689566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
68693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6870c58f1c22SToby Isaac }
6871c58f1c22SToby Isaac 
6872cc4c1da9SBarry Smith /*@
6873bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6874c58f1c22SToby Isaac 
6875c58f1c22SToby Isaac   Not Collective
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Input Parameters:
6878bb7acecfSBarry Smith + dm    - The `DM` object
6879c58f1c22SToby Isaac . name  - The label name
6880c58f1c22SToby Isaac . point - The mesh point
6881c58f1c22SToby Isaac - value - The label value for this point
6882c58f1c22SToby Isaac 
6883c58f1c22SToby Isaac   Output Parameter:
6884c58f1c22SToby Isaac 
6885c58f1c22SToby Isaac   Level: beginner
6886c58f1c22SToby Isaac 
68871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6888c58f1c22SToby Isaac @*/
DMSetLabelValue(DM dm,const char name[],PetscInt point,PetscInt value)6889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6890d71ae5a4SJacob Faibussowitsch {
6891c58f1c22SToby Isaac   DMLabel label;
6892c58f1c22SToby Isaac 
6893c58f1c22SToby Isaac   PetscFunctionBegin;
6894c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68954f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68969566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6897c58f1c22SToby Isaac   if (!label) {
68989566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68999566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6900c58f1c22SToby Isaac   }
69019566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
69023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6903c58f1c22SToby Isaac }
6904c58f1c22SToby Isaac 
6905cc4c1da9SBarry Smith /*@
6906bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   Not Collective
6909c58f1c22SToby Isaac 
6910c58f1c22SToby Isaac   Input Parameters:
6911bb7acecfSBarry Smith + dm    - The `DM` object
6912c58f1c22SToby Isaac . name  - The label name
6913c58f1c22SToby Isaac . point - The mesh point
6914c58f1c22SToby Isaac - value - The label value for this point
6915c58f1c22SToby Isaac 
6916c58f1c22SToby Isaac   Level: beginner
6917c58f1c22SToby Isaac 
69181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6919c58f1c22SToby Isaac @*/
DMClearLabelValue(DM dm,const char name[],PetscInt point,PetscInt value)6920d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6921d71ae5a4SJacob Faibussowitsch {
6922c58f1c22SToby Isaac   DMLabel label;
6923c58f1c22SToby Isaac 
6924c58f1c22SToby Isaac   PetscFunctionBegin;
6925c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69264f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69279566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69283ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69299566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
69303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6931c58f1c22SToby Isaac }
6932c58f1c22SToby Isaac 
6933cc4c1da9SBarry Smith /*@
6934bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6935c58f1c22SToby Isaac 
6936c58f1c22SToby Isaac   Not Collective
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac   Input Parameters:
6939bb7acecfSBarry Smith + dm   - The `DM` object
6940c58f1c22SToby Isaac - name - The label name
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   Output Parameter:
6943c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6944c58f1c22SToby Isaac 
6945c58f1c22SToby Isaac   Level: beginner
6946c58f1c22SToby Isaac 
694773ff1848SBarry Smith   Developer Note:
6948bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6949bb7acecfSBarry Smith 
69501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6951c58f1c22SToby Isaac @*/
DMGetLabelSize(DM dm,const char name[],PetscInt * size)6952d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6953d71ae5a4SJacob Faibussowitsch {
6954c58f1c22SToby Isaac   DMLabel label;
6955c58f1c22SToby Isaac 
6956c58f1c22SToby Isaac   PetscFunctionBegin;
6957c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69584f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69594f572ea9SToby Isaac   PetscAssertPointer(size, 3);
69609566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6961c58f1c22SToby Isaac   *size = 0;
69623ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69639566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
69643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6965c58f1c22SToby Isaac }
6966c58f1c22SToby Isaac 
6967cc4c1da9SBarry Smith /*@
6968bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   Not Collective
6971c58f1c22SToby Isaac 
6972c58f1c22SToby Isaac   Input Parameters:
697360225df5SJacob Faibussowitsch + dm   - The `DM` object
6974c58f1c22SToby Isaac - name - The label name
6975c58f1c22SToby Isaac 
6976c58f1c22SToby Isaac   Output Parameter:
697720f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac   Level: beginner
6980c58f1c22SToby Isaac 
69811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6982c58f1c22SToby Isaac @*/
DMGetLabelIdIS(DM dm,const char name[],IS * ids)6983d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6984d71ae5a4SJacob Faibussowitsch {
6985c58f1c22SToby Isaac   DMLabel label;
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac   PetscFunctionBegin;
6988c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69894f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69904f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
69919566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6992c58f1c22SToby Isaac   *ids = NULL;
6993dab2e251SBlaise Bourdin   if (label) {
69949566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6995dab2e251SBlaise Bourdin   } else {
6996dab2e251SBlaise Bourdin     /* returning an empty IS */
69979566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6998dab2e251SBlaise Bourdin   }
69993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7000c58f1c22SToby Isaac }
7001c58f1c22SToby Isaac 
7002cc4c1da9SBarry Smith /*@
7003c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7004c58f1c22SToby Isaac 
7005c58f1c22SToby Isaac   Not Collective
7006c58f1c22SToby Isaac 
7007c58f1c22SToby Isaac   Input Parameters:
7008bb7acecfSBarry Smith + dm    - The `DM` object
7009b6971eaeSBarry Smith . name  - The label name of the stratum
7010c58f1c22SToby Isaac - value - The stratum value
7011c58f1c22SToby Isaac 
7012c58f1c22SToby Isaac   Output Parameter:
7013bb7acecfSBarry Smith . size - The number of points, also called the stratum size
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac   Level: beginner
7016c58f1c22SToby Isaac 
70171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
7018c58f1c22SToby Isaac @*/
DMGetStratumSize(DM dm,const char name[],PetscInt value,PetscInt * size)7019d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7020d71ae5a4SJacob Faibussowitsch {
7021c58f1c22SToby Isaac   DMLabel label;
7022c58f1c22SToby Isaac 
7023c58f1c22SToby Isaac   PetscFunctionBegin;
7024c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70254f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70264f572ea9SToby Isaac   PetscAssertPointer(size, 4);
70279566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7028c58f1c22SToby Isaac   *size = 0;
70293ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70309566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
70313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7032c58f1c22SToby Isaac }
7033c58f1c22SToby Isaac 
7034cc4c1da9SBarry Smith /*@
7035c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7036c58f1c22SToby Isaac 
7037c58f1c22SToby Isaac   Not Collective
7038c58f1c22SToby Isaac 
7039c58f1c22SToby Isaac   Input Parameters:
7040bb7acecfSBarry Smith + dm    - The `DM` object
7041c58f1c22SToby Isaac . name  - The label name
7042c58f1c22SToby Isaac - value - The stratum value
7043c58f1c22SToby Isaac 
7044c58f1c22SToby Isaac   Output Parameter:
704520f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
7046c58f1c22SToby Isaac 
7047c58f1c22SToby Isaac   Level: beginner
7048c58f1c22SToby Isaac 
70491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
7050c58f1c22SToby Isaac @*/
DMGetStratumIS(DM dm,const char name[],PetscInt value,IS * points)7051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7052d71ae5a4SJacob Faibussowitsch {
7053c58f1c22SToby Isaac   DMLabel label;
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac   PetscFunctionBegin;
7056c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70574f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70584f572ea9SToby Isaac   PetscAssertPointer(points, 4);
70599566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7060c58f1c22SToby Isaac   *points = NULL;
70613ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70629566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
70633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7064c58f1c22SToby Isaac }
7065c58f1c22SToby Isaac 
7066cc4c1da9SBarry Smith /*@
70679044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
70684de306b1SToby Isaac 
70694de306b1SToby Isaac   Not Collective
70704de306b1SToby Isaac 
70714de306b1SToby Isaac   Input Parameters:
7072bb7acecfSBarry Smith + dm     - The `DM` object
70734de306b1SToby Isaac . name   - The label name
70744de306b1SToby Isaac . value  - The stratum value
70754de306b1SToby Isaac - points - The stratum points
70764de306b1SToby Isaac 
70774de306b1SToby Isaac   Level: beginner
70784de306b1SToby Isaac 
70791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
70804de306b1SToby Isaac @*/
DMSetStratumIS(DM dm,const char name[],PetscInt value,IS points)7081d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
7082d71ae5a4SJacob Faibussowitsch {
70834de306b1SToby Isaac   DMLabel label;
70844de306b1SToby Isaac 
70854de306b1SToby Isaac   PetscFunctionBegin;
70864de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70874f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7088292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
70899566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70903ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70919566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
70923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70934de306b1SToby Isaac }
70944de306b1SToby Isaac 
7095cc4c1da9SBarry Smith /*@
7096bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7097c58f1c22SToby Isaac 
7098c58f1c22SToby Isaac   Not Collective
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac   Input Parameters:
7101bb7acecfSBarry Smith + dm    - The `DM` object
7102c58f1c22SToby Isaac . name  - The label name
7103c58f1c22SToby Isaac - value - The label value for this point
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac   Output Parameter:
7106c58f1c22SToby Isaac 
7107c58f1c22SToby Isaac   Level: beginner
7108c58f1c22SToby Isaac 
71091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7110c58f1c22SToby Isaac @*/
DMClearLabelStratum(DM dm,const char name[],PetscInt value)7111d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7112d71ae5a4SJacob Faibussowitsch {
7113c58f1c22SToby Isaac   DMLabel label;
7114c58f1c22SToby Isaac 
7115c58f1c22SToby Isaac   PetscFunctionBegin;
7116c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71174f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
71193ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
71209566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
71213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7122c58f1c22SToby Isaac }
7123c58f1c22SToby Isaac 
7124c58f1c22SToby Isaac /*@
7125bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7126c58f1c22SToby Isaac 
7127c58f1c22SToby Isaac   Not Collective
7128c58f1c22SToby Isaac 
7129c58f1c22SToby Isaac   Input Parameter:
7130bb7acecfSBarry Smith . dm - The `DM` object
7131c58f1c22SToby Isaac 
7132c58f1c22SToby Isaac   Output Parameter:
7133c58f1c22SToby Isaac . numLabels - the number of Labels
7134c58f1c22SToby Isaac 
7135c58f1c22SToby Isaac   Level: intermediate
7136c58f1c22SToby Isaac 
71371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7138c58f1c22SToby Isaac @*/
DMGetNumLabels(DM dm,PetscInt * numLabels)7139d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7140d71ae5a4SJacob Faibussowitsch {
71415d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7142c58f1c22SToby Isaac   PetscInt    n    = 0;
7143c58f1c22SToby Isaac 
7144c58f1c22SToby Isaac   PetscFunctionBegin;
7145c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71464f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
71479371c9d4SSatish Balay   while (next) {
71489371c9d4SSatish Balay     ++n;
71499371c9d4SSatish Balay     next = next->next;
71509371c9d4SSatish Balay   }
7151c58f1c22SToby Isaac   *numLabels = n;
71523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7153c58f1c22SToby Isaac }
7154c58f1c22SToby Isaac 
7155cc4c1da9SBarry Smith /*@
7156c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7157c58f1c22SToby Isaac 
7158c58f1c22SToby Isaac   Not Collective
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac   Input Parameters:
7161bb7acecfSBarry Smith + dm - The `DM` object
7162c58f1c22SToby Isaac - n  - the label number
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac   Output Parameter:
7165c58f1c22SToby Isaac . name - the label name
7166c58f1c22SToby Isaac 
7167c58f1c22SToby Isaac   Level: intermediate
7168c58f1c22SToby Isaac 
716973ff1848SBarry Smith   Developer Note:
7170bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7171bb7acecfSBarry Smith 
71721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7173c58f1c22SToby Isaac @*/
DMGetLabelName(DM dm,PetscInt n,const char * name[])7174cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7175d71ae5a4SJacob Faibussowitsch {
71765d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7177c58f1c22SToby Isaac   PetscInt    l    = 0;
7178c58f1c22SToby Isaac 
7179c58f1c22SToby Isaac   PetscFunctionBegin;
7180c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71814f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7182c58f1c22SToby Isaac   while (next) {
7183c58f1c22SToby Isaac     if (l == n) {
71849566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
71853ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7186c58f1c22SToby Isaac     }
7187c58f1c22SToby Isaac     ++l;
7188c58f1c22SToby Isaac     next = next->next;
7189c58f1c22SToby Isaac   }
719063a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7191c58f1c22SToby Isaac }
7192c58f1c22SToby Isaac 
7193cc4c1da9SBarry Smith /*@
7194bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7195c58f1c22SToby Isaac 
7196c58f1c22SToby Isaac   Not Collective
7197c58f1c22SToby Isaac 
7198c58f1c22SToby Isaac   Input Parameters:
7199bb7acecfSBarry Smith + dm   - The `DM` object
7200c58f1c22SToby Isaac - name - The label name
7201c58f1c22SToby Isaac 
7202c58f1c22SToby Isaac   Output Parameter:
7203bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7204c58f1c22SToby Isaac 
7205c58f1c22SToby Isaac   Level: intermediate
7206c58f1c22SToby Isaac 
72071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7208c58f1c22SToby Isaac @*/
DMHasLabel(DM dm,const char name[],PetscBool * hasLabel)7209d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7210d71ae5a4SJacob Faibussowitsch {
72115d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7212d67d17b1SMatthew G. Knepley   const char *lname;
7213c58f1c22SToby Isaac 
7214c58f1c22SToby Isaac   PetscFunctionBegin;
7215c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72164f572ea9SToby Isaac   PetscAssertPointer(name, 2);
72174f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7218c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7219c58f1c22SToby Isaac   while (next) {
72209566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72219566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7222c58f1c22SToby Isaac     if (*hasLabel) break;
7223c58f1c22SToby Isaac     next = next->next;
7224c58f1c22SToby Isaac   }
72253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7226c58f1c22SToby Isaac }
7227c58f1c22SToby Isaac 
7228a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7229cc4c1da9SBarry Smith /*@
723020f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7231c58f1c22SToby Isaac 
7232c58f1c22SToby Isaac   Not Collective
7233c58f1c22SToby Isaac 
7234c58f1c22SToby Isaac   Input Parameters:
7235bb7acecfSBarry Smith + dm   - The `DM` object
7236c58f1c22SToby Isaac - name - The label name
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   Output Parameter:
723920f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7240c58f1c22SToby Isaac 
7241bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7242bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7243bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7244bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7245bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7246bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7247bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72486d7c9049SMatthew G. Knepley 
7249c58f1c22SToby Isaac   Level: intermediate
7250c58f1c22SToby Isaac 
725160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7252c58f1c22SToby Isaac @*/
DMGetLabel(DM dm,const char name[],DMLabel * label)7253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7254d71ae5a4SJacob Faibussowitsch {
72555d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7256c58f1c22SToby Isaac   PetscBool   hasLabel;
7257d67d17b1SMatthew G. Knepley   const char *lname;
7258c58f1c22SToby Isaac 
7259c58f1c22SToby Isaac   PetscFunctionBegin;
7260c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72614f572ea9SToby Isaac   PetscAssertPointer(name, 2);
72624f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7263c58f1c22SToby Isaac   *label = NULL;
7264c58f1c22SToby Isaac   while (next) {
72659566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72669566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7267c58f1c22SToby Isaac     if (hasLabel) {
7268c58f1c22SToby Isaac       *label = next->label;
7269c58f1c22SToby Isaac       break;
7270c58f1c22SToby Isaac     }
7271c58f1c22SToby Isaac     next = next->next;
7272c58f1c22SToby Isaac   }
72733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7274c58f1c22SToby Isaac }
7275c58f1c22SToby Isaac 
7276cc4c1da9SBarry Smith /*@
7277bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7278c58f1c22SToby Isaac 
7279c58f1c22SToby Isaac   Not Collective
7280c58f1c22SToby Isaac 
7281c58f1c22SToby Isaac   Input Parameters:
7282bb7acecfSBarry Smith + dm - The `DM` object
7283c58f1c22SToby Isaac - n  - the label number
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac   Output Parameter:
7286c58f1c22SToby Isaac . label - the label
7287c58f1c22SToby Isaac 
7288c58f1c22SToby Isaac   Level: intermediate
7289c58f1c22SToby Isaac 
72901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7291c58f1c22SToby Isaac @*/
DMGetLabelByNum(DM dm,PetscInt n,DMLabel * label)7292d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7293d71ae5a4SJacob Faibussowitsch {
72945d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7295c58f1c22SToby Isaac   PetscInt    l    = 0;
7296c58f1c22SToby Isaac 
7297c58f1c22SToby Isaac   PetscFunctionBegin;
7298c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72994f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7300c58f1c22SToby Isaac   while (next) {
7301c58f1c22SToby Isaac     if (l == n) {
7302c58f1c22SToby Isaac       *label = next->label;
73033ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7304c58f1c22SToby Isaac     }
7305c58f1c22SToby Isaac     ++l;
7306c58f1c22SToby Isaac     next = next->next;
7307c58f1c22SToby Isaac   }
730863a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7309c58f1c22SToby Isaac }
7310c58f1c22SToby Isaac 
7311cc4c1da9SBarry Smith /*@
7312bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7313c58f1c22SToby Isaac 
7314c58f1c22SToby Isaac   Not Collective
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac   Input Parameters:
7317bb7acecfSBarry Smith + dm    - The `DM` object
7318bb7acecfSBarry Smith - label - The `DMLabel`
7319c58f1c22SToby Isaac 
7320c58f1c22SToby Isaac   Level: developer
7321c58f1c22SToby Isaac 
732260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7323c58f1c22SToby Isaac @*/
DMAddLabel(DM dm,DMLabel label)7324d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7325d71ae5a4SJacob Faibussowitsch {
73265d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7327c58f1c22SToby Isaac   PetscBool   hasLabel;
7328d67d17b1SMatthew G. Knepley   const char *lname;
73295d80c0bfSVaclav Hapla   PetscBool   flg;
7330c58f1c22SToby Isaac 
7331c58f1c22SToby Isaac   PetscFunctionBegin;
7332c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73339566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
73349566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
73357a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
73369566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7337c58f1c22SToby Isaac   tmpLabel->label  = label;
7338c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
73395d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
73405d80c0bfSVaclav Hapla   *p = tmpLabel;
73419566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
73429566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
73435d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
73449566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7345ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
73463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7347c58f1c22SToby Isaac }
7348c58f1c22SToby Isaac 
7349a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7350cc4c1da9SBarry Smith /*@
73514a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
73524a7ee7d0SMatthew G. Knepley 
73534a7ee7d0SMatthew G. Knepley   Not Collective
73544a7ee7d0SMatthew G. Knepley 
73554a7ee7d0SMatthew G. Knepley   Input Parameters:
7356bb7acecfSBarry Smith + dm    - The `DM` object
7357bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
73584a7ee7d0SMatthew G. Knepley 
7359bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7360bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7361bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7362bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7363bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7364bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7365bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
73664a7ee7d0SMatthew G. Knepley 
73674a7ee7d0SMatthew G. Knepley   Level: intermediate
73684a7ee7d0SMatthew G. Knepley 
73691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
73704a7ee7d0SMatthew G. Knepley @*/
DMSetLabel(DM dm,DMLabel label)7371d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7372d71ae5a4SJacob Faibussowitsch {
73734a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
73744a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
73754a7ee7d0SMatthew G. Knepley   const char *name, *lname;
73764a7ee7d0SMatthew G. Knepley 
73774a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
73784a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73794a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
73809566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
73814a7ee7d0SMatthew G. Knepley   while (next) {
73829566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73839566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
73844a7ee7d0SMatthew G. Knepley     if (hasLabel) {
73859566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
73869566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
73874a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
73889566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
73894a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
73909566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
73914a7ee7d0SMatthew G. Knepley       next->label = label;
73924a7ee7d0SMatthew G. Knepley       break;
73934a7ee7d0SMatthew G. Knepley     }
73944a7ee7d0SMatthew G. Knepley     next = next->next;
73954a7ee7d0SMatthew G. Knepley   }
73963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73974a7ee7d0SMatthew G. Knepley }
73984a7ee7d0SMatthew G. Knepley 
7399cc4c1da9SBarry Smith /*@
7400bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7401c58f1c22SToby Isaac 
7402c58f1c22SToby Isaac   Not Collective
7403c58f1c22SToby Isaac 
7404c58f1c22SToby Isaac   Input Parameters:
7405bb7acecfSBarry Smith + dm   - The `DM` object
7406c58f1c22SToby Isaac - name - The label name
7407c58f1c22SToby Isaac 
7408c58f1c22SToby Isaac   Output Parameter:
740920f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7410bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7411c58f1c22SToby Isaac 
7412c58f1c22SToby Isaac   Level: developer
7413c58f1c22SToby Isaac 
74141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7415c58f1c22SToby Isaac @*/
DMRemoveLabel(DM dm,const char name[],DMLabel * label)7416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7417d71ae5a4SJacob Faibussowitsch {
741895d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7419c58f1c22SToby Isaac   PetscBool   hasLabel;
7420d67d17b1SMatthew G. Knepley   const char *lname;
7421c58f1c22SToby Isaac 
7422c58f1c22SToby Isaac   PetscFunctionBegin;
7423c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74244f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7425e5472504SVaclav Hapla   if (label) {
74264f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7427c58f1c22SToby Isaac     *label = NULL;
7428e5472504SVaclav Hapla   }
74295d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
74309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
74319566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7432c58f1c22SToby Isaac     if (hasLabel) {
743395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
74349566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
743595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
74369566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7437ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
743895d578d6SVaclav Hapla       if (label) *label = link->label;
74399566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
74409566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7441c58f1c22SToby Isaac       break;
7442c58f1c22SToby Isaac     }
7443c58f1c22SToby Isaac   }
74443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7445c58f1c22SToby Isaac }
7446c58f1c22SToby Isaac 
7447306894acSVaclav Hapla /*@
7448bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7449306894acSVaclav Hapla 
7450306894acSVaclav Hapla   Not Collective
7451306894acSVaclav Hapla 
7452306894acSVaclav Hapla   Input Parameters:
7453bb7acecfSBarry Smith + dm           - The `DM` object
7454bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
745520f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7456306894acSVaclav Hapla 
7457306894acSVaclav Hapla   Level: developer
7458306894acSVaclav Hapla 
7459bb7acecfSBarry Smith   Note:
7460306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7461bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7462306894acSVaclav Hapla   *label nullified.
7463306894acSVaclav Hapla 
74641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7465306894acSVaclav Hapla @*/
DMRemoveLabelBySelf(DM dm,DMLabel * label,PetscBool failNotFound)7466d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7467d71ae5a4SJacob Faibussowitsch {
746843e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7469306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7470306894acSVaclav Hapla 
7471306894acSVaclav Hapla   PetscFunctionBegin;
7472306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74734f572ea9SToby Isaac   PetscAssertPointer(label, 2);
74743ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7475306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7476306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
74775d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
747843e45a93SVaclav Hapla     if (*label == link->label) {
7479306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
748043e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7481306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7482ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
748343e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
74849566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
74859566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7486306894acSVaclav Hapla       break;
7487306894acSVaclav Hapla     }
7488306894acSVaclav Hapla   }
74897a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
74903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7491306894acSVaclav Hapla }
7492306894acSVaclav Hapla 
7493cc4c1da9SBarry Smith /*@
7494c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7495c58f1c22SToby Isaac 
7496c58f1c22SToby Isaac   Not Collective
7497c58f1c22SToby Isaac 
7498c58f1c22SToby Isaac   Input Parameters:
7499bb7acecfSBarry Smith + dm   - The `DM` object
7500c58f1c22SToby Isaac - name - The label name
7501c58f1c22SToby Isaac 
7502c58f1c22SToby Isaac   Output Parameter:
7503c58f1c22SToby Isaac . output - The flag for output
7504c58f1c22SToby Isaac 
7505c58f1c22SToby Isaac   Level: developer
7506c58f1c22SToby Isaac 
75071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7508c58f1c22SToby Isaac @*/
DMGetLabelOutput(DM dm,const char name[],PetscBool * output)7509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7510d71ae5a4SJacob Faibussowitsch {
75115d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7512d67d17b1SMatthew G. Knepley   const char *lname;
7513c58f1c22SToby Isaac 
7514c58f1c22SToby Isaac   PetscFunctionBegin;
7515c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75164f572ea9SToby Isaac   PetscAssertPointer(name, 2);
75174f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7518c58f1c22SToby Isaac   while (next) {
7519c58f1c22SToby Isaac     PetscBool flg;
7520c58f1c22SToby Isaac 
75219566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
75229566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
75239371c9d4SSatish Balay     if (flg) {
75249371c9d4SSatish Balay       *output = next->output;
75253ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
75269371c9d4SSatish Balay     }
7527c58f1c22SToby Isaac     next = next->next;
7528c58f1c22SToby Isaac   }
752998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7530c58f1c22SToby Isaac }
7531c58f1c22SToby Isaac 
7532cc4c1da9SBarry Smith /*@
7533bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7534c58f1c22SToby Isaac 
7535c58f1c22SToby Isaac   Not Collective
7536c58f1c22SToby Isaac 
7537c58f1c22SToby Isaac   Input Parameters:
7538bb7acecfSBarry Smith + dm     - The `DM` object
7539c58f1c22SToby Isaac . name   - The label name
7540bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7541c58f1c22SToby Isaac 
7542c58f1c22SToby Isaac   Level: developer
7543c58f1c22SToby Isaac 
75441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7545c58f1c22SToby Isaac @*/
DMSetLabelOutput(DM dm,const char name[],PetscBool output)7546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7547d71ae5a4SJacob Faibussowitsch {
75485d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7549d67d17b1SMatthew G. Knepley   const char *lname;
7550c58f1c22SToby Isaac 
7551c58f1c22SToby Isaac   PetscFunctionBegin;
7552c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75534f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7554c58f1c22SToby Isaac   while (next) {
7555c58f1c22SToby Isaac     PetscBool flg;
7556c58f1c22SToby Isaac 
75579566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
75589566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
75599371c9d4SSatish Balay     if (flg) {
75609371c9d4SSatish Balay       next->output = output;
75613ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
75629371c9d4SSatish Balay     }
7563c58f1c22SToby Isaac     next = next->next;
7564c58f1c22SToby Isaac   }
756598921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7566c58f1c22SToby Isaac }
7567c58f1c22SToby Isaac 
7568c58f1c22SToby Isaac /*@
7569bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7570c58f1c22SToby Isaac 
757120f4b53cSBarry Smith   Collective
7572c58f1c22SToby Isaac 
7573d8d19677SJose E. Roman   Input Parameters:
7574bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7575bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7576bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7577bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7578bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7579c58f1c22SToby Isaac 
7580c58f1c22SToby Isaac   Level: intermediate
7581c58f1c22SToby Isaac 
7582bb7acecfSBarry Smith   Note:
75832cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7584c58f1c22SToby Isaac 
75851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7586c58f1c22SToby Isaac @*/
DMCopyLabels(DM dmA,DM dmB,PetscCopyMode mode,PetscBool all,DMCopyLabelsMode emode)7587d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7588d71ae5a4SJacob Faibussowitsch {
75892cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7590c58f1c22SToby Isaac   const char *name;
7591c58f1c22SToby Isaac   PetscBool   flg;
75925d80c0bfSVaclav Hapla   DMLabelLink link;
7593c58f1c22SToby Isaac 
75945d80c0bfSVaclav Hapla   PetscFunctionBegin;
75955d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75965d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75975d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75985d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75997a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
76003ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
76015d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
76025d80c0bfSVaclav Hapla     label = link->label;
76039566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
76045d80c0bfSVaclav Hapla     if (!all) {
76059566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7606c58f1c22SToby Isaac       if (flg) continue;
76079566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
76087d5acc75SStefano Zampini       if (flg) continue;
76099566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7610ba2698f1SMatthew G. Knepley       if (flg) continue;
76115d80c0bfSVaclav Hapla     }
76129566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
76132cbb9b06SVaclav Hapla     if (labelOld) {
76142cbb9b06SVaclav Hapla       switch (emode) {
7615d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7616d71ae5a4SJacob Faibussowitsch         continue;
7617d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7618d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7619d71ae5a4SJacob Faibussowitsch         break;
7620d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7621d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7622d71ae5a4SJacob Faibussowitsch       default:
7623d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
76242cbb9b06SVaclav Hapla       }
76252cbb9b06SVaclav Hapla     }
76265d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
76279566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
76285d80c0bfSVaclav Hapla     } else {
76295d80c0bfSVaclav Hapla       labelNew = label;
76305d80c0bfSVaclav Hapla     }
76319566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
76329566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7633c58f1c22SToby Isaac   }
76343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7635c58f1c22SToby Isaac }
7636461a15a0SLisandro Dalcin 
7637609dae6eSVaclav Hapla /*@C
7638b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7639609dae6eSVaclav Hapla 
764020f4b53cSBarry Smith   Collective; No Fortran Support
7641609dae6eSVaclav Hapla 
7642609dae6eSVaclav Hapla   Input Parameters:
7643bb7acecfSBarry Smith + dm0 - First `DM` object
7644bb7acecfSBarry Smith - dm1 - Second `DM` object
7645609dae6eSVaclav Hapla 
7646a4e35b19SJacob Faibussowitsch   Output Parameters:
7647ce78bad3SBarry Smith + equal   - (Optional) Flag whether labels of `dm0` and `dm1` are the same
764820f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7649609dae6eSVaclav Hapla 
7650609dae6eSVaclav Hapla   Level: intermediate
7651609dae6eSVaclav Hapla 
7652609dae6eSVaclav Hapla   Notes:
7653bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7654bb7acecfSBarry Smith 
765520f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7656bb7acecfSBarry Smith 
765720f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7658609dae6eSVaclav Hapla 
76595efe38ccSVaclav Hapla   The output message is set independently on each rank.
7660bb7acecfSBarry Smith 
7661bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7662bb7acecfSBarry Smith 
7663ce78bad3SBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to `stderr` in synchronized manner.
7664bb7acecfSBarry Smith 
766520f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7666609dae6eSVaclav Hapla 
7667609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7668bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7669609dae6eSVaclav Hapla 
7670cc4c1da9SBarry Smith   Developer Note:
7671ce78bad3SBarry Smith   Cannot automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7672cc4c1da9SBarry Smith 
76731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7674609dae6eSVaclav Hapla @*/
DMCompareLabels(DM dm0,DM dm1,PetscBool * equal,char * message[])7675ce78bad3SBarry Smith PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char *message[]) PeNS
7676d71ae5a4SJacob Faibussowitsch {
76775efe38ccSVaclav Hapla   PetscInt    n, i;
7678609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
76795efe38ccSVaclav Hapla   PetscBool   eq;
7680609dae6eSVaclav Hapla   MPI_Comm    comm;
76815efe38ccSVaclav Hapla   PetscMPIInt rank;
7682609dae6eSVaclav Hapla 
7683609dae6eSVaclav Hapla   PetscFunctionBegin;
7684609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7685609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7686609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
76874f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
76884f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
76899566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
76909566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
76915efe38ccSVaclav Hapla   {
76925efe38ccSVaclav Hapla     PetscInt n1;
76935efe38ccSVaclav Hapla 
76949566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76959566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76965efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
769748a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
76985440e5dcSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPI_C_BOOL, MPI_LAND, comm));
76995efe38ccSVaclav Hapla     if (!eq) goto finish;
77005efe38ccSVaclav Hapla   }
77015efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7702609dae6eSVaclav Hapla     DMLabel     l0, l1;
7703609dae6eSVaclav Hapla     const char *name;
7704609dae6eSVaclav Hapla     char       *msgInner;
7705609dae6eSVaclav Hapla 
7706609dae6eSVaclav Hapla     /* Ignore label order */
77079566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
77089566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
77099566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7710609dae6eSVaclav Hapla     if (!l1) {
771163a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
77125efe38ccSVaclav Hapla       eq = PETSC_FALSE;
77135efe38ccSVaclav Hapla       break;
7714609dae6eSVaclav Hapla     }
77159566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
77169566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
77179566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
77185efe38ccSVaclav Hapla     if (!eq) break;
7719609dae6eSVaclav Hapla   }
77205440e5dcSBarry Smith   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPI_C_BOOL, MPI_LAND, comm));
7721609dae6eSVaclav Hapla finish:
77225efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7723609dae6eSVaclav Hapla   if (message) {
7724609dae6eSVaclav Hapla     *message = NULL;
772548a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
77265efe38ccSVaclav Hapla   } else {
772748a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
77289566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
77295efe38ccSVaclav Hapla   }
77305efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
77315efe38ccSVaclav Hapla   if (equal) *equal = eq;
77327a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
77333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7734609dae6eSVaclav Hapla }
7735609dae6eSVaclav Hapla 
DMSetLabelValue_Fast(DM dm,DMLabel * label,const char name[],PetscInt point,PetscInt value)7736d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7737d71ae5a4SJacob Faibussowitsch {
7738461a15a0SLisandro Dalcin   PetscFunctionBegin;
77394f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7740461a15a0SLisandro Dalcin   if (!*label) {
77419566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
77429566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7743461a15a0SLisandro Dalcin   }
77449566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
77453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7746461a15a0SLisandro Dalcin }
7747461a15a0SLisandro Dalcin 
77480fdc7489SMatthew Knepley /*
77490fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
77500fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
77510fdc7489SMatthew Knepley   (label, id) pair in the DM.
77520fdc7489SMatthew Knepley 
77530fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
77540fdc7489SMatthew Knepley   each label.
77550fdc7489SMatthew Knepley */
DMUniversalLabelCreate(DM dm,DMUniversalLabel * universal)7756d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7757d71ae5a4SJacob Faibussowitsch {
77580fdc7489SMatthew Knepley   DMUniversalLabel ul;
77590fdc7489SMatthew Knepley   PetscBool       *active;
77600fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
77610fdc7489SMatthew Knepley 
77620fdc7489SMatthew Knepley   PetscFunctionBegin;
77639566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
77649566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
77659566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
77669566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
77670fdc7489SMatthew Knepley   ul->Nl = 0;
77680fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77690fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
77700fdc7489SMatthew Knepley     const char *name;
77710fdc7489SMatthew Knepley 
77729566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77739566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
77749566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
77750fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
77760fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
77770fdc7489SMatthew Knepley   }
77789566063dSJacob 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));
77790fdc7489SMatthew Knepley   ul->Nv = 0;
77800fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77810fdc7489SMatthew Knepley     DMLabel     label;
77820fdc7489SMatthew Knepley     PetscInt    nv;
77830fdc7489SMatthew Knepley     const char *name;
77840fdc7489SMatthew Knepley 
77850fdc7489SMatthew Knepley     if (!active[l]) continue;
77869566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77879566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77889566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77899566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
77900fdc7489SMatthew Knepley     ul->indices[m] = l;
77910fdc7489SMatthew Knepley     ul->Nv += nv;
77920fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77930fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77940fdc7489SMatthew Knepley     ++m;
77950fdc7489SMatthew Knepley   }
77960fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77970fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77980fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77990fdc7489SMatthew Knepley   }
78000fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78010fdc7489SMatthew Knepley     PetscInt b;
78020fdc7489SMatthew Knepley 
78030fdc7489SMatthew Knepley     ul->masks[l] = 0;
78040fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
78050fdc7489SMatthew Knepley   }
78069566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
78070fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
78080fdc7489SMatthew Knepley     DMLabel         label;
78090fdc7489SMatthew Knepley     IS              valueIS;
78100fdc7489SMatthew Knepley     const PetscInt *varr;
78110fdc7489SMatthew Knepley     PetscInt        nv, v;
78120fdc7489SMatthew Knepley 
78130fdc7489SMatthew Knepley     if (!active[l]) continue;
78149566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
78159566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
78169566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
78179566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7818ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
78199566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
78209566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
78219566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
78220fdc7489SMatthew Knepley     ++m;
78230fdc7489SMatthew Knepley   }
78249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
78250fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
78260fdc7489SMatthew Knepley     PetscInt  uval   = 0;
78270fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
78280fdc7489SMatthew Knepley 
78290fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
78300fdc7489SMatthew Knepley       DMLabel  label;
78310649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
78320fdc7489SMatthew Knepley 
78330fdc7489SMatthew Knepley       if (!active[l]) continue;
78349566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
78359566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
78369566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
78379371c9d4SSatish Balay       if (val == defval) {
78389371c9d4SSatish Balay         ++m;
78399371c9d4SSatish Balay         continue;
78409371c9d4SSatish Balay       }
78410649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
78420fdc7489SMatthew Knepley       marked = PETSC_TRUE;
78439566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
784463a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
78450fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
78460fdc7489SMatthew Knepley       ++m;
78470fdc7489SMatthew Knepley     }
78489566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
78490fdc7489SMatthew Knepley   }
78509566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
78510fdc7489SMatthew Knepley   *universal = ul;
78523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78530fdc7489SMatthew Knepley }
78540fdc7489SMatthew Knepley 
DMUniversalLabelDestroy(DMUniversalLabel * universal)7855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7856d71ae5a4SJacob Faibussowitsch {
78570fdc7489SMatthew Knepley   PetscInt l;
78580fdc7489SMatthew Knepley 
78590fdc7489SMatthew Knepley   PetscFunctionBegin;
78609566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
78619566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
78629566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
78639566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
78649566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
78650fdc7489SMatthew Knepley   *universal = NULL;
78663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78670fdc7489SMatthew Knepley }
78680fdc7489SMatthew Knepley 
DMUniversalLabelGetLabel(DMUniversalLabel ul,DMLabel * ulabel)7869d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7870d71ae5a4SJacob Faibussowitsch {
78710fdc7489SMatthew Knepley   PetscFunctionBegin;
78724f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
78730fdc7489SMatthew Knepley   *ulabel = ul->label;
78743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78750fdc7489SMatthew Knepley }
78760fdc7489SMatthew Knepley 
DMUniversalLabelCreateLabels(DMUniversalLabel ul,PetscBool preserveOrder,DM dm)7877d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7878d71ae5a4SJacob Faibussowitsch {
78790fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
78800fdc7489SMatthew Knepley 
78810fdc7489SMatthew Knepley   PetscFunctionBegin;
7882064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
78830fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
78849566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
78859566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
78860fdc7489SMatthew Knepley   }
78870fdc7489SMatthew Knepley   if (preserveOrder) {
78880fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
78890fdc7489SMatthew Knepley       const char *name;
78900fdc7489SMatthew Knepley       PetscBool   match;
78910fdc7489SMatthew Knepley 
78929566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78939566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
789463a3b9bcSJacob 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]);
78950fdc7489SMatthew Knepley     }
78960fdc7489SMatthew Knepley   }
78973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78980fdc7489SMatthew Knepley }
78990fdc7489SMatthew Knepley 
DMUniversalLabelSetLabelValue(DMUniversalLabel ul,DM dm,PetscBool useIndex,PetscInt p,PetscInt value)7900d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7901d71ae5a4SJacob Faibussowitsch {
79020fdc7489SMatthew Knepley   PetscInt l;
79030fdc7489SMatthew Knepley 
79040fdc7489SMatthew Knepley   PetscFunctionBegin;
79050fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
79060fdc7489SMatthew Knepley     DMLabel  label;
79070fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
79080fdc7489SMatthew Knepley 
79090fdc7489SMatthew Knepley     if (lval) {
79109566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
79119566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
79129566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
79130fdc7489SMatthew Knepley     }
79140fdc7489SMatthew Knepley   }
79153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79160fdc7489SMatthew Knepley }
7917a8fb8f29SToby Isaac 
7918a8fb8f29SToby Isaac /*@
7919bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7920bb7acecfSBarry Smith 
792120f4b53cSBarry Smith   Not Collective
7922a8fb8f29SToby Isaac 
7923a8fb8f29SToby Isaac   Input Parameter:
7924bb7acecfSBarry Smith . dm - The `DM` object
7925a8fb8f29SToby Isaac 
7926a8fb8f29SToby Isaac   Output Parameter:
7927bb7acecfSBarry Smith . cdm - The coarse `DM`
7928a8fb8f29SToby Isaac 
7929a8fb8f29SToby Isaac   Level: intermediate
7930a8fb8f29SToby Isaac 
79311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7932a8fb8f29SToby Isaac @*/
DMGetCoarseDM(DM dm,DM * cdm)7933d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7934d71ae5a4SJacob Faibussowitsch {
7935a8fb8f29SToby Isaac   PetscFunctionBegin;
7936a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79374f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7938a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
79393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7940a8fb8f29SToby Isaac }
7941a8fb8f29SToby Isaac 
7942a8fb8f29SToby Isaac /*@
7943bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7944a8fb8f29SToby Isaac 
7945a8fb8f29SToby Isaac   Input Parameters:
7946bb7acecfSBarry Smith + dm  - The `DM` object
7947bb7acecfSBarry Smith - cdm - The coarse `DM`
7948a8fb8f29SToby Isaac 
7949a8fb8f29SToby Isaac   Level: intermediate
7950a8fb8f29SToby Isaac 
7951bb7acecfSBarry Smith   Note:
7952bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7953bb7acecfSBarry Smith 
79541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7955a8fb8f29SToby Isaac @*/
DMSetCoarseDM(DM dm,DM cdm)7956d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7957d71ae5a4SJacob Faibussowitsch {
7958a8fb8f29SToby Isaac   PetscFunctionBegin;
7959a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7960a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
796189d734beSBarry Smith   if (dm == cdm) cdm = NULL;
79629566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
79639566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7964a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
79653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7966a8fb8f29SToby Isaac }
7967a8fb8f29SToby Isaac 
796888bdff64SToby Isaac /*@
7969bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
797088bdff64SToby Isaac 
797188bdff64SToby Isaac   Input Parameter:
7972bb7acecfSBarry Smith . dm - The `DM` object
797388bdff64SToby Isaac 
797488bdff64SToby Isaac   Output Parameter:
7975bb7acecfSBarry Smith . fdm - The fine `DM`
797688bdff64SToby Isaac 
797788bdff64SToby Isaac   Level: intermediate
797888bdff64SToby Isaac 
79791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
798088bdff64SToby Isaac @*/
DMGetFineDM(DM dm,DM * fdm)7981d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7982d71ae5a4SJacob Faibussowitsch {
798388bdff64SToby Isaac   PetscFunctionBegin;
798488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79854f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
798688bdff64SToby Isaac   *fdm = dm->fineMesh;
79873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
798888bdff64SToby Isaac }
798988bdff64SToby Isaac 
799088bdff64SToby Isaac /*@
7991bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
799288bdff64SToby Isaac 
799388bdff64SToby Isaac   Input Parameters:
7994bb7acecfSBarry Smith + dm  - The `DM` object
7995bb7acecfSBarry Smith - fdm - The fine `DM`
799688bdff64SToby Isaac 
7997bb7acecfSBarry Smith   Level: developer
799888bdff64SToby Isaac 
7999bb7acecfSBarry Smith   Note:
8000bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
8001bb7acecfSBarry Smith 
80021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
800388bdff64SToby Isaac @*/
DMSetFineDM(DM dm,DM fdm)8004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
8005d71ae5a4SJacob Faibussowitsch {
800688bdff64SToby Isaac   PetscFunctionBegin;
800788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
800888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
800989d734beSBarry Smith   if (dm == fdm) fdm = NULL;
80109566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
80119566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
801288bdff64SToby Isaac   dm->fineMesh = fdm;
80133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
801488bdff64SToby Isaac }
801588bdff64SToby Isaac 
8016a6ba4734SToby Isaac /*@C
8017a1b2db74SAlbert Cowie   DMAddBoundary - Add a boundary condition, for a single field, to a model represented by a `DM`
8018a6ba4734SToby Isaac 
801920f4b53cSBarry Smith   Collective
8020783e2ec8SMatthew G. Knepley 
8021a6ba4734SToby Isaac   Input Parameters:
8022bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
8023bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
8024a6ba4734SToby Isaac . name     - The BC name
802545480ffeSMatthew G. Knepley . label    - The label defining constrained points
8026bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
802745480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8028a6ba4734SToby Isaac . field    - The field to constrain
8029258f4e96SStefano Zampini . Nc       - The number of constrained field components (0 will constrain all components)
8030a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8031a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
80325d550117SStefano Zampini . bcFunc_t - A pointwise function giving the time derivative of the boundary values, or `NULL`
8033a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8034a6ba4734SToby Isaac 
803545480ffeSMatthew G. Knepley   Output Parameter:
803645480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
803745480ffeSMatthew G. Knepley 
8038a6ba4734SToby Isaac   Options Database Keys:
8039a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
8040a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8041a6ba4734SToby Isaac 
804220f4b53cSBarry Smith   Level: intermediate
804320f4b53cSBarry Smith 
8044bb7acecfSBarry Smith   Notes:
8045fa74d514SMatthew G. Knepley   If the `DM` is of type `DMPLEX` and the field is of type `PetscFE`, then this function completes the label using `DMPlexLabelComplete()`.
8046fa74d514SMatthew G. Knepley 
8047aeebefc2SPierre 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\:
804873ff1848SBarry Smith .vb
804973ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
805073ff1848SBarry Smith .ve
805156cf3b9cSMatthew G. Knepley 
8052a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
805356cf3b9cSMatthew G. Knepley 
805420f4b53cSBarry Smith .vb
805520f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
805620f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
805720f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
805820f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
805920f4b53cSBarry Smith .ve
806056cf3b9cSMatthew G. Knepley + dim - the spatial dimension
806156cf3b9cSMatthew G. Knepley . Nf - the number of fields
806256cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
806356cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
806456cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
806556cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
806656cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
806756cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
806856cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
806956cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
807056cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
807156cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
807256cf3b9cSMatthew G. Knepley . t - current time
807356cf3b9cSMatthew G. Knepley . x - coordinates of the current point
807456cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
807556cf3b9cSMatthew G. Knepley . constants - constant parameters
807656cf3b9cSMatthew G. Knepley - bcval - output values at the current point
807756cf3b9cSMatthew G. Knepley 
80781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
8079a6ba4734SToby Isaac @*/
DMAddBoundary(DM dm,DMBoundaryConditionType type,const char name[],DMLabel label,PetscInt Nv,const PetscInt values[],PetscInt field,PetscInt Nc,const PetscInt comps[],PetscVoidFn * bcFunc,PetscVoidFn * bcFunc_t,PetscCtx ctx,PetscInt * bd)8080*2a8381b2SBarry Smith PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], DMLabel label, PetscInt Nv, const PetscInt values[], PetscInt field, PetscInt Nc, const PetscInt comps[], PetscVoidFn *bcFunc, PetscVoidFn *bcFunc_t, PetscCtx ctx, PetscInt *bd)
8081d71ae5a4SJacob Faibussowitsch {
8082e5e52638SMatthew G. Knepley   PetscDS ds;
8083a6ba4734SToby Isaac 
8084a6ba4734SToby Isaac   PetscFunctionBegin;
8085a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8086783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
808745480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
808845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
808945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
809045480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
809101a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
80929566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8093799db056SMatthew G. Knepley   /* Complete label */
8094799db056SMatthew G. Knepley   if (label) {
8095799db056SMatthew G. Knepley     PetscObject  obj;
8096799db056SMatthew G. Knepley     PetscClassId id;
8097799db056SMatthew G. Knepley 
8098799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8099799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8100799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8101799db056SMatthew G. Knepley       DM plex;
8102799db056SMatthew G. Knepley 
8103799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8104799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8105799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8106799db056SMatthew G. Knepley     }
8107799db056SMatthew G. Knepley   }
81089566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
81093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8110a6ba4734SToby Isaac }
8111a6ba4734SToby Isaac 
811245480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
DMPopulateBoundary(DM dm)8113d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8114d71ae5a4SJacob Faibussowitsch {
8115e5e52638SMatthew G. Knepley   PetscDS     ds;
8116dff059c6SToby Isaac   DMBoundary *lastnext;
8117e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8118e6f8dbb6SToby Isaac 
8119e6f8dbb6SToby Isaac   PetscFunctionBegin;
81209566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8121e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
812247a1f5adSToby Isaac   if (dm->boundary) {
812347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
812447a1f5adSToby Isaac 
812547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
81263ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
812747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
812847a1f5adSToby Isaac     while (next) {
812947a1f5adSToby Isaac       DMBoundary b = next;
813047a1f5adSToby Isaac 
813147a1f5adSToby Isaac       next = b->next;
81329566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8133a6ba4734SToby Isaac     }
813447a1f5adSToby Isaac     dm->boundary = NULL;
8135a6ba4734SToby Isaac   }
813647a1f5adSToby Isaac 
8137f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8138e6f8dbb6SToby Isaac   while (dsbound) {
8139e6f8dbb6SToby Isaac     DMBoundary dmbound;
8140e6f8dbb6SToby Isaac 
81419566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8142e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
814345480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
814447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8145dff059c6SToby Isaac     *lastnext = dmbound;
8146f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8147dff059c6SToby Isaac     dsbound   = dsbound->next;
8148a6ba4734SToby Isaac   }
81493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8150a6ba4734SToby Isaac }
8151a6ba4734SToby Isaac 
8152bb7acecfSBarry Smith /* TODO: missing manual page */
DMIsBoundaryPoint(DM dm,PetscInt point,PetscBool * isBd)8153d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8154d71ae5a4SJacob Faibussowitsch {
8155b95f2879SToby Isaac   DMBoundary b;
8156a6ba4734SToby Isaac 
8157a6ba4734SToby Isaac   PetscFunctionBegin;
8158a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81594f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8160a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
81619566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8162b95f2879SToby Isaac   b = dm->boundary;
816357508eceSPierre Jolivet   while (b && !*isBd) {
8164e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8165e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8166a6ba4734SToby Isaac     PetscInt   i;
8167a6ba4734SToby Isaac 
816845480ffeSMatthew G. Knepley     if (label) {
816957508eceSPierre Jolivet       for (i = 0; i < dsb->Nv && !*isBd; ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8170a6ba4734SToby Isaac     }
8171a6ba4734SToby Isaac     b = b->next;
8172a6ba4734SToby Isaac   }
81733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8174a6ba4734SToby Isaac }
81754d6f44ffSToby Isaac 
817601468941SMatthew G. Knepley /*@
817701468941SMatthew G. Knepley   DMHasBound - Determine whether a bound condition was specified
817801468941SMatthew G. Knepley 
817901468941SMatthew G. Knepley   Logically collective
818001468941SMatthew G. Knepley 
818101468941SMatthew G. Knepley   Input Parameter:
818201468941SMatthew G. Knepley . dm - The `DM`, with a `PetscDS` that matches the problem being constrained
818301468941SMatthew G. Knepley 
818401468941SMatthew G. Knepley   Output Parameter:
818501468941SMatthew G. Knepley . hasBound - Flag indicating if a bound condition was specified
818601468941SMatthew G. Knepley 
818701468941SMatthew G. Knepley   Level: intermediate
818801468941SMatthew G. Knepley 
818901468941SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DSAddBoundary()`, `PetscDSAddBoundary()`
819001468941SMatthew G. Knepley @*/
DMHasBound(DM dm,PetscBool * hasBound)819101468941SMatthew G. Knepley PetscErrorCode DMHasBound(DM dm, PetscBool *hasBound)
819201468941SMatthew G. Knepley {
819301468941SMatthew G. Knepley   PetscDS  ds;
819401468941SMatthew G. Knepley   PetscInt Nf, numBd;
819501468941SMatthew G. Knepley 
819601468941SMatthew G. Knepley   PetscFunctionBegin;
819701468941SMatthew G. Knepley   *hasBound = PETSC_FALSE;
819801468941SMatthew G. Knepley   PetscCall(DMGetDS(dm, &ds));
819901468941SMatthew G. Knepley   PetscCall(PetscDSGetNumFields(ds, &Nf));
820001468941SMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
82012192575eSBarry Smith     PetscSimplePointFn *lfunc, *ufunc;
820201468941SMatthew G. Knepley 
820301468941SMatthew G. Knepley     PetscCall(PetscDSGetLowerBound(ds, f, &lfunc, NULL));
820401468941SMatthew G. Knepley     PetscCall(PetscDSGetUpperBound(ds, f, &ufunc, NULL));
820501468941SMatthew G. Knepley     if (lfunc || ufunc) *hasBound = PETSC_TRUE;
820601468941SMatthew G. Knepley   }
820701468941SMatthew G. Knepley 
820801468941SMatthew G. Knepley   PetscCall(PetscDSGetNumBoundary(ds, &numBd));
820901468941SMatthew G. Knepley   PetscCall(PetscDSUpdateBoundaryLabels(ds, dm));
821001468941SMatthew G. Knepley   for (PetscInt b = 0; b < numBd; ++b) {
821101468941SMatthew G. Knepley     PetscWeakForm           wf;
821201468941SMatthew G. Knepley     DMBoundaryConditionType type;
821301468941SMatthew G. Knepley     const char             *name;
821401468941SMatthew G. Knepley     DMLabel                 label;
821501468941SMatthew G. Knepley     PetscInt                numids;
821601468941SMatthew G. Knepley     const PetscInt         *ids;
821701468941SMatthew G. Knepley     PetscInt                field, Nc;
821801468941SMatthew G. Knepley     const PetscInt         *comps;
821957d50842SBarry Smith     PetscVoidFn            *bvfunc;
822001468941SMatthew G. Knepley     void                   *ctx;
822101468941SMatthew G. Knepley 
822201468941SMatthew G. Knepley     PetscCall(PetscDSGetBoundary(ds, b, &wf, &type, &name, &label, &numids, &ids, &field, &Nc, &comps, &bvfunc, NULL, &ctx));
822301468941SMatthew G. Knepley     if (type == DM_BC_LOWER_BOUND || type == DM_BC_UPPER_BOUND) *hasBound = PETSC_TRUE;
822401468941SMatthew G. Knepley   }
822501468941SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
822601468941SMatthew G. Knepley }
822701468941SMatthew G. Knepley 
82284d6f44ffSToby Isaac /*@C
8229bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8230a6e0b375SMatthew G. Knepley 
823120f4b53cSBarry Smith   Collective
82324d6f44ffSToby Isaac 
82334d6f44ffSToby Isaac   Input Parameters:
8234bb7acecfSBarry Smith + dm    - The `DM`
82350709b2feSToby Isaac . time  - The time
82364d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
82374d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
82384d6f44ffSToby Isaac - mode  - The insertion mode for values
82394d6f44ffSToby Isaac 
82404d6f44ffSToby Isaac   Output Parameter:
82414d6f44ffSToby Isaac . X - vector
82424d6f44ffSToby Isaac 
824320f4b53cSBarry Smith   Calling sequence of `funcs`:
82444d6f44ffSToby Isaac + dim  - The spatial dimension
82458ec8862eSJed Brown . time - The time at which to sample
82464d6f44ffSToby Isaac . x    - The coordinates
824777b739a6SMatthew Knepley . Nc   - The number of components
82484d6f44ffSToby Isaac . u    - The output field values
82494d6f44ffSToby Isaac - ctx  - optional user-defined function context
82504d6f44ffSToby Isaac 
82514d6f44ffSToby Isaac   Level: developer
82524d6f44ffSToby Isaac 
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`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
82594d6f44ffSToby Isaac @*/
DMProjectFunction(DM dm,PetscReal time,PetscErrorCode (** funcs)(PetscInt dim,PetscReal time,const PetscReal x[],PetscInt Nc,PetscScalar * u,PetscCtx ctx),void ** ctxs,InsertMode mode,Vec X)8260*2a8381b2SBarry Smith PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, PetscCtx ctx), void **ctxs, InsertMode mode, Vec X)
8261d71ae5a4SJacob Faibussowitsch {
82624d6f44ffSToby Isaac   Vec localX;
82634d6f44ffSToby Isaac 
82644d6f44ffSToby Isaac   PetscFunctionBegin;
82654d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8266708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
82679566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8268f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82699566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
82709566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82719566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82729566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8273708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
82743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82754d6f44ffSToby Isaac }
82764d6f44ffSToby Isaac 
8277a6e0b375SMatthew G. Knepley /*@C
8278bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8279a6e0b375SMatthew G. Knepley 
828020f4b53cSBarry Smith   Not Collective
8281a6e0b375SMatthew G. Knepley 
8282a6e0b375SMatthew G. Knepley   Input Parameters:
8283bb7acecfSBarry Smith + dm    - The `DM`
8284a6e0b375SMatthew G. Knepley . time  - The time
8285a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8286a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8287a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8288a6e0b375SMatthew G. Knepley 
8289a6e0b375SMatthew G. Knepley   Output Parameter:
8290a6e0b375SMatthew G. Knepley . localX - vector
8291a6e0b375SMatthew G. Knepley 
829220f4b53cSBarry Smith   Calling sequence of `funcs`:
8293a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8294a4e35b19SJacob Faibussowitsch . time - The current timestep
8295a6e0b375SMatthew G. Knepley . x    - The coordinates
829677b739a6SMatthew Knepley . Nc   - The number of components
8297a6e0b375SMatthew G. Knepley . u    - The output field values
8298a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8299a6e0b375SMatthew G. Knepley 
8300a6e0b375SMatthew G. Knepley   Level: developer
8301a6e0b375SMatthew G. Knepley 
8302bb7acecfSBarry Smith   Developer Notes:
8303bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8304bb7acecfSBarry Smith 
8305bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8306bb7acecfSBarry Smith 
83071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8308a6e0b375SMatthew G. Knepley @*/
DMProjectFunctionLocal(DM dm,PetscReal time,PetscErrorCode (** funcs)(PetscInt dim,PetscReal time,const PetscReal x[],PetscInt Nc,PetscScalar * u,PetscCtx ctx),void ** ctxs,InsertMode mode,Vec localX)8309*2a8381b2SBarry Smith PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, PetscCtx ctx), void **ctxs, InsertMode mode, Vec localX)
8310d71ae5a4SJacob Faibussowitsch {
83114d6f44ffSToby Isaac   PetscFunctionBegin;
83124d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8313064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8314fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
83153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83164d6f44ffSToby Isaac }
83174d6f44ffSToby Isaac 
8318a6e0b375SMatthew G. Knepley /*@C
8319bb7acecfSBarry 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.
8320a6e0b375SMatthew G. Knepley 
832120f4b53cSBarry Smith   Collective
8322a6e0b375SMatthew G. Knepley 
8323a6e0b375SMatthew G. Knepley   Input Parameters:
8324bb7acecfSBarry Smith + dm     - The `DM`
8325a6e0b375SMatthew G. Knepley . time   - The time
8326a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8327a4e35b19SJacob Faibussowitsch . ids    - The ids
8328a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8329a4e35b19SJacob Faibussowitsch . comps  - The components
8330bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8331a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8332bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8333a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8334a6e0b375SMatthew G. Knepley 
8335a6e0b375SMatthew G. Knepley   Output Parameter:
8336a6e0b375SMatthew G. Knepley . X - vector
8337a6e0b375SMatthew G. Knepley 
833820f4b53cSBarry Smith   Calling sequence of `funcs`:
8339a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8340a4e35b19SJacob Faibussowitsch . time - The current timestep
8341a6e0b375SMatthew G. Knepley . x    - The coordinates
834277b739a6SMatthew Knepley . Nc   - The number of components
8343a6e0b375SMatthew G. Knepley . u    - The output field values
8344a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8345a6e0b375SMatthew G. Knepley 
8346a6e0b375SMatthew G. Knepley   Level: developer
8347a6e0b375SMatthew G. Knepley 
8348bb7acecfSBarry Smith   Developer Notes:
8349bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8350bb7acecfSBarry Smith 
8351bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8352bb7acecfSBarry Smith 
83531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8354a6e0b375SMatthew G. Knepley @*/
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,PetscCtx ctx),void ** ctxs,InsertMode mode,Vec X)8355*2a8381b2SBarry Smith 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, PetscCtx ctx), void **ctxs, InsertMode mode, Vec X)
8356d71ae5a4SJacob Faibussowitsch {
83572c53366bSMatthew G. Knepley   Vec localX;
83582c53366bSMatthew G. Knepley 
83592c53366bSMatthew G. Knepley   PetscFunctionBegin;
83602c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83619566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8362f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
83639566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
83649566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
83659566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
83669566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
83673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83682c53366bSMatthew G. Knepley }
83692c53366bSMatthew G. Knepley 
8370a6e0b375SMatthew G. Knepley /*@C
8371bb7acecfSBarry 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.
8372a6e0b375SMatthew G. Knepley 
837320f4b53cSBarry Smith   Not Collective
8374a6e0b375SMatthew G. Knepley 
8375a6e0b375SMatthew G. Knepley   Input Parameters:
8376bb7acecfSBarry Smith + dm     - The `DM`
8377a6e0b375SMatthew G. Knepley . time   - The time
8378bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8379a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8380a4e35b19SJacob Faibussowitsch . ids    - The ids
8381a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8382a4e35b19SJacob Faibussowitsch . comps  - The components
8383a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8384a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8385a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8386a6e0b375SMatthew G. Knepley 
8387a6e0b375SMatthew G. Knepley   Output Parameter:
8388a6e0b375SMatthew G. Knepley . localX - vector
8389a6e0b375SMatthew G. Knepley 
839020f4b53cSBarry Smith   Calling sequence of `funcs`:
8391a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8392a4e35b19SJacob Faibussowitsch . time - The current time
8393a6e0b375SMatthew G. Knepley . x    - The coordinates
839477b739a6SMatthew Knepley . Nc   - The number of components
8395a6e0b375SMatthew G. Knepley . u    - The output field values
8396a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8397a6e0b375SMatthew G. Knepley 
8398a6e0b375SMatthew G. Knepley   Level: developer
8399a6e0b375SMatthew G. Knepley 
8400bb7acecfSBarry Smith   Developer Notes:
8401bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8402bb7acecfSBarry Smith 
8403bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8404bb7acecfSBarry Smith 
84051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8406a6e0b375SMatthew G. Knepley @*/
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,PetscCtx ctx),void ** ctxs,InsertMode mode,Vec localX)8407*2a8381b2SBarry Smith 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, PetscCtx ctx), void **ctxs, InsertMode mode, Vec localX)
8408d71ae5a4SJacob Faibussowitsch {
84094d6f44ffSToby Isaac   PetscFunctionBegin;
84104d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8411064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8412fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
84133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84144d6f44ffSToby Isaac }
84152716604bSToby Isaac 
8416a6e0b375SMatthew G. Knepley /*@C
8417bb7acecfSBarry 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.
8418a6e0b375SMatthew G. Knepley 
841920f4b53cSBarry Smith   Not Collective
8420a6e0b375SMatthew G. Knepley 
8421a6e0b375SMatthew G. Knepley   Input Parameters:
8422bb7acecfSBarry Smith + dm     - The `DM`
8423a6e0b375SMatthew G. Knepley . time   - The time
842420f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8425a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8426a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8427a6e0b375SMatthew G. Knepley 
8428a6e0b375SMatthew G. Knepley   Output Parameter:
8429a6e0b375SMatthew G. Knepley . localX - The output vector
8430a6e0b375SMatthew G. Knepley 
843120f4b53cSBarry Smith   Calling sequence of `funcs`:
8432a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8433a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8434a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8435a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8436a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8437a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
84385d550117SStefano Zampini . u_t          - The field time derivative at this point in space (or `NULL`)
8439a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8440a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8441a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8442a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
84435d550117SStefano Zampini . a_t          - The auxiliary field time derivative at this point in space (or `NULL`)
8444a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8445a6e0b375SMatthew G. Knepley . t            - The current time
8446a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8447a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8448a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8449a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8450a6e0b375SMatthew G. Knepley 
845173ff1848SBarry Smith   Level: intermediate
845273ff1848SBarry Smith 
8453bb7acecfSBarry Smith   Note:
8454bb7acecfSBarry 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.
8455bb7acecfSBarry 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
8456bb7acecfSBarry 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
8457a6e0b375SMatthew 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.
8458a6e0b375SMatthew G. Knepley 
8459bb7acecfSBarry Smith   Developer Notes:
8460bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8461bb7acecfSBarry Smith 
8462bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8463bb7acecfSBarry Smith 
8464a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8465a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8466a6e0b375SMatthew G. Knepley @*/
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)8467a4e35b19SJacob 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)
8468d71ae5a4SJacob Faibussowitsch {
84698c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84708c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8471eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
84728c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8473fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
84743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84758c6c5593SMatthew G. Knepley }
84768c6c5593SMatthew G. Knepley 
8477a6e0b375SMatthew G. Knepley /*@C
8478a6e0b375SMatthew 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.
8479a6e0b375SMatthew G. Knepley 
848020f4b53cSBarry Smith   Not Collective
8481a6e0b375SMatthew G. Knepley 
8482a6e0b375SMatthew G. Knepley   Input Parameters:
8483bb7acecfSBarry Smith + dm     - The `DM`
8484a6e0b375SMatthew G. Knepley . time   - The time
8485bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8486a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8487a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8488bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
848920f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8490a6e0b375SMatthew G. Knepley . localU - The input field vector
8491a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8492a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8493a6e0b375SMatthew G. Knepley 
8494a6e0b375SMatthew G. Knepley   Output Parameter:
8495a6e0b375SMatthew G. Knepley . localX - The output vector
8496a6e0b375SMatthew G. Knepley 
849720f4b53cSBarry Smith   Calling sequence of `funcs`:
8498a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8499a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8500a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8501a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8502a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8503a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
85045d550117SStefano Zampini . u_t          - The field time derivative at this point in space (or `NULL`)
8505a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8506a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8507a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8508a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
85095d550117SStefano Zampini . a_t          - The auxiliary field time derivative at this point in space (or `NULL`)
8510a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8511a6e0b375SMatthew G. Knepley . t            - The current time
8512a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8513a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8514a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8515a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8516a6e0b375SMatthew G. Knepley 
851773ff1848SBarry Smith   Level: intermediate
851873ff1848SBarry Smith 
8519bb7acecfSBarry Smith   Note:
8520bb7acecfSBarry 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.
8521bb7acecfSBarry 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
8522bb7acecfSBarry 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
8523a6e0b375SMatthew 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.
8524a6e0b375SMatthew G. Knepley 
8525bb7acecfSBarry Smith   Developer Notes:
8526bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8527bb7acecfSBarry Smith 
8528bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8529bb7acecfSBarry Smith 
85301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8531a6e0b375SMatthew G. Knepley @*/
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)8532a4e35b19SJacob 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)
8533d71ae5a4SJacob Faibussowitsch {
85348c6c5593SMatthew G. Knepley   PetscFunctionBegin;
85358c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8536064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8537064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8538fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85408c6c5593SMatthew G. Knepley }
85418c6c5593SMatthew G. Knepley 
85422716604bSToby Isaac /*@C
8543d29d7c6eSMatthew 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.
8544d29d7c6eSMatthew G. Knepley 
854520f4b53cSBarry Smith   Not Collective
8546d29d7c6eSMatthew G. Knepley 
8547d29d7c6eSMatthew G. Knepley   Input Parameters:
8548bb7acecfSBarry Smith + dm     - The `DM`
8549d29d7c6eSMatthew G. Knepley . time   - The time
8550bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8551d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8552d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8553bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
855420f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8555d29d7c6eSMatthew G. Knepley . U      - The input field vector
8556d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8557d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8558d29d7c6eSMatthew G. Knepley 
8559d29d7c6eSMatthew G. Knepley   Output Parameter:
8560d29d7c6eSMatthew G. Knepley . X - The output vector
8561d29d7c6eSMatthew G. Knepley 
856220f4b53cSBarry Smith   Calling sequence of `funcs`:
8563d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8564d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8565d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8566d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8567d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8568d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
85695d550117SStefano Zampini . u_t          - The field time derivative at this point in space (or `NULL`)
8570d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8571d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8572d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8573d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
85745d550117SStefano Zampini . a_t          - The auxiliary field time derivative at this point in space (or `NULL`)
8575d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8576d29d7c6eSMatthew G. Knepley . t            - The current time
8577d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8578d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8579d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8580d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8581d29d7c6eSMatthew G. Knepley 
858273ff1848SBarry Smith   Level: intermediate
858373ff1848SBarry Smith 
8584bb7acecfSBarry Smith   Note:
8585bb7acecfSBarry 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.
8586bb7acecfSBarry 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
8587bb7acecfSBarry 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
8588d29d7c6eSMatthew 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.
8589d29d7c6eSMatthew G. Knepley 
8590bb7acecfSBarry Smith   Developer Notes:
8591bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8592bb7acecfSBarry Smith 
8593bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8594bb7acecfSBarry Smith 
85951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8596d29d7c6eSMatthew G. Knepley @*/
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)8597a4e35b19SJacob 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)
8598d71ae5a4SJacob Faibussowitsch {
8599d29d7c6eSMatthew G. Knepley   DM  dmIn;
8600d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8601d29d7c6eSMatthew G. Knepley 
8602d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8603d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8604d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8605d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8606d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8607f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
860872fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
860972fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8610d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8611d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8612d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8613d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8614d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
86153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8616d29d7c6eSMatthew G. Knepley }
8617d29d7c6eSMatthew G. Knepley 
8618d29d7c6eSMatthew G. Knepley /*@C
8619ece3a9fcSMatthew 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.
8620ece3a9fcSMatthew G. Knepley 
862120f4b53cSBarry Smith   Not Collective
8622ece3a9fcSMatthew G. Knepley 
8623ece3a9fcSMatthew G. Knepley   Input Parameters:
8624bb7acecfSBarry Smith + dm     - The `DM`
8625ece3a9fcSMatthew G. Knepley . time   - The time
8626bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8627ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8628ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8629bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
863020f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8631ece3a9fcSMatthew G. Knepley . localU - The input field vector
8632ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8633ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8634ece3a9fcSMatthew G. Knepley 
8635ece3a9fcSMatthew G. Knepley   Output Parameter:
8636ece3a9fcSMatthew G. Knepley . localX - The output vector
8637ece3a9fcSMatthew G. Knepley 
863820f4b53cSBarry Smith   Calling sequence of `funcs`:
8639ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8640ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8641ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8642ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8643ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8644ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
86455d550117SStefano Zampini . u_t          - The field time derivative at this point in space (or `NULL`)
8646ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8647ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8648ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8649ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
86505d550117SStefano Zampini . a_t          - The auxiliary field time derivative at this point in space (or `NULL`)
8651ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8652ece3a9fcSMatthew G. Knepley . t            - The current time
8653ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8654ece3a9fcSMatthew G. Knepley . n            - The face normal
8655ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8656ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8657ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8658ece3a9fcSMatthew G. Knepley 
865973ff1848SBarry Smith   Level: intermediate
866073ff1848SBarry Smith 
8661ece3a9fcSMatthew G. Knepley   Note:
8662bb7acecfSBarry 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.
8663bb7acecfSBarry 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
8664bb7acecfSBarry 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
8665ece3a9fcSMatthew 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.
8666ece3a9fcSMatthew G. Knepley 
8667bb7acecfSBarry Smith   Developer Notes:
8668bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8669bb7acecfSBarry Smith 
8670bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8671bb7acecfSBarry Smith 
86721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8673ece3a9fcSMatthew G. Knepley @*/
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)8674a4e35b19SJacob 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)
8675d71ae5a4SJacob Faibussowitsch {
8676ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8677ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8678064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8679064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8680fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
86813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8682ece3a9fcSMatthew G. Knepley }
8683ece3a9fcSMatthew G. Knepley 
8684ece3a9fcSMatthew G. Knepley /*@C
86852716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
86862716604bSToby Isaac 
868720f4b53cSBarry Smith   Collective
8688bb7acecfSBarry Smith 
86892716604bSToby Isaac   Input Parameters:
8690bb7acecfSBarry Smith + dm    - The `DM`
86910709b2feSToby Isaac . time  - The time
86922716604bSToby Isaac . funcs - The functions to evaluate for each field component
86935d550117SStefano Zampini . ctxs  - Optional array of contexts to pass to each function, or `NULL`.
8694574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86952716604bSToby Isaac 
86962716604bSToby Isaac   Output Parameter:
86972716604bSToby Isaac . diff - The diff ||u - u_h||_2
86982716604bSToby Isaac 
86992716604bSToby Isaac   Level: developer
87002716604bSToby Isaac 
8701bb7acecfSBarry Smith   Developer Notes:
8702bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8703bb7acecfSBarry Smith 
8704bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8705bb7acecfSBarry Smith 
87061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
87072716604bSToby Isaac @*/
DMComputeL2Diff(DM dm,PetscReal time,PetscErrorCode (** funcs)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void ** ctxs,Vec X,PetscReal * diff)8708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8709d71ae5a4SJacob Faibussowitsch {
87102716604bSToby Isaac   PetscFunctionBegin;
87112716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8712b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8713fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
87143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
87152716604bSToby Isaac }
8716b698f381SToby Isaac 
8717b698f381SToby Isaac /*@C
8718b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8719b698f381SToby Isaac 
872020f4b53cSBarry Smith   Collective
8721d083f849SBarry Smith 
8722b698f381SToby Isaac   Input Parameters:
8723bb7acecfSBarry Smith + dm    - The `DM`
8724a4e35b19SJacob Faibussowitsch . time  - The time
8725b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
87265d550117SStefano Zampini . ctxs  - Optional array of contexts to pass to each function, or `NULL`.
8727574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8728b698f381SToby Isaac - n     - The vector to project along
8729b698f381SToby Isaac 
8730b698f381SToby Isaac   Output Parameter:
8731b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8732b698f381SToby Isaac 
8733b698f381SToby Isaac   Level: developer
8734b698f381SToby Isaac 
8735bb7acecfSBarry Smith   Developer Notes:
8736bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8737bb7acecfSBarry Smith 
8738bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8739bb7acecfSBarry Smith 
87401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8741b698f381SToby Isaac @*/
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)8742d71ae5a4SJacob 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)
8743d71ae5a4SJacob Faibussowitsch {
8744b698f381SToby Isaac   PetscFunctionBegin;
8745b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8746b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8747fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
87483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8749b698f381SToby Isaac }
8750b698f381SToby Isaac 
87512a16baeaSToby Isaac /*@C
87522a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
87532a16baeaSToby Isaac 
875420f4b53cSBarry Smith   Collective
8755d083f849SBarry Smith 
87562a16baeaSToby Isaac   Input Parameters:
8757bb7acecfSBarry Smith + dm    - The `DM`
87582a16baeaSToby Isaac . time  - The time
87592a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
87605d550117SStefano Zampini . ctxs  - Optional array of contexts to pass to each function, or `NULL`.
8761574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
87622a16baeaSToby Isaac 
87632a16baeaSToby Isaac   Output Parameter:
87642a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
87652a16baeaSToby Isaac 
87662a16baeaSToby Isaac   Level: developer
87672a16baeaSToby Isaac 
8768bb7acecfSBarry Smith   Developer Notes:
8769bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8770bb7acecfSBarry Smith 
8771bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8772bb7acecfSBarry Smith 
877342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
87742a16baeaSToby Isaac @*/
DMComputeL2FieldDiff(DM dm,PetscReal time,PetscErrorCode (** funcs)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void ** ctxs,Vec X,PetscReal diff[])8775d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8776d71ae5a4SJacob Faibussowitsch {
87772a16baeaSToby Isaac   PetscFunctionBegin;
87782a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87792a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8780fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
87813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
87822a16baeaSToby Isaac }
87832a16baeaSToby Isaac 
8784df0b854cSToby Isaac /*@C
8785bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8786502a2867SDave May 
8787502a2867SDave May   Not Collective
8788502a2867SDave May 
8789502a2867SDave May   Input Parameter:
8790bb7acecfSBarry Smith . dm - The `DM`
8791502a2867SDave May 
87920a19bb7dSprj-   Output Parameters:
87930a19bb7dSprj- + nranks - the number of neighbours
87940a19bb7dSprj- - ranks  - the neighbors ranks
8795502a2867SDave May 
87969bdbcad8SBarry Smith   Level: beginner
87979bdbcad8SBarry Smith 
8798bb7acecfSBarry Smith   Note:
8799bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8800502a2867SDave May 
88011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8802502a2867SDave May @*/
DMGetNeighbors(DM dm,PetscInt * nranks,const PetscMPIInt * ranks[])8803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8804d71ae5a4SJacob Faibussowitsch {
8805502a2867SDave May   PetscFunctionBegin;
8806502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8807fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
88083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8809502a2867SDave May }
8810502a2867SDave May 
8811531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8812531c7667SBarry Smith 
8813531c7667SBarry Smith /*
8814531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
88152b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8816531c7667SBarry Smith */
MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void * sctx)881766976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8818d71ae5a4SJacob Faibussowitsch {
8819531c7667SBarry Smith   PetscFunctionBegin;
8820531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8821531c7667SBarry Smith     Vec x1local;
8822531c7667SBarry Smith     DM  dm;
88239566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
88247a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
88259566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
88269566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
88279566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8828531c7667SBarry Smith     x1 = x1local;
8829531c7667SBarry Smith   }
88309566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8831531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8832531c7667SBarry Smith     DM dm;
88339566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
88349566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8835531c7667SBarry Smith   }
88363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8837531c7667SBarry Smith }
8838531c7667SBarry Smith 
8839531c7667SBarry Smith /*@
8840bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8841531c7667SBarry Smith 
8842a4e35b19SJacob Faibussowitsch   Input Parameters:
8843a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8844a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8845531c7667SBarry Smith 
88469bdbcad8SBarry Smith   Level: advanced
88479bdbcad8SBarry Smith 
884873ff1848SBarry Smith   Developer Note:
884973ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8850531c7667SBarry Smith 
88511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8852531c7667SBarry Smith @*/
MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)8853d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8854d71ae5a4SJacob Faibussowitsch {
8855531c7667SBarry Smith   PetscFunctionBegin;
8856531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
88573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8858531c7667SBarry Smith }
88598320bc6fSPatrick Sanan 
88608320bc6fSPatrick Sanan /*@
8861bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
88628320bc6fSPatrick Sanan 
88638320bc6fSPatrick Sanan   Collective
88648320bc6fSPatrick Sanan 
88658320bc6fSPatrick Sanan   Input Parameters:
8866bb7acecfSBarry Smith + dm1 - the first `DM`
8867bb7acecfSBarry Smith - dm2 - the second `DM`
88688320bc6fSPatrick Sanan 
88698320bc6fSPatrick Sanan   Output Parameters:
8870bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8871bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
88728320bc6fSPatrick Sanan 
887320f4b53cSBarry Smith   Level: advanced
887420f4b53cSBarry Smith 
88758320bc6fSPatrick Sanan   Notes:
8876bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
88773d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
88788320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8879bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
88803d862458SPatrick Sanan   decomposition, but hold different data.
88818320bc6fSPatrick Sanan 
88828320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8883bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
88848320bc6fSPatrick Sanan 
8885bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
88868320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
88878320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8888bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
88898320bc6fSPatrick Sanan 
8890bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8891bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
88928320bc6fSPatrick Sanan .vb
88938320bc6fSPatrick Sanan   ...
88949566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
88958320bc6fSPatrick Sanan   if (set && compatible)  {
88969566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
88979566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
88989566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
88998320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
89008320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
89018320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
89028320bc6fSPatrick Sanan       }
89038320bc6fSPatrick Sanan     }
89049566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
89059566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
89068320bc6fSPatrick Sanan   } else {
89078320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
89088320bc6fSPatrick Sanan   }
89098320bc6fSPatrick Sanan   ...
89108320bc6fSPatrick Sanan .ve
89118320bc6fSPatrick Sanan 
8912bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
89138320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
89148320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
89158320bc6fSPatrick Sanan   always check the "set" output parameter.
89168320bc6fSPatrick Sanan 
8917bb7acecfSBarry Smith   A `DM` is always compatible with itself.
89188320bc6fSPatrick Sanan 
8919bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
89208320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
89218320bc6fSPatrick Sanan   incompatible.
89228320bc6fSPatrick Sanan 
89238320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8924bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
89258320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
89268320bc6fSPatrick Sanan 
892773ff1848SBarry Smith   Developer Note:
8928bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
89293d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8930a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8931bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
89328320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
89333d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8934bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
89358320bc6fSPatrick Sanan 
89361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
89378320bc6fSPatrick Sanan @*/
DMGetCompatibility(DM dm1,DM dm2,PetscBool * compatible,PetscBool * set)8938d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8939d71ae5a4SJacob Faibussowitsch {
89408320bc6fSPatrick Sanan   PetscMPIInt compareResult;
89418320bc6fSPatrick Sanan   DMType      type, type2;
89428320bc6fSPatrick Sanan   PetscBool   sameType;
89438320bc6fSPatrick Sanan 
89448320bc6fSPatrick Sanan   PetscFunctionBegin;
8945a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
89468320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
89478320bc6fSPatrick Sanan 
89488320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8949a5bc1bf3SBarry Smith   if (dm1 == dm2) {
89508320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
89518320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
89523ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
89538320bc6fSPatrick Sanan   }
89548320bc6fSPatrick Sanan 
89558320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
89568320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
89578320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
89588320bc6fSPatrick Sanan      determined by the implementation-specific logic */
89599566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
89608320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
89618320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
89628320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
89633ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
89648320bc6fSPatrick Sanan   }
89658320bc6fSPatrick Sanan 
89668320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8967a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8968dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
89693ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
89708320bc6fSPatrick Sanan   }
89718320bc6fSPatrick Sanan 
8972a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
89738320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
89749566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
89759566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
89769566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
89778320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8978dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
89798320bc6fSPatrick Sanan   } else {
89808320bc6fSPatrick Sanan     *set = PETSC_FALSE;
89818320bc6fSPatrick Sanan   }
89823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89838320bc6fSPatrick Sanan }
8984c0f0dcc3SMatthew G. Knepley 
8985c0f0dcc3SMatthew G. Knepley /*@C
8986bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8987c0f0dcc3SMatthew G. Knepley 
898820f4b53cSBarry Smith   Logically Collective
8989c0f0dcc3SMatthew G. Knepley 
8990c0f0dcc3SMatthew G. Knepley   Input Parameters:
899160225df5SJacob Faibussowitsch + dm             - the `DM`
8992c0f0dcc3SMatthew G. Knepley . f              - the monitor function
899320f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
899449abdd8aSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`), see `PetscCtxDestroyFn` for the calling sequence
8995c0f0dcc3SMatthew G. Knepley 
899620f4b53cSBarry Smith   Options Database Key:
899760225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8998c0f0dcc3SMatthew G. Knepley                        does not cancel those set via the options database.
8999c0f0dcc3SMatthew G. Knepley 
90009bdbcad8SBarry Smith   Level: intermediate
90019bdbcad8SBarry Smith 
9002bb7acecfSBarry Smith   Note:
9003c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9004bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
9005c0f0dcc3SMatthew G. Knepley   order in which they were set.
9006c0f0dcc3SMatthew G. Knepley 
900773ff1848SBarry Smith   Fortran Note:
9008bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
9009bb7acecfSBarry Smith 
901073ff1848SBarry Smith   Developer Note:
9011bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
9012c0f0dcc3SMatthew G. Knepley 
901349abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`, `PetscCtxDestroyFn`
9014c0f0dcc3SMatthew G. Knepley @*/
DMMonitorSet(DM dm,PetscErrorCode (* f)(DM,void *),void * mctx,PetscCtxDestroyFn * monitordestroy)901549abdd8aSBarry Smith PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscCtxDestroyFn *monitordestroy)
9016d71ae5a4SJacob Faibussowitsch {
9017c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9018c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9019453a69bbSBarry Smith   for (PetscInt m = 0; m < dm->numbermonitors; ++m) {
9020c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9021c0f0dcc3SMatthew G. Knepley 
9022453a69bbSBarry Smith     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))(PetscVoidFn *)f, mctx, monitordestroy, (PetscErrorCode (*)(void))(PetscVoidFn *)dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
90233ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
9024c0f0dcc3SMatthew G. Knepley   }
90257a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9026c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9027c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9028835f2295SStefano Zampini   dm->monitorcontext[dm->numbermonitors++] = mctx;
90293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9030c0f0dcc3SMatthew G. Knepley }
9031c0f0dcc3SMatthew G. Knepley 
9032c0f0dcc3SMatthew G. Knepley /*@
9033bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
9034c0f0dcc3SMatthew G. Knepley 
903520f4b53cSBarry Smith   Logically Collective
9036c0f0dcc3SMatthew G. Knepley 
9037c0f0dcc3SMatthew G. Knepley   Input Parameter:
9038c0f0dcc3SMatthew G. Knepley . dm - the DM
9039c0f0dcc3SMatthew G. Knepley 
9040c0f0dcc3SMatthew G. Knepley   Options Database Key:
9041c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9042bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
9043c0f0dcc3SMatthew G. Knepley   set via the options database
9044c0f0dcc3SMatthew G. Knepley 
90459bdbcad8SBarry Smith   Level: intermediate
90469bdbcad8SBarry Smith 
9047bb7acecfSBarry Smith   Note:
9048bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
9049c0f0dcc3SMatthew G. Knepley 
90501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
9051c0f0dcc3SMatthew G. Knepley @*/
DMMonitorCancel(DM dm)9052d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
9053d71ae5a4SJacob Faibussowitsch {
9054c0f0dcc3SMatthew G. Knepley   PetscInt m;
9055c0f0dcc3SMatthew G. Knepley 
9056c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9057c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9058c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
90599566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
9060c0f0dcc3SMatthew G. Knepley   }
9061c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
90623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9063c0f0dcc3SMatthew G. Knepley }
9064c0f0dcc3SMatthew G. Knepley 
9065c0f0dcc3SMatthew G. Knepley /*@C
9066c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9067c0f0dcc3SMatthew G. Knepley 
906820f4b53cSBarry Smith   Collective
9069c0f0dcc3SMatthew G. Knepley 
9070c0f0dcc3SMatthew G. Knepley   Input Parameters:
9071bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
9072c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
9073c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
9074c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
907549abdd8aSBarry Smith . monitor      - the monitor function, this must use a `PetscViewerFormat` as its context
9076bb7acecfSBarry 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
9077c0f0dcc3SMatthew G. Knepley 
9078c0f0dcc3SMatthew G. Knepley   Output Parameter:
9079c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9080c0f0dcc3SMatthew G. Knepley 
9081c0f0dcc3SMatthew G. Knepley   Level: developer
9082c0f0dcc3SMatthew G. Knepley 
9083648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
9084db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
908560225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
9086db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
9087c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
9088db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
9089bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
9090c0f0dcc3SMatthew G. Knepley @*/
DMMonitorSetFromOptions(DM dm,const char name[],const char help[],const char manual[],PetscErrorCode (* monitor)(DM,void *),PetscErrorCode (* monitorsetup)(DM,PetscViewerAndFormat *),PetscBool * flg)9091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
9092d71ae5a4SJacob Faibussowitsch {
9093c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9094c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9095c0f0dcc3SMatthew G. Knepley 
9096c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9097c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9098648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
9099c0f0dcc3SMatthew G. Knepley   if (*flg) {
9100c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9101c0f0dcc3SMatthew G. Knepley 
91029566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
9103648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
91049566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
9105835f2295SStefano Zampini     PetscCall(DMMonitorSet(dm, monitor, vf, (PetscCtxDestroyFn *)PetscViewerAndFormatDestroy));
9106c0f0dcc3SMatthew G. Knepley   }
91073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9108c0f0dcc3SMatthew G. Knepley }
9109c0f0dcc3SMatthew G. Knepley 
9110c0f0dcc3SMatthew G. Knepley /*@
9111c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
9112c0f0dcc3SMatthew G. Knepley 
911320f4b53cSBarry Smith   Collective
9114c0f0dcc3SMatthew G. Knepley 
91152fe279fdSBarry Smith   Input Parameter:
9116bb7acecfSBarry Smith . dm - The `DM`
9117c0f0dcc3SMatthew G. Knepley 
9118c0f0dcc3SMatthew G. Knepley   Level: developer
9119c0f0dcc3SMatthew G. Knepley 
912073ff1848SBarry Smith   Developer Note:
9121a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
9122a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
9123a4e35b19SJacob Faibussowitsch   concept of discretization.
9124bb7acecfSBarry Smith 
91251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
9126c0f0dcc3SMatthew G. Knepley @*/
DMMonitor(DM dm)9127d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
9128d71ae5a4SJacob Faibussowitsch {
9129c0f0dcc3SMatthew G. Knepley   PetscInt m;
9130c0f0dcc3SMatthew G. Knepley 
9131c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
91323ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
9133c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
913448a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
91353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9136c0f0dcc3SMatthew G. Knepley }
91372e4af2aeSMatthew G. Knepley 
91382e4af2aeSMatthew G. Knepley /*@
9139bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
91402e4af2aeSMatthew G. Knepley 
914120f4b53cSBarry Smith   Collective
91422e4af2aeSMatthew G. Knepley 
91432e4af2aeSMatthew G. Knepley   Input Parameters:
9144bb7acecfSBarry Smith + dm  - The `DM`
91456b867d5aSJose E. Roman - sol - The solution vector
91462e4af2aeSMatthew G. Knepley 
91476b867d5aSJose E. Roman   Input/Output Parameter:
914820f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
91496b867d5aSJose E. Roman            contains the error in each field
91506b867d5aSJose E. Roman 
91516b867d5aSJose E. Roman   Output Parameter:
915220f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
915320f4b53cSBarry Smith 
915420f4b53cSBarry Smith   Level: developer
91552e4af2aeSMatthew G. Knepley 
9156bb7acecfSBarry Smith   Note:
9157bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
91582e4af2aeSMatthew G. Knepley 
91591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
91602e4af2aeSMatthew G. Knepley @*/
DMComputeError(DM dm,Vec sol,PetscReal errors[],Vec * errorVec)9161d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9162d71ae5a4SJacob Faibussowitsch {
91632e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
91642e4af2aeSMatthew G. Knepley   void    **ctxs;
91652e4af2aeSMatthew G. Knepley   PetscReal time;
91662e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
91672e4af2aeSMatthew G. Knepley 
91682e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
91699566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
91709566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
91719566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
91722e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
91732e4af2aeSMatthew G. Knepley     PetscDS         ds;
91742e4af2aeSMatthew G. Knepley     DMLabel         label;
91752e4af2aeSMatthew G. Knepley     IS              fieldIS;
91762e4af2aeSMatthew G. Knepley     const PetscInt *fields;
91772e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
91782e4af2aeSMatthew G. Knepley 
917907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
91809566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
91819566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
91822e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
91832e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
91849566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
91852e4af2aeSMatthew G. Knepley     }
91869566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
91872e4af2aeSMatthew G. Knepley   }
9188ad540459SPierre 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);
91899566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
91909566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
91912e4af2aeSMatthew G. Knepley   if (errorVec) {
91922e4af2aeSMatthew G. Knepley     DM             edm;
91932e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
91942e4af2aeSMatthew G. Knepley     PetscBool      simplex;
91952e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
91962e4af2aeSMatthew G. Knepley 
91979566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
91989566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
91999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
92009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
92012e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
92029566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
92032e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
92042e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
92052e4af2aeSMatthew G. Knepley       PetscQuadrature q;
92062e4af2aeSMatthew G. Knepley       const char     *name;
92072e4af2aeSMatthew G. Knepley 
92089566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
92099566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
92109566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
92119566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
92129566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
92139566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
92149566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
92159566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
92162e4af2aeSMatthew G. Knepley     }
92179566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
92182e4af2aeSMatthew G. Knepley 
92199566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
92209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
92219566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
92229566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
92232e4af2aeSMatthew G. Knepley   }
92249566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
92253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92262e4af2aeSMatthew G. Knepley }
92279a2a23afSMatthew G. Knepley 
92289a2a23afSMatthew G. Knepley /*@
9229bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
92309a2a23afSMatthew G. Knepley 
923120f4b53cSBarry Smith   Not Collective
92329a2a23afSMatthew G. Knepley 
92339a2a23afSMatthew G. Knepley   Input Parameter:
9234bb7acecfSBarry Smith . dm - The `DM`
92359a2a23afSMatthew G. Knepley 
92369a2a23afSMatthew G. Knepley   Output Parameter:
9237a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
92389a2a23afSMatthew G. Knepley 
92399a2a23afSMatthew G. Knepley   Level: advanced
92409a2a23afSMatthew G. Knepley 
9241e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
92429a2a23afSMatthew G. Knepley @*/
DMGetNumAuxiliaryVec(DM dm,PetscInt * numAux)9243d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9244d71ae5a4SJacob Faibussowitsch {
92459a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92469a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92479566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
92483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92499a2a23afSMatthew G. Knepley }
92509a2a23afSMatthew G. Knepley 
92519a2a23afSMatthew G. Knepley /*@
9252ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
92539a2a23afSMatthew G. Knepley 
925420f4b53cSBarry Smith   Not Collective
92559a2a23afSMatthew G. Knepley 
92569a2a23afSMatthew G. Knepley   Input Parameters:
9257bb7acecfSBarry Smith + dm    - The `DM`
9258bb7acecfSBarry Smith . label - The `DMLabel`
9259ac17215fSMatthew G. Knepley . value - The label value indicating the region
9260ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
92619a2a23afSMatthew G. Knepley 
92629a2a23afSMatthew G. Knepley   Output Parameter:
9263bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
92649a2a23afSMatthew G. Knepley 
92659bdbcad8SBarry Smith   Level: advanced
92669bdbcad8SBarry Smith 
9267bb7acecfSBarry Smith   Note:
92685d550117SStefano Zampini   If no auxiliary vector is found for this (label, value), (`NULL`, 0, 0) is checked as well.
926904c51a94SMatthew G. Knepley 
9270e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
92719a2a23afSMatthew G. Knepley @*/
DMGetAuxiliaryVec(DM dm,DMLabel label,PetscInt value,PetscInt part,Vec * aux)9272d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9273d71ae5a4SJacob Faibussowitsch {
9274ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
927504c51a94SMatthew G. Knepley   PetscBool       has;
92769a2a23afSMatthew G. Knepley 
92779a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92789a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92799a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
92809a2a23afSMatthew G. Knepley   key.label = label;
92819a2a23afSMatthew G. Knepley   key.value = value;
9282ac17215fSMatthew G. Knepley   key.part  = part;
92839566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
92849566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
92859566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
92863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92879a2a23afSMatthew G. Knepley }
92889a2a23afSMatthew G. Knepley 
92899a2a23afSMatthew G. Knepley /*@
9290bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
92919a2a23afSMatthew G. Knepley 
929220f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
92939a2a23afSMatthew G. Knepley 
92949a2a23afSMatthew G. Knepley   Input Parameters:
9295bb7acecfSBarry Smith + dm    - The `DM`
9296bb7acecfSBarry Smith . label - The `DMLabel`
92979a2a23afSMatthew G. Knepley . value - The label value indicating the region
9298ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9299bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
93009a2a23afSMatthew G. Knepley 
93019a2a23afSMatthew G. Knepley   Level: advanced
93029a2a23afSMatthew G. Knepley 
9303e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
93049a2a23afSMatthew G. Knepley @*/
DMSetAuxiliaryVec(DM dm,DMLabel label,PetscInt value,PetscInt part,Vec aux)9305d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9306d71ae5a4SJacob Faibussowitsch {
93079a2a23afSMatthew G. Knepley   Vec             old;
93089a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
93099a2a23afSMatthew G. Knepley 
93109a2a23afSMatthew G. Knepley   PetscFunctionBegin;
93119a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93129a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
93139a2a23afSMatthew G. Knepley   key.label = label;
93149a2a23afSMatthew G. Knepley   key.value = value;
9315ac17215fSMatthew G. Knepley   key.part  = part;
93169566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
93179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
93189566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
93199566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9320d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
93213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93229a2a23afSMatthew G. Knepley }
93239a2a23afSMatthew G. Knepley 
9324cc4c1da9SBarry Smith /*@
9325bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
93269a2a23afSMatthew G. Knepley 
932720f4b53cSBarry Smith   Not Collective
93289a2a23afSMatthew G. Knepley 
93299a2a23afSMatthew G. Knepley   Input Parameter:
9330bb7acecfSBarry Smith . dm - The `DM`
93319a2a23afSMatthew G. Knepley 
93329a2a23afSMatthew G. Knepley   Output Parameters:
9333bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9334bb7acecfSBarry Smith . values - The label values for each `Vec`
9335bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
93369a2a23afSMatthew G. Knepley 
93379bdbcad8SBarry Smith   Level: advanced
93389bdbcad8SBarry Smith 
9339bb7acecfSBarry Smith   Note:
9340bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
93419a2a23afSMatthew G. Knepley 
9342e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
93439a2a23afSMatthew G. Knepley @*/
DMGetAuxiliaryLabels(DM dm,DMLabel labels[],PetscInt values[],PetscInt parts[])9344d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9345d71ae5a4SJacob Faibussowitsch {
93469a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
93479a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
93489a2a23afSMatthew G. Knepley 
93499a2a23afSMatthew G. Knepley   PetscFunctionBegin;
93509a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93514f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
93524f572ea9SToby Isaac   PetscAssertPointer(values, 3);
93534f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
93549566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
93559566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
93569566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
93579371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
93589371c9d4SSatish Balay     labels[i] = keys[i].label;
93599371c9d4SSatish Balay     values[i] = keys[i].value;
93609371c9d4SSatish Balay     parts[i]  = keys[i].part;
93619371c9d4SSatish Balay   }
93629566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
93633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93649a2a23afSMatthew G. Knepley }
93659a2a23afSMatthew G. Knepley 
93669a2a23afSMatthew G. Knepley /*@
9367bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
93689a2a23afSMatthew G. Knepley 
936920f4b53cSBarry Smith   Not Collective
93709a2a23afSMatthew G. Knepley 
93719a2a23afSMatthew G. Knepley   Input Parameter:
9372bb7acecfSBarry Smith . dm - The `DM`
93739a2a23afSMatthew G. Knepley 
93749a2a23afSMatthew G. Knepley   Output Parameter:
9375bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
93769a2a23afSMatthew G. Knepley 
93779a2a23afSMatthew G. Knepley   Level: advanced
93789a2a23afSMatthew G. Knepley 
9379bb7acecfSBarry Smith   Note:
9380bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9381bb7acecfSBarry Smith 
9382e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
93839a2a23afSMatthew G. Knepley @*/
DMCopyAuxiliaryVec(DM dm,DM dmNew)9384d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9385d71ae5a4SJacob Faibussowitsch {
93869a2a23afSMatthew G. Knepley   PetscFunctionBegin;
93879a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9388d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9389d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9390e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9391e4d5475eSStefano Zampini 
9392e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
93939566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9394d705d0eaSStefano Zampini   {
9395d705d0eaSStefano Zampini     Vec     *auxData;
9396d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9397d705d0eaSStefano Zampini 
9398d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9399d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9400d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9401d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9402d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9403e4d5475eSStefano Zampini   }
9404e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9405e4d5475eSStefano Zampini }
9406e4d5475eSStefano Zampini 
9407e4d5475eSStefano Zampini /*@
9408e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9409e4d5475eSStefano Zampini 
9410e4d5475eSStefano Zampini   Not Collective
9411e4d5475eSStefano Zampini 
9412e4d5475eSStefano Zampini   Input Parameter:
9413e4d5475eSStefano Zampini . dm - The `DM`
9414e4d5475eSStefano Zampini 
9415e4d5475eSStefano Zampini   Level: advanced
9416e4d5475eSStefano Zampini 
9417e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9418e4d5475eSStefano Zampini @*/
DMClearAuxiliaryVec(DM dm)9419e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9420e4d5475eSStefano Zampini {
9421e4d5475eSStefano Zampini   Vec     *auxData;
9422e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9423e4d5475eSStefano Zampini 
9424e4d5475eSStefano Zampini   PetscFunctionBegin;
9425e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9426d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9427e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9428d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9429d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9430e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9431e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
94323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
94339a2a23afSMatthew G. Knepley }
9434b5a892a1SMatthew G. Knepley 
9435cc4c1da9SBarry Smith /*@
9436bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9437b5a892a1SMatthew G. Knepley 
943820f4b53cSBarry Smith   Not Collective
9439b5a892a1SMatthew G. Knepley 
9440b5a892a1SMatthew G. Knepley   Input Parameters:
9441bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9442b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9443b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9444b5a892a1SMatthew G. Knepley 
9445b5a892a1SMatthew G. Knepley   Output Parameters:
9446bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9447b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9448b5a892a1SMatthew G. Knepley 
9449b5a892a1SMatthew G. Knepley   Level: advanced
9450b5a892a1SMatthew G. Knepley 
9451bb7acecfSBarry Smith   Note:
9452bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9453bb7acecfSBarry Smith 
945485036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9455bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9456bb7acecfSBarry Smith 
9457bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9458bb7acecfSBarry Smith 
94591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9460b5a892a1SMatthew G. Knepley @*/
DMPolytopeMatchOrientation(DMPolytopeType ct,const PetscInt sourceCone[],const PetscInt targetCone[],PetscInt * ornt,PetscBool * found)9461d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9462d71ae5a4SJacob Faibussowitsch {
9463b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
946485036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9465b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9466b5a892a1SMatthew G. Knepley 
9467b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94689371c9d4SSatish Balay   if (!nO) {
94699371c9d4SSatish Balay     *ornt  = 0;
94709371c9d4SSatish Balay     *found = PETSC_TRUE;
94713ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94729371c9d4SSatish Balay   }
9473b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
947485036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9475b5a892a1SMatthew G. Knepley 
94769371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94779371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
94789371c9d4SSatish Balay     if (c == cS) {
94799371c9d4SSatish Balay       *ornt = o;
94809371c9d4SSatish Balay       break;
94819371c9d4SSatish Balay     }
9482b5a892a1SMatthew G. Knepley   }
9483b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9485b5a892a1SMatthew G. Knepley }
9486b5a892a1SMatthew G. Knepley 
9487cc4c1da9SBarry Smith /*@
9488bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9489b5a892a1SMatthew G. Knepley 
949020f4b53cSBarry Smith   Not Collective
9491b5a892a1SMatthew G. Knepley 
9492b5a892a1SMatthew G. Knepley   Input Parameters:
9493bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9494b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9495b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9496b5a892a1SMatthew G. Knepley 
94972fe279fdSBarry Smith   Output Parameter:
9498bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9499b5a892a1SMatthew G. Knepley 
9500b5a892a1SMatthew G. Knepley   Level: advanced
9501b5a892a1SMatthew G. Knepley 
9502bb7acecfSBarry Smith   Note:
9503bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9504bb7acecfSBarry Smith 
950573ff1848SBarry Smith   Developer Note:
9506bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9507bb7acecfSBarry Smith 
95081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9509b5a892a1SMatthew G. Knepley @*/
DMPolytopeGetOrientation(DMPolytopeType ct,const PetscInt sourceCone[],const PetscInt targetCone[],PetscInt * ornt)9510d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9511d71ae5a4SJacob Faibussowitsch {
9512b5a892a1SMatthew G. Knepley   PetscBool found;
9513b5a892a1SMatthew G. Knepley 
9514b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
95159566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
95167a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
95173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9518b5a892a1SMatthew G. Knepley }
9519b5a892a1SMatthew G. Knepley 
9520cc4c1da9SBarry Smith /*@
9521bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9522b5a892a1SMatthew G. Knepley 
952320f4b53cSBarry Smith   Not Collective
9524b5a892a1SMatthew G. Knepley 
9525b5a892a1SMatthew G. Knepley   Input Parameters:
9526bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9527b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9528b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9529b5a892a1SMatthew G. Knepley 
9530b5a892a1SMatthew G. Knepley   Output Parameters:
9531bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9532b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9533b5a892a1SMatthew G. Knepley 
9534b5a892a1SMatthew G. Knepley   Level: advanced
9535b5a892a1SMatthew G. Knepley 
953673ff1848SBarry Smith   Notes:
9537bb7acecfSBarry Smith   An arrangement is a vertex order
9538bb7acecfSBarry Smith 
953985036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9540bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9541bb7acecfSBarry Smith 
9542bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9543bb7acecfSBarry Smith 
954485036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9545b5a892a1SMatthew G. Knepley @*/
DMPolytopeMatchVertexOrientation(DMPolytopeType ct,const PetscInt sourceVert[],const PetscInt targetVert[],PetscInt * ornt,PetscBool * found)9546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9547d71ae5a4SJacob Faibussowitsch {
9548b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
954985036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9550b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9551b5a892a1SMatthew G. Knepley 
9552b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
95539371c9d4SSatish Balay   if (!nO) {
95549371c9d4SSatish Balay     *ornt  = 0;
95559371c9d4SSatish Balay     *found = PETSC_TRUE;
95563ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
95579371c9d4SSatish Balay   }
9558b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
955985036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9560b5a892a1SMatthew G. Knepley 
95619371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
95629371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
95639371c9d4SSatish Balay     if (c == cS) {
95649371c9d4SSatish Balay       *ornt = o;
95659371c9d4SSatish Balay       break;
95669371c9d4SSatish Balay     }
9567b5a892a1SMatthew G. Knepley   }
9568b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
95693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9570b5a892a1SMatthew G. Knepley }
9571b5a892a1SMatthew G. Knepley 
9572cc4c1da9SBarry Smith /*@
9573bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9574b5a892a1SMatthew G. Knepley 
957520f4b53cSBarry Smith   Not Collective
9576b5a892a1SMatthew G. Knepley 
9577b5a892a1SMatthew G. Knepley   Input Parameters:
9578bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9579b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9580b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9581b5a892a1SMatthew G. Knepley 
95822fe279fdSBarry Smith   Output Parameter:
9583bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9584b5a892a1SMatthew G. Knepley 
9585b5a892a1SMatthew G. Knepley   Level: advanced
9586b5a892a1SMatthew G. Knepley 
9587bb7acecfSBarry Smith   Note:
9588bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9589bb7acecfSBarry Smith 
959073ff1848SBarry Smith   Developer Note:
9591bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9592bb7acecfSBarry Smith 
95931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9594b5a892a1SMatthew G. Knepley @*/
DMPolytopeGetVertexOrientation(DMPolytopeType ct,const PetscInt sourceCone[],const PetscInt targetCone[],PetscInt * ornt)9595d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9596d71ae5a4SJacob Faibussowitsch {
9597b5a892a1SMatthew G. Knepley   PetscBool found;
9598b5a892a1SMatthew G. Knepley 
9599b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
96009566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
96017a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
96023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9603b5a892a1SMatthew G. Knepley }
9604012bc364SMatthew G. Knepley 
9605cc4c1da9SBarry Smith /*@
9606012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9607012bc364SMatthew G. Knepley 
960820f4b53cSBarry Smith   Not Collective
9609012bc364SMatthew G. Knepley 
9610012bc364SMatthew G. Knepley   Input Parameters:
9611bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9612012bc364SMatthew G. Knepley - point - Coordinates of the point
9613012bc364SMatthew G. Knepley 
96142fe279fdSBarry Smith   Output Parameter:
9615012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9616012bc364SMatthew G. Knepley 
9617012bc364SMatthew G. Knepley   Level: advanced
9618012bc364SMatthew G. Knepley 
96191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9620012bc364SMatthew G. Knepley @*/
DMPolytopeInCellTest(DMPolytopeType ct,const PetscReal point[],PetscBool * inside)9621d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9622d71ae5a4SJacob Faibussowitsch {
9623012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9624012bc364SMatthew G. Knepley   PetscInt  d;
9625012bc364SMatthew G. Knepley 
9626012bc364SMatthew G. Knepley   PetscFunctionBegin;
9627012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9628012bc364SMatthew G. Knepley   switch (ct) {
9629012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9630012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9631012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
96329371c9d4SSatish Balay       if (point[d] < -1.0) {
96339371c9d4SSatish Balay         *inside = PETSC_FALSE;
96349371c9d4SSatish Balay         break;
96359371c9d4SSatish Balay       }
9636012bc364SMatthew G. Knepley       sum += point[d];
9637012bc364SMatthew G. Knepley     }
96389371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
96399371c9d4SSatish Balay       *inside = PETSC_FALSE;
96409371c9d4SSatish Balay       break;
96419371c9d4SSatish Balay     }
9642012bc364SMatthew G. Knepley     break;
9643012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9644012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9645012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
96469371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
96479371c9d4SSatish Balay         *inside = PETSC_FALSE;
9648012bc364SMatthew G. Knepley         break;
96499371c9d4SSatish Balay       }
96509371c9d4SSatish Balay     break;
9651d71ae5a4SJacob Faibussowitsch   default:
9652d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9653012bc364SMatthew G. Knepley   }
96543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9655012bc364SMatthew G. Knepley }
9656adc21957SMatthew G. Knepley 
9657adc21957SMatthew G. Knepley /*@
9658adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9659adc21957SMatthew G. Knepley 
9660adc21957SMatthew G. Knepley   Logically collective
9661adc21957SMatthew G. Knepley 
9662adc21957SMatthew G. Knepley   Input Parameters:
9663adc21957SMatthew G. Knepley + dm      - The DM
9664adc21957SMatthew G. Knepley - reorder - Flag for reordering
9665adc21957SMatthew G. Knepley 
9666adc21957SMatthew G. Knepley   Level: intermediate
9667adc21957SMatthew G. Knepley 
9668adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9669adc21957SMatthew G. Knepley @*/
DMReorderSectionSetDefault(DM dm,DMReorderDefaultFlag reorder)9670adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9671adc21957SMatthew G. Knepley {
9672adc21957SMatthew G. Knepley   PetscFunctionBegin;
9673adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9674adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9675adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9676adc21957SMatthew G. Knepley }
9677adc21957SMatthew G. Knepley 
9678adc21957SMatthew G. Knepley /*@
9679adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9680adc21957SMatthew G. Knepley 
9681adc21957SMatthew G. Knepley   Not collective
9682adc21957SMatthew G. Knepley 
9683adc21957SMatthew G. Knepley   Input Parameter:
9684adc21957SMatthew G. Knepley . dm - The DM
9685adc21957SMatthew G. Knepley 
9686adc21957SMatthew G. Knepley   Output Parameter:
9687adc21957SMatthew G. Knepley . reorder - Flag for reordering
9688adc21957SMatthew G. Knepley 
9689adc21957SMatthew G. Knepley   Level: intermediate
9690adc21957SMatthew G. Knepley 
9691adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9692adc21957SMatthew G. Knepley @*/
DMReorderSectionGetDefault(DM dm,DMReorderDefaultFlag * reorder)9693adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9694adc21957SMatthew G. Knepley {
9695adc21957SMatthew G. Knepley   PetscFunctionBegin;
9696adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9697adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9698adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9699adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9700adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9701adc21957SMatthew G. Knepley }
9702adc21957SMatthew G. Knepley 
9703cc4c1da9SBarry Smith /*@
9704adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9705adc21957SMatthew G. Knepley 
9706adc21957SMatthew G. Knepley   Logically collective
9707adc21957SMatthew G. Knepley 
9708adc21957SMatthew G. Knepley   Input Parameters:
9709adc21957SMatthew G. Knepley + dm      - The DM
9710adc21957SMatthew G. Knepley - reorder - The reordering method
9711adc21957SMatthew G. Knepley 
9712adc21957SMatthew G. Knepley   Level: intermediate
9713adc21957SMatthew G. Knepley 
9714adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9715adc21957SMatthew G. Knepley @*/
DMReorderSectionSetType(DM dm,MatOrderingType reorder)9716adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9717adc21957SMatthew G. Knepley {
9718adc21957SMatthew G. Knepley   PetscFunctionBegin;
9719adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9720adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9721adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9722adc21957SMatthew G. Knepley }
9723adc21957SMatthew G. Knepley 
9724cc4c1da9SBarry Smith /*@
9725adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9726adc21957SMatthew G. Knepley 
9727adc21957SMatthew G. Knepley   Not collective
9728adc21957SMatthew G. Knepley 
9729adc21957SMatthew G. Knepley   Input Parameter:
9730adc21957SMatthew G. Knepley . dm - The DM
9731adc21957SMatthew G. Knepley 
9732adc21957SMatthew G. Knepley   Output Parameter:
9733adc21957SMatthew G. Knepley . reorder - The reordering method
9734adc21957SMatthew G. Knepley 
9735adc21957SMatthew G. Knepley   Level: intermediate
9736adc21957SMatthew G. Knepley 
9737adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9738adc21957SMatthew G. Knepley @*/
DMReorderSectionGetType(DM dm,MatOrderingType * reorder)9739adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9740adc21957SMatthew G. Knepley {
9741adc21957SMatthew G. Knepley   PetscFunctionBegin;
9742adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9743adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9744adc21957SMatthew G. Knepley   *reorder = NULL;
9745adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9746adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9747adc21957SMatthew G. Knepley }
9748