xref: /petsc/src/dm/interface/dm.c (revision fc214432e4e741012e6747c192ccab49f639cc20)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>      /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h> /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6f19dbd58SToby Isaac #include <petscdmfield.h>
70c312b8eSJed Brown #include <petscsf.h>
82764a2aaSMatthew G. Knepley #include <petscds.h>
947c6ae99SBarry Smith 
10f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
11f918ec44SMatthew G. Knepley   #include <petscfeceed.h>
12f918ec44SMatthew G. Knepley #endif
13f918ec44SMatthew G. Knepley 
14cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
15cea3dcb8SSatish Balay   #include <petsc/private/valgrind/memcheck.h>
1600d952a4SJed Brown #endif
1700d952a4SJed Brown 
18732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
19d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
20708be2fdSJed Brown PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_CreateMassMatrix, DM_Load, DM_AdaptInterpolator, DM_ProjectFunction;
2167a56275SMatthew G Knepley 
22ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
23d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "INVALID", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "INVALID", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType", "DM_BC_", NULL};
240e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
259371c9d4SSatish Balay const char *const DMPolytopeTypes[]   = {"vertex",  "segment",       "tensor_segment",      "triangle", "quadrilateral", "tensor_quad",    "tetrahedron",  "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism",
269371c9d4SSatish Balay                                          "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown",  "invalid",       "DMPolytopeType", "DM_POLYTOPE_", NULL};
272cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2860c22052SBarry Smith 
29a4121054SBarry Smith /*@
30bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
31bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
32a4121054SBarry Smith 
33d083f849SBarry Smith   Collective
34a4121054SBarry Smith 
35a4121054SBarry Smith   Input Parameter:
36bb7acecfSBarry Smith . comm - The communicator for the `DM` object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Output Parameter:
39bb7acecfSBarry Smith . dm - The `DM` object
40a4121054SBarry Smith 
41a4121054SBarry Smith   Level: beginner
42a4121054SBarry Smith 
43bb7acecfSBarry Smith   Notes:
44bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
45bb7acecfSBarry Smith 
46bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
47bb7acecfSBarry Smith   error when you try to use the dm.
48bb7acecfSBarry Smith 
491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
50a4121054SBarry Smith @*/
51d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
52d71ae5a4SJacob Faibussowitsch {
53a4121054SBarry Smith   DM      v;
54e5e52638SMatthew G. Knepley   PetscDS ds;
55a4121054SBarry Smith 
56a4121054SBarry Smith   PetscFunctionBegin;
571411c6eeSJed Brown   PetscValidPointer(dm, 2);
580298fd71SBarry Smith   *dm = NULL;
599566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
60a4121054SBarry Smith 
619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
62e7c4fc90SDmitry Karpeev 
6362e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6462e5d2d2SJDBetteridge 
6549be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6649be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
670298fd71SBarry Smith   v->ltogmap              = NULL;
68a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
691411c6eeSJed Brown   v->bs                   = 1;
70171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
719566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
729566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
73c58f1c22SToby Isaac   v->labels                    = NULL;
7434aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7534aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
76c58f1c22SToby Isaac   v->depthLabel                = NULL;
77ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
781bb6d2a8SBarry Smith   v->localSection              = NULL;
791bb6d2a8SBarry Smith   v->globalSection             = NULL;
803b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
813b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8279769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
836858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
846858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
856858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8696173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
87435a35e8SMatthew G Knepley   {
88435a35e8SMatthew G Knepley     PetscInt i;
89435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
900298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
91f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
92435a35e8SMatthew G Knepley     }
93435a35e8SMatthew G Knepley   }
949566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9507218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
969566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
979566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9814f150ffSMatthew G. Knepley   v->dmBC              = NULL;
99a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
100f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
101cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1029566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1039566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1044a7a4c06SLawrence Mitchell 
1051411c6eeSJed Brown   *dm = v;
1063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
107a4121054SBarry Smith }
108a4121054SBarry Smith 
10938221697SMatthew G. Knepley /*@
110bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11138221697SMatthew G. Knepley 
112d083f849SBarry Smith   Collective
11338221697SMatthew G. Knepley 
11438221697SMatthew G. Knepley   Input Parameter:
115bb7acecfSBarry Smith . dm - The original `DM` object
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   Output Parameter:
118bb7acecfSBarry Smith . newdm  - The new `DM` object
11938221697SMatthew G. Knepley 
12038221697SMatthew G. Knepley   Level: beginner
12138221697SMatthew G. Knepley 
1221cb8cacdSPatrick Sanan   Notes:
123bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
124bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
125bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1261bb6d2a8SBarry Smith 
127bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12889706ed2SPatrick Sanan 
129bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
130bb7acecfSBarry Smith 
1311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMSetType()`, `DMConvert()`
13238221697SMatthew G. Knepley @*/
133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
134d71ae5a4SJacob Faibussowitsch {
13538221697SMatthew G. Knepley   PetscSF  sf;
13638221697SMatthew G. Knepley   Vec      coords;
13738221697SMatthew G. Knepley   void    *ctx;
1386858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
13938221697SMatthew G. Knepley 
14038221697SMatthew G. Knepley   PetscFunctionBegin;
14138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14238221697SMatthew G. Knepley   PetscValidPointer(newdm, 2);
1439566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1449566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
145ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
146ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
147c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
148*fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1499566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1509566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1519566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1529566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1539566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1549566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
155dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1563f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1579566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1589566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1599566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1609566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1616858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1626858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
163be4c1c3eSMatthew G. Knepley       DM           ncdm;
164be4c1c3eSMatthew G. Knepley       PetscSection cs;
1655a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
166be4c1c3eSMatthew G. Knepley 
1676858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1689566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
169712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1705a0206caSToby Isaac       if (pEndMax >= 0) {
1716858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1726858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1739566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1746858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1756858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1769566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
177be4c1c3eSMatthew G. Knepley       }
178be4c1c3eSMatthew G. Knepley     }
1796858538eSMatthew G. Knepley   }
1809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1819566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1829566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18338221697SMatthew G. Knepley   if (coords) {
1849566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18538221697SMatthew G. Knepley   } else {
1869566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1879566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18838221697SMatthew G. Knepley   }
1896858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1906858538eSMatthew G. Knepley   if (coords) {
1916858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1926858538eSMatthew G. Knepley   } else {
1936858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1946858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1956858538eSMatthew G. Knepley   }
19690b157c4SStefano Zampini   {
1974fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1986858538eSMatthew G. Knepley 
1994fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2004fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
201c6b900c6SMatthew G. Knepley   }
20234aa8a36SMatthew G. Knepley   {
20334aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20434aa8a36SMatthew G. Knepley 
2059566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2069566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20734aa8a36SMatthew G. Knepley   }
2083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20938221697SMatthew G. Knepley }
21038221697SMatthew G. Knepley 
2119a42bb27SBarry Smith /*@C
212bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2139a42bb27SBarry Smith 
21420f4b53cSBarry Smith    Logically Collective
2159a42bb27SBarry Smith 
216147403d9SBarry Smith    Input Parameters:
2179a42bb27SBarry Smith +  da - initial distributed array
218bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2199a42bb27SBarry Smith 
22020f4b53cSBarry Smith    Options Database Key:
221147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2229a42bb27SBarry Smith 
2239a42bb27SBarry Smith    Level: intermediate
2249a42bb27SBarry Smith 
2251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
226bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2279a42bb27SBarry Smith @*/
228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype)
229d71ae5a4SJacob Faibussowitsch {
2309a42bb27SBarry Smith   PetscFunctionBegin;
2319a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2329566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2339566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
2343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2359a42bb27SBarry Smith }
2369a42bb27SBarry Smith 
237c0dedaeaSBarry Smith /*@C
238bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
239c0dedaeaSBarry Smith 
24020f4b53cSBarry Smith    Logically Collective
241c0dedaeaSBarry Smith 
242c0dedaeaSBarry Smith    Input Parameter:
243c0dedaeaSBarry Smith .  da - initial distributed array
244c0dedaeaSBarry Smith 
245c0dedaeaSBarry Smith    Output Parameter:
246c0dedaeaSBarry Smith .  ctype - the vector type
247c0dedaeaSBarry Smith 
248c0dedaeaSBarry Smith    Level: intermediate
249c0dedaeaSBarry Smith 
2501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
251c0dedaeaSBarry Smith @*/
252d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
253d71ae5a4SJacob Faibussowitsch {
254c0dedaeaSBarry Smith   PetscFunctionBegin;
255c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
256c0dedaeaSBarry Smith   *ctype = da->vectype;
2573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
258c0dedaeaSBarry Smith }
259c0dedaeaSBarry Smith 
2605f1ad066SMatthew G Knepley /*@
261bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2625f1ad066SMatthew G Knepley 
26320f4b53cSBarry Smith   Not Collective
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley   Input Parameter:
266bb7acecfSBarry Smith . v - The `Vec`
2675f1ad066SMatthew G Knepley 
2685f1ad066SMatthew G Knepley   Output Parameter:
269bb7acecfSBarry Smith . dm - The `DM`
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley   Level: intermediate
2725f1ad066SMatthew G Knepley 
273bb7acecfSBarry Smith   Note:
274bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
275bb7acecfSBarry Smith 
2761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2775f1ad066SMatthew G Knepley @*/
278d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
279d71ae5a4SJacob Faibussowitsch {
2805f1ad066SMatthew G Knepley   PetscFunctionBegin;
2815f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2825f1ad066SMatthew G Knepley   PetscValidPointer(dm, 2);
2839566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2855f1ad066SMatthew G Knepley }
2865f1ad066SMatthew G Knepley 
2875f1ad066SMatthew G Knepley /*@
288bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2895f1ad066SMatthew G Knepley 
29020f4b53cSBarry Smith   Not Collective
2915f1ad066SMatthew G Knepley 
2925f1ad066SMatthew G Knepley   Input Parameters:
293bb7acecfSBarry Smith + v - The `Vec`
294bb7acecfSBarry Smith - dm - The `DM`
2955f1ad066SMatthew G Knepley 
29620f4b53cSBarry Smith   Level: developer
29720f4b53cSBarry Smith 
298bb7acecfSBarry Smith   Note:
299bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
300d9805387SMatthew G. Knepley 
301bb7acecfSBarry 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.
302bb7acecfSBarry Smith 
3031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3045f1ad066SMatthew G Knepley @*/
305d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
306d71ae5a4SJacob Faibussowitsch {
3075f1ad066SMatthew G Knepley   PetscFunctionBegin;
3085f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
309d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3109566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3125f1ad066SMatthew G Knepley }
3135f1ad066SMatthew G Knepley 
314521d9a4cSLisandro Dalcin /*@C
315bb7acecfSBarry Smith        DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3168f1509bcSBarry Smith 
31720f4b53cSBarry Smith    Logically Collective
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith    Input Parameters:
320bb7acecfSBarry Smith +  dm - the `DM` context
3218f1509bcSBarry Smith -  ctype - the matrix type
3228f1509bcSBarry Smith 
32320f4b53cSBarry Smith    Options Database Key:
3248f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3258f1509bcSBarry Smith 
3268f1509bcSBarry Smith    Level: intermediate
3278f1509bcSBarry Smith 
3281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
329bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3308f1509bcSBarry Smith @*/
331d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
332d71ae5a4SJacob Faibussowitsch {
3338f1509bcSBarry Smith   PetscFunctionBegin;
3348f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3358f1509bcSBarry Smith   dm->coloringtype = ctype;
3363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3378f1509bcSBarry Smith }
3388f1509bcSBarry Smith 
3398f1509bcSBarry Smith /*@C
340bb7acecfSBarry Smith        DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
341521d9a4cSLisandro Dalcin 
34220f4b53cSBarry Smith    Logically Collective
343521d9a4cSLisandro Dalcin 
344521d9a4cSLisandro Dalcin    Input Parameter:
345bb7acecfSBarry Smith .  dm - the `DM` context
3468f1509bcSBarry Smith 
3478f1509bcSBarry Smith    Output Parameter:
3488f1509bcSBarry Smith .  ctype - the matrix type
3498f1509bcSBarry Smith 
35020f4b53cSBarry Smith    Options Database Key:
3518f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3528f1509bcSBarry Smith 
3538f1509bcSBarry Smith    Level: intermediate
3548f1509bcSBarry Smith 
3551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
356bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3578f1509bcSBarry Smith @*/
358d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
359d71ae5a4SJacob Faibussowitsch {
3608f1509bcSBarry Smith   PetscFunctionBegin;
3618f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3628f1509bcSBarry Smith   *ctype = dm->coloringtype;
3633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3648f1509bcSBarry Smith }
3658f1509bcSBarry Smith 
3668f1509bcSBarry Smith /*@C
367bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3688f1509bcSBarry Smith 
36920f4b53cSBarry Smith    Logically Collective
3708f1509bcSBarry Smith 
3718f1509bcSBarry Smith    Input Parameters:
372bb7acecfSBarry Smith +  dm - the `DM` context
373bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
374521d9a4cSLisandro Dalcin 
37520f4b53cSBarry Smith    Options Database Key:
376bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
377521d9a4cSLisandro Dalcin 
378521d9a4cSLisandro Dalcin    Level: intermediate
379521d9a4cSLisandro Dalcin 
3801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
381521d9a4cSLisandro Dalcin @*/
382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
383d71ae5a4SJacob Faibussowitsch {
384521d9a4cSLisandro Dalcin   PetscFunctionBegin;
385521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3869566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3879566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
3883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
389521d9a4cSLisandro Dalcin }
390521d9a4cSLisandro Dalcin 
391c0dedaeaSBarry Smith /*@C
39220f4b53cSBarry Smith        DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
393c0dedaeaSBarry Smith 
39420f4b53cSBarry Smith    Logically Collective
395c0dedaeaSBarry Smith 
396c0dedaeaSBarry Smith    Input Parameter:
397bb7acecfSBarry Smith .  dm - the `DM` context
398c0dedaeaSBarry Smith 
399c0dedaeaSBarry Smith    Output Parameter:
400c0dedaeaSBarry Smith .  ctype - the matrix type
401c0dedaeaSBarry Smith 
402c0dedaeaSBarry Smith    Level: intermediate
403c0dedaeaSBarry Smith 
4041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
405c0dedaeaSBarry Smith @*/
406d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
407d71ae5a4SJacob Faibussowitsch {
408c0dedaeaSBarry Smith   PetscFunctionBegin;
409c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
410c0dedaeaSBarry Smith   *ctype = dm->mattype;
4113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
412c0dedaeaSBarry Smith }
413c0dedaeaSBarry Smith 
414c688c046SMatthew G Knepley /*@
415bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
416c688c046SMatthew G Knepley 
41720f4b53cSBarry Smith   Not Collective
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   Input Parameter:
420bb7acecfSBarry Smith . A - The `Mat`
421c688c046SMatthew G Knepley 
422c688c046SMatthew G Knepley   Output Parameter:
423bb7acecfSBarry Smith . dm - The `DM`
424c688c046SMatthew G Knepley 
425c688c046SMatthew G Knepley   Level: intermediate
426c688c046SMatthew G Knepley 
427bb7acecfSBarry Smith   Note:
428bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
429bb7acecfSBarry Smith 
430bb7acecfSBarry Smith   Developer Note:
431bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4328f1509bcSBarry Smith 
4331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
434c688c046SMatthew G Knepley @*/
435d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
436d71ae5a4SJacob Faibussowitsch {
437c688c046SMatthew G Knepley   PetscFunctionBegin;
438c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
439c688c046SMatthew G Knepley   PetscValidPointer(dm, 2);
4409566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
442c688c046SMatthew G Knepley }
443c688c046SMatthew G Knepley 
444c688c046SMatthew G Knepley /*@
445bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
446c688c046SMatthew G Knepley 
44720f4b53cSBarry Smith   Not Collective
448c688c046SMatthew G Knepley 
449c688c046SMatthew G Knepley   Input Parameters:
45020f4b53cSBarry Smith + A - The `Mat`
45120f4b53cSBarry Smith - dm - The `DM`
452c688c046SMatthew G Knepley 
453bb7acecfSBarry Smith   Level: developer
454c688c046SMatthew G Knepley 
455bb7acecfSBarry Smith   Note:
456bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
457bb7acecfSBarry Smith 
458bb7acecfSBarry Smith   Developer Note:
459bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
460bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4618f1509bcSBarry Smith 
4621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
463c688c046SMatthew G Knepley @*/
464d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
465d71ae5a4SJacob Faibussowitsch {
466c688c046SMatthew G Knepley   PetscFunctionBegin;
467c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4688865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4699566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471c688c046SMatthew G Knepley }
472c688c046SMatthew G Knepley 
4739a42bb27SBarry Smith /*@C
474bb7acecfSBarry Smith    DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4759a42bb27SBarry Smith 
47620f4b53cSBarry Smith    Logically Collective
4779a42bb27SBarry Smith 
478d8d19677SJose E. Roman    Input Parameters:
479bb7acecfSBarry Smith +  da - the `DM` context
480bb7acecfSBarry Smith -  prefix - the prefix to prepend
4819a42bb27SBarry Smith 
48220f4b53cSBarry Smith    Level: advanced
48320f4b53cSBarry Smith 
48420f4b53cSBarry Smith    Note:
4859a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4869a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4879a42bb27SBarry Smith 
4881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4899a42bb27SBarry Smith @*/
490d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
491d71ae5a4SJacob Faibussowitsch {
4929a42bb27SBarry Smith   PetscFunctionBegin;
4939a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4949566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4951baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4961baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
4973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4989a42bb27SBarry Smith }
4999a42bb27SBarry Smith 
50031697293SDave May /*@C
501da81f932SPierre Jolivet    DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
502bb7acecfSBarry Smith    `DM` options in the options database.
50331697293SDave May 
50420f4b53cSBarry Smith    Logically Collective
50531697293SDave May 
50631697293SDave May    Input Parameters:
507bb7acecfSBarry Smith +  dm - the `DM` context
508bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
50931697293SDave May 
51020f4b53cSBarry Smith    Level: advanced
51120f4b53cSBarry Smith 
51220f4b53cSBarry Smith    Note:
513bb7acecfSBarry 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.
51431697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
51531697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51631697293SDave May 
5171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51831697293SDave May @*/
519d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
520d71ae5a4SJacob Faibussowitsch {
52131697293SDave May   PetscFunctionBegin;
52231697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5239566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
52531697293SDave May }
52631697293SDave May 
52731697293SDave May /*@C
52831697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
529bb7acecfSBarry Smith    DM options in the options database.
53031697293SDave May 
53131697293SDave May    Not Collective
53231697293SDave May 
5332fe279fdSBarry Smith    Input Parameter:
534bb7acecfSBarry Smith .  dm - the `DM` context
53531697293SDave May 
5362fe279fdSBarry Smith    Output Parameter:
53731697293SDave May .  prefix - pointer to the prefix string used is returned
53831697293SDave May 
53931697293SDave May    Level: advanced
54031697293SDave May 
54120f4b53cSBarry Smith    Fortran Note:
54220f4b53cSBarry Smith    Pass in a string 'prefix' of
54320f4b53cSBarry Smith    sufficient length to hold the prefix.
54420f4b53cSBarry Smith 
5451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54631697293SDave May @*/
547d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
548d71ae5a4SJacob Faibussowitsch {
54931697293SDave May   PetscFunctionBegin;
55031697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55331697293SDave May }
55431697293SDave May 
55562e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
556d71ae5a4SJacob Faibussowitsch {
5576eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
55888bdff64SToby Isaac 
55988bdff64SToby Isaac   PetscFunctionBegin;
560aab5bcd8SJed Brown   *ncrefct = 0;
56188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56288bdff64SToby Isaac     refct--;
56388bdff64SToby Isaac     if (recurseCoarse) {
56488bdff64SToby Isaac       PetscInt coarseCount;
56588bdff64SToby Isaac 
56662e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
56788bdff64SToby Isaac       refct += coarseCount;
56888bdff64SToby Isaac     }
56988bdff64SToby Isaac   }
57088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
57188bdff64SToby Isaac     refct--;
57288bdff64SToby Isaac     if (recurseFine) {
57388bdff64SToby Isaac       PetscInt fineCount;
57488bdff64SToby Isaac 
57562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
57688bdff64SToby Isaac       refct += fineCount;
57788bdff64SToby Isaac     }
57888bdff64SToby Isaac   }
57988bdff64SToby Isaac   *ncrefct = refct;
5803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58188bdff64SToby Isaac }
58288bdff64SToby Isaac 
58362e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
58462e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
58562e5d2d2SJDBetteridge {
58662e5d2d2SJDBetteridge   PetscFunctionBegin;
58762e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
5883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58962e5d2d2SJDBetteridge }
59062e5d2d2SJDBetteridge 
591d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
592d71ae5a4SJacob Faibussowitsch {
5935d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
594354557abSToby Isaac 
595354557abSToby Isaac   PetscFunctionBegin;
596354557abSToby Isaac   /* destroy the labels */
597354557abSToby Isaac   while (next) {
598354557abSToby Isaac     DMLabelLink tmp = next->next;
599354557abSToby Isaac 
6005d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
601ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6029566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6039566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
604354557abSToby Isaac     next = tmp;
605354557abSToby Isaac   }
6065d80c0bfSVaclav Hapla   dm->labels = NULL;
6073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
608354557abSToby Isaac }
609354557abSToby Isaac 
610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
611d71ae5a4SJacob Faibussowitsch {
6126858538eSMatthew G. Knepley   PetscFunctionBegin;
6136858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6146858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6156858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6166858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6176858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6196858538eSMatthew G. Knepley }
6206858538eSMatthew G. Knepley 
6211fb7b255SJunchao Zhang /*@C
622bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
62347c6ae99SBarry Smith 
62420f4b53cSBarry Smith     Collective
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith     Input Parameter:
627bb7acecfSBarry Smith .   dm - the `DM` object to destroy
62847c6ae99SBarry Smith 
62947c6ae99SBarry Smith     Level: developer
63047c6ae99SBarry Smith 
6311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
63247c6ae99SBarry Smith @*/
633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
634d71ae5a4SJacob Faibussowitsch {
6356eb26441SStefano Zampini   PetscInt       cnt;
636dfe15315SJed Brown   DMNamedVecLink nlink, nnext;
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith   PetscFunctionBegin;
6393ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
6406bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
64187e657c6SBarry Smith 
64288bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
64362e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
64488bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6459371c9d4SSatish Balay   if (--cnt > 0) {
6469371c9d4SSatish Balay     *dm = NULL;
6473ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6489371c9d4SSatish Balay   }
6493ba16761SJacob Faibussowitsch   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
6506bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6516eb26441SStefano Zampini 
6529566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6539566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6546eb26441SStefano Zampini 
655f490541aSPeter Brune   nnext              = (*dm)->namedglobal;
6560298fd71SBarry Smith   (*dm)->namedglobal = NULL;
657f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named vectors */
6582348bcf4SPeter Brune     nnext = nlink->next;
6597a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6609566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6619566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6629566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6632348bcf4SPeter Brune   }
664f490541aSPeter Brune   nnext             = (*dm)->namedlocal;
6650298fd71SBarry Smith   (*dm)->namedlocal = NULL;
666f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named local vectors */
667f490541aSPeter Brune     nnext = nlink->next;
6687a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6699566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6709566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6719566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
672f490541aSPeter Brune   }
6732348bcf4SPeter Brune 
674b17ce1afSJed Brown   /* Destroy the list of hooks */
675c833c3b5SJed Brown   {
676c833c3b5SJed Brown     DMCoarsenHookLink link, next;
677b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
678b17ce1afSJed Brown       next = link->next;
6799566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
680b17ce1afSJed Brown     }
6810298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
682c833c3b5SJed Brown   }
683c833c3b5SJed Brown   {
684c833c3b5SJed Brown     DMRefineHookLink link, next;
685c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
686c833c3b5SJed Brown       next = link->next;
6879566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
688c833c3b5SJed Brown     }
6890298fd71SBarry Smith     (*dm)->refinehook = NULL;
690c833c3b5SJed Brown   }
691be081cd6SPeter Brune   {
692be081cd6SPeter Brune     DMSubDomainHookLink link, next;
693be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
694be081cd6SPeter Brune       next = link->next;
6959566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
696be081cd6SPeter Brune     }
6970298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
698be081cd6SPeter Brune   }
699baf369e7SPeter Brune   {
700baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
701baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
702baf369e7SPeter Brune       next = link->next;
7039566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
704baf369e7SPeter Brune     }
7050298fd71SBarry Smith     (*dm)->gtolhook = NULL;
706baf369e7SPeter Brune   }
707d4d07f1eSToby Isaac   {
708d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
709d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
710d4d07f1eSToby Isaac       next = link->next;
7119566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
712d4d07f1eSToby Isaac     }
713d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
714d4d07f1eSToby Isaac   }
715aa1993deSMatthew G Knepley   /* Destroy the work arrays */
716aa1993deSMatthew G Knepley   {
717aa1993deSMatthew G Knepley     DMWorkLink link, next;
718442f3b32SStefano Zampini     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (*dm)->workout, (*dm)->workout->mem);
719aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
720aa1993deSMatthew G Knepley       next = link->next;
7219566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7229566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
723aa1993deSMatthew G Knepley     }
7240298fd71SBarry Smith     (*dm)->workin = NULL;
725aa1993deSMatthew G Knepley   }
726c58f1c22SToby Isaac   /* destroy the labels */
7279566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
728f4cdcedcSVaclav Hapla   /* destroy the fields */
7299566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
730f4cdcedcSVaclav Hapla   /* destroy the boundaries */
731e6f8dbb6SToby Isaac   {
732e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
733e6f8dbb6SToby Isaac     while (next) {
734e6f8dbb6SToby Isaac       DMBoundary b = next;
735e6f8dbb6SToby Isaac 
736e6f8dbb6SToby Isaac       next = b->next;
7379566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
738e6f8dbb6SToby Isaac     }
739e6f8dbb6SToby Isaac   }
740b17ce1afSJed Brown 
7419566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7429566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7439566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
74452536dc3SBarry Smith 
74548a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7469566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7479566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7499566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
75088ed4aceSMatthew G Knepley 
7519566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7529566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7549566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7559566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7569566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7579566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
758736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
75948a46eb9SPierre Jolivet     if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7609566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
761736995cdSBlaise Bourdin   }
7629a2a23afSMatthew G. Knepley   {
7639a2a23afSMatthew G. Knepley     Vec     *auxData;
7649a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7659a2a23afSMatthew G. Knepley 
7669566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7679566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7689566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7699566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7709566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7719566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7729a2a23afSMatthew G. Knepley   }
77348a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7746eb26441SStefano Zampini 
7759566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
77648a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7779566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7784fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7799566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7809566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7816858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7826858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7839566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7849566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7859566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7866725e60dSJed Brown   PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local));
7876725e60dSJed Brown   PetscCall(VecDestroy(&(*dm)->periodic.affine));
7886636e97aSMatthew G Knepley 
7899566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7909566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
791e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7929566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
793732e2eb9SMatthew G Knepley 
79448a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7959566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
796f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7979566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7989566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
799f918ec44SMatthew G. Knepley #endif
800435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
8019566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
8023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80347c6ae99SBarry Smith }
80447c6ae99SBarry Smith 
805d7bf68aeSBarry Smith /*@
806bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
807d7bf68aeSBarry Smith 
80820f4b53cSBarry Smith     Collective
809d7bf68aeSBarry Smith 
810d7bf68aeSBarry Smith     Input Parameter:
811bb7acecfSBarry Smith .   dm - the `DM` object to setup
812d7bf68aeSBarry Smith 
813bb7acecfSBarry Smith     Level: intermediate
814d7bf68aeSBarry Smith 
815bb7acecfSBarry Smith     Note:
816bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
817bb7acecfSBarry Smith 
8181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
819d7bf68aeSBarry Smith @*/
820d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
821d71ae5a4SJacob Faibussowitsch {
822d7bf68aeSBarry Smith   PetscFunctionBegin;
823171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8243ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
825dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8268387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
828d7bf68aeSBarry Smith }
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith /*@
831bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
832d7bf68aeSBarry Smith 
83320f4b53cSBarry Smith     Collective
834d7bf68aeSBarry Smith 
835d7bf68aeSBarry Smith     Input Parameter:
836bb7acecfSBarry Smith .   dm - the `DM` object to set options for
837d7bf68aeSBarry Smith 
83820f4b53cSBarry Smith     Options Database Keys:
839bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
840bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
841bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
842a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
84320f4b53cSBarry 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`
84420f4b53cSBarry Smith . -dm_plex_filename <str>           - File containing a mesh
8459318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
846cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8475dca41c3SJed Brown . -dm_plex_shape <shape>            - The domain shape, such as `BOX`, `SPHERE`, etc.
8489318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8499318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8509318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
851bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
852bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8539318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8549318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8559318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8569318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
857bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType` for each direction
8589318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8599318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8609318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8619318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
862bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8639318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8649318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8659318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8669318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
867bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
871d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
872d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
873909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
874909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8759318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8769318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8779318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
87820f4b53cSBarry Smith .  -dm_plex_adj_closure <bool>       - Set adjacency size
87920f4b53cSBarry Smith .   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
880bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
881bb7acecfSBarry 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()`
882bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
883bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
884bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
885384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
886d7bf68aeSBarry Smith 
88795eb5ee5SVaclav Hapla     Level: intermediate
88895eb5ee5SVaclav Hapla 
8891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
890bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
891bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
892d7bf68aeSBarry Smith @*/
893d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
894d71ae5a4SJacob Faibussowitsch {
8957781c08eSBarry Smith   char      typeName[256];
896ca266f36SBarry Smith   PetscBool flg;
897d7bf68aeSBarry Smith 
898d7bf68aeSBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9019566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9029566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
903dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
904d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9071baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9091baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
910863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9129566063dSJacob 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));
913dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
914f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
915dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
916d0609cedSBarry Smith   PetscOptionsEnd();
9173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
918d7bf68aeSBarry Smith }
919d7bf68aeSBarry Smith 
920fc9bc008SSatish Balay /*@C
921bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
922fe2efc57SMark 
92320f4b53cSBarry Smith    Collective
924fe2efc57SMark 
925fe2efc57SMark    Input Parameters:
926bb7acecfSBarry Smith +  dm - the `DM` object
92720f4b53cSBarry Smith .  obj - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
928bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
929fe2efc57SMark 
930fe2efc57SMark    Level: intermediate
931bb7acecfSBarry Smith 
932bb7acecfSBarry Smith    Note:
933bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
934bb7acecfSBarry Smith 
9351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
936fe2efc57SMark @*/
937d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
938d71ae5a4SJacob Faibussowitsch {
939fe2efc57SMark   PetscFunctionBegin;
940fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9419566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
943fe2efc57SMark }
944fe2efc57SMark 
945fe2efc57SMark /*@C
946bb7acecfSBarry 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
947bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94847c6ae99SBarry Smith 
94920f4b53cSBarry Smith     Collective
95047c6ae99SBarry Smith 
951d8d19677SJose E. Roman     Input Parameters:
952bb7acecfSBarry Smith +   dm - the `DM` object to view
95347c6ae99SBarry Smith -   v - the viewer
95447c6ae99SBarry Smith 
95520f4b53cSBarry Smith     Level: beginner
95620f4b53cSBarry Smith 
957cd7e8a5eSksagiyam     Notes:
958bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
959bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
960bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
961cd7e8a5eSksagiyam 
9621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96347c6ae99SBarry Smith @*/
964d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
965d71ae5a4SJacob Faibussowitsch {
96632c0f0efSBarry Smith   PetscBool         isbinary;
96776a8abe0SBarry Smith   PetscMPIInt       size;
96876a8abe0SBarry Smith   PetscViewerFormat format;
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith   PetscFunctionBegin;
971171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97248a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
973b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98074903a4fSStefano Zampini      in an error here */
98174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9829566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
983b1b135c8SBarry Smith 
9849566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9859566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9863ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9879566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9889566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98932c0f0efSBarry Smith   if (isbinary) {
99055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99132c0f0efSBarry Smith     char     type[256];
99232c0f0efSBarry Smith 
9939566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
994c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9959566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99632c0f0efSBarry Smith   }
997dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
99947c6ae99SBarry Smith }
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith /*@
1002bb7acecfSBarry 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,
1003bb7acecfSBarry Smith     that is it has no ghost locations.
100447c6ae99SBarry Smith 
100520f4b53cSBarry Smith     Collective
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith     Input Parameter:
1008bb7acecfSBarry Smith .   dm - the `DM` object
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Output Parameter:
101147c6ae99SBarry Smith .   vec - the global vector
101247c6ae99SBarry Smith 
1013073dac72SJed Brown     Level: beginner
101447c6ae99SBarry Smith 
10151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1016bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101747c6ae99SBarry Smith @*/
1018d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1019d71ae5a4SJacob Faibussowitsch {
102047c6ae99SBarry Smith   PetscFunctionBegin;
1021171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1022b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1023dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1025c6b011d8SStefano Zampini     DM vdm;
1026c6b011d8SStefano Zampini 
10279566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10287a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1029c6b011d8SStefano Zampini   }
10303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
103147c6ae99SBarry Smith }
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith /*@
1034bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith     Not Collective
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith     Input Parameter:
1039bb7acecfSBarry Smith .   dm - the `DM` object
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith     Output Parameter:
104247c6ae99SBarry Smith .   vec - the local vector
104347c6ae99SBarry Smith 
1044073dac72SJed Brown     Level: beginner
104547c6ae99SBarry Smith 
104620f4b53cSBarry Smith     Note:
1047bb7acecfSBarry 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.
1048bb7acecfSBarry Smith 
10491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1050bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105147c6ae99SBarry Smith @*/
1052d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1053d71ae5a4SJacob Faibussowitsch {
105447c6ae99SBarry Smith   PetscFunctionBegin;
1055171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1056b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1057dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1059c6b011d8SStefano Zampini     DM vdm;
1060c6b011d8SStefano Zampini 
10619566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10627a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1063c6b011d8SStefano Zampini   }
10643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106547c6ae99SBarry Smith }
106647c6ae99SBarry Smith 
10671411c6eeSJed Brown /*@
1068bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10691411c6eeSJed Brown 
107020f4b53cSBarry Smith    Collective
10711411c6eeSJed Brown 
10721411c6eeSJed Brown    Input Parameter:
1073bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10741411c6eeSJed Brown 
10751411c6eeSJed Brown    Output Parameter:
10761411c6eeSJed Brown .  ltog - the mapping
10771411c6eeSJed Brown 
1078bb7acecfSBarry Smith    Level: advanced
10791411c6eeSJed Brown 
10801411c6eeSJed Brown    Notes:
1081bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10821411c6eeSJed Brown 
1083bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1084bb7acecfSBarry Smith    need to use this function with those objects.
1085bb7acecfSBarry Smith 
1086bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1087bb7acecfSBarry Smith 
10881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1089bb7acecfSBarry Smith           `DMCreateMatrix()`
10901411c6eeSJed Brown @*/
1091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1092d71ae5a4SJacob Faibussowitsch {
10930be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10941411c6eeSJed Brown 
10951411c6eeSJed Brown   PetscFunctionBegin;
10961411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10971411c6eeSJed Brown   PetscValidPointer(ltog, 2);
10981411c6eeSJed Brown   if (!dm->ltogmap) {
109937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110037d0c07bSMatthew G Knepley 
11019566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110237d0c07bSMatthew G Knepley     if (section) {
1103a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110437d0c07bSMatthew G Knepley       PetscInt       *ltog;
1105ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110637d0c07bSMatthew G Knepley 
11079566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11109566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1112e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111337d0c07bSMatthew G Knepley 
111437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11189566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11191a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11201a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1121ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11225227eafbSStefano Zampini 
1123e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11245227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1125e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1126e6befd46SJed Brown             cind++;
1127e6befd46SJed Brown           } else {
11285227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1129e6befd46SJed Brown           }
113037d0c07bSMatthew G Knepley         }
113137d0c07bSMatthew G Knepley       }
1132bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11339371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11349371c9d4SSatish Balay       bsLocal[1] = bs;
11359566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11369371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11379371c9d4SSatish Balay         bs = 1;
11389371c9d4SSatish Balay       } else {
11399371c9d4SSatish Balay         bs = bsMinMax[0];
11409371c9d4SSatish Balay       }
11417591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11427591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1143ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1144ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1145ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1146ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1147ca469d19SJed Brown         }
1148ccf3bd66SMatthew G. Knepley         n /= bs;
1149ccf3bd66SMatthew G. Knepley       }
11509566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1151dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115237d0c07bSMatthew G Knepley   }
11531411c6eeSJed Brown   *ltog = dm->ltogmap;
11543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11551411c6eeSJed Brown }
11561411c6eeSJed Brown 
11571411c6eeSJed Brown /*@
1158bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11591411c6eeSJed Brown 
11601411c6eeSJed Brown    Not Collective
11611411c6eeSJed Brown 
11621411c6eeSJed Brown    Input Parameter:
1163bb7acecfSBarry Smith .  dm - the `DM` with block structure
11641411c6eeSJed Brown 
11651411c6eeSJed Brown    Output Parameter:
11661411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11671411c6eeSJed Brown 
11681411c6eeSJed Brown    Level: intermediate
11691411c6eeSJed Brown 
1170bb7acecfSBarry Smith    Note:
1171bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1172bb7acecfSBarry Smith 
1173bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1174bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1175bb7acecfSBarry Smith 
11761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11771411c6eeSJed Brown @*/
1178d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1179d71ae5a4SJacob Faibussowitsch {
11801411c6eeSJed Brown   PetscFunctionBegin;
11811411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1182534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11837a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11841411c6eeSJed Brown   *bs = dm->bs;
11853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11861411c6eeSJed Brown }
11871411c6eeSJed Brown 
118848eeb7c8SBarry Smith /*@C
1189bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1190bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119147c6ae99SBarry Smith 
119220f4b53cSBarry Smith     Collective
119347c6ae99SBarry Smith 
1194d8d19677SJose E. Roman     Input Parameters:
1195bb7acecfSBarry Smith +   dmc - the `DM` object
1196bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
119747c6ae99SBarry Smith 
1198d8d19677SJose E. Roman     Output Parameters:
119947c6ae99SBarry Smith +  mat - the interpolation
1200bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
120147c6ae99SBarry Smith 
120247c6ae99SBarry Smith     Level: developer
120347c6ae99SBarry Smith 
120495452b02SPatrick Sanan     Notes:
1205bb7acecfSBarry 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
1206bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1207d52bd9f3SBarry Smith 
1208bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1209bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121085afcc9aSBarry Smith 
12111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121247c6ae99SBarry Smith @*/
1213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1214d71ae5a4SJacob Faibussowitsch {
121547c6ae99SBarry Smith   PetscFunctionBegin;
1216a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1217a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1218c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
12199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1220dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122347c6ae99SBarry Smith }
122447c6ae99SBarry Smith 
12253ad4599aSBarry Smith /*@
1226bb7acecfSBarry Smith     DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is the transpose of the interpolation between the `DM`.
1227bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1228bb7acecfSBarry Smith     representation of xfine.
12292ed6491fSPatrick Sanan 
12302ed6491fSPatrick Sanan   Input Parameters:
1231bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1232bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12332ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12342ed6491fSPatrick Sanan 
12352ed6491fSPatrick Sanan   Output Parameter:
12362ed6491fSPatrick Sanan .    scale - the scaled vector
12372ed6491fSPatrick Sanan 
1238bb7acecfSBarry Smith   Level: advanced
12392ed6491fSPatrick Sanan 
124020f4b53cSBarry Smith   Developer Note:
1241bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1242e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1243e9c74fd6SRichard Tran Mills 
12441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12452ed6491fSPatrick Sanan @*/
1246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1247d71ae5a4SJacob Faibussowitsch {
12482ed6491fSPatrick Sanan   Vec         fine;
12492ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12509704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1251e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12529704db99SRichard Tran Mills #endif
12532ed6491fSPatrick Sanan 
12542ed6491fSPatrick Sanan   PetscFunctionBegin;
12559566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12569566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12579566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12589704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12599704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12609704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12619704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12629566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1263e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12649566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12659566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12669566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126783aa49f4SRichard Tran Mills   }
12689704db99SRichard Tran Mills #endif
12699566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12719566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12732ed6491fSPatrick Sanan }
12742ed6491fSPatrick Sanan 
12752ed6491fSPatrick Sanan /*@
1276bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1277bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12783ad4599aSBarry Smith 
127920f4b53cSBarry Smith     Collective
12803ad4599aSBarry Smith 
1281d8d19677SJose E. Roman     Input Parameters:
1282bb7acecfSBarry Smith +   dmc - the `DM` object
1283bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12843ad4599aSBarry Smith 
12853ad4599aSBarry Smith     Output Parameter:
12863ad4599aSBarry Smith .  mat - the restriction
12873ad4599aSBarry Smith 
12883ad4599aSBarry Smith     Level: developer
12893ad4599aSBarry Smith 
1290bb7acecfSBarry Smith     Note:
1291bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1292bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12933ad4599aSBarry Smith 
12941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12953ad4599aSBarry Smith @*/
1296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1297d71ae5a4SJacob Faibussowitsch {
12983ad4599aSBarry Smith   PetscFunctionBegin;
1299a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1300a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13015a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1303dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13063ad4599aSBarry Smith }
13073ad4599aSBarry Smith 
130847c6ae99SBarry Smith /*@
1309bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1310bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1311bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1312bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
131347c6ae99SBarry Smith 
131420f4b53cSBarry Smith     Collective
131547c6ae99SBarry Smith 
1316d8d19677SJose E. Roman     Input Parameters:
1317bb7acecfSBarry Smith +   dac - the `DM` object
1318bb7acecfSBarry Smith -   daf - the second, finer `DM` object
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith     Output Parameter:
13216dbf9973SLawrence Mitchell .   mat - the injection
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Level: developer
132447c6ae99SBarry Smith 
1325bb7acecfSBarry Smith    Note:
1326bb7acecfSBarry 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
1327bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132885afcc9aSBarry Smith 
13291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1330bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133147c6ae99SBarry Smith @*/
1332d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1333d71ae5a4SJacob Faibussowitsch {
133447c6ae99SBarry Smith   PetscFunctionBegin;
1335a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1336a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13375a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13389566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1339dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13409566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134247c6ae99SBarry Smith }
134347c6ae99SBarry Smith 
1344b412c318SBarry Smith /*@
1345bb7acecfSBarry 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
1346bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1347bd041c0cSMatthew G. Knepley 
134820f4b53cSBarry Smith   Collective
1349bd041c0cSMatthew G. Knepley 
1350d8d19677SJose E. Roman   Input Parameters:
1351bb7acecfSBarry Smith + dmc - the target `DM` object
1352bb7acecfSBarry Smith - dmf - the source `DM` object
1353bd041c0cSMatthew G. Knepley 
1354bd041c0cSMatthew G. Knepley   Output Parameter:
1355b4937a87SMatthew G. Knepley . mat - the mass matrix
1356bd041c0cSMatthew G. Knepley 
1357bd041c0cSMatthew G. Knepley   Level: developer
1358bd041c0cSMatthew G. Knepley 
1359bb7acecfSBarry Smith   Notes:
1360bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1361bb7acecfSBarry Smith 
136220f4b53cSBarry Smith   if `dmc` is `dmf` then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1363bb7acecfSBarry Smith 
13641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1365bd041c0cSMatthew G. Knepley @*/
1366d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1367d71ae5a4SJacob Faibussowitsch {
1368bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1369b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1370b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13715a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13725b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1373dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13745b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1376b4937a87SMatthew G. Knepley }
1377b4937a87SMatthew G. Knepley 
1378b4937a87SMatthew G. Knepley /*@
1379bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1380b4937a87SMatthew G. Knepley 
138120f4b53cSBarry Smith   Collective
1382b4937a87SMatthew G. Knepley 
1383b4937a87SMatthew G. Knepley   Input Parameter:
1384bb7acecfSBarry Smith . dm - the `DM` object
1385b4937a87SMatthew G. Knepley 
1386b4937a87SMatthew G. Knepley   Output Parameter:
1387bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1388b4937a87SMatthew G. Knepley 
1389b4937a87SMatthew G. Knepley   Level: developer
1390b4937a87SMatthew G. Knepley 
1391bb7acecfSBarry Smith   Note:
1392bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1393bb7acecfSBarry Smith 
13941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1395b4937a87SMatthew G. Knepley @*/
1396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1397d71ae5a4SJacob Faibussowitsch {
1398b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1399b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1400b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1401dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1403bd041c0cSMatthew G. Knepley }
1404bd041c0cSMatthew G. Knepley 
1405bd041c0cSMatthew G. Knepley /*@
1406bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1407bb7acecfSBarry Smith     of a PDE on the `DM`.
140847c6ae99SBarry Smith 
140920f4b53cSBarry Smith     Collective
141047c6ae99SBarry Smith 
1411d8d19677SJose E. Roman     Input Parameters:
1412bb7acecfSBarry Smith +   dm - the `DM` object
1413bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith     Output Parameter:
141647c6ae99SBarry Smith .   coloring - the coloring
141747c6ae99SBarry Smith 
14181bf8429eSBarry Smith     Level: developer
14191bf8429eSBarry Smith 
1420ec5066bdSBarry Smith     Notes:
1421bb7acecfSBarry 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
1422bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1423ec5066bdSBarry Smith 
1424bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14251bf8429eSBarry 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,
14261bf8429eSBarry Smith     otherwise an error will be generated.
1427ec5066bdSBarry Smith 
14281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1429aab9d709SJed Brown @*/
1430d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1431d71ae5a4SJacob Faibussowitsch {
143247c6ae99SBarry Smith   PetscFunctionBegin;
1433171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14345a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1435dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
143747c6ae99SBarry Smith }
143847c6ae99SBarry Smith 
1439b412c318SBarry Smith /*@
1440bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144147c6ae99SBarry Smith 
144220f4b53cSBarry Smith     Collective
144347c6ae99SBarry Smith 
144447c6ae99SBarry Smith     Input Parameter:
1445bb7acecfSBarry Smith .   dm - the `DM` object
144647c6ae99SBarry Smith 
144747c6ae99SBarry Smith     Output Parameter:
144847c6ae99SBarry Smith .   mat - the empty Jacobian
144947c6ae99SBarry Smith 
145020f4b53cSBarry Smith     Options Database Key:
1451bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1452f27dd7c6SMatthew G. Knepley 
145320f4b53cSBarry Smith     Level: beginner
145420f4b53cSBarry Smith 
145595452b02SPatrick Sanan     Notes:
145695452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
145794013140SBarry Smith     do not need to do it yourself.
145894013140SBarry Smith 
145994013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1460bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146194013140SBarry Smith 
1462bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146394013140SBarry Smith     internally by PETSc.
146494013140SBarry Smith 
1465bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1466bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146794013140SBarry Smith 
14681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1469aab9d709SJed Brown @*/
1470d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1471d71ae5a4SJacob Faibussowitsch {
147247c6ae99SBarry Smith   PetscFunctionBegin;
1473171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1474064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14759566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14769566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1477dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1479c6b011d8SStefano Zampini     DM mdm;
1480c6b011d8SStefano Zampini 
14819566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14827a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1483c6b011d8SStefano Zampini   }
1484e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1485e5e52638SMatthew G. Knepley   if (dm->Nf) {
1486e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1487649ef022SMatthew Knepley     PetscInt     Nf, f;
1488e571a35bSMatthew G. Knepley 
14899566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1490649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1491649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14929566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14939566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14949566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1495649ef022SMatthew Knepley         break;
1496e571a35bSMatthew G. Knepley       }
1497649ef022SMatthew Knepley     }
1498649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1499649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15009566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15019566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15029566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1503e571a35bSMatthew G. Knepley       }
1504e571a35bSMatthew G. Knepley     }
1505e571a35bSMatthew G. Knepley   }
15069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
150847c6ae99SBarry Smith }
150947c6ae99SBarry Smith 
1510732e2eb9SMatthew G Knepley /*@
1511bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1512bb7acecfSBarry Smith   properly set, but the data structures to store values in the matrices will not be preallocated. This is most useful to reduce initialization costs when
1513bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1514aa0f6e3cSJed Brown 
151520f4b53cSBarry Smith   Logically Collective
1516aa0f6e3cSJed Brown 
1517aa0f6e3cSJed Brown   Input Parameters:
1518bb7acecfSBarry Smith + dm - the `DM`
1519bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1520aa0f6e3cSJed Brown 
1521aa0f6e3cSJed Brown   Level: developer
1522aa0f6e3cSJed Brown 
15231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1524aa0f6e3cSJed Brown @*/
1525d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1526d71ae5a4SJacob Faibussowitsch {
1527aa0f6e3cSJed Brown   PetscFunctionBegin;
1528aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1529aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1531aa0f6e3cSJed Brown }
1532aa0f6e3cSJed Brown 
1533aa0f6e3cSJed Brown /*@
1534bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1535732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1536732e2eb9SMatthew G Knepley 
153720f4b53cSBarry Smith   Logically Collective
1538732e2eb9SMatthew G Knepley 
1539d8d19677SJose E. Roman   Input Parameters:
1540bb7acecfSBarry Smith + dm - the `DM`
1541bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1542732e2eb9SMatthew G Knepley 
154320f4b53cSBarry Smith   Options Database Key:
1544bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1545f27dd7c6SMatthew G. Knepley 
154620f4b53cSBarry Smith   Level: developer
154720f4b53cSBarry Smith 
15481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1549732e2eb9SMatthew G Knepley @*/
1550d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1551d71ae5a4SJacob Faibussowitsch {
1552732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1553732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1554732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1556732e2eb9SMatthew G Knepley }
1557732e2eb9SMatthew G Knepley 
1558b06ff27eSHong Zhang /*@
1559bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1560bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1561b06ff27eSHong Zhang 
156220f4b53cSBarry Smith   Logically Collective
1563b06ff27eSHong Zhang 
1564d8d19677SJose E. Roman   Input Parameters:
1565bb7acecfSBarry Smith + dm - the `DM`
1566da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1567b06ff27eSHong Zhang 
1568b06ff27eSHong Zhang   Level: developer
1569bb7acecfSBarry Smith 
15701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1571b06ff27eSHong Zhang @*/
1572d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1573d71ae5a4SJacob Faibussowitsch {
1574b06ff27eSHong Zhang   PetscFunctionBegin;
1575b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1576b06ff27eSHong Zhang   dm->structure_only = only;
15773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1578b06ff27eSHong Zhang }
1579b06ff27eSHong Zhang 
1580863027abSJed Brown /*@
1581863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1582863027abSJed Brown 
158320f4b53cSBarry Smith   Logically Collective
1584863027abSJed Brown 
1585863027abSJed Brown   Input Parameters:
1586863027abSJed Brown + dm - the `DM`
1587863027abSJed Brown - btype - block by topological point or field node
1588863027abSJed Brown 
158920f4b53cSBarry Smith   Options Database Key:
15900e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1591863027abSJed Brown 
159220f4b53cSBarry Smith   Level: advanced
159320f4b53cSBarry Smith 
15941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1595863027abSJed Brown @*/
1596863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1597863027abSJed Brown {
1598863027abSJed Brown   PetscFunctionBegin;
1599863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1600863027abSJed Brown   dm->blocking_type = btype;
16013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1602863027abSJed Brown }
1603863027abSJed Brown 
1604863027abSJed Brown /*@
1605863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1606863027abSJed Brown 
1607863027abSJed Brown   Not Collective
1608863027abSJed Brown 
16092fe279fdSBarry Smith   Input Parameter:
1610863027abSJed Brown . dm - the `DM`
1611863027abSJed Brown 
16122fe279fdSBarry Smith   Output Parameter:
1613863027abSJed Brown . btype - block by topological point or field node
1614863027abSJed Brown 
1615863027abSJed Brown   Level: advanced
1616863027abSJed Brown 
16171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1618863027abSJed Brown @*/
1619863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1620863027abSJed Brown {
1621863027abSJed Brown   PetscFunctionBegin;
1622863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1623863027abSJed Brown   PetscValidPointer(btype, 2);
1624863027abSJed Brown   *btype = dm->blocking_type;
16253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1626863027abSJed Brown }
1627863027abSJed Brown 
1628a89ea682SMatthew G Knepley /*@C
1629bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1630a89ea682SMatthew G Knepley 
1631a89ea682SMatthew G Knepley   Not Collective
1632a89ea682SMatthew G Knepley 
1633a89ea682SMatthew G Knepley   Input Parameters:
1634bb7acecfSBarry Smith + dm - the `DM` object
1635a5b23f4aSJose E. Roman . count - The minimum size
163620f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1637a89ea682SMatthew G Knepley 
1638a89ea682SMatthew G Knepley   Output Parameter:
1639a89ea682SMatthew G Knepley . array - the work array
1640a89ea682SMatthew G Knepley 
1641a89ea682SMatthew G Knepley   Level: developer
1642a89ea682SMatthew G Knepley 
1643bb7acecfSBarry Smith   Note:
1644da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1645bb7acecfSBarry Smith 
1646bb7acecfSBarry Smith   The array may contain nonzero values
1647bb7acecfSBarry Smith 
16481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1649a89ea682SMatthew G Knepley @*/
1650d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1651d71ae5a4SJacob Faibussowitsch {
1652aa1993deSMatthew G Knepley   DMWorkLink  link;
165369291d52SBarry Smith   PetscMPIInt dsize;
1654a89ea682SMatthew G Knepley 
1655a89ea682SMatthew G Knepley   PetscFunctionBegin;
1656a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1657aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1658442f3b32SStefano Zampini   if (!count) {
1659442f3b32SStefano Zampini     *(void **)mem = NULL;
1660442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1661442f3b32SStefano Zampini   }
1662aa1993deSMatthew G Knepley   if (dm->workin) {
1663aa1993deSMatthew G Knepley     link       = dm->workin;
1664aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1665aa1993deSMatthew G Knepley   } else {
16664dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1667a89ea682SMatthew G Knepley   }
1668b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1669b77fa653SStefano Zampini      Get size directly */
1670b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1671b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1672b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1673b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1674b77fa653SStefano Zampini #endif
1675b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1676b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1677b77fa653SStefano Zampini #endif
1678b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1679b77fa653SStefano Zampini 
16805056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16819566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16829566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1683854ce69bSBarry Smith     link->bytes = dsize * count;
1684aa1993deSMatthew G Knepley   }
1685aa1993deSMatthew G Knepley   link->next  = dm->workout;
1686aa1993deSMatthew G Knepley   dm->workout = link;
1687cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
168800d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
168900d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
169000d952a4SJed Brown #endif
1691aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1693a89ea682SMatthew G Knepley }
1694a89ea682SMatthew G Knepley 
1695aa1993deSMatthew G Knepley /*@C
1696bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1697aa1993deSMatthew G Knepley 
1698aa1993deSMatthew G Knepley   Not Collective
1699aa1993deSMatthew G Knepley 
1700aa1993deSMatthew G Knepley   Input Parameters:
1701bb7acecfSBarry Smith + dm - the `DM` object
1702a5b23f4aSJose E. Roman . count - The minimum size
170320f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1704aa1993deSMatthew G Knepley 
1705aa1993deSMatthew G Knepley   Output Parameter:
1706aa1993deSMatthew G Knepley . array - the work array
1707aa1993deSMatthew G Knepley 
1708aa1993deSMatthew G Knepley   Level: developer
1709aa1993deSMatthew G Knepley 
171020f4b53cSBarry Smith   Developer Note:
1711bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1712147403d9SBarry Smith 
17131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1714aa1993deSMatthew G Knepley @*/
1715d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1716d71ae5a4SJacob Faibussowitsch {
1717aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1718aa1993deSMatthew G Knepley 
1719aa1993deSMatthew G Knepley   PetscFunctionBegin;
1720aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1721aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1722442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1723aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1724aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1725aa1993deSMatthew G Knepley       *p            = link->next;
1726aa1993deSMatthew G Knepley       link->next    = dm->workin;
1727aa1993deSMatthew G Knepley       dm->workin    = link;
17280298fd71SBarry Smith       *(void **)mem = NULL;
17293ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1730aa1993deSMatthew G Knepley     }
1731aa1993deSMatthew G Knepley   }
1732aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1733aa1993deSMatthew G Knepley }
1734e7c4fc90SDmitry Karpeev 
17358cda7954SMatthew G. Knepley /*@C
1736bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1737bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17388cda7954SMatthew G. Knepley 
173920f4b53cSBarry Smith   Logically Collective; No Fortran Support
17408cda7954SMatthew G. Knepley 
17418cda7954SMatthew G. Knepley   Input Parameters:
1742bb7acecfSBarry Smith + dm     - The `DM`
17438cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17448cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17458cda7954SMatthew G. Knepley 
174620f4b53cSBarry Smith   Calling sequence of `nullsp`:
1747147403d9SBarry Smith .vb
1748147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1749147403d9SBarry Smith .ve
1750bb7acecfSBarry Smith +  dm        - The present `DM`
1751bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1752147403d9SBarry Smith .  field     - The field number in dm
1753147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17548cda7954SMatthew G. Knepley 
175549762cbcSSatish Balay   Level: intermediate
175649762cbcSSatish Balay 
17571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1758147403d9SBarry Smith @*/
1759d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1760d71ae5a4SJacob Faibussowitsch {
1761435a35e8SMatthew G Knepley   PetscFunctionBegin;
1762435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17637a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1764435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1766435a35e8SMatthew G Knepley }
1767435a35e8SMatthew G Knepley 
17688cda7954SMatthew G. Knepley /*@C
1769bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17708cda7954SMatthew G. Knepley 
177120f4b53cSBarry Smith   Not Collective; No Fortran Support
17728cda7954SMatthew G. Knepley 
17738cda7954SMatthew G. Knepley   Input Parameters:
1774bb7acecfSBarry Smith + dm     - The `DM`
17758cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17768cda7954SMatthew G. Knepley 
17778cda7954SMatthew G. Knepley   Output Parameter:
17788cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17798cda7954SMatthew G. Knepley 
178020f4b53cSBarry Smith   Calling sequence of `nullsp`:
1781147403d9SBarry Smith .vb
1782147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1783147403d9SBarry Smith .ve
1784147403d9SBarry Smith +  dm        - The present DM
1785147403d9SBarry Smith .  origField - The field number given above, in the original DM
1786147403d9SBarry Smith .  field     - The field number in dm
1787147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17888cda7954SMatthew G. Knepley 
178949762cbcSSatish Balay    Level: intermediate
179049762cbcSSatish Balay 
17911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1792147403d9SBarry Smith @*/
1793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1794d71ae5a4SJacob Faibussowitsch {
17950a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17960a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1797f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17987a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17990a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18010a50eb56SMatthew G. Knepley }
18020a50eb56SMatthew G. Knepley 
18038cda7954SMatthew G. Knepley /*@C
1804bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18058cda7954SMatthew G. Knepley 
180620f4b53cSBarry Smith   Logically Collective; No Fortran Support
18078cda7954SMatthew G. Knepley 
18088cda7954SMatthew G. Knepley   Input Parameters:
1809bb7acecfSBarry Smith + dm     - The `DM`
18108cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18118cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18128cda7954SMatthew G. Knepley 
181320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1814147403d9SBarry Smith .vb
1815147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1816147403d9SBarry Smith .ve
1817bb7acecfSBarry Smith +  dm        - The present `DM`
1818bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1819147403d9SBarry Smith .  field     - The field number in dm
1820147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18218cda7954SMatthew G. Knepley 
182249762cbcSSatish Balay    Level: intermediate
182349762cbcSSatish Balay 
18241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1825bb7acecfSBarry Smith           `MatNullSpace`
1826147403d9SBarry Smith @*/
1827d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1828d71ae5a4SJacob Faibussowitsch {
1829f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1830f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18317a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1832f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1834f9d4088aSMatthew G. Knepley }
1835f9d4088aSMatthew G. Knepley 
18368cda7954SMatthew G. Knepley /*@C
1837bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18388cda7954SMatthew G. Knepley 
183920f4b53cSBarry Smith   Not Collective; No Fortran Support
18408cda7954SMatthew G. Knepley 
18418cda7954SMatthew G. Knepley   Input Parameters:
1842bb7acecfSBarry Smith + dm     - The `DM`
18438cda7954SMatthew G. Knepley - field  - The field number for the nullspace
18448cda7954SMatthew G. Knepley 
18458cda7954SMatthew G. Knepley   Output Parameter:
18468cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18478cda7954SMatthew G. Knepley 
184820f4b53cSBarry Smith   Calling sequence of `nullsp`:
1849147403d9SBarry Smith .vb
1850147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1851147403d9SBarry Smith .ve
1852bb7acecfSBarry Smith +  dm        - The present `DM`
1853bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1854147403d9SBarry Smith .  field     - The field number in dm
1855147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18568cda7954SMatthew G. Knepley 
185749762cbcSSatish Balay    Level: intermediate
185849762cbcSSatish Balay 
18591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1860bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1861147403d9SBarry Smith @*/
1862d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1863d71ae5a4SJacob Faibussowitsch {
1864f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1865f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1866f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18677a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1868f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1870f9d4088aSMatthew G. Knepley }
1871f9d4088aSMatthew G. Knepley 
18724f3b5142SJed Brown /*@C
1873bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18744d343eeaSMatthew G Knepley 
187520f4b53cSBarry Smith   Not Collective; No Fortran Support
18764d343eeaSMatthew G Knepley 
18774d343eeaSMatthew G Knepley   Input Parameter:
1878bb7acecfSBarry Smith . dm - the `DM` object
18794d343eeaSMatthew G Knepley 
18804d343eeaSMatthew G Knepley   Output Parameters:
188120f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
188220f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
188320f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18844d343eeaSMatthew G Knepley 
18854d343eeaSMatthew G Knepley   Level: intermediate
18864d343eeaSMatthew G Knepley 
1887bb7acecfSBarry Smith   Note:
188821c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1889bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1890bb7acecfSBarry Smith   `PetscFree()`.
189121c9b008SJed Brown 
1892bb7acecfSBarry Smith   Developer Note:
1893bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1894bb7acecfSBarry Smith   likely be removed.
1895bb7acecfSBarry Smith 
18961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1897bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18984d343eeaSMatthew G Knepley @*/
1899d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1900d71ae5a4SJacob Faibussowitsch {
190137d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19024d343eeaSMatthew G Knepley 
19034d343eeaSMatthew G Knepley   PetscFunctionBegin;
19044d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190569ca1f37SDmitry Karpeev   if (numFields) {
1906534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
190769ca1f37SDmitry Karpeev     *numFields = 0;
190869ca1f37SDmitry Karpeev   }
190937d0c07bSMatthew G Knepley   if (fieldNames) {
191037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
19110298fd71SBarry Smith     *fieldNames = NULL;
191269ca1f37SDmitry Karpeev   }
191369ca1f37SDmitry Karpeev   if (fields) {
191469ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
19150298fd71SBarry Smith     *fields = NULL;
191669ca1f37SDmitry Karpeev   }
19179566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
191837d0c07bSMatthew G Knepley   if (section) {
19193a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
192037d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
192137d0c07bSMatthew G Knepley 
19229566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19249566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
192737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
192937d0c07bSMatthew G Knepley     }
193037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193137d0c07bSMatthew G Knepley       PetscInt gdof;
193237d0c07bSMatthew G Knepley 
19339566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193437d0c07bSMatthew G Knepley       if (gdof > 0) {
193537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19363a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
193737d0c07bSMatthew G Knepley 
19389566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19399566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19403a544194SStefano Zampini           fpdof = fdof - fcdof;
19413a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19423a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19433a544194SStefano Zampini             fieldNc[f] = 1;
19443a544194SStefano Zampini           }
19453a544194SStefano Zampini           fieldSizes[f] += fpdof;
194637d0c07bSMatthew G Knepley         }
194737d0c07bSMatthew G Knepley       }
194837d0c07bSMatthew G Knepley     }
194937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19509566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
195137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195237d0c07bSMatthew G Knepley     }
195337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195437d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195537d0c07bSMatthew G Knepley 
19569566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195737d0c07bSMatthew G Knepley       if (gdof > 0) {
19589566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
195937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
196037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
196137d0c07bSMatthew G Knepley 
19629566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19639566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1964ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196537d0c07bSMatthew G Knepley         }
196637d0c07bSMatthew G Knepley       }
196737d0c07bSMatthew G Knepley     }
19688865f1eaSKarl Rupp     if (numFields) *numFields = nF;
196937d0c07bSMatthew G Knepley     if (fieldNames) {
19709566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
197137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197237d0c07bSMatthew G Knepley         const char *fieldName;
197337d0c07bSMatthew G Knepley 
19749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19759566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197637d0c07bSMatthew G Knepley       }
197737d0c07bSMatthew G Knepley     }
197837d0c07bSMatthew G Knepley     if (fields) {
19799566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
198037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19813a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19823a544194SStefano Zampini 
19839566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19843a544194SStefano Zampini         in[0] = -fieldNc[f];
19853a544194SStefano Zampini         in[1] = fieldNc[f];
19861c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19873a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19889566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
198937d0c07bSMatthew G Knepley       }
199037d0c07bSMatthew G Knepley     }
19919566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1992dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19944d343eeaSMatthew G Knepley }
19954d343eeaSMatthew G Knepley 
199616621825SDmitry Karpeev /*@C
1997bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1998bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1999bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2000bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2001e7c4fc90SDmitry Karpeev 
200220f4b53cSBarry Smith   Not Collective; No Fortran Support
2003e7c4fc90SDmitry Karpeev 
2004e7c4fc90SDmitry Karpeev   Input Parameter:
2005bb7acecfSBarry Smith . dm - the `DM` object
2006e7c4fc90SDmitry Karpeev 
2007e7c4fc90SDmitry Karpeev   Output Parameters:
200820f4b53cSBarry Smith + len       - The number of fields (or `NULL` if not requested)
200920f4b53cSBarry Smith . namelist  - The name for each field (or `NULL` if not requested)
201020f4b53cSBarry Smith . islist    - The global indices for each field (or `NULL` if not requested)
201120f4b53cSBarry Smith - dmlist    - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2012e7c4fc90SDmitry Karpeev 
2013e7c4fc90SDmitry Karpeev   Level: intermediate
2014e7c4fc90SDmitry Karpeev 
2015bb7acecfSBarry Smith   Note:
2016e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2017bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2018bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2019e7c4fc90SDmitry Karpeev 
2020bb7acecfSBarry Smith   Developer Note:
2021bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2022bb7acecfSBarry Smith 
20231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2024e7c4fc90SDmitry Karpeev @*/
2025d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2026d71ae5a4SJacob Faibussowitsch {
2027e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2028e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20298865f1eaSKarl Rupp   if (len) {
2030534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
20318865f1eaSKarl Rupp     *len = 0;
20328865f1eaSKarl Rupp   }
20338865f1eaSKarl Rupp   if (namelist) {
20348865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
2035ea78f98cSLisandro Dalcin     *namelist = NULL;
20368865f1eaSKarl Rupp   }
20378865f1eaSKarl Rupp   if (islist) {
20388865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
2039ea78f98cSLisandro Dalcin     *islist = NULL;
20408865f1eaSKarl Rupp   }
20418865f1eaSKarl Rupp   if (dmlist) {
20428865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
2043ea78f98cSLisandro Dalcin     *dmlist = NULL;
20448865f1eaSKarl Rupp   }
2045f3f0edfdSDmitry Karpeev   /*
2046f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2047f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2048f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2049f3f0edfdSDmitry Karpeev    */
20507a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2052435a35e8SMatthew G Knepley     PetscSection section;
2053435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2054435a35e8SMatthew G Knepley 
20559566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20569566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2057435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2058f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20599566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20609566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20619566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2062435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2063435a35e8SMatthew G Knepley         const char *fieldName;
2064435a35e8SMatthew G Knepley 
20659566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
206603dc3394SMatthew G. Knepley         if (namelist) {
20679566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20689566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2069435a35e8SMatthew G Knepley         }
207003dc3394SMatthew G. Knepley       }
2071435a35e8SMatthew G Knepley     } else {
20729566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2073e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20740298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2075e7c4fc90SDmitry Karpeev     }
2076dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
207816621825SDmitry Karpeev }
207916621825SDmitry Karpeev 
2080412a4547SAlexis Marboeuf /*@C
208120f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
208220f4b53cSBarry Smith                   The fields are defined by `DMCreateFieldIS()`.
2083435a35e8SMatthew G Knepley 
2084435a35e8SMatthew G Knepley   Not collective
2085435a35e8SMatthew G Knepley 
2086435a35e8SMatthew G Knepley   Input Parameters:
2087bb7acecfSBarry Smith + dm        - The `DM` object
2088bb7acecfSBarry Smith . numFields - The number of fields to select
20892adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2090435a35e8SMatthew G Knepley 
2091435a35e8SMatthew G Knepley   Output Parameters:
2092bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2093bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2094435a35e8SMatthew G Knepley 
209520f4b53cSBarry Smith   Level: intermediate
209620f4b53cSBarry Smith 
2097bb7acecfSBarry Smith   Note:
2098bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20995d3b26e6SMatthew G. Knepley 
21001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2101435a35e8SMatthew G Knepley @*/
2102d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2103d71ae5a4SJacob Faibussowitsch {
2104435a35e8SMatthew G Knepley   PetscFunctionBegin;
2105435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2106dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
21078865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
21088865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2109dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2111435a35e8SMatthew G Knepley }
2112435a35e8SMatthew G Knepley 
21132adcc780SMatthew G. Knepley /*@C
2114bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21152adcc780SMatthew G. Knepley 
21162adcc780SMatthew G. Knepley   Not collective
21172adcc780SMatthew G. Knepley 
2118d8d19677SJose E. Roman   Input Parameters:
2119bb7acecfSBarry Smith + dms - The `DM` objects
2120bb7acecfSBarry Smith - n - The number of `DM`s
21212adcc780SMatthew G. Knepley 
21222adcc780SMatthew G. Knepley   Output Parameters:
2123bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2124bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21252adcc780SMatthew G. Knepley 
212620f4b53cSBarry Smith   Level: intermediate
212720f4b53cSBarry Smith 
2128bb7acecfSBarry Smith   Note:
2129bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21305d3b26e6SMatthew G. Knepley 
21311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21322adcc780SMatthew G. Knepley @*/
2133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2134d71ae5a4SJacob Faibussowitsch {
21352adcc780SMatthew G. Knepley   PetscInt i;
21362adcc780SMatthew G. Knepley 
21372adcc780SMatthew G. Knepley   PetscFunctionBegin;
21382adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2139ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21402adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2141a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2142bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2143bb7acecfSBarry Smith   if (n) {
2144b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2145dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21462adcc780SMatthew G. Knepley   }
21473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21482adcc780SMatthew G. Knepley }
21492adcc780SMatthew G. Knepley 
215016621825SDmitry Karpeev /*@C
2151bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2152bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2153bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2154bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2155bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
215616621825SDmitry Karpeev 
215720f4b53cSBarry Smith   Not Collective
215816621825SDmitry Karpeev 
215916621825SDmitry Karpeev   Input Parameter:
2160bb7acecfSBarry Smith . dm - the `DM` object
216116621825SDmitry Karpeev 
216216621825SDmitry Karpeev   Output Parameters:
216320f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
216420f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21650298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21660298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
216720f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
216816621825SDmitry Karpeev 
216916621825SDmitry Karpeev   Level: intermediate
217016621825SDmitry Karpeev 
2171bb7acecfSBarry Smith   Note:
217216621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2173bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2174bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
217516621825SDmitry Karpeev 
2176bb7acecfSBarry Smith   Questions:
217720f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2178bb7acecfSBarry Smith 
21791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
218016621825SDmitry Karpeev @*/
2181d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2182d71ae5a4SJacob Faibussowitsch {
2183be081cd6SPeter Brune   DMSubDomainHookLink link;
2184be081cd6SPeter Brune   PetscInt            i, l;
218516621825SDmitry Karpeev 
218616621825SDmitry Karpeev   PetscFunctionBegin;
218716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21889371c9d4SSatish Balay   if (n) {
21899371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
21909371c9d4SSatish Balay     *n = 0;
21919371c9d4SSatish Balay   }
21929371c9d4SSatish Balay   if (namelist) {
21939371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
21949371c9d4SSatish Balay     *namelist = NULL;
21959371c9d4SSatish Balay   }
21969371c9d4SSatish Balay   if (innerislist) {
21979371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
21989371c9d4SSatish Balay     *innerislist = NULL;
21999371c9d4SSatish Balay   }
22009371c9d4SSatish Balay   if (outerislist) {
22019371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
22029371c9d4SSatish Balay     *outerislist = NULL;
22039371c9d4SSatish Balay   }
22049371c9d4SSatish Balay   if (dmlist) {
22059371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
22069371c9d4SSatish Balay     *dmlist = NULL;
22079371c9d4SSatish Balay   }
2208f3f0edfdSDmitry Karpeev   /*
2209f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2210f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2211f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2212f3f0edfdSDmitry Karpeev    */
22137a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
221416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2215dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
221614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2217f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2218be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2219be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22209566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2221be081cd6SPeter Brune         }
2222648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2223e7c4fc90SDmitry Karpeev       }
222414a18fd3SPeter Brune     }
2225bb7acecfSBarry Smith     if (n) *n = l;
222614a18fd3SPeter Brune   }
22273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2228e30e807fSPeter Brune }
2229e30e807fSPeter Brune 
2230e30e807fSPeter Brune /*@C
2231e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2232e30e807fSPeter Brune 
223320f4b53cSBarry Smith   Not Collective
2234e30e807fSPeter Brune 
2235e30e807fSPeter Brune   Input Parameters:
2236bb7acecfSBarry Smith + dm - the `DM` object
2237e30e807fSPeter Brune . n  - the number of subdomain scatters
2238e30e807fSPeter Brune - subdms - the local subdomains
2239e30e807fSPeter Brune 
2240e30e807fSPeter Brune   Output Parameters:
22416b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2242e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2243e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2244e30e807fSPeter Brune 
224520f4b53cSBarry Smith   Level: developer
224620f4b53cSBarry Smith 
2247bb7acecfSBarry Smith   Note:
2248bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2249e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2250e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2251e30e807fSPeter Brune   solution and residual data.
2252e30e807fSPeter Brune 
2253bb7acecfSBarry Smith   Questions:
2254bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2255bb7acecfSBarry Smith 
22561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2257e30e807fSPeter Brune @*/
2258d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2259d71ae5a4SJacob Faibussowitsch {
2260e30e807fSPeter Brune   PetscFunctionBegin;
2261e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2262e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2263dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2265e7c4fc90SDmitry Karpeev }
2266e7c4fc90SDmitry Karpeev 
226747c6ae99SBarry Smith /*@
2268bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
226947c6ae99SBarry Smith 
227020f4b53cSBarry Smith   Collective
227147c6ae99SBarry Smith 
2272d8d19677SJose E. Roman   Input Parameters:
2273bb7acecfSBarry Smith + dm   - the `DM` object
2274bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
227547c6ae99SBarry Smith 
227647c6ae99SBarry Smith   Output Parameter:
227720f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2278ae0a1c52SMatthew G Knepley 
227920f4b53cSBarry Smith   Options Database Key:
2280412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2281412e9a14SMatthew G. Knepley 
228247c6ae99SBarry Smith   Level: developer
228347c6ae99SBarry Smith 
228420f4b53cSBarry Smith   Note:
228520f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
228620f4b53cSBarry Smith 
22871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
228847c6ae99SBarry Smith @*/
2289d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2290d71ae5a4SJacob Faibussowitsch {
2291c833c3b5SJed Brown   DMRefineHookLink link;
229247c6ae99SBarry Smith 
229347c6ae99SBarry Smith   PetscFunctionBegin;
2294732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22959566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2296dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22974057135bSMatthew G Knepley   if (*dmf) {
229843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22998865f1eaSKarl Rupp 
23009566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23018865f1eaSKarl Rupp 
2302644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23030598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2304656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23058865f1eaSKarl Rupp 
23069566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2307c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23081baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2309c833c3b5SJed Brown     }
2310c833c3b5SJed Brown   }
23119566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2313c833c3b5SJed Brown }
2314c833c3b5SJed Brown 
2315bb9467b5SJed Brown /*@C
2316c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2317c833c3b5SJed Brown 
231820f4b53cSBarry Smith    Logically Collective; No Fortran Support
2319c833c3b5SJed Brown 
23204165533cSJose E. Roman    Input Parameters:
2321bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2322bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2323f826b5fcSPierre Jolivet .  interphook - function to run to update data on finer levels (once per `SNESSolve()`)
232420f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2325c833c3b5SJed Brown 
232620f4b53cSBarry Smith    Calling sequence of `refinehook`:
232720f4b53cSBarry Smith $  PetscErrorCode refinehook(DM coarse, DM fine, void *ctx);
2328bb7acecfSBarry Smith +  coarse - coarse level `DM`
2329bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2330c833c3b5SJed Brown -  ctx - optional user-defined function context
2331c833c3b5SJed Brown 
233220f4b53cSBarry Smith    Calling sequence of `interphook`:
233320f4b53cSBarry Smith $  PetscErrorCode interphook(DM coarse, Mat interp, DM fine, void *ctx)
2334bb7acecfSBarry Smith +  coarse - coarse level `DM`
2335c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2336bb7acecfSBarry Smith .  fine - fine level `DM` to update
2337c833c3b5SJed Brown -  ctx - optional user-defined function context
2338c833c3b5SJed Brown 
2339c833c3b5SJed Brown    Level: advanced
2340c833c3b5SJed Brown 
2341c833c3b5SJed Brown    Notes:
2342bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2343bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2344bb7acecfSBarry Smith 
2345bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2346c833c3b5SJed Brown 
2347c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2348c833c3b5SJed Brown 
23491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2350c833c3b5SJed Brown @*/
2351d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2352d71ae5a4SJacob Faibussowitsch {
2353c833c3b5SJed Brown   DMRefineHookLink link, *p;
2354c833c3b5SJed Brown 
2355c833c3b5SJed Brown   PetscFunctionBegin;
2356c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23573d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23583ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23593d8e3701SJed Brown   }
23609566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2361c833c3b5SJed Brown   link->refinehook = refinehook;
2362c833c3b5SJed Brown   link->interphook = interphook;
2363c833c3b5SJed Brown   link->ctx        = ctx;
23640298fd71SBarry Smith   link->next       = NULL;
2365c833c3b5SJed Brown   *p               = link;
23663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2367c833c3b5SJed Brown }
2368c833c3b5SJed Brown 
23693d8e3701SJed Brown /*@C
2370bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2371bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23723d8e3701SJed Brown 
237320f4b53cSBarry Smith    Logically Collective; No Fortran Support
23743d8e3701SJed Brown 
23754165533cSJose E. Roman    Input Parameters:
2376bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2377bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2378bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
237920f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23803d8e3701SJed Brown 
23813d8e3701SJed Brown    Level: advanced
23823d8e3701SJed Brown 
2383bb7acecfSBarry Smith    Note:
23843d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23853d8e3701SJed Brown 
23861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23873d8e3701SJed Brown @*/
2388d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2389d71ae5a4SJacob Faibussowitsch {
23903d8e3701SJed Brown   DMRefineHookLink link, *p;
23913d8e3701SJed Brown 
23923d8e3701SJed Brown   PetscFunctionBegin;
23933d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23943d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23953d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23963d8e3701SJed Brown       link = *p;
23973d8e3701SJed Brown       *p   = link->next;
23989566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23993d8e3701SJed Brown       break;
24003d8e3701SJed Brown     }
24013d8e3701SJed Brown   }
24023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24033d8e3701SJed Brown }
24043d8e3701SJed Brown 
2405c833c3b5SJed Brown /*@
2406bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2407c833c3b5SJed Brown 
2408c833c3b5SJed Brown    Collective if any hooks are
2409c833c3b5SJed Brown 
24104165533cSJose E. Roman    Input Parameters:
2411bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2412bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2413bb7acecfSBarry Smith -  fine - finer `DM` to update
2414c833c3b5SJed Brown 
2415c833c3b5SJed Brown    Level: developer
2416c833c3b5SJed Brown 
2417bb7acecfSBarry Smith    Developer Note:
2418bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2419bb7acecfSBarry Smith    an API with consistent terminology.
2420bb7acecfSBarry Smith 
24211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2422c833c3b5SJed Brown @*/
2423d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2424d71ae5a4SJacob Faibussowitsch {
2425c833c3b5SJed Brown   DMRefineHookLink link;
2426c833c3b5SJed Brown 
2427c833c3b5SJed Brown   PetscFunctionBegin;
2428c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24291baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24304057135bSMatthew G Knepley   }
24313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
243247c6ae99SBarry Smith }
243347c6ae99SBarry Smith 
2434eb3f98d2SBarry Smith /*@
24351f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24361f3379b2SToby Isaac 
243720f4b53cSBarry Smith    Collective
24381f3379b2SToby Isaac 
24394165533cSJose E. Roman    Input Parameters:
2440bb7acecfSBarry Smith +  coarse - coarse `DM`
2441bb7acecfSBarry Smith .  fine   - fine `DM`
2442bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2443bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2444bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24451f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24461f3379b2SToby Isaac 
24474165533cSJose E. Roman    Output Parameter:
24481f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24491f3379b2SToby Isaac 
24501f3379b2SToby Isaac    Level: developer
24511f3379b2SToby Isaac 
2452bb7acecfSBarry Smith    Note:
2453bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24541f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24551f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24561f3379b2SToby Isaac    slope-limiting reconstruction.
24571f3379b2SToby Isaac 
2458bb7acecfSBarry Smith    Developer Note:
2459bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2460bb7acecfSBarry Smith 
24611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24621f3379b2SToby Isaac @*/
2463d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2464d71ae5a4SJacob Faibussowitsch {
24651f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24661f3379b2SToby Isaac 
24671f3379b2SToby Isaac   PetscFunctionBegin;
24681f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24691f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24701f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24711f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24721f3379b2SToby Isaac 
24739566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24741f3379b2SToby Isaac   if (interpsol) {
24759566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24761f3379b2SToby Isaac   } else if (interp) {
24779566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
247898921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24801f3379b2SToby Isaac }
24811f3379b2SToby Isaac 
24821f3379b2SToby Isaac /*@
2483bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2484eb3f98d2SBarry Smith 
2485eb3f98d2SBarry Smith     Not Collective
2486eb3f98d2SBarry Smith 
2487eb3f98d2SBarry Smith     Input Parameter:
2488bb7acecfSBarry Smith .   dm - the `DM` object
2489eb3f98d2SBarry Smith 
2490eb3f98d2SBarry Smith     Output Parameter:
2491eb3f98d2SBarry Smith .   level - number of refinements
2492eb3f98d2SBarry Smith 
2493eb3f98d2SBarry Smith     Level: developer
2494eb3f98d2SBarry Smith 
2495bb7acecfSBarry Smith     Note:
2496bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2497bb7acecfSBarry Smith 
24981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2499eb3f98d2SBarry Smith @*/
2500d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2501d71ae5a4SJacob Faibussowitsch {
2502eb3f98d2SBarry Smith   PetscFunctionBegin;
2503eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2504eb3f98d2SBarry Smith   *level = dm->levelup;
25053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2506eb3f98d2SBarry Smith }
2507eb3f98d2SBarry Smith 
2508fef3a512SBarry Smith /*@
2509bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2510fef3a512SBarry Smith 
2511fef3a512SBarry Smith     Not Collective
2512fef3a512SBarry Smith 
2513d8d19677SJose E. Roman     Input Parameters:
2514bb7acecfSBarry Smith +   dm - the `DM` object
2515fef3a512SBarry Smith -   level - number of refinements
2516fef3a512SBarry Smith 
2517fef3a512SBarry Smith     Level: advanced
2518fef3a512SBarry Smith 
251995452b02SPatrick Sanan     Notes:
2520bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2521fef3a512SBarry Smith 
2522bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2523bb7acecfSBarry Smith 
25241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2525fef3a512SBarry Smith @*/
2526d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2527d71ae5a4SJacob Faibussowitsch {
2528fef3a512SBarry Smith   PetscFunctionBegin;
2529fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2530fef3a512SBarry Smith   dm->levelup = level;
25313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2532fef3a512SBarry Smith }
2533fef3a512SBarry Smith 
2534d410b0cfSMatthew G. Knepley /*@
2535bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2536d410b0cfSMatthew G. Knepley 
253720f4b53cSBarry Smith   Collective
2538d410b0cfSMatthew G. Knepley 
2539f1a722f8SMatthew G. Knepley   Input Parameters:
2540bb7acecfSBarry Smith + dm     - the `DM` object
2541d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2542d410b0cfSMatthew G. Knepley 
2543d410b0cfSMatthew G. Knepley   Output Parameter:
254420f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2545d410b0cfSMatthew G. Knepley 
2546d410b0cfSMatthew G. Knepley   Level: developer
2547d410b0cfSMatthew G. Knepley 
254820f4b53cSBarry Smith   Note:
254920f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
255020f4b53cSBarry Smith 
25511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2552d410b0cfSMatthew G. Knepley @*/
2553d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2554d71ae5a4SJacob Faibussowitsch {
2555d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2556d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2557dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2558d410b0cfSMatthew G. Knepley   if (*dme) {
2559d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25609566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2561d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25629566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2563d410b0cfSMatthew G. Knepley   }
25643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2565d410b0cfSMatthew G. Knepley }
2566d410b0cfSMatthew G. Knepley 
2567d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2568d71ae5a4SJacob Faibussowitsch {
2569ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2570ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2571ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2572ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2574ca3d3a14SMatthew G. Knepley }
2575ca3d3a14SMatthew G. Knepley 
2576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2577d71ae5a4SJacob Faibussowitsch {
2578ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2579ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2580ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2581ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2583ca3d3a14SMatthew G. Knepley }
2584ca3d3a14SMatthew G. Knepley 
2585ca3d3a14SMatthew G. Knepley /*@
2586bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2587ca3d3a14SMatthew G. Knepley 
2588ca3d3a14SMatthew G. Knepley   Input Parameter:
258920f4b53cSBarry Smith . dm - The `DM`
2590ca3d3a14SMatthew G. Knepley 
2591ca3d3a14SMatthew G. Knepley   Output Parameter:
259220f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2593ca3d3a14SMatthew G. Knepley 
2594ca3d3a14SMatthew G. Knepley   Level: developer
2595ca3d3a14SMatthew G. Knepley 
25961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2597ca3d3a14SMatthew G. Knepley @*/
2598d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2599d71ae5a4SJacob Faibussowitsch {
2600ca3d3a14SMatthew G. Knepley   Vec tv;
2601ca3d3a14SMatthew G. Knepley 
2602ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2603ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2604534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
26059566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2606ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2608ca3d3a14SMatthew G. Knepley }
2609ca3d3a14SMatthew G. Knepley 
2610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2611d71ae5a4SJacob Faibussowitsch {
2612ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2613ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2614ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2615ca3d3a14SMatthew G. Knepley 
2616ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26189566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26199566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26209566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26219566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26229566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26239566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2625ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26269566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2627ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2628ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2629ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2631ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26329566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26339566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2634ca3d3a14SMatthew G. Knepley     }
2635ca3d3a14SMatthew G. Knepley   }
26369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26379566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26389566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2639ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2640ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26419566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2642ca3d3a14SMatthew G. Knepley       if (dof) {
2643ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2644ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2645ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2646ca3d3a14SMatthew G. Knepley 
2647ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26489566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26499566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26509566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2651ca3d3a14SMatthew G. Knepley       }
2652ca3d3a14SMatthew G. Knepley     }
2653ca3d3a14SMatthew G. Knepley   }
26549566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2656ca3d3a14SMatthew G. Knepley }
2657ca3d3a14SMatthew G. Knepley 
2658d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2659d71ae5a4SJacob Faibussowitsch {
2660ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2661ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2662ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2663ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2664ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2665ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2666ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26679566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2669ca3d3a14SMatthew G. Knepley }
2670ca3d3a14SMatthew G. Knepley 
2671bb9467b5SJed Brown /*@C
2672bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2673baf369e7SPeter Brune 
267420f4b53cSBarry Smith    Logically Collective
2675baf369e7SPeter Brune 
26764165533cSJose E. Roman    Input Parameters:
2677bb7acecfSBarry Smith +  dm - the `DM`
2678bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2679bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
268020f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2681baf369e7SPeter Brune 
268220f4b53cSBarry Smith    Calling sequence of `beginhook`:
268320f4b53cSBarry Smith $  PetscErrorCode  beginhook(DM fine, VecScatter out, VecScatter in, DM coarse, void *ctx)
2684baf369e7SPeter Brune +  dm - global DM
2685baf369e7SPeter Brune .  g - global vector
2686baf369e7SPeter Brune .  mode - mode
2687baf369e7SPeter Brune .  l - local vector
2688baf369e7SPeter Brune -  ctx - optional user-defined function context
2689baf369e7SPeter Brune 
269020f4b53cSBarry Smith    Calling sequence of `endhook`:
269120f4b53cSBarry Smith $  PetscErrorCode  endhook(DM fine, VecScatter out, VecScatter in, DM coarse, void *ctx)
2692baf369e7SPeter Brune +  global - global DM
2693baf369e7SPeter Brune -  ctx - optional user-defined function context
2694baf369e7SPeter Brune 
2695baf369e7SPeter Brune    Level: advanced
2696baf369e7SPeter Brune 
2697bb7acecfSBarry Smith    Note:
2698bb7acecfSBarry 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.
2699bb7acecfSBarry Smith 
27001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2701baf369e7SPeter Brune @*/
2702d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2703d71ae5a4SJacob Faibussowitsch {
2704baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2705baf369e7SPeter Brune 
2706baf369e7SPeter Brune   PetscFunctionBegin;
2707baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2708baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27099566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2710baf369e7SPeter Brune   link->beginhook = beginhook;
2711baf369e7SPeter Brune   link->endhook   = endhook;
2712baf369e7SPeter Brune   link->ctx       = ctx;
27130298fd71SBarry Smith   link->next      = NULL;
2714baf369e7SPeter Brune   *p              = link;
27153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2716baf369e7SPeter Brune }
2717baf369e7SPeter Brune 
2718d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2719d71ae5a4SJacob Faibussowitsch {
27204c274da1SToby Isaac   Mat          cMat;
272179769bd5SJed Brown   Vec          cVec, cBias;
27224c274da1SToby Isaac   PetscSection section, cSec;
27234c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27244c274da1SToby Isaac 
27254c274da1SToby Isaac   PetscFunctionBegin;
27264c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27279566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27284c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27295db9a05bSToby Isaac     PetscInt nRows;
27305db9a05bSToby Isaac 
27319566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27323ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27339566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27349566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27359566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27369566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27384c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27404c274da1SToby Isaac       if (dof) {
27414c274da1SToby Isaac         PetscScalar *vals;
27429566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27439566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27444c274da1SToby Isaac       }
27454c274da1SToby Isaac     }
27469566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27474c274da1SToby Isaac   }
27483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27494c274da1SToby Isaac }
27504c274da1SToby Isaac 
275147c6ae99SBarry Smith /*@
275201729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
275301729b5cSPatrick Sanan 
275420f4b53cSBarry Smith     Neighbor-wise Collective
275501729b5cSPatrick Sanan 
275601729b5cSPatrick Sanan     Input Parameters:
2757bb7acecfSBarry Smith +   dm - the `DM` object
275801729b5cSPatrick Sanan .   g - the global vector
2759bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
276001729b5cSPatrick Sanan -   l - the local vector
276101729b5cSPatrick Sanan 
276220f4b53cSBarry Smith     Level: beginner
276320f4b53cSBarry Smith 
276401729b5cSPatrick Sanan     Notes:
2765bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2766bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2767bb7acecfSBarry Smith 
2768bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
276901729b5cSPatrick Sanan 
27701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2771bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2772bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
277301729b5cSPatrick Sanan @*/
2774d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2775d71ae5a4SJacob Faibussowitsch {
277601729b5cSPatrick Sanan   PetscFunctionBegin;
27779566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27789566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
278001729b5cSPatrick Sanan }
278101729b5cSPatrick Sanan 
278201729b5cSPatrick Sanan /*@
278347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
278447c6ae99SBarry Smith 
278520f4b53cSBarry Smith     Neighbor-wise Collective
278647c6ae99SBarry Smith 
278747c6ae99SBarry Smith     Input Parameters:
2788bb7acecfSBarry Smith +   dm - the `DM` object
278947c6ae99SBarry Smith .   g - the global vector
2790bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
279147c6ae99SBarry Smith -   l - the local vector
279247c6ae99SBarry Smith 
279301729b5cSPatrick Sanan     Level: intermediate
279447c6ae99SBarry Smith 
2795bb7acecfSBarry Smith     Notes:
2796bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2797bb7acecfSBarry Smith 
2798bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2799bb7acecfSBarry Smith 
2800bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2801bb7acecfSBarry Smith 
2802bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2803bb7acecfSBarry Smith 
28041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
280547c6ae99SBarry Smith @*/
2806d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2807d71ae5a4SJacob Faibussowitsch {
28087128ae9fSMatthew G Knepley   PetscSF                 sf;
2809baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
281047c6ae99SBarry Smith 
281147c6ae99SBarry Smith   PetscFunctionBegin;
2812171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2813baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28141baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2815baf369e7SPeter Brune   }
28169566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28177128ae9fSMatthew G Knepley   if (sf) {
2818ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2819ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2820d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28217128ae9fSMatthew G Knepley 
28227a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28239566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28249566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28259566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28269566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28279566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28287128ae9fSMatthew G Knepley   } else {
28299566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28307128ae9fSMatthew G Knepley   }
28313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
283247c6ae99SBarry Smith }
283347c6ae99SBarry Smith 
283447c6ae99SBarry Smith /*@
283547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
283647c6ae99SBarry Smith 
283720f4b53cSBarry Smith     Neighbor-wise Collective
283847c6ae99SBarry Smith 
283947c6ae99SBarry Smith     Input Parameters:
2840bb7acecfSBarry Smith +   dm - the `DM` object
284147c6ae99SBarry Smith .   g - the global vector
2842bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
284347c6ae99SBarry Smith -   l - the local vector
284447c6ae99SBarry Smith 
284501729b5cSPatrick Sanan     Level: intermediate
284647c6ae99SBarry Smith 
2847bb7acecfSBarry Smith     Note:
2848bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2849bb7acecfSBarry Smith 
28501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
285147c6ae99SBarry Smith @*/
2852d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2853d71ae5a4SJacob Faibussowitsch {
28547128ae9fSMatthew G Knepley   PetscSF                 sf;
2855ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2856ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2857ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2858baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2859d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
286047c6ae99SBarry Smith 
286147c6ae99SBarry Smith   PetscFunctionBegin;
2862171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28639566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28649566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28657128ae9fSMatthew G Knepley   if (sf) {
28667a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28677128ae9fSMatthew G Knepley 
28689566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28699566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28709566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28719566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28729566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28739566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28747128ae9fSMatthew G Knepley   } else {
28759566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28767128ae9fSMatthew G Knepley   }
28779566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2878baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28799566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2880baf369e7SPeter Brune   }
28813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
288247c6ae99SBarry Smith }
288347c6ae99SBarry Smith 
2884d4d07f1eSToby Isaac /*@C
2885d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2886d4d07f1eSToby Isaac 
288720f4b53cSBarry Smith    Logically Collective
2888d4d07f1eSToby Isaac 
28894165533cSJose E. Roman    Input Parameters:
2890bb7acecfSBarry Smith +  dm - the `DM`
2891bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2892bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
289320f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2894d4d07f1eSToby Isaac 
289520f4b53cSBarry Smith    Calling sequence of `beginhook`:
289620f4b53cSBarry Smith $  PetscErrorCode  beginhook(DM fine, Vec l, InsertMode mode, Vec g, void *ctx)
2897bb7acecfSBarry Smith +  dm - global `DM`
2898d4d07f1eSToby Isaac .  l - local vector
2899d4d07f1eSToby Isaac .  mode - mode
2900d4d07f1eSToby Isaac .  g - global vector
2901d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2902d4d07f1eSToby Isaac 
290320f4b53cSBarry Smith    Calling sequence of `endhook`:
290420f4b53cSBarry Smith $  PetscErrorCode  endhook(DM fine, Vec l, InsertMode mode, Vec g, void *ctx)
2905bb7acecfSBarry Smith +  global - global `DM`
2906d4d07f1eSToby Isaac .  l - local vector
2907d4d07f1eSToby Isaac .  mode - mode
2908d4d07f1eSToby Isaac .  g - global vector
2909d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2910d4d07f1eSToby Isaac 
2911d4d07f1eSToby Isaac    Level: advanced
2912d4d07f1eSToby Isaac 
29131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2914d4d07f1eSToby Isaac @*/
2915d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2916d71ae5a4SJacob Faibussowitsch {
2917d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2918d4d07f1eSToby Isaac 
2919d4d07f1eSToby Isaac   PetscFunctionBegin;
2920d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2921d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29229566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2923d4d07f1eSToby Isaac   link->beginhook = beginhook;
2924d4d07f1eSToby Isaac   link->endhook   = endhook;
2925d4d07f1eSToby Isaac   link->ctx       = ctx;
2926d4d07f1eSToby Isaac   link->next      = NULL;
2927d4d07f1eSToby Isaac   *p              = link;
29283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2929d4d07f1eSToby Isaac }
2930d4d07f1eSToby Isaac 
2931d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2932d71ae5a4SJacob Faibussowitsch {
29334c274da1SToby Isaac   Mat          cMat;
29344c274da1SToby Isaac   Vec          cVec;
29354c274da1SToby Isaac   PetscSection section, cSec;
29364c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29374c274da1SToby Isaac 
29384c274da1SToby Isaac   PetscFunctionBegin;
29394c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29409566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29414c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29425db9a05bSToby Isaac     PetscInt nRows;
29435db9a05bSToby Isaac 
29449566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29453ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29469566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29479566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29494c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29509566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29514c274da1SToby Isaac       if (dof) {
29524c274da1SToby Isaac         PetscInt     d;
29534c274da1SToby Isaac         PetscScalar *vals;
29549566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29559566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29564c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29574c274da1SToby Isaac          * we just extracted */
2958ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29594c274da1SToby Isaac       }
29604c274da1SToby Isaac     }
29619566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29629566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29634c274da1SToby Isaac   }
29643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29654c274da1SToby Isaac }
296601729b5cSPatrick Sanan /*@
296701729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
296801729b5cSPatrick Sanan 
296920f4b53cSBarry Smith     Neighbor-wise Collective
297001729b5cSPatrick Sanan 
297101729b5cSPatrick Sanan     Input Parameters:
2972bb7acecfSBarry Smith +   dm - the `DM` object
297301729b5cSPatrick Sanan .   l - the local vector
2974bb7acecfSBarry 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.
297501729b5cSPatrick Sanan -   g - the global vector
297601729b5cSPatrick Sanan 
297720f4b53cSBarry Smith     Level: beginner
297820f4b53cSBarry Smith 
297901729b5cSPatrick Sanan     Notes:
298001729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2981bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
298201729b5cSPatrick Sanan 
2983bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2984bb7acecfSBarry Smith 
2985bb7acecfSBarry 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.
2986bb7acecfSBarry Smith 
2987bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
298801729b5cSPatrick Sanan 
29891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
299001729b5cSPatrick Sanan @*/
2991d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2992d71ae5a4SJacob Faibussowitsch {
299301729b5cSPatrick Sanan   PetscFunctionBegin;
29949566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29959566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
29963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
299701729b5cSPatrick Sanan }
29984c274da1SToby Isaac 
299947c6ae99SBarry Smith /*@
300001729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
30019a42bb27SBarry Smith 
300220f4b53cSBarry Smith     Neighbor-wise Collective
30039a42bb27SBarry Smith 
30049a42bb27SBarry Smith     Input Parameters:
3005bb7acecfSBarry Smith +   dm - the `DM` object
3006f6813fd5SJed Brown .   l - the local vector
3007aa624791SPierre 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.
30081eb28f2eSBarry Smith -   g - the global vector
30099a42bb27SBarry Smith 
301020f4b53cSBarry Smith     Level: intermediate
301120f4b53cSBarry Smith 
301295452b02SPatrick Sanan     Notes:
3013bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3014bb7acecfSBarry Smith 
3015bb7acecfSBarry Smith     `INSERT_VALUES is` not supported for `DMDA`, in that case simply compute the values directly into a global vector instead of a local one.
3016bb7acecfSBarry Smith 
3017bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
3018bb7acecfSBarry Smith 
3019bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3020bb7acecfSBarry Smith 
3021bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30229a42bb27SBarry Smith 
30231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30249a42bb27SBarry Smith @*/
3025d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3026d71ae5a4SJacob Faibussowitsch {
30277128ae9fSMatthew G Knepley   PetscSF                 sf;
302884330215SMatthew G. Knepley   PetscSection            s, gs;
3029d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3030ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3031ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3032ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3033fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3034d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30359a42bb27SBarry Smith 
30369a42bb27SBarry Smith   PetscFunctionBegin;
3037171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3038d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30391baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3040d4d07f1eSToby Isaac   }
30419566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30429566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30439566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30447128ae9fSMatthew G Knepley   switch (mode) {
30457128ae9fSMatthew G Knepley   case INSERT_VALUES:
30467128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3047d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3048d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3049d71ae5a4SJacob Faibussowitsch     break;
30507128ae9fSMatthew G Knepley   case ADD_VALUES:
30517128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3052d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3053d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3054d71ae5a4SJacob Faibussowitsch     break;
3055d71ae5a4SJacob Faibussowitsch   default:
3056d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30577128ae9fSMatthew G Knepley   }
3058ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30599566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3060ca3d3a14SMatthew G. Knepley     if (transform) {
30619566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30629566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30639566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30649566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3065fa88e482SJed Brown     } else if (isInsert) {
30669566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3067fa88e482SJed Brown     } else {
30689566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3069fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3070ca3d3a14SMatthew G. Knepley     }
3071fa88e482SJed Brown     if (s && isInsert) {
30729566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3073fa88e482SJed Brown     } else {
30749566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3075fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3076fa88e482SJed Brown     }
3077ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30789566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
307984330215SMatthew G. Knepley     } else if (s && isInsert) {
308084330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
308184330215SMatthew G. Knepley 
30829566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30849566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
308584330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3086b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
308784330215SMatthew G. Knepley 
30889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30909566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30919566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30929566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30939566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3094b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
309503442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30967a8be351SBarry 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);
3097b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3098b3b16f48SMatthew G. Knepley         if (!gcdof) {
309984330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3100b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3101b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
310284330215SMatthew G. Knepley           const PetscInt *cdofs;
310384330215SMatthew G. Knepley           PetscInt        cind = 0;
310484330215SMatthew G. Knepley 
31059566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3106b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31079371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31089371c9d4SSatish Balay               ++cind;
31099371c9d4SSatish Balay               continue;
31109371c9d4SSatish Balay             }
3111b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
311284330215SMatthew G. Knepley           }
31137a8be351SBarry 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);
311484330215SMatthew G. Knepley       }
3115ca3d3a14SMatthew G. Knepley     }
3116fa88e482SJed Brown     if (g_inplace) {
31179566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3118fa88e482SJed Brown     } else {
31199566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3120fa88e482SJed Brown     }
3121ca3d3a14SMatthew G. Knepley     if (transform) {
31229566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31239566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3124fa88e482SJed Brown     } else if (l_inplace) {
31259566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3126ca3d3a14SMatthew G. Knepley     } else {
31279566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3128ca3d3a14SMatthew G. Knepley     }
31297128ae9fSMatthew G Knepley   } else {
31309566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31317128ae9fSMatthew G Knepley   }
31323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31339a42bb27SBarry Smith }
31349a42bb27SBarry Smith 
31359a42bb27SBarry Smith /*@
31369a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
313747c6ae99SBarry Smith 
313820f4b53cSBarry Smith     Neighbor-wise Collective
313947c6ae99SBarry Smith 
314047c6ae99SBarry Smith     Input Parameters:
3141bb7acecfSBarry Smith +   dm - the `DM` object
3142f6813fd5SJed Brown .   l - the local vector
3143bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3144f6813fd5SJed Brown -   g - the global vector
314547c6ae99SBarry Smith 
314601729b5cSPatrick Sanan     Level: intermediate
314747c6ae99SBarry Smith 
3148bb7acecfSBarry Smith     Note:
3149bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3150bb7acecfSBarry Smith 
31511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
315247c6ae99SBarry Smith @*/
3153d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3154d71ae5a4SJacob Faibussowitsch {
31557128ae9fSMatthew G Knepley   PetscSF                 sf;
315684330215SMatthew G. Knepley   PetscSection            s;
3157d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3158ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
315947c6ae99SBarry Smith 
316047c6ae99SBarry Smith   PetscFunctionBegin;
3161171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31629566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31639566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31647128ae9fSMatthew G Knepley   switch (mode) {
31657128ae9fSMatthew G Knepley   case INSERT_VALUES:
3166d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3167d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3168d71ae5a4SJacob Faibussowitsch     break;
31697128ae9fSMatthew G Knepley   case ADD_VALUES:
3170d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3171d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3172d71ae5a4SJacob Faibussowitsch     break;
3173d71ae5a4SJacob Faibussowitsch   default:
3174d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31757128ae9fSMatthew G Knepley   }
317684330215SMatthew G. Knepley   if (sf && !isInsert) {
3177ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3178ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3179ca3d3a14SMatthew G. Knepley     Vec                tmpl;
318084330215SMatthew G. Knepley 
31819566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3182ca3d3a14SMatthew G. Knepley     if (transform) {
31839566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31849566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3185ca3d3a14SMatthew G. Knepley     } else {
31869566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3187ca3d3a14SMatthew G. Knepley     }
31889566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31899566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3190ca3d3a14SMatthew G. Knepley     if (transform) {
31919566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31929566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3193ca3d3a14SMatthew G. Knepley     } else {
31949566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3195ca3d3a14SMatthew G. Knepley     }
31969566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
319784330215SMatthew G. Knepley   } else if (s && isInsert) {
31987128ae9fSMatthew G Knepley   } else {
31999566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
32007128ae9fSMatthew G Knepley   }
3201d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32029566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3203d4d07f1eSToby Isaac   }
32043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
320547c6ae99SBarry Smith }
320647c6ae99SBarry Smith 
3207f089877aSRichard Tran Mills /*@
3208bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3209bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3210bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3211f089877aSRichard Tran Mills 
321220f4b53cSBarry Smith    Neighbor-wise Collective
3213f089877aSRichard Tran Mills 
3214f089877aSRichard Tran Mills    Input Parameters:
3215bb7acecfSBarry Smith +  dm - the `DM` object
3216bc0a1609SRichard Tran Mills .  g - the original local vector
3217bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3218f089877aSRichard Tran Mills 
3219bc0a1609SRichard Tran Mills    Output Parameter:
3220bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3221f089877aSRichard Tran Mills 
3222f089877aSRichard Tran Mills    Level: intermediate
3223f089877aSRichard Tran Mills 
32241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3225f089877aSRichard Tran Mills @*/
3226d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3227d71ae5a4SJacob Faibussowitsch {
3228f089877aSRichard Tran Mills   PetscFunctionBegin;
3229f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32309f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32319f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32329f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3234f089877aSRichard Tran Mills }
3235f089877aSRichard Tran Mills 
3236f089877aSRichard Tran Mills /*@
3237bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3238bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3239f089877aSRichard Tran Mills 
324020f4b53cSBarry Smith    Neighbor-wise Collective
3241f089877aSRichard Tran Mills 
3242f089877aSRichard Tran Mills    Input Parameters:
3243bb7acecfSBarry Smith +  da - the `DM` object
3244bc0a1609SRichard Tran Mills .  g - the original local vector
3245bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3246f089877aSRichard Tran Mills 
3247bc0a1609SRichard Tran Mills    Output Parameter:
3248bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3249f089877aSRichard Tran Mills 
3250f089877aSRichard Tran Mills    Level: intermediate
3251f089877aSRichard Tran Mills 
32521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3253f089877aSRichard Tran Mills @*/
3254d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3255d71ae5a4SJacob Faibussowitsch {
3256f089877aSRichard Tran Mills   PetscFunctionBegin;
3257f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32589f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32599f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32609f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3262f089877aSRichard Tran Mills }
3263f089877aSRichard Tran Mills 
326447c6ae99SBarry Smith /*@
3265bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
326647c6ae99SBarry Smith 
326720f4b53cSBarry Smith     Collective
326847c6ae99SBarry Smith 
3269d8d19677SJose E. Roman     Input Parameters:
3270bb7acecfSBarry Smith +   dm - the `DM` object
327120f4b53cSBarry Smith -   comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
327247c6ae99SBarry Smith 
327347c6ae99SBarry Smith     Output Parameter:
3274bb7acecfSBarry Smith .   dmc - the coarsened `DM`
327547c6ae99SBarry Smith 
327647c6ae99SBarry Smith     Level: developer
327747c6ae99SBarry Smith 
32781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
327947c6ae99SBarry Smith @*/
3280d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3281d71ae5a4SJacob Faibussowitsch {
3282b17ce1afSJed Brown   DMCoarsenHookLink link;
328347c6ae99SBarry Smith 
328447c6ae99SBarry Smith   PetscFunctionBegin;
3285171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32869566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3287dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3288b9d85ea2SLisandro Dalcin   if (*dmc) {
3289a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32909566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
329143842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32929566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3293644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
32940598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3295656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
32969566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3297b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
32989566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3299b17ce1afSJed Brown     }
3300b9d85ea2SLisandro Dalcin   }
33019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33027a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3304b17ce1afSJed Brown }
3305b17ce1afSJed Brown 
3306bb9467b5SJed Brown /*@C
3307b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3308b17ce1afSJed Brown 
330920f4b53cSBarry Smith    Logically Collective; No Fortran Support
3310b17ce1afSJed Brown 
33114165533cSJose E. Roman    Input Parameters:
3312bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3313b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3314bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
331520f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3316b17ce1afSJed Brown 
331720f4b53cSBarry Smith    Calling sequence of `coarsenhook`:
331820f4b53cSBarry Smith $  PetscErrorCode  coarsenhook(DM fine, DM coarse, void *ctx);
3319bb7acecfSBarry Smith +  fine - fine level `DM`
3320bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3321b17ce1afSJed Brown -  ctx - optional user-defined function context
3322b17ce1afSJed Brown 
332320f4b53cSBarry Smith    Calling sequence of `restricthook`:
332420f4b53cSBarry Smith $  PetscErrorCode  restricthook(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx)
3325bb7acecfSBarry Smith +  fine - fine level `DM`
3326bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3327c833c3b5SJed Brown .  rscale - scaling vector for restriction
3328c833c3b5SJed Brown .  inject - matrix restricting by injection
3329b17ce1afSJed Brown .  coarse - coarse level DM to update
3330b17ce1afSJed Brown -  ctx - optional user-defined function context
3331b17ce1afSJed Brown 
3332b17ce1afSJed Brown    Level: advanced
3333b17ce1afSJed Brown 
3334b17ce1afSJed Brown    Notes:
3335bb7acecfSBarry 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`.
3336b17ce1afSJed Brown 
3337b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3338b17ce1afSJed Brown 
3339b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3340bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3341b17ce1afSJed Brown 
3342bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3343bb7acecfSBarry Smith 
33441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3345b17ce1afSJed Brown @*/
3346d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3347d71ae5a4SJacob Faibussowitsch {
3348b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3349b17ce1afSJed Brown 
3350b17ce1afSJed Brown   PetscFunctionBegin;
3351b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33521e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33533ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33541e3d8eccSJed Brown   }
33559566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3356b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3357b17ce1afSJed Brown   link->restricthook = restricthook;
3358b17ce1afSJed Brown   link->ctx          = ctx;
33590298fd71SBarry Smith   link->next         = NULL;
3360b17ce1afSJed Brown   *p                 = link;
33613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3362b17ce1afSJed Brown }
3363b17ce1afSJed Brown 
3364dc822a44SJed Brown /*@C
3365bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3366dc822a44SJed Brown 
336720f4b53cSBarry Smith    Logically Collective; No Fortran Support
3368dc822a44SJed Brown 
33694165533cSJose E. Roman    Input Parameters:
3370bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3371dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3372bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
337320f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3374dc822a44SJed Brown 
3375dc822a44SJed Brown    Level: advanced
3376dc822a44SJed Brown 
3377bb7acecfSBarry Smith    Note:
3378dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3379dc822a44SJed Brown 
33801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3381dc822a44SJed Brown @*/
3382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3383d71ae5a4SJacob Faibussowitsch {
3384dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3385dc822a44SJed Brown 
3386dc822a44SJed Brown   PetscFunctionBegin;
3387dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3388dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3389dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3390dc822a44SJed Brown       link = *p;
3391dc822a44SJed Brown       *p   = link->next;
33929566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3393dc822a44SJed Brown       break;
3394dc822a44SJed Brown     }
3395dc822a44SJed Brown   }
33963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3397dc822a44SJed Brown }
3398dc822a44SJed Brown 
3399b17ce1afSJed Brown /*@
3400bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3401b17ce1afSJed Brown 
3402b17ce1afSJed Brown    Collective if any hooks are
3403b17ce1afSJed Brown 
34044165533cSJose E. Roman    Input Parameters:
3405bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3406bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3407e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3408bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
340920f4b53cSBarry Smith -  coarse - coarser `DM` to update
3410b17ce1afSJed Brown 
3411b17ce1afSJed Brown    Level: developer
3412b17ce1afSJed Brown 
3413bb7acecfSBarry Smith    Developer Note:
3414bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3415bb7acecfSBarry Smith 
34161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3417b17ce1afSJed Brown @*/
3418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3419d71ae5a4SJacob Faibussowitsch {
3420b17ce1afSJed Brown   DMCoarsenHookLink link;
3421b17ce1afSJed Brown 
3422b17ce1afSJed Brown   PetscFunctionBegin;
3423b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34241baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3425b17ce1afSJed Brown   }
34263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
342747c6ae99SBarry Smith }
342847c6ae99SBarry Smith 
3429bb9467b5SJed Brown /*@C
3430be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34315dbd56e3SPeter Brune 
343220f4b53cSBarry Smith    Logically Collective; No Fortran Support
34335dbd56e3SPeter Brune 
34344165533cSJose E. Roman    Input Parameters:
3435bb7acecfSBarry Smith +  global - global `DM`
3436bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34375dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
343820f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34395dbd56e3SPeter Brune 
344020f4b53cSBarry Smith    Calling sequence of `ddhook`:
344120f4b53cSBarry Smith $  PetscErrorCode ddhook(DM global, DM block, void *ctx)
3442bb7acecfSBarry Smith +  global - global `DM`
3443bb7acecfSBarry Smith .  block  - block `DM`
3444ec4806b8SPeter Brune -  ctx - optional user-defined function context
3445ec4806b8SPeter Brune 
344620f4b53cSBarry Smith    Calling sequence of `restricthook`:
344720f4b53cSBarry Smith $  PetscErrorCode restricthook(DM global, VecScatter out, VecScatter in, DM block, void *ctx)
3448bb7acecfSBarry Smith +  global - global `DM`
34495dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34505dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3451bb7acecfSBarry Smith .  block  - block `DM`
34525dbd56e3SPeter Brune -  ctx - optional user-defined function context
34535dbd56e3SPeter Brune 
34545dbd56e3SPeter Brune    Level: advanced
34555dbd56e3SPeter Brune 
34565dbd56e3SPeter Brune    Notes:
3457bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34585dbd56e3SPeter Brune 
34595dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34605dbd56e3SPeter Brune 
34615dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3462bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34635dbd56e3SPeter Brune 
34641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34655dbd56e3SPeter Brune @*/
3466d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3467d71ae5a4SJacob Faibussowitsch {
3468be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34695dbd56e3SPeter Brune 
34705dbd56e3SPeter Brune   PetscFunctionBegin;
34715dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3472b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34733ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3474b3a6b972SJed Brown   }
34759566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34765dbd56e3SPeter Brune   link->restricthook = restricthook;
3477be081cd6SPeter Brune   link->ddhook       = ddhook;
34785dbd56e3SPeter Brune   link->ctx          = ctx;
34790298fd71SBarry Smith   link->next         = NULL;
34805dbd56e3SPeter Brune   *p                 = link;
34813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34825dbd56e3SPeter Brune }
34835dbd56e3SPeter Brune 
3484b3a6b972SJed Brown /*@C
3485b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3486b3a6b972SJed Brown 
348720f4b53cSBarry Smith    Logically Collective; No Fortran Support
3488b3a6b972SJed Brown 
34894165533cSJose E. Roman    Input Parameters:
3490bb7acecfSBarry Smith +  global - global `DM`
3491bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3492b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
349320f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3494b3a6b972SJed Brown 
3495b3a6b972SJed Brown    Level: advanced
3496b3a6b972SJed Brown 
34971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3498b3a6b972SJed Brown @*/
3499d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3500d71ae5a4SJacob Faibussowitsch {
3501b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3502b3a6b972SJed Brown 
3503b3a6b972SJed Brown   PetscFunctionBegin;
3504b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3505b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3506b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3507b3a6b972SJed Brown       link = *p;
3508b3a6b972SJed Brown       *p   = link->next;
35099566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3510b3a6b972SJed Brown       break;
3511b3a6b972SJed Brown     }
3512b3a6b972SJed Brown   }
35133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3514b3a6b972SJed Brown }
3515b3a6b972SJed Brown 
35165dbd56e3SPeter Brune /*@
3517bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35185dbd56e3SPeter Brune 
35195dbd56e3SPeter Brune    Collective if any hooks are
35205dbd56e3SPeter Brune 
35214165533cSJose E. Roman    Input Parameters:
3522bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3523be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3524be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3525bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35265dbd56e3SPeter Brune 
35275dbd56e3SPeter Brune    Level: developer
35285dbd56e3SPeter Brune 
35291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`
35305dbd56e3SPeter Brune @*/
3531d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3532d71ae5a4SJacob Faibussowitsch {
3533be081cd6SPeter Brune   DMSubDomainHookLink link;
35345dbd56e3SPeter Brune 
35355dbd56e3SPeter Brune   PetscFunctionBegin;
3536be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35371baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35385dbd56e3SPeter Brune   }
35393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35405dbd56e3SPeter Brune }
35415dbd56e3SPeter Brune 
35425fe1f584SPeter Brune /*@
3543bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35445fe1f584SPeter Brune 
35455fe1f584SPeter Brune     Not Collective
35465fe1f584SPeter Brune 
35475fe1f584SPeter Brune     Input Parameter:
3548bb7acecfSBarry Smith .   dm - the `DM` object
35495fe1f584SPeter Brune 
35505fe1f584SPeter Brune     Output Parameter:
35516a7d9d85SPeter Brune .   level - number of coarsenings
35525fe1f584SPeter Brune 
35535fe1f584SPeter Brune     Level: developer
35545fe1f584SPeter Brune 
35551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35565fe1f584SPeter Brune @*/
3557d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3558d71ae5a4SJacob Faibussowitsch {
35595fe1f584SPeter Brune   PetscFunctionBegin;
35605fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3561b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
35625fe1f584SPeter Brune   *level = dm->leveldown;
35633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35645fe1f584SPeter Brune }
35655fe1f584SPeter Brune 
35669a64c4a8SMatthew G. Knepley /*@
3567bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35689a64c4a8SMatthew G. Knepley 
356920f4b53cSBarry Smith     Collective
35709a64c4a8SMatthew G. Knepley 
35719a64c4a8SMatthew G. Knepley     Input Parameters:
3572bb7acecfSBarry Smith +   dm - the `DM` object
35739a64c4a8SMatthew G. Knepley -   level - number of coarsenings
35749a64c4a8SMatthew G. Knepley 
35759a64c4a8SMatthew G. Knepley     Level: developer
35769a64c4a8SMatthew G. Knepley 
3577bb7acecfSBarry Smith     Note:
3578bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3579bb7acecfSBarry Smith 
35801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35819a64c4a8SMatthew G. Knepley @*/
3582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3583d71ae5a4SJacob Faibussowitsch {
35849a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35859a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35869a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35889a64c4a8SMatthew G. Knepley }
35899a64c4a8SMatthew G. Knepley 
359047c6ae99SBarry Smith /*@C
3591bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
359247c6ae99SBarry Smith 
359320f4b53cSBarry Smith     Collective
359447c6ae99SBarry Smith 
3595d8d19677SJose E. Roman     Input Parameters:
3596bb7acecfSBarry Smith +   dm - the `DM` object
359747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
359847c6ae99SBarry Smith 
359947c6ae99SBarry Smith     Output Parameter:
3600bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
360147c6ae99SBarry Smith 
360247c6ae99SBarry Smith     Level: developer
360347c6ae99SBarry Smith 
36041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
360547c6ae99SBarry Smith @*/
3606d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3607d71ae5a4SJacob Faibussowitsch {
360847c6ae99SBarry Smith   PetscFunctionBegin;
3609171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36107a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36113ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
3612b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
361347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3614dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
361547c6ae99SBarry Smith   } else if (dm->ops->refine) {
361647c6ae99SBarry Smith     PetscInt i;
361747c6ae99SBarry Smith 
36189566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
361948a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3620ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
36213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
362247c6ae99SBarry Smith }
362347c6ae99SBarry Smith 
362447c6ae99SBarry Smith /*@C
3625bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
362647c6ae99SBarry Smith 
362720f4b53cSBarry Smith     Collective
362847c6ae99SBarry Smith 
3629d8d19677SJose E. Roman     Input Parameters:
3630bb7acecfSBarry Smith +   dm - the `DM` object
363147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
363247c6ae99SBarry Smith 
363347c6ae99SBarry Smith     Output Parameter:
3634bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
363547c6ae99SBarry Smith 
363647c6ae99SBarry Smith     Level: developer
363747c6ae99SBarry Smith 
36381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363947c6ae99SBarry Smith @*/
3640d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3641d71ae5a4SJacob Faibussowitsch {
364247c6ae99SBarry Smith   PetscFunctionBegin;
3643171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36447a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36453ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
364647c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
364747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3648dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
364947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
365047c6ae99SBarry Smith     PetscInt i;
365147c6ae99SBarry Smith 
36529566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
365348a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3654ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
36553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365647c6ae99SBarry Smith }
365747c6ae99SBarry Smith 
36581a266240SBarry Smith /*@C
3659bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36601a266240SBarry Smith 
3661bb7acecfSBarry Smith     Logically Collective if the function is collective
36621a266240SBarry Smith 
36631a266240SBarry Smith     Input Parameters:
3664bb7acecfSBarry Smith +   dm - the `DM` object
36651a266240SBarry Smith -   destroy - the destroy function
36661a266240SBarry Smith 
36671a266240SBarry Smith     Level: intermediate
36681a266240SBarry Smith 
36691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3670f07f9ceaSJed Brown @*/
3671d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3672d71ae5a4SJacob Faibussowitsch {
36731a266240SBarry Smith   PetscFunctionBegin;
3674171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36751a266240SBarry Smith   dm->ctxdestroy = destroy;
36763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36771a266240SBarry Smith }
36781a266240SBarry Smith 
3679b07ff414SBarry Smith /*@
3680bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
368147c6ae99SBarry Smith 
368247c6ae99SBarry Smith     Not Collective
368347c6ae99SBarry Smith 
368447c6ae99SBarry Smith     Input Parameters:
3685bb7acecfSBarry Smith +   dm - the `DM` object
368647c6ae99SBarry Smith -   ctx - the user context
368747c6ae99SBarry Smith 
368847c6ae99SBarry Smith     Level: intermediate
368947c6ae99SBarry Smith 
3690bb7acecfSBarry Smith     Note:
369135cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3692bb7acecfSBarry Smith 
36931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
369447c6ae99SBarry Smith @*/
3695d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3696d71ae5a4SJacob Faibussowitsch {
369747c6ae99SBarry Smith   PetscFunctionBegin;
3698171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369947c6ae99SBarry Smith   dm->ctx = ctx;
37003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
370147c6ae99SBarry Smith }
370247c6ae99SBarry Smith 
370347c6ae99SBarry Smith /*@
3704bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
370547c6ae99SBarry Smith 
370647c6ae99SBarry Smith     Not Collective
370747c6ae99SBarry Smith 
370847c6ae99SBarry Smith     Input Parameter:
3709bb7acecfSBarry Smith .   dm - the `DM` object
371047c6ae99SBarry Smith 
371147c6ae99SBarry Smith     Output Parameter:
371247c6ae99SBarry Smith .   ctx - the user context
371347c6ae99SBarry Smith 
371447c6ae99SBarry Smith     Level: intermediate
371547c6ae99SBarry Smith 
3716bb7acecfSBarry Smith     Note:
371735cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3718bb7acecfSBarry Smith 
37191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
372047c6ae99SBarry Smith @*/
3721d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3722d71ae5a4SJacob Faibussowitsch {
372347c6ae99SBarry Smith   PetscFunctionBegin;
3724171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37251b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
372747c6ae99SBarry Smith }
372847c6ae99SBarry Smith 
372908da532bSDmitry Karpeev /*@C
3730bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
373108da532bSDmitry Karpeev 
373220f4b53cSBarry Smith     Logically Collective
373308da532bSDmitry Karpeev 
3734d8d19677SJose E. Roman     Input Parameters:
373508da532bSDmitry Karpeev +   dm - the DM object
373620f4b53cSBarry Smith -   f - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
373708da532bSDmitry Karpeev 
373808da532bSDmitry Karpeev     Level: intermediate
373908da532bSDmitry Karpeev 
37401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3741db781477SPatrick Sanan          `DMSetJacobian()`
374208da532bSDmitry Karpeev @*/
3743d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3744d71ae5a4SJacob Faibussowitsch {
374508da532bSDmitry Karpeev   PetscFunctionBegin;
37465a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374908da532bSDmitry Karpeev }
375008da532bSDmitry Karpeev 
375108da532bSDmitry Karpeev /*@
3752bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
375308da532bSDmitry Karpeev 
375408da532bSDmitry Karpeev     Not Collective
375508da532bSDmitry Karpeev 
375608da532bSDmitry Karpeev     Input Parameter:
3757bb7acecfSBarry Smith .   dm - the `DM` object to destroy
375808da532bSDmitry Karpeev 
375908da532bSDmitry Karpeev     Output Parameter:
3760bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
376108da532bSDmitry Karpeev 
376208da532bSDmitry Karpeev     Level: developer
376308da532bSDmitry Karpeev 
37641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
376508da532bSDmitry Karpeev @*/
3766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3767d71ae5a4SJacob Faibussowitsch {
376808da532bSDmitry Karpeev   PetscFunctionBegin;
37695a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3770534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
377108da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377308da532bSDmitry Karpeev }
377408da532bSDmitry Karpeev 
377508da532bSDmitry Karpeev /*@C
3776bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
377708da532bSDmitry Karpeev 
377820f4b53cSBarry Smith     Logically Collective
377908da532bSDmitry Karpeev 
3780f899ff85SJose E. Roman     Input Parameter:
3781bb7acecfSBarry Smith .   dm - the `DM` object
378208da532bSDmitry Karpeev 
378308da532bSDmitry Karpeev     Output parameters:
378408da532bSDmitry Karpeev +   xl - lower bound
378508da532bSDmitry Karpeev -   xu - upper bound
378608da532bSDmitry Karpeev 
3787907376e6SBarry Smith     Level: advanced
3788907376e6SBarry Smith 
378920f4b53cSBarry Smith     Note:
379095452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
379108da532bSDmitry Karpeev 
37921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379308da532bSDmitry Karpeev @*/
3794d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3795d71ae5a4SJacob Faibussowitsch {
379608da532bSDmitry Karpeev   PetscFunctionBegin;
37975a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37995a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3800dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380208da532bSDmitry Karpeev }
380308da532bSDmitry Karpeev 
3804b0ae01b7SPeter Brune /*@
3805bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3806b0ae01b7SPeter Brune 
3807b0ae01b7SPeter Brune     Not Collective
3808b0ae01b7SPeter Brune 
3809b0ae01b7SPeter Brune     Input Parameter:
3810b0ae01b7SPeter Brune .   dm - the DM object
3811b0ae01b7SPeter Brune 
3812b0ae01b7SPeter Brune     Output Parameter:
3813bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3814b0ae01b7SPeter Brune 
3815b0ae01b7SPeter Brune     Level: developer
3816b0ae01b7SPeter Brune 
38171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3818b0ae01b7SPeter Brune @*/
3819d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3820d71ae5a4SJacob Faibussowitsch {
3821b0ae01b7SPeter Brune   PetscFunctionBegin;
38225a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3823534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3824b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3826b0ae01b7SPeter Brune }
3827b0ae01b7SPeter Brune 
38283ad4599aSBarry Smith /*@
3829bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38303ad4599aSBarry Smith 
38313ad4599aSBarry Smith     Not Collective
38323ad4599aSBarry Smith 
38333ad4599aSBarry Smith     Input Parameter:
3834bb7acecfSBarry Smith .   dm - the `DM` object
38353ad4599aSBarry Smith 
38363ad4599aSBarry Smith     Output Parameter:
3837bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38383ad4599aSBarry Smith 
38393ad4599aSBarry Smith     Level: developer
38403ad4599aSBarry Smith 
38411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38423ad4599aSBarry Smith @*/
3843d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3844d71ae5a4SJacob Faibussowitsch {
38453ad4599aSBarry Smith   PetscFunctionBegin;
38465a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3847534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
38483ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38503ad4599aSBarry Smith }
38513ad4599aSBarry Smith 
3852a7058e45SLawrence Mitchell /*@
3853bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3854a7058e45SLawrence Mitchell 
3855a7058e45SLawrence Mitchell     Not Collective
3856a7058e45SLawrence Mitchell 
3857a7058e45SLawrence Mitchell     Input Parameter:
3858bb7acecfSBarry Smith .   dm - the `DM` object
3859a7058e45SLawrence Mitchell 
3860a7058e45SLawrence Mitchell     Output Parameter:
3861bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3862a7058e45SLawrence Mitchell 
3863a7058e45SLawrence Mitchell     Level: developer
3864a7058e45SLawrence Mitchell 
38651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3866a7058e45SLawrence Mitchell @*/
3867d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3868d71ae5a4SJacob Faibussowitsch {
3869a7058e45SLawrence Mitchell   PetscFunctionBegin;
38705a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3871534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3872dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3873ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3875a7058e45SLawrence Mitchell }
3876a7058e45SLawrence Mitchell 
38770298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3878264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3879264ace61SBarry Smith 
3880264ace61SBarry Smith /*@C
3881bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3882264ace61SBarry Smith 
388320f4b53cSBarry Smith   Collective
3884264ace61SBarry Smith 
3885264ace61SBarry Smith   Input Parameters:
3886bb7acecfSBarry Smith + dm     - The `DM` object
3887bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3888264ace61SBarry Smith 
3889264ace61SBarry Smith   Options Database Key:
3890bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3891264ace61SBarry Smith 
3892264ace61SBarry Smith   Level: intermediate
3893264ace61SBarry Smith 
3894bb7acecfSBarry Smith   Note:
38950462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3896bb7acecfSBarry Smith 
38971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3898264ace61SBarry Smith @*/
3899d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3900d71ae5a4SJacob Faibussowitsch {
3901264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3902264ace61SBarry Smith   PetscBool match;
3903264ace61SBarry Smith 
3904264ace61SBarry Smith   PetscFunctionBegin;
3905264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39069566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39073ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3908264ace61SBarry Smith 
39099566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39109566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39117a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3912264ace61SBarry Smith 
3913dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39149566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39159566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39169566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3918264ace61SBarry Smith }
3919264ace61SBarry Smith 
3920264ace61SBarry Smith /*@C
3921bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3922264ace61SBarry Smith 
3923264ace61SBarry Smith   Not Collective
3924264ace61SBarry Smith 
3925264ace61SBarry Smith   Input Parameter:
3926bb7acecfSBarry Smith . dm  - The `DM`
3927264ace61SBarry Smith 
3928264ace61SBarry Smith   Output Parameter:
3929bb7acecfSBarry Smith . type - The `DMType` name
3930264ace61SBarry Smith 
3931264ace61SBarry Smith   Level: intermediate
3932264ace61SBarry Smith 
39331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3934264ace61SBarry Smith @*/
3935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3936d71ae5a4SJacob Faibussowitsch {
3937264ace61SBarry Smith   PetscFunctionBegin;
3938264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3939c959eef4SJed Brown   PetscValidPointer(type, 2);
39409566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3941264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3943264ace61SBarry Smith }
3944264ace61SBarry Smith 
394567a56275SMatthew G Knepley /*@C
3946bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
394767a56275SMatthew G Knepley 
394820f4b53cSBarry Smith   Collective
394967a56275SMatthew G Knepley 
395067a56275SMatthew G Knepley   Input Parameters:
3951bb7acecfSBarry Smith + dm - the `DM`
3952bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
395367a56275SMatthew G Knepley 
395467a56275SMatthew G Knepley   Output Parameter:
3955bb7acecfSBarry Smith . M - pointer to new `DM`
395667a56275SMatthew G Knepley 
395720f4b53cSBarry Smith   Level: intermediate
395820f4b53cSBarry Smith 
395967a56275SMatthew G Knepley   Notes:
3960bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3961bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3962bb7acecfSBarry Smith   of the input `DM`.
396367a56275SMatthew G Knepley 
39641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
396567a56275SMatthew G Knepley @*/
3966d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3967d71ae5a4SJacob Faibussowitsch {
396867a56275SMatthew G Knepley   DM        B;
396967a56275SMatthew G Knepley   char      convname[256];
3970c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
397167a56275SMatthew G Knepley 
397267a56275SMatthew G Knepley   PetscFunctionBegin;
397367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397467a56275SMatthew G Knepley   PetscValidType(dm, 1);
397567a56275SMatthew G Knepley   PetscValidPointer(M, 3);
39769566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39779566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3978c067b6caSMatthew G. Knepley   if (sametype) {
3979c067b6caSMatthew G. Knepley     *M = dm;
39809566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39813ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3982c067b6caSMatthew G. Knepley   } else {
39830298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
398467a56275SMatthew G Knepley 
398567a56275SMatthew G Knepley     /*
398667a56275SMatthew G Knepley        Order of precedence:
398767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
398867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
398967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
399067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
399167a56275SMatthew G Knepley        5) Use a really basic converter.
399267a56275SMatthew G Knepley     */
399367a56275SMatthew G Knepley 
399467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39959566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39969566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39979566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40009566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
400167a56275SMatthew G Knepley     if (conv) goto foundconv;
400267a56275SMatthew G Knepley 
400367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40049566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40059566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40069566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40079566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40089566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40099566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40109566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40119566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
401267a56275SMatthew G Knepley     if (conv) {
40139566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
401467a56275SMatthew G Knepley       goto foundconv;
401567a56275SMatthew G Knepley     }
401667a56275SMatthew G Knepley 
401767a56275SMatthew G Knepley #if 0
401867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
401967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40209566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
402167a56275SMatthew G Knepley     if (conv) goto foundconv;
402267a56275SMatthew G Knepley 
402367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
402467a56275SMatthew G Knepley     if (dm->ops->convert) {
402567a56275SMatthew G Knepley       conv = dm->ops->convert;
402667a56275SMatthew G Knepley     }
402767a56275SMatthew G Knepley     if (conv) goto foundconv;
402867a56275SMatthew G Knepley #endif
402967a56275SMatthew G Knepley 
403067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
403198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
403267a56275SMatthew G Knepley 
403367a56275SMatthew G Knepley   foundconv:
40349566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40359566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
403612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
403790b157c4SStefano Zampini     {
40384fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40396858538eSMatthew G. Knepley 
40404fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40414fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4042c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40439566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40449566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40459566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40469566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
404712fa691eSMatthew G. Knepley     }
40489566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
404967a56275SMatthew G Knepley   }
40509566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
405267a56275SMatthew G Knepley }
4053264ace61SBarry Smith 
4054264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4055264ace61SBarry Smith 
4056264ace61SBarry Smith /*@C
4057bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40581c84c290SBarry Smith 
40591c84c290SBarry Smith   Not Collective
40601c84c290SBarry Smith 
40611c84c290SBarry Smith   Input Parameters:
406220f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
406320f4b53cSBarry Smith - function - The creation routine itself
406420f4b53cSBarry Smith 
406520f4b53cSBarry Smith   Level: advanced
40661c84c290SBarry Smith 
40671c84c290SBarry Smith   Notes:
406820f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40691c84c290SBarry Smith 
40701c84c290SBarry Smith   Sample usage:
40711c84c290SBarry Smith .vb
4072bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40731c84c290SBarry Smith .ve
40741c84c290SBarry Smith 
407520f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40761c84c290SBarry Smith .vb
40771c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40781c84c290SBarry Smith     DMSetType(DM,"my_da");
40791c84c290SBarry Smith .ve
40801c84c290SBarry Smith    or at runtime via the option
40811c84c290SBarry Smith .vb
40821c84c290SBarry Smith     -da_type my_da
40831c84c290SBarry Smith .ve
4084264ace61SBarry Smith 
40851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4086264ace61SBarry Smith @*/
4087d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4088d71ae5a4SJacob Faibussowitsch {
4089264ace61SBarry Smith   PetscFunctionBegin;
40909566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40919566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4093264ace61SBarry Smith }
4094264ace61SBarry Smith 
4095b859378eSBarry Smith /*@C
4096bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4097b859378eSBarry Smith 
409820f4b53cSBarry Smith   Collective
4099b859378eSBarry Smith 
4100b859378eSBarry Smith   Input Parameters:
4101bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4102bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4103bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4104bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4105b859378eSBarry Smith 
4106b859378eSBarry Smith    Level: intermediate
4107b859378eSBarry Smith 
4108b859378eSBarry Smith   Notes:
410955849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4110b859378eSBarry Smith 
4111bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4112bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4113bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4114cd7e8a5eSksagiyam 
41151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4116b859378eSBarry Smith @*/
4117d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4118d71ae5a4SJacob Faibussowitsch {
41199331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4120b859378eSBarry Smith 
4121b859378eSBarry Smith   PetscFunctionBegin;
4122b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4123b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41249566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41259566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41269566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41279566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41289331c7a4SMatthew G. Knepley   if (isbinary) {
41299331c7a4SMatthew G. Knepley     PetscInt classid;
41309331c7a4SMatthew G. Knepley     char     type[256];
4131b859378eSBarry Smith 
41329566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41337a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41349566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41359566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4136dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41379331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4138dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41399331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41409566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4142b859378eSBarry Smith }
4143b859378eSBarry Smith 
41447da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41457da65231SMatthew G Knepley 
4146d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4147d71ae5a4SJacob Faibussowitsch {
41481d47ebbbSSatish Balay   PetscInt f;
41491b30c384SMatthew G Knepley 
41507da65231SMatthew G Knepley   PetscFunctionBegin;
415163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
415248a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41547da65231SMatthew G Knepley }
41557da65231SMatthew G Knepley 
4156d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4157d71ae5a4SJacob Faibussowitsch {
41581b30c384SMatthew G Knepley   PetscInt f, g;
41597da65231SMatthew G Knepley 
41607da65231SMatthew G Knepley   PetscFunctionBegin;
416163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41621d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41639566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
416448a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41659566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41667da65231SMatthew G Knepley   }
41673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41687da65231SMatthew G Knepley }
4169e7c4fc90SDmitry Karpeev 
4170d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4171d71ae5a4SJacob Faibussowitsch {
41720c5b8624SToby Isaac   PetscInt           localSize, bs;
41730c5b8624SToby Isaac   PetscMPIInt        size;
41740c5b8624SToby Isaac   Vec                x, xglob;
41750c5b8624SToby Isaac   const PetscScalar *xarray;
4176e759306cSMatthew G. Knepley 
4177e759306cSMatthew G. Knepley   PetscFunctionBegin;
41789566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41799566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41809566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
41819566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
41829566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
41830c5b8624SToby Isaac   if (size > 1) {
41849566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
41859566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
41869566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
41879566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
41880c5b8624SToby Isaac   } else {
41890c5b8624SToby Isaac     xglob = x;
41900c5b8624SToby Isaac   }
41919566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
41920c5b8624SToby Isaac   if (size > 1) {
41939566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
41949566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
41950c5b8624SToby Isaac   }
41969566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
41973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4198e759306cSMatthew G. Knepley }
4199e759306cSMatthew G. Knepley 
420088ed4aceSMatthew G Knepley /*@
4201bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4202061576a5SJed Brown 
4203061576a5SJed Brown   Input Parameter:
4204bb7acecfSBarry Smith . dm - The `DM`
4205061576a5SJed Brown 
4206061576a5SJed Brown   Output Parameter:
4207bb7acecfSBarry Smith . section - The `PetscSection`
4208061576a5SJed Brown 
420920f4b53cSBarry Smith   Options Database Key:
4210bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4211061576a5SJed Brown 
4212061576a5SJed Brown   Level: advanced
4213061576a5SJed Brown 
4214061576a5SJed Brown   Notes:
4215bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4216061576a5SJed Brown 
4217bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4218061576a5SJed Brown 
42191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4220061576a5SJed Brown @*/
4221d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4222d71ae5a4SJacob Faibussowitsch {
4223061576a5SJed Brown   PetscFunctionBegin;
42249566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4226061576a5SJed Brown }
4227061576a5SJed Brown 
4228061576a5SJed Brown /*@
4229bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
423088ed4aceSMatthew G Knepley 
423188ed4aceSMatthew G Knepley   Input Parameter:
4232bb7acecfSBarry Smith . dm - The `DM`
423388ed4aceSMatthew G Knepley 
423488ed4aceSMatthew G Knepley   Output Parameter:
4235bb7acecfSBarry Smith . section - The `PetscSection`
423688ed4aceSMatthew G Knepley 
423720f4b53cSBarry Smith   Options Database Key:
4238bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4239e5893cccSMatthew G. Knepley 
424088ed4aceSMatthew G Knepley   Level: intermediate
424188ed4aceSMatthew G Knepley 
4242bb7acecfSBarry Smith   Note:
4243bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
424488ed4aceSMatthew G Knepley 
42451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
424688ed4aceSMatthew G Knepley @*/
4247d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4248d71ae5a4SJacob Faibussowitsch {
424988ed4aceSMatthew G Knepley   PetscFunctionBegin;
425088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42521bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4253e5e52638SMatthew G. Knepley     PetscInt d;
4254e5e52638SMatthew G. Knepley 
425545480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
425645480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
425745480ffeSMatthew G. Knepley       PetscViewer       viewer;
425845480ffeSMatthew G. Knepley       PetscViewerFormat format;
425945480ffeSMatthew G. Knepley       PetscBool         flg;
426045480ffeSMatthew G. Knepley 
42619566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42629566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
426345480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42649566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42659566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
426645480ffeSMatthew G. Knepley       }
426745480ffeSMatthew G. Knepley       if (flg) {
42689566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42699566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42709566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
427145480ffeSMatthew G. Knepley       }
427245480ffeSMatthew G. Knepley     }
4273dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42749566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42752f0f8703SMatthew G. Knepley   }
42761bb6d2a8SBarry Smith   *section = dm->localSection;
42773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
427888ed4aceSMatthew G Knepley }
427988ed4aceSMatthew G Knepley 
428088ed4aceSMatthew G Knepley /*@
4281bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4282061576a5SJed Brown 
4283061576a5SJed Brown   Input Parameters:
4284bb7acecfSBarry Smith + dm - The `DM`
4285bb7acecfSBarry Smith - section - The `PetscSection`
4286061576a5SJed Brown 
4287061576a5SJed Brown   Level: advanced
4288061576a5SJed Brown 
4289061576a5SJed Brown   Notes:
4290bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4291061576a5SJed Brown 
4292bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4293061576a5SJed Brown 
42941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4295061576a5SJed Brown @*/
4296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4297d71ae5a4SJacob Faibussowitsch {
4298061576a5SJed Brown   PetscFunctionBegin;
42999566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4301061576a5SJed Brown }
4302061576a5SJed Brown 
4303061576a5SJed Brown /*@
4304bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
430588ed4aceSMatthew G Knepley 
430688ed4aceSMatthew G Knepley   Input Parameters:
4307bb7acecfSBarry Smith + dm - The `DM`
4308bb7acecfSBarry Smith - section - The `PetscSection`
430988ed4aceSMatthew G Knepley 
431088ed4aceSMatthew G Knepley   Level: intermediate
431188ed4aceSMatthew G Knepley 
4312bb7acecfSBarry Smith   Note:
4313bb7acecfSBarry Smith   Any existing Section will be destroyed
431488ed4aceSMatthew G Knepley 
43151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
431688ed4aceSMatthew G Knepley @*/
4317d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4318d71ae5a4SJacob Faibussowitsch {
4319c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4320af122d2aSMatthew G Knepley   PetscInt f;
432188ed4aceSMatthew G Knepley 
432288ed4aceSMatthew G Knepley   PetscFunctionBegin;
432388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4324b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43269566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43271bb6d2a8SBarry Smith   dm->localSection = section;
43289566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4329af122d2aSMatthew G Knepley   if (numFields) {
43309566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4331af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43320f21e855SMatthew G. Knepley       PetscObject disc;
4333af122d2aSMatthew G Knepley       const char *name;
4334af122d2aSMatthew G Knepley 
43359566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43369566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43379566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4338af122d2aSMatthew G Knepley     }
4339af122d2aSMatthew G Knepley   }
4340e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43419566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
43423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
434388ed4aceSMatthew G Knepley }
434488ed4aceSMatthew G Knepley 
43459435951eSToby Isaac /*@
4346bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43479435951eSToby Isaac 
434820f4b53cSBarry Smith   not Collective
4349e228b242SToby Isaac 
43509435951eSToby Isaac   Input Parameter:
4351bb7acecfSBarry Smith . dm - The `DM`
43529435951eSToby Isaac 
4353d8d19677SJose E. Roman   Output Parameters:
435420f4b53cSBarry 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.
435520f4b53cSBarry 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.
435679769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43579435951eSToby Isaac 
43589435951eSToby Isaac   Level: advanced
43599435951eSToby Isaac 
4360bb7acecfSBarry Smith   Note:
4361bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43629435951eSToby Isaac 
43631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
43649435951eSToby Isaac @*/
4365d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4366d71ae5a4SJacob Faibussowitsch {
43679435951eSToby Isaac   PetscFunctionBegin;
43689435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4369dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
43703b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43713b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
437279769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43749435951eSToby Isaac }
43759435951eSToby Isaac 
43769435951eSToby Isaac /*@
4377bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
43789435951eSToby Isaac 
437920f4b53cSBarry Smith   Collective
4380e228b242SToby Isaac 
43819435951eSToby Isaac   Input Parameters:
4382bb7acecfSBarry Smith + dm - The `DM`
4383bb7acecfSBarry 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).
438420f4b53cSBarry 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).
438520f4b53cSBarry 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).
43869435951eSToby Isaac 
43879435951eSToby Isaac   Level: advanced
43889435951eSToby Isaac 
438920f4b53cSBarry Smith   Notes:
439020f4b53cSBarry 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()`.
439120f4b53cSBarry Smith 
439220f4b53cSBarry 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.
439320f4b53cSBarry Smith 
4394bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
43959435951eSToby Isaac 
43961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
43979435951eSToby Isaac @*/
4398d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4399d71ae5a4SJacob Faibussowitsch {
4400e228b242SToby Isaac   PetscMPIInt result;
44019435951eSToby Isaac 
44029435951eSToby Isaac   PetscFunctionBegin;
44039435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4404e228b242SToby Isaac   if (section) {
4405e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44069566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44077a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4408e228b242SToby Isaac   }
4409e228b242SToby Isaac   if (mat) {
4410e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44119566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44127a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4413e228b242SToby Isaac   }
441479769bd5SJed Brown   if (bias) {
441579769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44169566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
441779769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
441879769bd5SJed Brown   }
44199566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44209566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44213b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44229566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44239566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44243b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44269566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
442779769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44299435951eSToby Isaac }
44309435951eSToby Isaac 
4431497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4432507e4973SMatthew G. Knepley /*
4433bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4434507e4973SMatthew G. Knepley 
4435507e4973SMatthew G. Knepley   Input Parameters:
4436bb7acecfSBarry Smith + dm - The `DM`
4437bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4438bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4439507e4973SMatthew G. Knepley 
4440507e4973SMatthew G. Knepley   Level: intermediate
4441507e4973SMatthew G. Knepley 
44421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4443507e4973SMatthew G. Knepley */
4444d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4445d71ae5a4SJacob Faibussowitsch {
4446507e4973SMatthew G. Knepley   MPI_Comm        comm;
4447507e4973SMatthew G. Knepley   PetscLayout     layout;
4448507e4973SMatthew G. Knepley   const PetscInt *ranges;
4449507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4450507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4451507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4452507e4973SMatthew G. Knepley 
4453507e4973SMatthew G. Knepley   PetscFunctionBegin;
44549566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4455507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44589566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44599566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44609566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44619566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44629566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44639566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44649566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4465507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4466f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4467507e4973SMatthew G. Knepley 
44689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44699566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44709566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44719566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4474507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
44759371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
44769371c9d4SSatish 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));
44779371c9d4SSatish Balay       valid = PETSC_FALSE;
44789371c9d4SSatish Balay     }
44799371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
44809371c9d4SSatish 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));
44819371c9d4SSatish Balay       valid = PETSC_FALSE;
44829371c9d4SSatish Balay     }
4483507e4973SMatthew G. Knepley     if (gdof < 0) {
4484507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4485507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4486507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4487507e4973SMatthew G. Knepley 
44889566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4489507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
44909371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
44919371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
44929371c9d4SSatish Balay           valid = PETSC_FALSE;
44939371c9d4SSatish Balay           break;
44949371c9d4SSatish Balay         }
4495507e4973SMatthew G. Knepley       }
4496507e4973SMatthew G. Knepley     }
4497507e4973SMatthew G. Knepley   }
44989566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
44999566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45001c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4501507e4973SMatthew G. Knepley   if (!gvalid) {
45029566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4503507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4504507e4973SMatthew G. Knepley   }
45053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4506507e4973SMatthew G. Knepley }
4507f741bcd2SMatthew G. Knepley #endif
4508507e4973SMatthew G. Knepley 
45095f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45106725e60dSJed Brown {
45116725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45126725e60dSJed Brown   PetscFunctionBegin;
45136725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45146725e60dSJed Brown   PetscValidPointer(sf, 2);
45155f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45166725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45176725e60dSJed Brown   else *sf = dm->sf;
45183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45196725e60dSJed Brown }
45206725e60dSJed Brown 
452188ed4aceSMatthew G Knepley /*@
4522bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
452388ed4aceSMatthew G Knepley 
452420f4b53cSBarry Smith   Collective
45258b1ab98fSJed Brown 
452688ed4aceSMatthew G Knepley   Input Parameter:
4527bb7acecfSBarry Smith . dm - The `DM`
452888ed4aceSMatthew G Knepley 
452988ed4aceSMatthew G Knepley   Output Parameter:
4530bb7acecfSBarry Smith . section - The `PetscSection`
453188ed4aceSMatthew G Knepley 
453288ed4aceSMatthew G Knepley   Level: intermediate
453388ed4aceSMatthew G Knepley 
4534bb7acecfSBarry Smith   Note:
4535bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
453688ed4aceSMatthew G Knepley 
45371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
453888ed4aceSMatthew G Knepley @*/
4539d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4540d71ae5a4SJacob Faibussowitsch {
454188ed4aceSMatthew G Knepley   PetscFunctionBegin;
454288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
454388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45441bb6d2a8SBarry Smith   if (!dm->globalSection) {
4545fd59a867SMatthew G. Knepley     PetscSection s;
45466725e60dSJed Brown     PetscSF      sf;
4547fd59a867SMatthew G. Knepley 
45489566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45497a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45507a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45515f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45526725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45539566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45559566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
455688ed4aceSMatthew G Knepley   }
45571bb6d2a8SBarry Smith   *section = dm->globalSection;
45583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
455988ed4aceSMatthew G Knepley }
456088ed4aceSMatthew G Knepley 
4561b21d0597SMatthew G Knepley /*@
4562bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4563b21d0597SMatthew G Knepley 
4564b21d0597SMatthew G Knepley   Input Parameters:
4565bb7acecfSBarry Smith + dm - The `DM`
45662fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4567b21d0597SMatthew G Knepley 
4568b21d0597SMatthew G Knepley   Level: intermediate
4569b21d0597SMatthew G Knepley 
4570bb7acecfSBarry Smith   Note:
4571bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4572b21d0597SMatthew G Knepley 
45731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4574b21d0597SMatthew G Knepley @*/
4575d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4576d71ae5a4SJacob Faibussowitsch {
4577b21d0597SMatthew G Knepley   PetscFunctionBegin;
4578b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45795080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45809566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45819566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45821bb6d2a8SBarry Smith   dm->globalSection = section;
4583497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45849566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4585507e4973SMatthew G. Knepley #endif
45863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4587b21d0597SMatthew G Knepley }
4588b21d0597SMatthew G Knepley 
458988ed4aceSMatthew G Knepley /*@
4590bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4591bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
459288ed4aceSMatthew G Knepley 
459388ed4aceSMatthew G Knepley   Input Parameter:
4594bb7acecfSBarry Smith . dm - The `DM`
459588ed4aceSMatthew G Knepley 
459688ed4aceSMatthew G Knepley   Output Parameter:
4597bb7acecfSBarry Smith . sf - The `PetscSF`
459888ed4aceSMatthew G Knepley 
459988ed4aceSMatthew G Knepley   Level: intermediate
460088ed4aceSMatthew G Knepley 
4601bb7acecfSBarry Smith   Note:
4602bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
460388ed4aceSMatthew G Knepley 
46041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
460588ed4aceSMatthew G Knepley @*/
4606d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4607d71ae5a4SJacob Faibussowitsch {
460888ed4aceSMatthew G Knepley   PetscInt nroots;
460988ed4aceSMatthew G Knepley 
461088ed4aceSMatthew G Knepley   PetscFunctionBegin;
461188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
461288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
461348a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46149566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
461588ed4aceSMatthew G Knepley   if (nroots < 0) {
461688ed4aceSMatthew G Knepley     PetscSection section, gSection;
461788ed4aceSMatthew G Knepley 
46189566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
461931ea6d37SMatthew G Knepley     if (section) {
46209566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46219566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
462231ea6d37SMatthew G Knepley     } else {
46230298fd71SBarry Smith       *sf = NULL;
46243ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
462531ea6d37SMatthew G Knepley     }
462688ed4aceSMatthew G Knepley   }
46271bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
462988ed4aceSMatthew G Knepley }
463088ed4aceSMatthew G Knepley 
463188ed4aceSMatthew G Knepley /*@
4632bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
463388ed4aceSMatthew G Knepley 
463488ed4aceSMatthew G Knepley   Input Parameters:
4635bb7acecfSBarry Smith + dm - The `DM`
4636bb7acecfSBarry Smith - sf - The `PetscSF`
463788ed4aceSMatthew G Knepley 
463888ed4aceSMatthew G Knepley   Level: intermediate
463988ed4aceSMatthew G Knepley 
4640bb7acecfSBarry Smith   Note:
4641bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
464288ed4aceSMatthew G Knepley 
46431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
464488ed4aceSMatthew G Knepley @*/
4645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4646d71ae5a4SJacob Faibussowitsch {
464788ed4aceSMatthew G Knepley   PetscFunctionBegin;
464888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4649b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46509566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46519566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46521bb6d2a8SBarry Smith   dm->sectionSF = sf;
46533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
465488ed4aceSMatthew G Knepley }
465588ed4aceSMatthew G Knepley 
465688ed4aceSMatthew G Knepley /*@C
4657bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
465888ed4aceSMatthew G Knepley   describing the data layout.
465988ed4aceSMatthew G Knepley 
466088ed4aceSMatthew G Knepley   Input Parameters:
4661bb7acecfSBarry Smith + dm - The `DM`
4662bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4663bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
466488ed4aceSMatthew G Knepley 
46651bb6d2a8SBarry Smith   Level: developer
46661bb6d2a8SBarry Smith 
4667bb7acecfSBarry Smith   Note:
4668bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4669bb7acecfSBarry Smith 
4670bb7acecfSBarry Smith   Developer Note:
4671bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4672bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4673bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46741bb6d2a8SBarry Smith 
46751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
467688ed4aceSMatthew G Knepley @*/
4677d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4678d71ae5a4SJacob Faibussowitsch {
467988ed4aceSMatthew G Knepley   PetscFunctionBegin;
468088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46819566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
46823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
468388ed4aceSMatthew G Knepley }
4684af122d2aSMatthew G Knepley 
4685b21d0597SMatthew G Knepley /*@
4686bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4687bb7acecfSBarry Smith 
4688bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4689b21d0597SMatthew G Knepley 
4690b21d0597SMatthew G Knepley   Input Parameter:
4691bb7acecfSBarry Smith . dm - The `DM`
4692b21d0597SMatthew G Knepley 
4693b21d0597SMatthew G Knepley   Output Parameter:
4694bb7acecfSBarry Smith . sf - The `PetscSF`
4695b21d0597SMatthew G Knepley 
4696b21d0597SMatthew G Knepley   Level: intermediate
4697b21d0597SMatthew G Knepley 
4698bb7acecfSBarry Smith   Note:
4699bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4700b21d0597SMatthew G Knepley 
47011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4702b21d0597SMatthew G Knepley @*/
4703d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4704d71ae5a4SJacob Faibussowitsch {
4705b21d0597SMatthew G Knepley   PetscFunctionBegin;
4706b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4707b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4708b21d0597SMatthew G Knepley   *sf = dm->sf;
47093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4710b21d0597SMatthew G Knepley }
4711b21d0597SMatthew G Knepley 
4712057b4bcdSMatthew G Knepley /*@
4713bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4714bb7acecfSBarry Smith 
471520f4b53cSBarry Smith   Collective
4716057b4bcdSMatthew G Knepley 
4717057b4bcdSMatthew G Knepley   Input Parameters:
4718bb7acecfSBarry Smith + dm - The `DM`
4719bb7acecfSBarry Smith - sf - The `PetscSF`
4720057b4bcdSMatthew G Knepley 
4721057b4bcdSMatthew G Knepley   Level: intermediate
4722057b4bcdSMatthew G Knepley 
47231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4724057b4bcdSMatthew G Knepley @*/
4725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4726d71ae5a4SJacob Faibussowitsch {
4727057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4728057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4729b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47309566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47319566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4732057b4bcdSMatthew G Knepley   dm->sf = sf;
47333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4734057b4bcdSMatthew G Knepley }
4735057b4bcdSMatthew G Knepley 
47364f37162bSMatthew G. Knepley /*@
4737bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47384f37162bSMatthew G. Knepley 
47394f37162bSMatthew G. Knepley   Input Parameter:
4740bb7acecfSBarry Smith . dm - The `DM`
47414f37162bSMatthew G. Knepley 
47424f37162bSMatthew G. Knepley   Output Parameter:
4743bb7acecfSBarry Smith . sf - The `PetscSF`
47444f37162bSMatthew G. Knepley 
47454f37162bSMatthew G. Knepley   Level: intermediate
47464f37162bSMatthew G. Knepley 
4747bb7acecfSBarry Smith   Note:
4748bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47494f37162bSMatthew G. Knepley 
47501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47514f37162bSMatthew G. Knepley @*/
4752d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4753d71ae5a4SJacob Faibussowitsch {
47544f37162bSMatthew G. Knepley   PetscFunctionBegin;
47554f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47564f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47574f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47594f37162bSMatthew G. Knepley }
47604f37162bSMatthew G. Knepley 
47614f37162bSMatthew G. Knepley /*@
47624f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47634f37162bSMatthew G. Knepley 
47644f37162bSMatthew G. Knepley   Input Parameters:
47654f37162bSMatthew G. Knepley + dm - The DM
47664f37162bSMatthew G. Knepley - sf - The PetscSF
47674f37162bSMatthew G. Knepley 
47684f37162bSMatthew G. Knepley   Level: intermediate
47694f37162bSMatthew G. Knepley 
47701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47714f37162bSMatthew G. Knepley @*/
4772d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4773d71ae5a4SJacob Faibussowitsch {
47744f37162bSMatthew G. Knepley   PetscFunctionBegin;
47754f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47764f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47789566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47794f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47814f37162bSMatthew G. Knepley }
47824f37162bSMatthew G. Knepley 
4783d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4784d71ae5a4SJacob Faibussowitsch {
478534aa8a36SMatthew G. Knepley   PetscClassId id;
478634aa8a36SMatthew G. Knepley 
478734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47889566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
478934aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47909566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
479134aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47929566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
479317c1d62eSMatthew G. Knepley   } else {
47949566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
479534aa8a36SMatthew G. Knepley   }
47963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
479734aa8a36SMatthew G. Knepley }
479834aa8a36SMatthew G. Knepley 
4799d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4800d71ae5a4SJacob Faibussowitsch {
480144a7f3ddSMatthew G. Knepley   RegionField *tmpr;
480244a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
480344a7f3ddSMatthew G. Knepley 
480444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48053ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48069566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
480744a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48089371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48099371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48109371c9d4SSatish Balay     tmpr[f].label       = NULL;
48119371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48129371c9d4SSatish Balay   }
48139566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
481444a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
481544a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
481744a7f3ddSMatthew G. Knepley }
481844a7f3ddSMatthew G. Knepley 
481944a7f3ddSMatthew G. Knepley /*@
482020f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
482144a7f3ddSMatthew G. Knepley 
482220f4b53cSBarry Smith   Logically Collective
482344a7f3ddSMatthew G. Knepley 
482444a7f3ddSMatthew G. Knepley   Input Parameter:
482520f4b53cSBarry Smith . dm - The `DM`
482644a7f3ddSMatthew G. Knepley 
482744a7f3ddSMatthew G. Knepley   Level: intermediate
482844a7f3ddSMatthew G. Knepley 
48291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
483044a7f3ddSMatthew G. Knepley @*/
4831d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4832d71ae5a4SJacob Faibussowitsch {
483344a7f3ddSMatthew G. Knepley   PetscInt f;
483444a7f3ddSMatthew G. Knepley 
483544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
483644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483744a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48389566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48399566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
484044a7f3ddSMatthew G. Knepley   }
48419566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
484244a7f3ddSMatthew G. Knepley   dm->fields = NULL;
484344a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
484544a7f3ddSMatthew G. Knepley }
484644a7f3ddSMatthew G. Knepley 
4847689b5837SMatthew G. Knepley /*@
484820f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4849689b5837SMatthew G. Knepley 
485020f4b53cSBarry Smith   Not Collective
4851689b5837SMatthew G. Knepley 
4852689b5837SMatthew G. Knepley   Input Parameter:
485320f4b53cSBarry Smith . dm - The `DM`
4854689b5837SMatthew G. Knepley 
4855689b5837SMatthew G. Knepley   Output Parameter:
4856689b5837SMatthew G. Knepley . Nf - The number of fields
4857689b5837SMatthew G. Knepley 
4858689b5837SMatthew G. Knepley   Level: intermediate
4859689b5837SMatthew G. Knepley 
48601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4861689b5837SMatthew G. Knepley @*/
4862d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4863d71ae5a4SJacob Faibussowitsch {
48640f21e855SMatthew G. Knepley   PetscFunctionBegin;
48650f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4866534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
486744a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
48683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4869af122d2aSMatthew G Knepley }
4870af122d2aSMatthew G Knepley 
4871689b5837SMatthew G. Knepley /*@
487220f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4873689b5837SMatthew G. Knepley 
487420f4b53cSBarry Smith   Logically Collective
4875689b5837SMatthew G. Knepley 
4876689b5837SMatthew G. Knepley   Input Parameters:
487720f4b53cSBarry Smith + dm - The `DM`
4878689b5837SMatthew G. Knepley - Nf - The number of fields
4879689b5837SMatthew G. Knepley 
4880689b5837SMatthew G. Knepley   Level: intermediate
4881689b5837SMatthew G. Knepley 
48821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4883689b5837SMatthew G. Knepley @*/
4884d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4885d71ae5a4SJacob Faibussowitsch {
48860f21e855SMatthew G. Knepley   PetscInt Nf, f;
4887af122d2aSMatthew G Knepley 
4888af122d2aSMatthew G Knepley   PetscFunctionBegin;
4889af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48909566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48910f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48920f21e855SMatthew G. Knepley     PetscContainer obj;
48930f21e855SMatthew G. Knepley 
48949566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
48959566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
48969566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4897af122d2aSMatthew G Knepley   }
48983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4899af122d2aSMatthew G Knepley }
4900af122d2aSMatthew G Knepley 
4901c1929be8SMatthew G. Knepley /*@
4902bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4903c1929be8SMatthew G. Knepley 
490420f4b53cSBarry Smith   Not Collective
4905c1929be8SMatthew G. Knepley 
4906c1929be8SMatthew G. Knepley   Input Parameters:
4907bb7acecfSBarry Smith + dm - The `DM`
4908c1929be8SMatthew G. Knepley - f  - The field number
4909c1929be8SMatthew G. Knepley 
491044a7f3ddSMatthew G. Knepley   Output Parameters:
491120f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
491220f4b53cSBarry Smith - disc - The discretization object (pass in `NULL` if not needed)
4913c1929be8SMatthew G. Knepley 
491444a7f3ddSMatthew G. Knepley   Level: intermediate
4915c1929be8SMatthew G. Knepley 
49161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4917c1929be8SMatthew G. Knepley @*/
4918d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4919d71ae5a4SJacob Faibussowitsch {
4920af122d2aSMatthew G Knepley   PetscFunctionBegin;
4921af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4922bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
49237a8be351SBarry 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);
492444a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4925bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4927decb47aaSMatthew G. Knepley }
4928decb47aaSMatthew G. Knepley 
4929083401c6SMatthew G. Knepley /* Does not clear the DS */
4930d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4931d71ae5a4SJacob Faibussowitsch {
4932083401c6SMatthew G. Knepley   PetscFunctionBegin;
49339566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49349566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49359566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4936083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4937bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4939bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4941083401c6SMatthew G. Knepley }
4942083401c6SMatthew G. Knepley 
494346560f82SMatthew G. Knepley /*@C
4944bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4945bb7acecfSBarry Smith   the field numbering.
4946c1929be8SMatthew G. Knepley 
494720f4b53cSBarry Smith   Logically Collective
4948c1929be8SMatthew G. Knepley 
4949c1929be8SMatthew G. Knepley   Input Parameters:
4950bb7acecfSBarry Smith + dm    - The `DM`
4951c1929be8SMatthew G. Knepley . f     - The field number
495220f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4953bb7acecfSBarry Smith - disc - The discretization object
4954c1929be8SMatthew G. Knepley 
495544a7f3ddSMatthew G. Knepley   Level: intermediate
4956c1929be8SMatthew G. Knepley 
49571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
4958c1929be8SMatthew G. Knepley @*/
4959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4960d71ae5a4SJacob Faibussowitsch {
4961decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4962decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4963e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4964bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49657a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4966bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4967bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49689566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
49693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
497044a7f3ddSMatthew G. Knepley }
497144a7f3ddSMatthew G. Knepley 
497246560f82SMatthew G. Knepley /*@C
4973bb7acecfSBarry 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)
4974bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
497544a7f3ddSMatthew G. Knepley 
497620f4b53cSBarry Smith   Logically Collective
497744a7f3ddSMatthew G. Knepley 
497844a7f3ddSMatthew G. Knepley   Input Parameters:
4979bb7acecfSBarry Smith + dm    - The `DM`
498020f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4981bb7acecfSBarry Smith - disc - The discretization object
498244a7f3ddSMatthew G. Knepley 
498344a7f3ddSMatthew G. Knepley   Level: intermediate
498444a7f3ddSMatthew G. Knepley 
4985bb7acecfSBarry Smith   Notes:
4986bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4987bb7acecfSBarry Smith 
4988da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4989bb7acecfSBarry 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
4990bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4991bb7acecfSBarry Smith 
49921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
499344a7f3ddSMatthew G. Knepley @*/
4994d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
4995d71ae5a4SJacob Faibussowitsch {
499644a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
499744a7f3ddSMatthew G. Knepley 
499844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
499944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5000064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5001bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50029566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
500344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5004bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50059566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5006bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5007bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50089566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5010af122d2aSMatthew G Knepley }
50116636e97aSMatthew G Knepley 
5012e5e52638SMatthew G. Knepley /*@
5013e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5014e0b68406SMatthew Knepley 
501520f4b53cSBarry Smith   Logically Collective
5016e0b68406SMatthew Knepley 
5017e0b68406SMatthew Knepley   Input Parameters:
5018bb7acecfSBarry Smith + dm          - The `DM`
5019e0b68406SMatthew Knepley . f           - The field index
5020bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5021e0b68406SMatthew Knepley 
5022e0b68406SMatthew Knepley   Level: intermediate
5023e0b68406SMatthew Knepley 
50241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5025e0b68406SMatthew Knepley @*/
5026d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5027d71ae5a4SJacob Faibussowitsch {
5028e0b68406SMatthew Knepley   PetscFunctionBegin;
502963a3b9bcSJacob 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);
5030e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5032e0b68406SMatthew Knepley }
5033e0b68406SMatthew Knepley 
5034e0b68406SMatthew Knepley /*@
5035e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5036e0b68406SMatthew Knepley 
503720f4b53cSBarry Smith   Not Collective
5038e0b68406SMatthew Knepley 
5039e0b68406SMatthew Knepley   Input Parameters:
5040bb7acecfSBarry Smith + dm          - The `DM`
5041e0b68406SMatthew Knepley - f           - The field index
5042e0b68406SMatthew Knepley 
5043e0b68406SMatthew Knepley   Output Parameter:
5044e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5045e0b68406SMatthew Knepley 
5046e0b68406SMatthew Knepley   Level: intermediate
5047e0b68406SMatthew Knepley 
50481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5049e0b68406SMatthew Knepley @*/
5050d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5051d71ae5a4SJacob Faibussowitsch {
5052e0b68406SMatthew Knepley   PetscFunctionBegin;
505363a3b9bcSJacob 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);
5054e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
50553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5056e0b68406SMatthew Knepley }
5057e0b68406SMatthew Knepley 
5058e0b68406SMatthew Knepley /*@
5059bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5060e5e52638SMatthew G. Knepley 
506120f4b53cSBarry Smith   Collective
5062e5e52638SMatthew G. Knepley 
5063e5e52638SMatthew G. Knepley   Input Parameter:
5064bb7acecfSBarry Smith . dm - The `DM`
5065e5e52638SMatthew G. Knepley 
5066e5e52638SMatthew G. Knepley   Output Parameter:
5067bb7acecfSBarry Smith . newdm - The `DM`
5068e5e52638SMatthew G. Knepley 
5069e5e52638SMatthew G. Knepley   Level: advanced
5070e5e52638SMatthew G. Knepley 
50711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5072e5e52638SMatthew G. Knepley @*/
5073d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5074d71ae5a4SJacob Faibussowitsch {
5075e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5076e5e52638SMatthew G. Knepley 
5077e5e52638SMatthew G. Knepley   PetscFunctionBegin;
50783ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
50799566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50809566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5081e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5082e5e52638SMatthew G. Knepley     DMLabel     label;
5083e5e52638SMatthew G. Knepley     PetscObject field;
508434aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5085e5e52638SMatthew G. Knepley 
50869566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50879566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50889566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50899566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
509034aa8a36SMatthew G. Knepley   }
50913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
509234aa8a36SMatthew G. Knepley }
509334aa8a36SMatthew G. Knepley 
509434aa8a36SMatthew G. Knepley /*@
509534aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
509634aa8a36SMatthew G. Knepley 
509720f4b53cSBarry Smith   Not Collective
509834aa8a36SMatthew G. Knepley 
509934aa8a36SMatthew G. Knepley   Input Parameters:
510020f4b53cSBarry Smith + dm - The `DM` object
510120f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
510234aa8a36SMatthew G. Knepley 
5103d8d19677SJose E. Roman   Output Parameters:
510434aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
510534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
510634aa8a36SMatthew G. Knepley 
510734aa8a36SMatthew G. Knepley   Level: developer
510834aa8a36SMatthew G. Knepley 
510920f4b53cSBarry Smith   Notes:
511020f4b53cSBarry Smith .vb
511120f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
511220f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
511320f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
511420f4b53cSBarry Smith .ve
511520f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
511620f4b53cSBarry Smith 
51171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
511834aa8a36SMatthew G. Knepley @*/
5119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5120d71ae5a4SJacob Faibussowitsch {
512134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
512234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5123534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
5124534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
512534aa8a36SMatthew G. Knepley   if (f < 0) {
512634aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
512734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
512834aa8a36SMatthew G. Knepley   } else {
512934aa8a36SMatthew G. Knepley     PetscInt Nf;
513034aa8a36SMatthew G. Knepley 
51319566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51327a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
513334aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
513434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
513534aa8a36SMatthew G. Knepley   }
51363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
513734aa8a36SMatthew G. Knepley }
513834aa8a36SMatthew G. Knepley 
513934aa8a36SMatthew G. Knepley /*@
514034aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
514134aa8a36SMatthew G. Knepley 
514220f4b53cSBarry Smith   Not Collective
514334aa8a36SMatthew G. Knepley 
514434aa8a36SMatthew G. Knepley   Input Parameters:
514520f4b53cSBarry Smith + dm         - The `DM` object
514634aa8a36SMatthew G. Knepley . f          - The field number
514734aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
514834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
514934aa8a36SMatthew G. Knepley 
515034aa8a36SMatthew G. Knepley   Level: developer
515134aa8a36SMatthew G. Knepley 
515220f4b53cSBarry Smith   Notes:
515320f4b53cSBarry Smith .vb
515420f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
515520f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
515620f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
515720f4b53cSBarry Smith .ve
515820f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
515920f4b53cSBarry Smith 
51601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
516134aa8a36SMatthew G. Knepley @*/
5162d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5163d71ae5a4SJacob Faibussowitsch {
516434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
516534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
516634aa8a36SMatthew G. Knepley   if (f < 0) {
516734aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
516834aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
516934aa8a36SMatthew G. Knepley   } else {
517034aa8a36SMatthew G. Knepley     PetscInt Nf;
517134aa8a36SMatthew G. Knepley 
51729566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51737a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
517434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
517534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5176e5e52638SMatthew G. Knepley   }
51773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5178e5e52638SMatthew G. Knepley }
5179e5e52638SMatthew G. Knepley 
5180b0441da4SMatthew G. Knepley /*@
5181b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5182b0441da4SMatthew G. Knepley 
5183b0441da4SMatthew G. Knepley   Not collective
5184b0441da4SMatthew G. Knepley 
5185f899ff85SJose E. Roman   Input Parameter:
518620f4b53cSBarry Smith . dm - The `DM` object
5187b0441da4SMatthew G. Knepley 
5188d8d19677SJose E. Roman   Output Parameters:
5189b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5190b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5191b0441da4SMatthew G. Knepley 
5192b0441da4SMatthew G. Knepley   Level: developer
5193b0441da4SMatthew G. Knepley 
519420f4b53cSBarry Smith   Notes:
519520f4b53cSBarry Smith .vb
519620f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
519720f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
519820f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
519920f4b53cSBarry Smith .ve
520020f4b53cSBarry Smith 
52011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5202b0441da4SMatthew G. Knepley @*/
5203d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5204d71ae5a4SJacob Faibussowitsch {
5205b0441da4SMatthew G. Knepley   PetscInt Nf;
5206b0441da4SMatthew G. Knepley 
5207b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5208b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5209064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5210064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
52119566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5212b0441da4SMatthew G. Knepley   if (!Nf) {
52139566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5214b0441da4SMatthew G. Knepley   } else {
52159566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5216b0441da4SMatthew G. Knepley   }
52173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5218b0441da4SMatthew G. Knepley }
5219b0441da4SMatthew G. Knepley 
5220b0441da4SMatthew G. Knepley /*@
5221b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5222b0441da4SMatthew G. Knepley 
522320f4b53cSBarry Smith   Not Collective
5224b0441da4SMatthew G. Knepley 
5225b0441da4SMatthew G. Knepley   Input Parameters:
522620f4b53cSBarry Smith + dm         - The `DM` object
5227b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5228b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5229b0441da4SMatthew G. Knepley 
5230b0441da4SMatthew G. Knepley   Level: developer
5231b0441da4SMatthew G. Knepley 
523220f4b53cSBarry Smith   Notes:
523320f4b53cSBarry Smith .vb
523420f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
523520f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
523620f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
523720f4b53cSBarry Smith .ve
523820f4b53cSBarry Smith 
52391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5240b0441da4SMatthew G. Knepley @*/
5241d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5242d71ae5a4SJacob Faibussowitsch {
5243b0441da4SMatthew G. Knepley   PetscInt Nf;
5244b0441da4SMatthew G. Knepley 
5245b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5246b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52479566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5248b0441da4SMatthew G. Knepley   if (!Nf) {
52499566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5250b0441da4SMatthew G. Knepley   } else {
52519566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5252e5e52638SMatthew G. Knepley   }
52533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5254e5e52638SMatthew G. Knepley }
5255e5e52638SMatthew G. Knepley 
5256d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5257d71ae5a4SJacob Faibussowitsch {
5258799db056SMatthew G. Knepley   DM           plex;
5259799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5260799db056SMatthew G. Knepley   const char **names;
5261799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5262799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5263799db056SMatthew G. Knepley   size_t       len;
5264799db056SMatthew G. Knepley   MPI_Comm     comm;
5265799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5266783e2ec8SMatthew G. Knepley 
5267783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5268799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5269799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5270799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5271799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5272799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5273799db056SMatthew G. Knepley     PetscDS  dsBC;
5274799db056SMatthew G. Knepley     PetscInt numBd;
5275799db056SMatthew G. Knepley 
527607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5277799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5278799db056SMatthew G. Knepley     maxLabels += numBd;
5279799db056SMatthew G. Knepley   }
5280799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5281799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5282799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5283799db056SMatthew G. Knepley     PetscDS  dsBC;
5284799db056SMatthew G. Knepley     PetscInt numBd, bd;
5285799db056SMatthew G. Knepley 
528607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5287799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5288799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5289799db056SMatthew G. Knepley       DMLabel      label;
5290799db056SMatthew G. Knepley       PetscInt     field;
5291799db056SMatthew G. Knepley       PetscObject  obj;
5292799db056SMatthew G. Knepley       PetscClassId id;
5293799db056SMatthew G. Knepley 
5294799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52959566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52969566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5297799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
52989371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
52999371c9d4SSatish Balay         if (labels[l] == label) break;
5300799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5301783e2ec8SMatthew G. Knepley     }
5302799db056SMatthew G. Knepley   }
5303799db056SMatthew G. Knepley   /* Get label names */
5304799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5305799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53069371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53079371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53089371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53099371c9d4SSatish Balay   }
5310799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5311712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5312799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5313c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5314799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5315799db056SMatthew G. Knepley   /* Put all names on all processes */
5316799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5317799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5318799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5319799db056SMatthew G. Knepley   gNl = displs[size];
53209371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53219371c9d4SSatish Balay     counts[p] *= gmaxLen;
53229371c9d4SSatish Balay     displs[p] *= gmaxLen;
53239371c9d4SSatish Balay   }
5324799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5325799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5326799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5327799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5328799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5329799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5330799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53319371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53329371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53339566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5334799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53359566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5336799db056SMatthew G. Knepley     ++gl;
5337783e2ec8SMatthew G. Knepley   }
5338799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5340783e2ec8SMatthew G. Knepley }
5341783e2ec8SMatthew G. Knepley 
5342d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5343d71ae5a4SJacob Faibussowitsch {
5344e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5345e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5346e5e52638SMatthew G. Knepley 
5347e5e52638SMatthew G. Knepley   PetscFunctionBegin;
53483ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
53499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5350e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53519371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53529371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53539371c9d4SSatish Balay     tmpd[s].label  = NULL;
53549371c9d4SSatish Balay     tmpd[s].fields = NULL;
53559371c9d4SSatish Balay   }
53569566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5357e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5358e5e52638SMatthew G. Knepley   dm->probs = tmpd;
53593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5360e5e52638SMatthew G. Knepley }
5361e5e52638SMatthew G. Knepley 
5362e5e52638SMatthew G. Knepley /*@
536320f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5364e5e52638SMatthew G. Knepley 
536520f4b53cSBarry Smith   Not Collective
5366e5e52638SMatthew G. Knepley 
5367e5e52638SMatthew G. Knepley   Input Parameter:
536820f4b53cSBarry Smith . dm - The `DM`
5369e5e52638SMatthew G. Knepley 
5370e5e52638SMatthew G. Knepley   Output Parameter:
537120f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5372e5e52638SMatthew G. Knepley 
5373e5e52638SMatthew G. Knepley   Level: intermediate
5374e5e52638SMatthew G. Knepley 
53751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5376e5e52638SMatthew G. Knepley @*/
5377d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5378d71ae5a4SJacob Faibussowitsch {
5379e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5380e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5381534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5382e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
53833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5384e5e52638SMatthew G. Knepley }
5385e5e52638SMatthew G. Knepley 
5386e5e52638SMatthew G. Knepley /*@
538720f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5388e5e52638SMatthew G. Knepley 
538920f4b53cSBarry Smith   Logically Collective
5390e5e52638SMatthew G. Knepley 
5391e5e52638SMatthew G. Knepley   Input Parameter:
539220f4b53cSBarry Smith . dm - The `DM`
5393e5e52638SMatthew G. Knepley 
5394e5e52638SMatthew G. Knepley   Level: intermediate
5395e5e52638SMatthew G. Knepley 
53961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5397e5e52638SMatthew G. Knepley @*/
5398d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5399d71ae5a4SJacob Faibussowitsch {
5400e5e52638SMatthew G. Knepley   PetscInt s;
5401e5e52638SMatthew G. Knepley 
5402e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5403e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5404e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54059566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
540607218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54079566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54089566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5409e5e52638SMatthew G. Knepley   }
54109566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5411e5e52638SMatthew G. Knepley   dm->probs = NULL;
5412e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5414e5e52638SMatthew G. Knepley }
5415e5e52638SMatthew G. Knepley 
5416e5e52638SMatthew G. Knepley /*@
541720f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5418e5e52638SMatthew G. Knepley 
541920f4b53cSBarry Smith   Not Collective
5420e5e52638SMatthew G. Knepley 
5421e5e52638SMatthew G. Knepley   Input Parameter:
542220f4b53cSBarry Smith . dm - The `DM`
5423e5e52638SMatthew G. Knepley 
5424e5e52638SMatthew G. Knepley   Output Parameter:
542507218a29SMatthew G. Knepley . ds - The default `PetscDS`
5426e5e52638SMatthew G. Knepley 
5427e5e52638SMatthew G. Knepley   Level: intermediate
5428e5e52638SMatthew G. Knepley 
54291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5430e5e52638SMatthew G. Knepley @*/
543107218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5432d71ae5a4SJacob Faibussowitsch {
5433e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5434e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
543507218a29SMatthew G. Knepley   PetscValidPointer(ds, 2);
543607218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
543707218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
54383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5439e5e52638SMatthew G. Knepley }
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley /*@
544220f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5443e5e52638SMatthew G. Knepley 
544420f4b53cSBarry Smith   Not Collective
5445e5e52638SMatthew G. Knepley 
5446e5e52638SMatthew G. Knepley   Input Parameters:
544720f4b53cSBarry Smith + dm    - The `DM`
544820f4b53cSBarry Smith - point - Cell for the `PetscDS`
5449e5e52638SMatthew G. Knepley 
545007218a29SMatthew G. Knepley   Output Parameters:
545107218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
545207218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5453e5e52638SMatthew G. Knepley 
5454e5e52638SMatthew G. Knepley   Level: developer
5455e5e52638SMatthew G. Knepley 
54561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5457e5e52638SMatthew G. Knepley @*/
545807218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5459d71ae5a4SJacob Faibussowitsch {
546007218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5461e5e52638SMatthew G. Knepley   PetscInt s;
5462e5e52638SMatthew G. Knepley 
5463e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5464e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
546507218a29SMatthew G. Knepley   if (ds) PetscValidPointer(ds, 3);
546607218a29SMatthew G. Knepley   if (dsIn) PetscValidPointer(dsIn, 4);
546763a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
546807218a29SMatthew G. Knepley   if (ds) *ds = NULL;
546907218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5470e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5471e5e52638SMatthew G. Knepley     PetscInt val;
5472e5e52638SMatthew G. Knepley 
54739371c9d4SSatish Balay     if (!dm->probs[s].label) {
547407218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
54759371c9d4SSatish Balay     } else {
54769566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
54779371c9d4SSatish Balay       if (val >= 0) {
547807218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
547907218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
54809371c9d4SSatish Balay         break;
54819371c9d4SSatish Balay       }
5482e5e52638SMatthew G. Knepley     }
5483e5e52638SMatthew G. Knepley   }
548407218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
54853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5486e5e52638SMatthew G. Knepley }
5487e5e52638SMatthew G. Knepley 
5488e5e52638SMatthew G. Knepley /*@
548920f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5490e5e52638SMatthew G. Knepley 
549120f4b53cSBarry Smith   Not Collective
5492e5e52638SMatthew G. Knepley 
5493e5e52638SMatthew G. Knepley   Input Parameters:
549420f4b53cSBarry Smith + dm    - The `DM`
549520f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5496e5e52638SMatthew G. Knepley 
5497b3cf3223SMatthew G. Knepley   Output Parameters:
549820f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
549907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
550007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley   Level: advanced
5503e5e52638SMatthew G. Knepley 
550420f4b53cSBarry Smith   Note:
550520f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
550620f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
550707218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
550820f4b53cSBarry Smith 
55091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5510e5e52638SMatthew G. Knepley @*/
551107218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5512d71ae5a4SJacob Faibussowitsch {
5513e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5514e5e52638SMatthew G. Knepley 
5515e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5516e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5517e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55189371c9d4SSatish Balay   if (fields) {
55199371c9d4SSatish Balay     PetscValidPointer(fields, 3);
55209371c9d4SSatish Balay     *fields = NULL;
55219371c9d4SSatish Balay   }
55229371c9d4SSatish Balay   if (ds) {
55239371c9d4SSatish Balay     PetscValidPointer(ds, 4);
55249371c9d4SSatish Balay     *ds = NULL;
55259371c9d4SSatish Balay   }
552607218a29SMatthew G. Knepley   if (dsIn) {
552707218a29SMatthew G. Knepley     PetscValidPointer(dsIn, 5);
552807218a29SMatthew G. Knepley     *dsIn = NULL;
552907218a29SMatthew G. Knepley   }
5530e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5531154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5532b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5533b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
553407218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55353ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5536b3cf3223SMatthew G. Knepley     }
5537e5e52638SMatthew G. Knepley   }
55383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5539e5e52638SMatthew G. Knepley }
5540e5e52638SMatthew G. Knepley 
5541e5e52638SMatthew G. Knepley /*@
5542bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5543083401c6SMatthew G. Knepley 
554420f4b53cSBarry Smith   Collective
5545083401c6SMatthew G. Knepley 
5546083401c6SMatthew G. Knepley   Input Parameters:
5547bb7acecfSBarry Smith + dm     - The `DM`
554820f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
554907218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
555007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
555107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5552083401c6SMatthew G. Knepley 
555320f4b53cSBarry Smith   Level: advanced
555420f4b53cSBarry Smith 
5555bb7acecfSBarry Smith   Note:
5556bb7acecfSBarry 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,
5557083401c6SMatthew G. Knepley   the fields argument is ignored.
5558083401c6SMatthew G. Knepley 
55591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5560083401c6SMatthew G. Knepley @*/
556107218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5562d71ae5a4SJacob Faibussowitsch {
5563083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5564083401c6SMatthew G. Knepley 
5565083401c6SMatthew G. Knepley   PetscFunctionBegin;
5566083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5567083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
556807218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5569064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
557007218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5571083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5572083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55739566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
557407218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5575083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
557607218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
55773ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5578083401c6SMatthew G. Knepley     }
5579083401c6SMatthew G. Knepley   }
55809566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
55819566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
55829566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
55839566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
558407218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5585083401c6SMatthew G. Knepley   if (!label) {
5586083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5587083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5588083401c6SMatthew G. Knepley     Nds = 0;
5589083401c6SMatthew G. Knepley   }
5590083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5591083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5592083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
559307218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
55943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5595083401c6SMatthew G. Knepley }
5596083401c6SMatthew G. Knepley 
5597083401c6SMatthew G. Knepley /*@
559820f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5599e5e52638SMatthew G. Knepley 
560020f4b53cSBarry Smith   Not Collective
5601e5e52638SMatthew G. Knepley 
5602e5e52638SMatthew G. Knepley   Input Parameters:
560320f4b53cSBarry Smith + dm  - The `DM`
5604e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5605e5e52638SMatthew G. Knepley 
5606e5e52638SMatthew G. Knepley   Output Parameters:
560720f4b53cSBarry Smith + label  - The region label, or `NULL`
560820f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
560907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
561007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5611e5e52638SMatthew G. Knepley 
5612e5e52638SMatthew G. Knepley   Level: advanced
5613e5e52638SMatthew G. Knepley 
56141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5615e5e52638SMatthew G. Knepley @*/
561607218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5617d71ae5a4SJacob Faibussowitsch {
5618e5e52638SMatthew G. Knepley   PetscInt Nds;
5619e5e52638SMatthew G. Knepley 
5620e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5621e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56229566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
562363a3b9bcSJacob 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);
5624e5e52638SMatthew G. Knepley   if (label) {
5625e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5626e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5627e5e52638SMatthew G. Knepley   }
5628b3cf3223SMatthew G. Knepley   if (fields) {
5629b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5630b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5631b3cf3223SMatthew G. Knepley   }
5632e5e52638SMatthew G. Knepley   if (ds) {
5633b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5634e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5635e5e52638SMatthew G. Knepley   }
563607218a29SMatthew G. Knepley   if (dsIn) {
563707218a29SMatthew G. Knepley     PetscValidPointer(dsIn, 6);
563807218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
563907218a29SMatthew G. Knepley   }
56403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5641e5e52638SMatthew G. Knepley }
5642e5e52638SMatthew G. Knepley 
5643e5e52638SMatthew G. Knepley /*@
564420f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5645e5e52638SMatthew G. Knepley 
564620f4b53cSBarry Smith   Not Collective
5647e5e52638SMatthew G. Knepley 
5648e5e52638SMatthew G. Knepley   Input Parameters:
564920f4b53cSBarry Smith + dm     - The `DM`
5650083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
565120f4b53cSBarry Smith . label  - The region label, or `NULL`
565207218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
565307218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
565407218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5655e5e52638SMatthew G. Knepley 
5656e5e52638SMatthew G. Knepley   Level: advanced
5657e5e52638SMatthew G. Knepley 
56581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5659e5e52638SMatthew G. Knepley @*/
566007218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5661d71ae5a4SJacob Faibussowitsch {
5662083401c6SMatthew G. Knepley   PetscInt Nds;
5663e5e52638SMatthew G. Knepley 
5664e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5665e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5666ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56679566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
566863a3b9bcSJacob 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);
56699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56709566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5671083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5672083401c6SMatthew G. Knepley   if (fields) {
5673083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56749566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56759566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5676083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5677e5e52638SMatthew G. Knepley   }
5678083401c6SMatthew G. Knepley   if (ds) {
5679083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56809566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56819566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5682083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5683083401c6SMatthew G. Knepley   }
568407218a29SMatthew G. Knepley   if (dsIn) {
568507218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
568607218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
568707218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
568807218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
568907218a29SMatthew G. Knepley   }
56903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5691e5e52638SMatthew G. Knepley }
5692e5e52638SMatthew G. Knepley 
5693e5e52638SMatthew G. Knepley /*@
569420f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
56951d3af9e0SMatthew G. Knepley 
569620f4b53cSBarry Smith   Not Collective
56971d3af9e0SMatthew G. Knepley 
56981d3af9e0SMatthew G. Knepley   Input Parameters:
569920f4b53cSBarry Smith + dm  - The `DM`
570020f4b53cSBarry Smith - ds  - The `PetscDS` defined on the given region
57011d3af9e0SMatthew G. Knepley 
57021d3af9e0SMatthew G. Knepley   Output Parameter:
57031d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57041d3af9e0SMatthew G. Knepley 
57051d3af9e0SMatthew G. Knepley   Level: advanced
57061d3af9e0SMatthew G. Knepley 
57071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57081d3af9e0SMatthew G. Knepley @*/
5709d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5710d71ae5a4SJacob Faibussowitsch {
57111d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57121d3af9e0SMatthew G. Knepley 
57131d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57141d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57151d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5716dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
57179566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57189371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57199371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57201d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57211d3af9e0SMatthew G. Knepley   else *num = n;
57223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57231d3af9e0SMatthew G. Knepley }
57241d3af9e0SMatthew G. Knepley 
57252df84da0SMatthew G. Knepley /*@C
5726bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57272df84da0SMatthew G. Knepley 
572820f4b53cSBarry Smith   Not Collective
57292df84da0SMatthew G. Knepley 
5730f1a722f8SMatthew G. Knepley   Input Parameters:
5731bb7acecfSBarry Smith + dm     - The `DM`
57322df84da0SMatthew G. Knepley . Nc     - The number of components for the field
573320f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5734bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57352df84da0SMatthew G. Knepley 
57362df84da0SMatthew G. Knepley   Output Parameter:
5737bb7acecfSBarry Smith . fem - The `PetscFE`
57382df84da0SMatthew G. Knepley 
573920f4b53cSBarry Smith   Level: intermediate
574020f4b53cSBarry Smith 
5741bb7acecfSBarry Smith   Note:
5742bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57432df84da0SMatthew G. Knepley 
57441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57452df84da0SMatthew G. Knepley @*/
5746d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5747d71ae5a4SJacob Faibussowitsch {
57482df84da0SMatthew G. Knepley   DMPolytopeType ct;
57492df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57502df84da0SMatthew G. Knepley 
57512df84da0SMatthew G. Knepley   PetscFunctionBegin;
57522df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57532df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57542df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57552df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57562df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57609566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57622df84da0SMatthew G. Knepley }
57632df84da0SMatthew G. Knepley 
57641d3af9e0SMatthew G. Knepley /*@
5765bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5766e5e52638SMatthew G. Knepley 
576720f4b53cSBarry Smith   Collective
5768e5e52638SMatthew G. Knepley 
5769e5e52638SMatthew G. Knepley   Input Parameter:
5770bb7acecfSBarry Smith . dm - The `DM`
5771e5e52638SMatthew G. Knepley 
577220f4b53cSBarry Smith   Options Database Key:
5773bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
577445480ffeSMatthew G. Knepley 
577520f4b53cSBarry Smith   Level: intermediate
577620f4b53cSBarry Smith 
57771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5778e5e52638SMatthew G. Knepley @*/
5779d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5780d71ae5a4SJacob Faibussowitsch {
5781e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5782083401c6SMatthew G. Knepley   PetscDS   dsDef;
5783083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5784f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5785f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5786e5e52638SMatthew G. Knepley 
5787e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5788e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57893ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
57909566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
57919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5792083401c6SMatthew G. Knepley   /* Determine how many regions we have */
57939566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5794083401c6SMatthew G. Knepley   Nl   = 0;
5795083401c6SMatthew G. Knepley   Ndef = 0;
5796083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5797083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5798083401c6SMatthew G. Knepley     PetscInt l;
5799083401c6SMatthew G. Knepley 
5800f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5801f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5802f918ec44SMatthew G. Knepley     {
5803f918ec44SMatthew G. Knepley       PetscClassId id;
5804f918ec44SMatthew G. Knepley 
58059566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5806f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5807f918ec44SMatthew G. Knepley         Ceed ceed;
5808f918ec44SMatthew G. Knepley 
58099566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58109566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5811f918ec44SMatthew G. Knepley       }
5812f918ec44SMatthew G. Knepley     }
5813f918ec44SMatthew G. Knepley #endif
58149371c9d4SSatish Balay     if (!label) {
58159371c9d4SSatish Balay       ++Ndef;
58169371c9d4SSatish Balay       continue;
58179371c9d4SSatish Balay     }
58189371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58199371c9d4SSatish Balay       if (label == labelSet[l]) break;
5820083401c6SMatthew G. Knepley     if (l < Nl) continue;
5821083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5822083401c6SMatthew G. Knepley   }
5823083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
582407218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5825083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5826b3cf3223SMatthew G. Knepley     IS        fields;
5827b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5828b3cf3223SMatthew G. Knepley 
58299371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58309371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58317a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58329566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58339371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58349371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58359566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58369566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58379566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58389566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
583988f0c812SMatthew G. Knepley 
58409566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
584107218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
58429566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58439566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58442df9ee95SMatthew G. Knepley   }
584507218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
58469566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5847083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5848083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58490122748bSMatthew G. Knepley     DM              plex;
5850083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5851083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5852083401c6SMatthew G. Knepley     PetscInt       *fields;
5853083401c6SMatthew G. Knepley     const PetscInt *cells;
5854083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58550122748bSMatthew G. Knepley 
58569566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58589566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58599566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58605fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5861083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5862083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5863083401c6SMatthew G. Knepley       IS      pointIS;
5864083401c6SMatthew G. Knepley 
58659566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58669566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58679566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58689566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5869083401c6SMatthew G. Knepley     }
58709566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5871083401c6SMatthew G. Knepley 
58729566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58739566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58749566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58759566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58769566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58789566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5879083401c6SMatthew G. Knepley 
58809566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58819371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58829371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58839566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58859566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58869566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5887083401c6SMatthew G. Knepley 
58889566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
588907218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
58909566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
58919566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
58929566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58939566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
58949566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5895083401c6SMatthew G. Knepley   }
5896083401c6SMatthew G. Knepley   /* Create label DSes
5897083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5898083401c6SMatthew G. Knepley   */
5899083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5900083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5901083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
590207218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5903083401c6SMatthew G. Knepley     IS        fields;
5904083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5905083401c6SMatthew G. Knepley 
59069566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59079371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59089371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59099566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59109371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59119371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59129566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59139566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59149566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59159566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59169566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5917083401c6SMatthew G. Knepley     {
5918083401c6SMatthew G. Knepley       DMPolytopeType ct;
5919083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59205fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59210122748bSMatthew G. Knepley 
59229566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5923665f567fSMatthew G. Knepley       if (lStart >= 0) {
59249566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5925412e9a14SMatthew G. Knepley         switch (ct) {
5926412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5927412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5928412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5929d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5930d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5931d71ae5a4SJacob Faibussowitsch           break;
5932d71ae5a4SJacob Faibussowitsch         default:
5933d71ae5a4SJacob Faibussowitsch           break;
5934412e9a14SMatthew G. Knepley         }
5935665f567fSMatthew G. Knepley       }
5936712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
593707218a29SMatthew G. Knepley       if (isCohesive) {
593807218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
593907218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
594007218a29SMatthew G. Knepley       }
59415fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59425fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59435fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59449566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59459566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
594607218a29SMatthew G. Knepley             if (dsIn) {
594707218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
594807218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
594907218a29SMatthew G. Knepley             }
59505fedec97SMatthew G. Knepley           }
59515fedec97SMatthew G. Knepley           ++nf;
59525fedec97SMatthew G. Knepley         }
59535fedec97SMatthew G. Knepley       }
5954e5e52638SMatthew G. Knepley     }
595507218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
595607218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
59579566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
595807218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
5959e5e52638SMatthew G. Knepley   }
59609566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5961e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5962083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5963083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
596407218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
5965083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5966083401c6SMatthew G. Knepley     const PetscInt *fld;
59675fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59685fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5969e5e52638SMatthew G. Knepley 
59709566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59719566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59729566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5974083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5975083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59765fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5977e5e52638SMatthew G. Knepley       PetscClassId id;
5978e5e52638SMatthew G. Knepley 
59795fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59809566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59815fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
598207218a29SMatthew G. Knepley       if (isCohesive) {
598307218a29SMatthew G. Knepley         if (!isCohesiveField) {
598407218a29SMatthew G. Knepley           PetscObject bdDisc;
598507218a29SMatthew G. Knepley 
598607218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
598707218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
598807218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
598907218a29SMatthew G. Knepley         } else {
59909566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
599107218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
599207218a29SMatthew G. Knepley         }
599307218a29SMatthew G. Knepley       } else {
599407218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
599507218a29SMatthew G. Knepley       }
5996083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59979566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5998e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5999e5e52638SMatthew G. Knepley     }
60009566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6001e5e52638SMatthew G. Knepley   }
6002f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6004f9244615SMatthew G. Knepley   if (flg) {
60053b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60063b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
600707218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60083b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60093b4aee56SMatthew G. Knepley 
60109566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
601107218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
601207218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
601307218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
601407218a29SMatthew G. Knepley       }
60153b4aee56SMatthew G. Knepley     }
6016f9244615SMatthew G. Knepley   }
6017e5e52638SMatthew G. Knepley   /* Setup DSes */
6018e5e52638SMatthew G. Knepley   if (doSetup) {
601907218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
602007218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
602107218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
602207218a29SMatthew G. Knepley     }
6023e5e52638SMatthew G. Knepley   }
60243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6025e5e52638SMatthew G. Knepley }
6026e5e52638SMatthew G. Knepley 
6027e5e52638SMatthew G. Knepley /*@
6028bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
60297f96f943SMatthew G. Knepley 
603020f4b53cSBarry Smith   Collective
6031f2cacb80SMatthew G. Knepley 
60327f96f943SMatthew G. Knepley   Input Parameters:
6033bb7acecfSBarry Smith + dm   - The `DM`
60347f96f943SMatthew G. Knepley - time - The time
60357f96f943SMatthew G. Knepley 
60367f96f943SMatthew G. Knepley   Output Parameters:
603720f4b53cSBarry Smith + u    - The vector will be filled with exact solution values, or `NULL`
603820f4b53cSBarry Smith - u_t  - The vector will be filled with the time derivative of exact solution values, or `NULL`
603920f4b53cSBarry Smith 
604020f4b53cSBarry Smith   Level: developer
60417f96f943SMatthew G. Knepley 
6042bb7acecfSBarry Smith   Note:
6043bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
60447f96f943SMatthew G. Knepley 
60451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
60467f96f943SMatthew G. Knepley @*/
6047d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6048d71ae5a4SJacob Faibussowitsch {
60497f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
60507f96f943SMatthew G. Knepley   void   **ectxs;
6051f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
60527f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
60537f96f943SMatthew G. Knepley 
60547f96f943SMatthew G. Knepley   PetscFunctionBegin;
6055f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6056f60fa741SMatthew G. Knepley   if (u) {
6057f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6058f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6059f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6060f60fa741SMatthew G. Knepley   }
6061f60fa741SMatthew G. Knepley   if (u_t) {
6062f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6063f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6064f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6065f60fa741SMatthew G. Knepley   }
60669566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
60679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
60689566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60697f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
60707f96f943SMatthew G. Knepley     PetscDS         ds;
60717f96f943SMatthew G. Knepley     DMLabel         label;
60727f96f943SMatthew G. Knepley     IS              fieldIS;
60737f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60747f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60757f96f943SMatthew G. Knepley 
607607218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
60779566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
60789566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
60799566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
60809566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6081f2cacb80SMatthew G. Knepley     if (u) {
6082f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6083f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6084f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
60857f96f943SMatthew G. Knepley     }
6086f2cacb80SMatthew G. Knepley     if (u_t) {
60879566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60889566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6089f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6090f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6091f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6092f2cacb80SMatthew G. Knepley     }
60939566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6094f2cacb80SMatthew G. Knepley   }
6095f2cacb80SMatthew G. Knepley   if (u) {
60969566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60979566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6098f2cacb80SMatthew G. Knepley   }
6099f2cacb80SMatthew G. Knepley   if (u_t) {
61009566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
61019566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6102f2cacb80SMatthew G. Knepley   }
61039566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6104f60fa741SMatthew G. Knepley   if (u) {
6105f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6106f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6107f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6108f60fa741SMatthew G. Knepley   }
6109f60fa741SMatthew G. Knepley   if (u_t) {
6110f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6111f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6112f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6113f60fa741SMatthew G. Knepley   }
61143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61157f96f943SMatthew G. Knepley }
61167f96f943SMatthew G. Knepley 
611707218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6118d71ae5a4SJacob Faibussowitsch {
611907218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
612045480ffeSMatthew G. Knepley 
612145480ffeSMatthew G. Knepley   PetscFunctionBegin;
61229566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
612307218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
612407218a29SMatthew G. Knepley   if (dsIn) {
612507218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
612607218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
612745480ffeSMatthew G. Knepley   }
612807218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
61299566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
613007218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
61313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
613245480ffeSMatthew G. Knepley }
613345480ffeSMatthew G. Knepley 
61347f96f943SMatthew G. Knepley /*@
6135bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6136e5e52638SMatthew G. Knepley 
613720f4b53cSBarry Smith   Collective
6138e5e52638SMatthew G. Knepley 
6139e5e52638SMatthew G. Knepley   Input Parameter:
6140bb7acecfSBarry Smith . dm - The `DM`
6141e5e52638SMatthew G. Knepley 
6142e5e52638SMatthew G. Knepley   Output Parameter:
6143bb7acecfSBarry Smith . newdm - The `DM`
6144e5e52638SMatthew G. Knepley 
6145e5e52638SMatthew G. Knepley   Level: advanced
6146e5e52638SMatthew G. Knepley 
61471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6148e5e52638SMatthew G. Knepley @*/
6149d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6150d71ae5a4SJacob Faibussowitsch {
6151e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6152e5e52638SMatthew G. Knepley 
6153e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61543ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
61559566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61569566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6157e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6158e5e52638SMatthew G. Knepley     DMLabel  label;
6159b3cf3223SMatthew G. Knepley     IS       fields;
616007218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6161783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6162e5e52638SMatthew G. Knepley 
616307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6164b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
616507218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6166d5b43468SJose E. Roman     /* Complete new labels in the new DS */
616707218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
61689566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6169783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6170b8025e53SMatthew G. Knepley       PetscWeakForm wf;
617145480ffeSMatthew G. Knepley       DMLabel       label;
6172783e2ec8SMatthew G. Knepley       PetscInt      field;
6173783e2ec8SMatthew G. Knepley 
61749566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61759566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6176783e2ec8SMatthew G. Knepley     }
6177e5e52638SMatthew G. Knepley   }
6178799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
61793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6180e5e52638SMatthew G. Knepley }
6181e5e52638SMatthew G. Knepley 
6182e5e52638SMatthew G. Knepley /*@
6183bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6184e5e52638SMatthew G. Knepley 
618520f4b53cSBarry Smith   Collective
6186e5e52638SMatthew G. Knepley 
6187e5e52638SMatthew G. Knepley   Input Parameter:
6188bb7acecfSBarry Smith . dm - The `DM`
6189e5e52638SMatthew G. Knepley 
6190e5e52638SMatthew G. Knepley   Output Parameter:
6191bb7acecfSBarry Smith . newdm - The `DM`
6192e5e52638SMatthew G. Knepley 
6193e5e52638SMatthew G. Knepley   Level: advanced
6194e5e52638SMatthew G. Knepley 
6195bb7acecfSBarry Smith   Developer Note:
6196bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6197bb7acecfSBarry Smith 
61981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6199e5e52638SMatthew G. Knepley @*/
6200d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6201d71ae5a4SJacob Faibussowitsch {
6202e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62039566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
62049566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
62053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6206e5e52638SMatthew G. Knepley }
6207e5e52638SMatthew G. Knepley 
6208c73cfb54SMatthew G. Knepley /*@
6209bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6210c73cfb54SMatthew G. Knepley 
621120f4b53cSBarry Smith   Not Collective
6212c73cfb54SMatthew G. Knepley 
6213c73cfb54SMatthew G. Knepley   Input Parameter:
6214bb7acecfSBarry Smith . dm - The `DM`
6215c73cfb54SMatthew G. Knepley 
6216c73cfb54SMatthew G. Knepley   Output Parameter:
6217c73cfb54SMatthew G. Knepley . dim - The topological dimension
6218c73cfb54SMatthew G. Knepley 
6219c73cfb54SMatthew G. Knepley   Level: beginner
6220c73cfb54SMatthew G. Knepley 
62211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6222c73cfb54SMatthew G. Knepley @*/
6223d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6224d71ae5a4SJacob Faibussowitsch {
6225c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6226c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6227534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6228c73cfb54SMatthew G. Knepley   *dim = dm->dim;
62293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6230c73cfb54SMatthew G. Knepley }
6231c73cfb54SMatthew G. Knepley 
6232c73cfb54SMatthew G. Knepley /*@
6233bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6234c73cfb54SMatthew G. Knepley 
623520f4b53cSBarry Smith   Collective
6236c73cfb54SMatthew G. Knepley 
6237c73cfb54SMatthew G. Knepley   Input Parameters:
6238bb7acecfSBarry Smith + dm - The `DM`
6239c73cfb54SMatthew G. Knepley - dim - The topological dimension
6240c73cfb54SMatthew G. Knepley 
6241c73cfb54SMatthew G. Knepley   Level: beginner
6242c73cfb54SMatthew G. Knepley 
62431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6244c73cfb54SMatthew G. Knepley @*/
6245d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6246d71ae5a4SJacob Faibussowitsch {
6247e5e52638SMatthew G. Knepley   PetscDS  ds;
624845480ffeSMatthew G. Knepley   PetscInt Nds, n;
6249f17e8794SMatthew G. Knepley 
6250c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6251c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6252c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6253c73cfb54SMatthew G. Knepley   dm->dim = dim;
6254d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
62559566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
625645480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
625707218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
62589566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
625945480ffeSMatthew G. Knepley     }
6260d17bd122SMatthew G. Knepley   }
62613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6262c73cfb54SMatthew G. Knepley }
6263c73cfb54SMatthew G. Knepley 
6264793f3fe5SMatthew G. Knepley /*@
6265793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6266793f3fe5SMatthew G. Knepley 
626720f4b53cSBarry Smith   Collective
6268793f3fe5SMatthew G. Knepley 
6269793f3fe5SMatthew G. Knepley   Input Parameters:
6270bb7acecfSBarry Smith + dm - the `DM`
6271793f3fe5SMatthew G. Knepley - dim - the dimension
6272793f3fe5SMatthew G. Knepley 
6273793f3fe5SMatthew G. Knepley   Output Parameters:
6274793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6275aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6276793f3fe5SMatthew G. Knepley 
627720f4b53cSBarry Smith   Level: intermediate
627820f4b53cSBarry Smith 
6279793f3fe5SMatthew G. Knepley   Note:
6280793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6281a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6282793f3fe5SMatthew G. Knepley   then the interval is empty.
6283793f3fe5SMatthew G. Knepley 
62841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6285793f3fe5SMatthew G. Knepley @*/
6286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6287d71ae5a4SJacob Faibussowitsch {
6288793f3fe5SMatthew G. Knepley   PetscInt d;
6289793f3fe5SMatthew G. Knepley 
6290793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6291793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62929566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62937a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6294dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
62953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6296793f3fe5SMatthew G. Knepley }
6297793f3fe5SMatthew G. Knepley 
62986636e97aSMatthew G Knepley /*@
6299bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6300f4d763aaSMatthew G. Knepley 
630120f4b53cSBarry Smith   Collective
63028f700142SStefano Zampini 
6303f4d763aaSMatthew G. Knepley   Input Parameter:
6304bb7acecfSBarry Smith . dm - The original `DM`
6305f4d763aaSMatthew G. Knepley 
6306f4d763aaSMatthew G. Knepley   Output Parameter:
6307bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6308f4d763aaSMatthew G. Knepley 
6309f4d763aaSMatthew G. Knepley   Level: intermediate
6310f4d763aaSMatthew G. Knepley 
6311bb7acecfSBarry Smith   Note:
6312bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6313bb7acecfSBarry 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
6314bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6315bb7acecfSBarry Smith 
63161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6317f4d763aaSMatthew G. Knepley @*/
6318d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6319d71ae5a4SJacob Faibussowitsch {
6320c26acbdeSMatthew G. Knepley   PetscSection section;
63212d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
632214f150ffSMatthew G. Knepley 
632314f150ffSMatthew G. Knepley   PetscFunctionBegin;
632414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
632514f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
63269566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
63279566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6328712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
63292d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6330c26acbdeSMatthew G. Knepley     *odm = dm;
63313ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6332c26acbdeSMatthew G. Knepley   }
633314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6334c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
633514f150ffSMatthew G. Knepley     PetscSF      sf;
633614f150ffSMatthew G. Knepley 
63379566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
63389566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
63399566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
63409566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
63419566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
63429566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
63439566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
63449566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
63459566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
634614f150ffSMatthew G. Knepley   }
634714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
63483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
634914f150ffSMatthew G. Knepley }
6350f4d763aaSMatthew G. Knepley 
6351f4d763aaSMatthew G. Knepley /*@
6352cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6353f4d763aaSMatthew G. Knepley 
6354f4d763aaSMatthew G. Knepley   Input Parameter:
6355bb7acecfSBarry Smith . dm - The original `DM`
6356f4d763aaSMatthew G. Knepley 
6357cdb7a50dSMatthew G. Knepley   Output Parameters:
6358cdb7a50dSMatthew G. Knepley + num - The output sequence number
6359cdb7a50dSMatthew G. Knepley - val - The output sequence value
6360f4d763aaSMatthew G. Knepley 
6361f4d763aaSMatthew G. Knepley   Level: intermediate
6362f4d763aaSMatthew G. Knepley 
6363bb7acecfSBarry Smith   Note:
6364bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6365bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6366bb7acecfSBarry Smith 
6367bb7acecfSBarry Smith   Developer Note:
6368bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6369bb7acecfSBarry Smith   not directly related to the `DM`.
6370f4d763aaSMatthew G. Knepley 
63711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6372f4d763aaSMatthew G. Knepley @*/
6373d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6374d71ae5a4SJacob Faibussowitsch {
6375f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6376f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63779371c9d4SSatish Balay   if (num) {
63789371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
63799371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63809371c9d4SSatish Balay   }
63819371c9d4SSatish Balay   if (val) {
63829371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
63839371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63849371c9d4SSatish Balay   }
63853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6386f4d763aaSMatthew G. Knepley }
6387f4d763aaSMatthew G. Knepley 
6388f4d763aaSMatthew G. Knepley /*@
6389cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6390f4d763aaSMatthew G. Knepley 
6391f4d763aaSMatthew G. Knepley   Input Parameters:
6392bb7acecfSBarry Smith + dm - The original `DM`
6393cdb7a50dSMatthew G. Knepley . num - The output sequence number
6394cdb7a50dSMatthew G. Knepley - val - The output sequence value
6395f4d763aaSMatthew G. Knepley 
6396f4d763aaSMatthew G. Knepley   Level: intermediate
6397f4d763aaSMatthew G. Knepley 
6398bb7acecfSBarry Smith   Note:
6399bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6400bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6401f4d763aaSMatthew G. Knepley 
64021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6403f4d763aaSMatthew G. Knepley @*/
6404d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6405d71ae5a4SJacob Faibussowitsch {
6406f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6407f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6408f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6409cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
64103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6411cdb7a50dSMatthew G. Knepley }
6412cdb7a50dSMatthew G. Knepley 
6413cdb7a50dSMatthew G. Knepley /*@C
6414bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6415cdb7a50dSMatthew G. Knepley 
6416cdb7a50dSMatthew G. Knepley   Input Parameters:
6417bb7acecfSBarry Smith + dm   - The original `DM`
6418cdb7a50dSMatthew G. Knepley . name - The sequence name
6419cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6420cdb7a50dSMatthew G. Knepley 
6421cdb7a50dSMatthew G. Knepley   Output Parameter:
6422cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6423cdb7a50dSMatthew G. Knepley 
6424cdb7a50dSMatthew G. Knepley   Level: intermediate
6425cdb7a50dSMatthew G. Knepley 
6426bb7acecfSBarry Smith   Note:
6427bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6428bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6429bb7acecfSBarry Smith 
6430bb7acecfSBarry Smith   Developer Note:
6431bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6432cdb7a50dSMatthew G. Knepley 
64331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6434cdb7a50dSMatthew G. Knepley @*/
6435d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6436d71ae5a4SJacob Faibussowitsch {
6437cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6438cdb7a50dSMatthew G. Knepley 
6439cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6440cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6441cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6442064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
64439566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6444cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6445cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6446cdb7a50dSMatthew G. Knepley     PetscScalar value;
6447cdb7a50dSMatthew G. Knepley 
64489566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
64494aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6450cdb7a50dSMatthew G. Knepley #endif
6451cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
64523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6453f4d763aaSMatthew G. Knepley }
64548e4ac7eaSMatthew G. Knepley 
64558e4ac7eaSMatthew G. Knepley /*@
6456bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64578e4ac7eaSMatthew G. Knepley 
645820f4b53cSBarry Smith   Not Collective
64598e4ac7eaSMatthew G. Knepley 
64608e4ac7eaSMatthew G. Knepley   Input Parameter:
6461bb7acecfSBarry Smith . dm - The `DM`
64628e4ac7eaSMatthew G. Knepley 
64638e4ac7eaSMatthew G. Knepley   Output Parameter:
6464bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64658e4ac7eaSMatthew G. Knepley 
64668e4ac7eaSMatthew G. Knepley   Level: beginner
64678e4ac7eaSMatthew G. Knepley 
64681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
64698e4ac7eaSMatthew G. Knepley @*/
6470d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6471d71ae5a4SJacob Faibussowitsch {
64728e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64738e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6474534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64758e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64778e4ac7eaSMatthew G. Knepley }
64788e4ac7eaSMatthew G. Knepley 
64798e4ac7eaSMatthew G. Knepley /*@
6480bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64818e4ac7eaSMatthew G. Knepley 
648220f4b53cSBarry Smith   Collective
64838e4ac7eaSMatthew G. Knepley 
64848e4ac7eaSMatthew G. Knepley   Input Parameters:
6485bb7acecfSBarry Smith  + dm - The `DM`
6486bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64878e4ac7eaSMatthew G. Knepley 
6488bb7acecfSBarry Smith   Note:
6489bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64905d3b26e6SMatthew G. Knepley 
64918e4ac7eaSMatthew G. Knepley   Level: beginner
64928e4ac7eaSMatthew G. Knepley 
64931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64948e4ac7eaSMatthew G. Knepley @*/
6495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6496d71ae5a4SJacob Faibussowitsch {
64978e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64988e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64998833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
65008e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
65013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65028e4ac7eaSMatthew G. Knepley }
6503c58f1c22SToby Isaac 
6504c58f1c22SToby Isaac /*@C
6505bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6506c58f1c22SToby Isaac 
6507c58f1c22SToby Isaac   Not Collective
6508c58f1c22SToby Isaac 
6509c58f1c22SToby Isaac   Input Parameters:
6510bb7acecfSBarry Smith + dm   - The `DM` object
6511c58f1c22SToby Isaac - name - The label name
6512c58f1c22SToby Isaac 
6513c58f1c22SToby Isaac   Level: intermediate
6514c58f1c22SToby Isaac 
65151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6516c58f1c22SToby Isaac @*/
6517d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6518d71ae5a4SJacob Faibussowitsch {
65195d80c0bfSVaclav Hapla   PetscBool flg;
65205d80c0bfSVaclav Hapla   DMLabel   label;
6521c58f1c22SToby Isaac 
6522c58f1c22SToby Isaac   PetscFunctionBegin;
6523c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6524c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65259566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6526c58f1c22SToby Isaac   if (!flg) {
65279566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65289566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65299566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6530c58f1c22SToby Isaac   }
65313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6532c58f1c22SToby Isaac }
6533c58f1c22SToby Isaac 
6534c58f1c22SToby Isaac /*@C
6535bb7acecfSBarry 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.
65360fdc7489SMatthew Knepley 
65370fdc7489SMatthew Knepley   Not Collective
65380fdc7489SMatthew Knepley 
65390fdc7489SMatthew Knepley   Input Parameters:
6540bb7acecfSBarry Smith + dm   - The `DM` object
65410fdc7489SMatthew Knepley . l    - The index for the label
65420fdc7489SMatthew Knepley - name - The label name
65430fdc7489SMatthew Knepley 
65440fdc7489SMatthew Knepley   Level: intermediate
65450fdc7489SMatthew Knepley 
65461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
65470fdc7489SMatthew Knepley @*/
6548d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6549d71ae5a4SJacob Faibussowitsch {
65500fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
65510fdc7489SMatthew Knepley   DMLabel     label;
65520fdc7489SMatthew Knepley   PetscInt    Nl, m;
65530fdc7489SMatthew Knepley   PetscBool   flg, match;
65540fdc7489SMatthew Knepley   const char *lname;
65550fdc7489SMatthew Knepley 
65560fdc7489SMatthew Knepley   PetscFunctionBegin;
65570fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6558064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
65599566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
65600fdc7489SMatthew Knepley   if (!flg) {
65619566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65629566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65639566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65640fdc7489SMatthew Knepley   }
65659566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
656663a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65670fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65689566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65699566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65700fdc7489SMatthew Knepley     if (match) break;
65710fdc7489SMatthew Knepley   }
65723ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
65730fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65740fdc7489SMatthew Knepley   else prev->next = orig->next;
65750fdc7489SMatthew Knepley   if (!l) {
65760fdc7489SMatthew Knepley     orig->next = dm->labels;
65770fdc7489SMatthew Knepley     dm->labels = orig;
65780fdc7489SMatthew Knepley   } else {
65799371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65809371c9d4SSatish Balay       ;
65810fdc7489SMatthew Knepley     orig->next = prev->next;
65820fdc7489SMatthew Knepley     prev->next = orig;
65830fdc7489SMatthew Knepley   }
65843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65850fdc7489SMatthew Knepley }
65860fdc7489SMatthew Knepley 
65870fdc7489SMatthew Knepley /*@C
6588bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6589c58f1c22SToby Isaac 
6590c58f1c22SToby Isaac   Not Collective
6591c58f1c22SToby Isaac 
6592c58f1c22SToby Isaac   Input Parameters:
6593bb7acecfSBarry Smith + dm   - The `DM` object
6594c58f1c22SToby Isaac . name - The label name
6595c58f1c22SToby Isaac - point - The mesh point
6596c58f1c22SToby Isaac 
6597c58f1c22SToby Isaac   Output Parameter:
6598c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6599c58f1c22SToby Isaac 
6600c58f1c22SToby Isaac   Level: beginner
6601c58f1c22SToby Isaac 
66021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6603c58f1c22SToby Isaac @*/
6604d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6605d71ae5a4SJacob Faibussowitsch {
6606c58f1c22SToby Isaac   DMLabel label;
6607c58f1c22SToby Isaac 
6608c58f1c22SToby Isaac   PetscFunctionBegin;
6609c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6610c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66127a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
66139566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
66143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6615c58f1c22SToby Isaac }
6616c58f1c22SToby Isaac 
6617c58f1c22SToby Isaac /*@C
6618bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6619c58f1c22SToby Isaac 
6620c58f1c22SToby Isaac   Not Collective
6621c58f1c22SToby Isaac 
6622c58f1c22SToby Isaac   Input Parameters:
6623bb7acecfSBarry Smith + dm   - The `DM` object
6624c58f1c22SToby Isaac . name - The label name
6625c58f1c22SToby Isaac . point - The mesh point
6626c58f1c22SToby Isaac - value - The label value for this point
6627c58f1c22SToby Isaac 
6628c58f1c22SToby Isaac   Output Parameter:
6629c58f1c22SToby Isaac 
6630c58f1c22SToby Isaac   Level: beginner
6631c58f1c22SToby Isaac 
66321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6633c58f1c22SToby Isaac @*/
6634d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6635d71ae5a4SJacob Faibussowitsch {
6636c58f1c22SToby Isaac   DMLabel label;
6637c58f1c22SToby Isaac 
6638c58f1c22SToby Isaac   PetscFunctionBegin;
6639c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6640c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66419566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6642c58f1c22SToby Isaac   if (!label) {
66439566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
66449566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6645c58f1c22SToby Isaac   }
66469566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
66473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6648c58f1c22SToby Isaac }
6649c58f1c22SToby Isaac 
6650c58f1c22SToby Isaac /*@C
6651bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6652c58f1c22SToby Isaac 
6653c58f1c22SToby Isaac   Not Collective
6654c58f1c22SToby Isaac 
6655c58f1c22SToby Isaac   Input Parameters:
6656bb7acecfSBarry Smith + dm   - The `DM` object
6657c58f1c22SToby Isaac . name - The label name
6658c58f1c22SToby Isaac . point - The mesh point
6659c58f1c22SToby Isaac - value - The label value for this point
6660c58f1c22SToby Isaac 
6661c58f1c22SToby Isaac   Level: beginner
6662c58f1c22SToby Isaac 
66631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6664c58f1c22SToby Isaac @*/
6665d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6666d71ae5a4SJacob Faibussowitsch {
6667c58f1c22SToby Isaac   DMLabel label;
6668c58f1c22SToby Isaac 
6669c58f1c22SToby Isaac   PetscFunctionBegin;
6670c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6671c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66729566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66733ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
66749566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
66753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6676c58f1c22SToby Isaac }
6677c58f1c22SToby Isaac 
6678c58f1c22SToby Isaac /*@C
6679bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac   Not Collective
6682c58f1c22SToby Isaac 
6683c58f1c22SToby Isaac   Input Parameters:
6684bb7acecfSBarry Smith + dm   - The `DM` object
6685c58f1c22SToby Isaac - name - The label name
6686c58f1c22SToby Isaac 
6687c58f1c22SToby Isaac   Output Parameter:
6688c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6689c58f1c22SToby Isaac 
6690c58f1c22SToby Isaac   Level: beginner
6691c58f1c22SToby Isaac 
6692bb7acecfSBarry Smith   Developer Note:
6693bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6694bb7acecfSBarry Smith 
66951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6696c58f1c22SToby Isaac @*/
6697d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6698d71ae5a4SJacob Faibussowitsch {
6699c58f1c22SToby Isaac   DMLabel label;
6700c58f1c22SToby Isaac 
6701c58f1c22SToby Isaac   PetscFunctionBegin;
6702c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6703c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6704534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
67059566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6706c58f1c22SToby Isaac   *size = 0;
67073ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67089566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
67093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6710c58f1c22SToby Isaac }
6711c58f1c22SToby Isaac 
6712c58f1c22SToby Isaac /*@C
6713bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac   Not Collective
6716c58f1c22SToby Isaac 
6717c58f1c22SToby Isaac   Input Parameters:
6718bb7acecfSBarry Smith + mesh - The `DM` object
6719c58f1c22SToby Isaac - name - The label name
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac   Output Parameter:
672220f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6723c58f1c22SToby Isaac 
6724c58f1c22SToby Isaac   Level: beginner
6725c58f1c22SToby Isaac 
67261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6727c58f1c22SToby Isaac @*/
6728d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6729d71ae5a4SJacob Faibussowitsch {
6730c58f1c22SToby Isaac   DMLabel label;
6731c58f1c22SToby Isaac 
6732c58f1c22SToby Isaac   PetscFunctionBegin;
6733c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6734c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6735c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
67369566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6737c58f1c22SToby Isaac   *ids = NULL;
6738dab2e251SBlaise Bourdin   if (label) {
67399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6740dab2e251SBlaise Bourdin   } else {
6741dab2e251SBlaise Bourdin     /* returning an empty IS */
67429566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6743dab2e251SBlaise Bourdin   }
67443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6745c58f1c22SToby Isaac }
6746c58f1c22SToby Isaac 
6747c58f1c22SToby Isaac /*@C
6748c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6749c58f1c22SToby Isaac 
6750c58f1c22SToby Isaac   Not Collective
6751c58f1c22SToby Isaac 
6752c58f1c22SToby Isaac   Input Parameters:
6753bb7acecfSBarry Smith + dm - The `DM` object
6754c58f1c22SToby Isaac . name - The label name
6755c58f1c22SToby Isaac - value - The stratum value
6756c58f1c22SToby Isaac 
6757c58f1c22SToby Isaac   Output Parameter:
6758bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6759c58f1c22SToby Isaac 
6760c58f1c22SToby Isaac   Level: beginner
6761c58f1c22SToby Isaac 
67621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6763c58f1c22SToby Isaac @*/
6764d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6765d71ae5a4SJacob Faibussowitsch {
6766c58f1c22SToby Isaac   DMLabel label;
6767c58f1c22SToby Isaac 
6768c58f1c22SToby Isaac   PetscFunctionBegin;
6769c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6770c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6771534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67729566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6773c58f1c22SToby Isaac   *size = 0;
67743ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67759566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
67763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6777c58f1c22SToby Isaac }
6778c58f1c22SToby Isaac 
6779c58f1c22SToby Isaac /*@C
6780c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6781c58f1c22SToby Isaac 
6782c58f1c22SToby Isaac   Not Collective
6783c58f1c22SToby Isaac 
6784c58f1c22SToby Isaac   Input Parameters:
6785bb7acecfSBarry Smith + dm - The `DM` object
6786c58f1c22SToby Isaac . name - The label name
6787c58f1c22SToby Isaac - value - The stratum value
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac   Output Parameter:
679020f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6791c58f1c22SToby Isaac 
6792c58f1c22SToby Isaac   Level: beginner
6793c58f1c22SToby Isaac 
67941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6795c58f1c22SToby Isaac @*/
6796d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6797d71ae5a4SJacob Faibussowitsch {
6798c58f1c22SToby Isaac   DMLabel label;
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   PetscFunctionBegin;
6801c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6802c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6803c58f1c22SToby Isaac   PetscValidPointer(points, 4);
68049566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6805c58f1c22SToby Isaac   *points = NULL;
68063ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68079566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
68083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6809c58f1c22SToby Isaac }
6810c58f1c22SToby Isaac 
68114de306b1SToby Isaac /*@C
68129044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68134de306b1SToby Isaac 
68144de306b1SToby Isaac   Not Collective
68154de306b1SToby Isaac 
68164de306b1SToby Isaac   Input Parameters:
6817bb7acecfSBarry Smith + dm - The `DM` object
68184de306b1SToby Isaac . name - The label name
68194de306b1SToby Isaac . value - The stratum value
68204de306b1SToby Isaac - points - The stratum points
68214de306b1SToby Isaac 
68224de306b1SToby Isaac   Level: beginner
68234de306b1SToby Isaac 
68241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
68254de306b1SToby Isaac @*/
6826d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6827d71ae5a4SJacob Faibussowitsch {
68284de306b1SToby Isaac   DMLabel label;
68294de306b1SToby Isaac 
68304de306b1SToby Isaac   PetscFunctionBegin;
68314de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68324de306b1SToby Isaac   PetscValidCharPointer(name, 2);
68334de306b1SToby Isaac   PetscValidPointer(points, 4);
68349566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68353ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68369566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
68373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
68384de306b1SToby Isaac }
68394de306b1SToby Isaac 
6840c58f1c22SToby Isaac /*@C
6841bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   Not Collective
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   Input Parameters:
6846bb7acecfSBarry Smith + dm   - The `DM` object
6847c58f1c22SToby Isaac . name - The label name
6848c58f1c22SToby Isaac - value - The label value for this point
6849c58f1c22SToby Isaac 
6850c58f1c22SToby Isaac   Output Parameter:
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   Level: beginner
6853c58f1c22SToby Isaac 
68541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6855c58f1c22SToby Isaac @*/
6856d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6857d71ae5a4SJacob Faibussowitsch {
6858c58f1c22SToby Isaac   DMLabel label;
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   PetscFunctionBegin;
6861c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6862c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68639566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68643ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68659566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
68663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6867c58f1c22SToby Isaac }
6868c58f1c22SToby Isaac 
6869c58f1c22SToby Isaac /*@
6870bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6871c58f1c22SToby Isaac 
6872c58f1c22SToby Isaac   Not Collective
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac   Input Parameter:
6875bb7acecfSBarry Smith . dm   - The `DM` object
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Output Parameter:
6878c58f1c22SToby Isaac . numLabels - the number of Labels
6879c58f1c22SToby Isaac 
6880c58f1c22SToby Isaac   Level: intermediate
6881c58f1c22SToby Isaac 
68821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6883c58f1c22SToby Isaac @*/
6884d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6885d71ae5a4SJacob Faibussowitsch {
68865d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6887c58f1c22SToby Isaac   PetscInt    n    = 0;
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac   PetscFunctionBegin;
6890c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6891534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
68929371c9d4SSatish Balay   while (next) {
68939371c9d4SSatish Balay     ++n;
68949371c9d4SSatish Balay     next = next->next;
68959371c9d4SSatish Balay   }
6896c58f1c22SToby Isaac   *numLabels = n;
68973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6898c58f1c22SToby Isaac }
6899c58f1c22SToby Isaac 
6900c58f1c22SToby Isaac /*@C
6901c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Not Collective
6904c58f1c22SToby Isaac 
6905c58f1c22SToby Isaac   Input Parameters:
6906bb7acecfSBarry Smith + dm - The `DM` object
6907c58f1c22SToby Isaac - n  - the label number
6908c58f1c22SToby Isaac 
6909c58f1c22SToby Isaac   Output Parameter:
6910c58f1c22SToby Isaac . name - the label name
6911c58f1c22SToby Isaac 
6912c58f1c22SToby Isaac   Level: intermediate
6913c58f1c22SToby Isaac 
6914bb7acecfSBarry Smith   Developer Note:
6915bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6916bb7acecfSBarry Smith 
69171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6918c58f1c22SToby Isaac @*/
6919d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6920d71ae5a4SJacob Faibussowitsch {
69215d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6922c58f1c22SToby Isaac   PetscInt    l    = 0;
6923c58f1c22SToby Isaac 
6924c58f1c22SToby Isaac   PetscFunctionBegin;
6925c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6926c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6927c58f1c22SToby Isaac   while (next) {
6928c58f1c22SToby Isaac     if (l == n) {
69299566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
69303ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
6931c58f1c22SToby Isaac     }
6932c58f1c22SToby Isaac     ++l;
6933c58f1c22SToby Isaac     next = next->next;
6934c58f1c22SToby Isaac   }
693563a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6936c58f1c22SToby Isaac }
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac /*@C
6939bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6940c58f1c22SToby Isaac 
6941c58f1c22SToby Isaac   Not Collective
6942c58f1c22SToby Isaac 
6943c58f1c22SToby Isaac   Input Parameters:
6944bb7acecfSBarry Smith + dm   - The `DM` object
6945c58f1c22SToby Isaac - name - The label name
6946c58f1c22SToby Isaac 
6947c58f1c22SToby Isaac   Output Parameter:
6948bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6949c58f1c22SToby Isaac 
6950c58f1c22SToby Isaac   Level: intermediate
6951c58f1c22SToby Isaac 
69521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6953c58f1c22SToby Isaac @*/
6954d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6955d71ae5a4SJacob Faibussowitsch {
69565d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6957d67d17b1SMatthew G. Knepley   const char *lname;
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   PetscFunctionBegin;
6960c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6961c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6962534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6963c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6964c58f1c22SToby Isaac   while (next) {
69659566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69669566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6967c58f1c22SToby Isaac     if (*hasLabel) break;
6968c58f1c22SToby Isaac     next = next->next;
6969c58f1c22SToby Isaac   }
69703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6971c58f1c22SToby Isaac }
6972c58f1c22SToby Isaac 
6973c58f1c22SToby Isaac /*@C
697420f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
6975c58f1c22SToby Isaac 
6976c58f1c22SToby Isaac   Not Collective
6977c58f1c22SToby Isaac 
6978c58f1c22SToby Isaac   Input Parameters:
6979bb7acecfSBarry Smith + dm   - The `DM` object
6980c58f1c22SToby Isaac - name - The label name
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   Output Parameter:
698320f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
6984c58f1c22SToby Isaac 
6985bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6986bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6987bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6988bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6989bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6990bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6991bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69926d7c9049SMatthew G. Knepley 
6993c58f1c22SToby Isaac   Level: intermediate
6994c58f1c22SToby Isaac 
69951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6996c58f1c22SToby Isaac @*/
6997d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6998d71ae5a4SJacob Faibussowitsch {
69995d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7000c58f1c22SToby Isaac   PetscBool   hasLabel;
7001d67d17b1SMatthew G. Knepley   const char *lname;
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   PetscFunctionBegin;
7004c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7005c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7006c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7007c58f1c22SToby Isaac   *label = NULL;
7008c58f1c22SToby Isaac   while (next) {
70099566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70109566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7011c58f1c22SToby Isaac     if (hasLabel) {
7012c58f1c22SToby Isaac       *label = next->label;
7013c58f1c22SToby Isaac       break;
7014c58f1c22SToby Isaac     }
7015c58f1c22SToby Isaac     next = next->next;
7016c58f1c22SToby Isaac   }
70173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7018c58f1c22SToby Isaac }
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac /*@C
7021bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7022c58f1c22SToby Isaac 
7023c58f1c22SToby Isaac   Not Collective
7024c58f1c22SToby Isaac 
7025c58f1c22SToby Isaac   Input Parameters:
7026bb7acecfSBarry Smith + dm - The `DM` object
7027c58f1c22SToby Isaac - n  - the label number
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac   Output Parameter:
7030c58f1c22SToby Isaac . label - the label
7031c58f1c22SToby Isaac 
7032c58f1c22SToby Isaac   Level: intermediate
7033c58f1c22SToby Isaac 
70341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7035c58f1c22SToby Isaac @*/
7036d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7037d71ae5a4SJacob Faibussowitsch {
70385d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7039c58f1c22SToby Isaac   PetscInt    l    = 0;
7040c58f1c22SToby Isaac 
7041c58f1c22SToby Isaac   PetscFunctionBegin;
7042c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7043c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7044c58f1c22SToby Isaac   while (next) {
7045c58f1c22SToby Isaac     if (l == n) {
7046c58f1c22SToby Isaac       *label = next->label;
70473ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7048c58f1c22SToby Isaac     }
7049c58f1c22SToby Isaac     ++l;
7050c58f1c22SToby Isaac     next = next->next;
7051c58f1c22SToby Isaac   }
705263a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7053c58f1c22SToby Isaac }
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac /*@C
7056bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7057c58f1c22SToby Isaac 
7058c58f1c22SToby Isaac   Not Collective
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac   Input Parameters:
7061bb7acecfSBarry Smith + dm   - The `DM` object
7062bb7acecfSBarry Smith - label - The `DMLabel`
7063c58f1c22SToby Isaac 
7064c58f1c22SToby Isaac   Level: developer
7065c58f1c22SToby Isaac 
70661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7067c58f1c22SToby Isaac @*/
7068d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7069d71ae5a4SJacob Faibussowitsch {
70705d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7071c58f1c22SToby Isaac   PetscBool   hasLabel;
7072d67d17b1SMatthew G. Knepley   const char *lname;
70735d80c0bfSVaclav Hapla   PetscBool   flg;
7074c58f1c22SToby Isaac 
7075c58f1c22SToby Isaac   PetscFunctionBegin;
7076c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70779566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70789566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70797a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70809566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7081c58f1c22SToby Isaac   tmpLabel->label  = label;
7082c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70835d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70845d80c0bfSVaclav Hapla   *p = tmpLabel;
70859566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70869566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70875d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70889566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7089ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
70903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7091c58f1c22SToby Isaac }
7092c58f1c22SToby Isaac 
7093c58f1c22SToby Isaac /*@C
70944a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
70954a7ee7d0SMatthew G. Knepley 
70964a7ee7d0SMatthew G. Knepley   Not Collective
70974a7ee7d0SMatthew G. Knepley 
70984a7ee7d0SMatthew G. Knepley   Input Parameters:
7099bb7acecfSBarry Smith + dm    - The `DM` object
7100bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
71014a7ee7d0SMatthew G. Knepley 
7102bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7103bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7104bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7105bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7106bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7107bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7108bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71094a7ee7d0SMatthew G. Knepley 
71104a7ee7d0SMatthew G. Knepley   Level: intermediate
71114a7ee7d0SMatthew G. Knepley 
71121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
71134a7ee7d0SMatthew G. Knepley @*/
7114d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7115d71ae5a4SJacob Faibussowitsch {
71164a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
71174a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
71184a7ee7d0SMatthew G. Knepley   const char *name, *lname;
71194a7ee7d0SMatthew G. Knepley 
71204a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
71214a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71224a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
71239566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
71244a7ee7d0SMatthew G. Knepley   while (next) {
71259566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71269566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
71274a7ee7d0SMatthew G. Knepley     if (hasLabel) {
71289566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
71299566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
71304a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
71319566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
71324a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
71339566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
71344a7ee7d0SMatthew G. Knepley       next->label = label;
71354a7ee7d0SMatthew G. Knepley       break;
71364a7ee7d0SMatthew G. Knepley     }
71374a7ee7d0SMatthew G. Knepley     next = next->next;
71384a7ee7d0SMatthew G. Knepley   }
71393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
71404a7ee7d0SMatthew G. Knepley }
71414a7ee7d0SMatthew G. Knepley 
71424a7ee7d0SMatthew G. Knepley /*@C
7143bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac   Not Collective
7146c58f1c22SToby Isaac 
7147c58f1c22SToby Isaac   Input Parameters:
7148bb7acecfSBarry Smith + dm   - The `DM` object
7149c58f1c22SToby Isaac - name - The label name
7150c58f1c22SToby Isaac 
7151c58f1c22SToby Isaac   Output Parameter:
715220f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7153bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7154c58f1c22SToby Isaac 
7155c58f1c22SToby Isaac   Level: developer
7156c58f1c22SToby Isaac 
71571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7158c58f1c22SToby Isaac @*/
7159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7160d71ae5a4SJacob Faibussowitsch {
716195d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7162c58f1c22SToby Isaac   PetscBool   hasLabel;
7163d67d17b1SMatthew G. Knepley   const char *lname;
7164c58f1c22SToby Isaac 
7165c58f1c22SToby Isaac   PetscFunctionBegin;
7166c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7167e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7168e5472504SVaclav Hapla   if (label) {
7169e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7170c58f1c22SToby Isaac     *label = NULL;
7171e5472504SVaclav Hapla   }
71725d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71739566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71749566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7175c58f1c22SToby Isaac     if (hasLabel) {
717695d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71779566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
717895d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71799566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7180ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
718195d578d6SVaclav Hapla       if (label) *label = link->label;
71829566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71839566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7184c58f1c22SToby Isaac       break;
7185c58f1c22SToby Isaac     }
7186c58f1c22SToby Isaac   }
71873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7188c58f1c22SToby Isaac }
7189c58f1c22SToby Isaac 
7190306894acSVaclav Hapla /*@
7191bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7192306894acSVaclav Hapla 
7193306894acSVaclav Hapla   Not Collective
7194306894acSVaclav Hapla 
7195306894acSVaclav Hapla   Input Parameters:
7196bb7acecfSBarry Smith + dm   - The `DM` object
7197bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
719820f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7199306894acSVaclav Hapla 
7200306894acSVaclav Hapla   Level: developer
7201306894acSVaclav Hapla 
7202bb7acecfSBarry Smith   Note:
7203306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7204bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7205306894acSVaclav Hapla   *label nullified.
7206306894acSVaclav Hapla 
72071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7208306894acSVaclav Hapla @*/
7209d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7210d71ae5a4SJacob Faibussowitsch {
721143e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7212306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7213306894acSVaclav Hapla 
7214306894acSVaclav Hapla   PetscFunctionBegin;
7215306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7216306894acSVaclav Hapla   PetscValidPointer(label, 2);
72173ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7218306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7219306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
72205d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
722143e45a93SVaclav Hapla     if (*label == link->label) {
7222306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
722343e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7224306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7225ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
722643e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
72279566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
72289566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7229306894acSVaclav Hapla       break;
7230306894acSVaclav Hapla     }
7231306894acSVaclav Hapla   }
72327a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
72333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7234306894acSVaclav Hapla }
7235306894acSVaclav Hapla 
7236c58f1c22SToby Isaac /*@C
7237c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7238c58f1c22SToby Isaac 
7239c58f1c22SToby Isaac   Not Collective
7240c58f1c22SToby Isaac 
7241c58f1c22SToby Isaac   Input Parameters:
7242bb7acecfSBarry Smith + dm   - The `DM` object
7243c58f1c22SToby Isaac - name - The label name
7244c58f1c22SToby Isaac 
7245c58f1c22SToby Isaac   Output Parameter:
7246c58f1c22SToby Isaac . output - The flag for output
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Level: developer
7249c58f1c22SToby Isaac 
72501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7251c58f1c22SToby Isaac @*/
7252d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7253d71ae5a4SJacob Faibussowitsch {
72545d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7255d67d17b1SMatthew G. Knepley   const char *lname;
7256c58f1c22SToby Isaac 
7257c58f1c22SToby Isaac   PetscFunctionBegin;
7258c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7259dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7260dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7261c58f1c22SToby Isaac   while (next) {
7262c58f1c22SToby Isaac     PetscBool flg;
7263c58f1c22SToby Isaac 
72649566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72659566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72669371c9d4SSatish Balay     if (flg) {
72679371c9d4SSatish Balay       *output = next->output;
72683ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
72699371c9d4SSatish Balay     }
7270c58f1c22SToby Isaac     next = next->next;
7271c58f1c22SToby Isaac   }
727298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7273c58f1c22SToby Isaac }
7274c58f1c22SToby Isaac 
7275c58f1c22SToby Isaac /*@C
7276bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7277c58f1c22SToby Isaac 
7278c58f1c22SToby Isaac   Not Collective
7279c58f1c22SToby Isaac 
7280c58f1c22SToby Isaac   Input Parameters:
7281bb7acecfSBarry Smith + dm     - The `DM` object
7282c58f1c22SToby Isaac . name   - The label name
7283bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac   Level: developer
7286c58f1c22SToby Isaac 
72871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7288c58f1c22SToby Isaac @*/
7289d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7290d71ae5a4SJacob Faibussowitsch {
72915d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7292d67d17b1SMatthew G. Knepley   const char *lname;
7293c58f1c22SToby Isaac 
7294c58f1c22SToby Isaac   PetscFunctionBegin;
7295c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7296534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7297c58f1c22SToby Isaac   while (next) {
7298c58f1c22SToby Isaac     PetscBool flg;
7299c58f1c22SToby Isaac 
73009566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73019566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73029371c9d4SSatish Balay     if (flg) {
73039371c9d4SSatish Balay       next->output = output;
73043ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73059371c9d4SSatish Balay     }
7306c58f1c22SToby Isaac     next = next->next;
7307c58f1c22SToby Isaac   }
730898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7309c58f1c22SToby Isaac }
7310c58f1c22SToby Isaac 
7311c58f1c22SToby Isaac /*@
7312bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7313c58f1c22SToby Isaac 
731420f4b53cSBarry Smith   Collective
7315c58f1c22SToby Isaac 
7316d8d19677SJose E. Roman   Input Parameters:
7317bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7318bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7319bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7320bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7321bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7322c58f1c22SToby Isaac 
7323c58f1c22SToby Isaac   Level: intermediate
7324c58f1c22SToby Isaac 
7325bb7acecfSBarry Smith   Note:
73262cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7327c58f1c22SToby Isaac 
73281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7329c58f1c22SToby Isaac @*/
7330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7331d71ae5a4SJacob Faibussowitsch {
73322cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7333c58f1c22SToby Isaac   const char *name;
7334c58f1c22SToby Isaac   PetscBool   flg;
73355d80c0bfSVaclav Hapla   DMLabelLink link;
7336c58f1c22SToby Isaac 
73375d80c0bfSVaclav Hapla   PetscFunctionBegin;
73385d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73395d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73405d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
73415d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73427a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73433ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
73445d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
73455d80c0bfSVaclav Hapla     label = link->label;
73469566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
73475d80c0bfSVaclav Hapla     if (!all) {
73489566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7349c58f1c22SToby Isaac       if (flg) continue;
73509566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
73517d5acc75SStefano Zampini       if (flg) continue;
73529566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7353ba2698f1SMatthew G. Knepley       if (flg) continue;
73545d80c0bfSVaclav Hapla     }
73559566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
73562cbb9b06SVaclav Hapla     if (labelOld) {
73572cbb9b06SVaclav Hapla       switch (emode) {
7358d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7359d71ae5a4SJacob Faibussowitsch         continue;
7360d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7361d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7362d71ae5a4SJacob Faibussowitsch         break;
7363d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7364d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7365d71ae5a4SJacob Faibussowitsch       default:
7366d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73672cbb9b06SVaclav Hapla       }
73682cbb9b06SVaclav Hapla     }
73695d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73709566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73715d80c0bfSVaclav Hapla     } else {
73725d80c0bfSVaclav Hapla       labelNew = label;
73735d80c0bfSVaclav Hapla     }
73749566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73759566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7376c58f1c22SToby Isaac   }
73773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7378c58f1c22SToby Isaac }
7379461a15a0SLisandro Dalcin 
7380609dae6eSVaclav Hapla /*@C
7381b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7382609dae6eSVaclav Hapla 
738320f4b53cSBarry Smith   Collective; No Fortran Support
7384609dae6eSVaclav Hapla 
7385609dae6eSVaclav Hapla   Input Parameters:
7386bb7acecfSBarry Smith + dm0 - First `DM` object
7387bb7acecfSBarry Smith - dm1 - Second `DM` object
7388609dae6eSVaclav Hapla 
7389609dae6eSVaclav Hapla   Output Parameters
73905efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
739120f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7392609dae6eSVaclav Hapla 
7393609dae6eSVaclav Hapla   Level: intermediate
7394609dae6eSVaclav Hapla 
7395609dae6eSVaclav Hapla   Notes:
7396bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7397bb7acecfSBarry Smith 
739820f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7399bb7acecfSBarry Smith 
740020f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7401609dae6eSVaclav Hapla 
74025efe38ccSVaclav Hapla   The output message is set independently on each rank.
7403bb7acecfSBarry Smith 
7404bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7405bb7acecfSBarry Smith 
740620f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7407bb7acecfSBarry Smith 
740820f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7409609dae6eSVaclav Hapla 
7410609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7411bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7412609dae6eSVaclav Hapla 
74131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7414609dae6eSVaclav Hapla @*/
7415d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7416d71ae5a4SJacob Faibussowitsch {
74175efe38ccSVaclav Hapla   PetscInt    n, i;
7418609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
74195efe38ccSVaclav Hapla   PetscBool   eq;
7420609dae6eSVaclav Hapla   MPI_Comm    comm;
74215efe38ccSVaclav Hapla   PetscMPIInt rank;
7422609dae6eSVaclav Hapla 
7423609dae6eSVaclav Hapla   PetscFunctionBegin;
7424609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7425609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7426609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
74275efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7428609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
74299566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
74309566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
74315efe38ccSVaclav Hapla   {
74325efe38ccSVaclav Hapla     PetscInt n1;
74335efe38ccSVaclav Hapla 
74349566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
74359566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
74365efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
743748a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7438712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
74395efe38ccSVaclav Hapla     if (!eq) goto finish;
74405efe38ccSVaclav Hapla   }
74415efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7442609dae6eSVaclav Hapla     DMLabel     l0, l1;
7443609dae6eSVaclav Hapla     const char *name;
7444609dae6eSVaclav Hapla     char       *msgInner;
7445609dae6eSVaclav Hapla 
7446609dae6eSVaclav Hapla     /* Ignore label order */
74479566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
74489566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
74499566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7450609dae6eSVaclav Hapla     if (!l1) {
745163a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
74525efe38ccSVaclav Hapla       eq = PETSC_FALSE;
74535efe38ccSVaclav Hapla       break;
7454609dae6eSVaclav Hapla     }
74559566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
74569566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
74579566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74585efe38ccSVaclav Hapla     if (!eq) break;
7459609dae6eSVaclav Hapla   }
7460712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7461609dae6eSVaclav Hapla finish:
74625efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7463609dae6eSVaclav Hapla   if (message) {
7464609dae6eSVaclav Hapla     *message = NULL;
746548a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74665efe38ccSVaclav Hapla   } else {
746748a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74689566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74695efe38ccSVaclav Hapla   }
74705efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74715efe38ccSVaclav Hapla   if (equal) *equal = eq;
74727a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
74733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7474609dae6eSVaclav Hapla }
7475609dae6eSVaclav Hapla 
7476d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7477d71ae5a4SJacob Faibussowitsch {
7478461a15a0SLisandro Dalcin   PetscFunctionBegin;
7479461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7480461a15a0SLisandro Dalcin   if (!*label) {
74819566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74829566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7483461a15a0SLisandro Dalcin   }
74849566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
74853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7486461a15a0SLisandro Dalcin }
7487461a15a0SLisandro Dalcin 
74880fdc7489SMatthew Knepley /*
74890fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74900fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74910fdc7489SMatthew Knepley   (label, id) pair in the DM.
74920fdc7489SMatthew Knepley 
74930fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
74940fdc7489SMatthew Knepley   each label.
74950fdc7489SMatthew Knepley */
7496d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7497d71ae5a4SJacob Faibussowitsch {
74980fdc7489SMatthew Knepley   DMUniversalLabel ul;
74990fdc7489SMatthew Knepley   PetscBool       *active;
75000fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
75010fdc7489SMatthew Knepley 
75020fdc7489SMatthew Knepley   PetscFunctionBegin;
75039566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
75049566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
75059566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
75069566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
75070fdc7489SMatthew Knepley   ul->Nl = 0;
75080fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75090fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
75100fdc7489SMatthew Knepley     const char *name;
75110fdc7489SMatthew Knepley 
75129566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75139566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
75149566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
75150fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
75160fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
75170fdc7489SMatthew Knepley   }
75189566063dSJacob 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));
75190fdc7489SMatthew Knepley   ul->Nv = 0;
75200fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75210fdc7489SMatthew Knepley     DMLabel     label;
75220fdc7489SMatthew Knepley     PetscInt    nv;
75230fdc7489SMatthew Knepley     const char *name;
75240fdc7489SMatthew Knepley 
75250fdc7489SMatthew Knepley     if (!active[l]) continue;
75269566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75279566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75289566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75299566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
75300fdc7489SMatthew Knepley     ul->indices[m] = l;
75310fdc7489SMatthew Knepley     ul->Nv += nv;
75320fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
75330fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
75340fdc7489SMatthew Knepley     ++m;
75350fdc7489SMatthew Knepley   }
75360fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
75370fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
75380fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
75390fdc7489SMatthew Knepley   }
75400fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
75410fdc7489SMatthew Knepley     PetscInt b;
75420fdc7489SMatthew Knepley 
75430fdc7489SMatthew Knepley     ul->masks[l] = 0;
75440fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
75450fdc7489SMatthew Knepley   }
75469566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
75470fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75480fdc7489SMatthew Knepley     DMLabel         label;
75490fdc7489SMatthew Knepley     IS              valueIS;
75500fdc7489SMatthew Knepley     const PetscInt *varr;
75510fdc7489SMatthew Knepley     PetscInt        nv, v;
75520fdc7489SMatthew Knepley 
75530fdc7489SMatthew Knepley     if (!active[l]) continue;
75549566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75559566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75569566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
75579566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7558ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75599566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75619566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75620fdc7489SMatthew Knepley     ++m;
75630fdc7489SMatthew Knepley   }
75649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75650fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75660fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75670fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75680fdc7489SMatthew Knepley 
75690fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75700fdc7489SMatthew Knepley       DMLabel  label;
75710649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75720fdc7489SMatthew Knepley 
75730fdc7489SMatthew Knepley       if (!active[l]) continue;
75749566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75759566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75769566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75779371c9d4SSatish Balay       if (val == defval) {
75789371c9d4SSatish Balay         ++m;
75799371c9d4SSatish Balay         continue;
75809371c9d4SSatish Balay       }
75810649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75820fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75839566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
758463a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75850fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75860fdc7489SMatthew Knepley       ++m;
75870fdc7489SMatthew Knepley     }
75889566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75890fdc7489SMatthew Knepley   }
75909566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75910fdc7489SMatthew Knepley   *universal = ul;
75923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75930fdc7489SMatthew Knepley }
75940fdc7489SMatthew Knepley 
7595d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7596d71ae5a4SJacob Faibussowitsch {
75970fdc7489SMatthew Knepley   PetscInt l;
75980fdc7489SMatthew Knepley 
75990fdc7489SMatthew Knepley   PetscFunctionBegin;
76009566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
76019566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
76029566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
76039566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
76049566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
76050fdc7489SMatthew Knepley   *universal = NULL;
76063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76070fdc7489SMatthew Knepley }
76080fdc7489SMatthew Knepley 
7609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7610d71ae5a4SJacob Faibussowitsch {
76110fdc7489SMatthew Knepley   PetscFunctionBegin;
76120fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
76130fdc7489SMatthew Knepley   *ulabel = ul->label;
76143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76150fdc7489SMatthew Knepley }
76160fdc7489SMatthew Knepley 
7617d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7618d71ae5a4SJacob Faibussowitsch {
76190fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
76200fdc7489SMatthew Knepley 
76210fdc7489SMatthew Knepley   PetscFunctionBegin;
7622064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
76230fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76249566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
76259566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
76260fdc7489SMatthew Knepley   }
76270fdc7489SMatthew Knepley   if (preserveOrder) {
76280fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
76290fdc7489SMatthew Knepley       const char *name;
76300fdc7489SMatthew Knepley       PetscBool   match;
76310fdc7489SMatthew Knepley 
76329566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
76339566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
763463a3b9bcSJacob 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]);
76350fdc7489SMatthew Knepley     }
76360fdc7489SMatthew Knepley   }
76373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76380fdc7489SMatthew Knepley }
76390fdc7489SMatthew Knepley 
7640d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7641d71ae5a4SJacob Faibussowitsch {
76420fdc7489SMatthew Knepley   PetscInt l;
76430fdc7489SMatthew Knepley 
76440fdc7489SMatthew Knepley   PetscFunctionBegin;
76450fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76460fdc7489SMatthew Knepley     DMLabel  label;
76470fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
76480fdc7489SMatthew Knepley 
76490fdc7489SMatthew Knepley     if (lval) {
76509566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
76519566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
76529566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
76530fdc7489SMatthew Knepley     }
76540fdc7489SMatthew Knepley   }
76553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76560fdc7489SMatthew Knepley }
7657a8fb8f29SToby Isaac 
7658a8fb8f29SToby Isaac /*@
7659bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7660bb7acecfSBarry Smith 
766120f4b53cSBarry Smith   Not Collective
7662a8fb8f29SToby Isaac 
7663a8fb8f29SToby Isaac   Input Parameter:
7664bb7acecfSBarry Smith . dm - The `DM` object
7665a8fb8f29SToby Isaac 
7666a8fb8f29SToby Isaac   Output Parameter:
7667bb7acecfSBarry Smith . cdm - The coarse `DM`
7668a8fb8f29SToby Isaac 
7669a8fb8f29SToby Isaac   Level: intermediate
7670a8fb8f29SToby Isaac 
76711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7672a8fb8f29SToby Isaac @*/
7673d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7674d71ae5a4SJacob Faibussowitsch {
7675a8fb8f29SToby Isaac   PetscFunctionBegin;
7676a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7677a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7678a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
76793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7680a8fb8f29SToby Isaac }
7681a8fb8f29SToby Isaac 
7682a8fb8f29SToby Isaac /*@
7683bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7684a8fb8f29SToby Isaac 
7685a8fb8f29SToby Isaac   Input Parameters:
7686bb7acecfSBarry Smith + dm - The `DM` object
7687bb7acecfSBarry Smith - cdm - The coarse `DM`
7688a8fb8f29SToby Isaac 
7689a8fb8f29SToby Isaac   Level: intermediate
7690a8fb8f29SToby Isaac 
7691bb7acecfSBarry Smith   Note:
7692bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7693bb7acecfSBarry Smith 
76941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7695a8fb8f29SToby Isaac @*/
7696d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7697d71ae5a4SJacob Faibussowitsch {
7698a8fb8f29SToby Isaac   PetscFunctionBegin;
7699a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7700a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
770189d734beSBarry Smith   if (dm == cdm) cdm = NULL;
77029566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
77039566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7704a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
77053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7706a8fb8f29SToby Isaac }
7707a8fb8f29SToby Isaac 
770888bdff64SToby Isaac /*@
7709bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
771088bdff64SToby Isaac 
771188bdff64SToby Isaac   Input Parameter:
7712bb7acecfSBarry Smith . dm - The `DM` object
771388bdff64SToby Isaac 
771488bdff64SToby Isaac   Output Parameter:
7715bb7acecfSBarry Smith . fdm - The fine `DM`
771688bdff64SToby Isaac 
771788bdff64SToby Isaac   Level: intermediate
771888bdff64SToby Isaac 
77191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
772088bdff64SToby Isaac @*/
7721d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7722d71ae5a4SJacob Faibussowitsch {
772388bdff64SToby Isaac   PetscFunctionBegin;
772488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
772588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
772688bdff64SToby Isaac   *fdm = dm->fineMesh;
77273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
772888bdff64SToby Isaac }
772988bdff64SToby Isaac 
773088bdff64SToby Isaac /*@
7731bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
773288bdff64SToby Isaac 
773388bdff64SToby Isaac   Input Parameters:
7734bb7acecfSBarry Smith + dm - The `DM` object
7735bb7acecfSBarry Smith - fdm - The fine `DM`
773688bdff64SToby Isaac 
7737bb7acecfSBarry Smith   Level: developer
773888bdff64SToby Isaac 
7739bb7acecfSBarry Smith   Note:
7740bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7741bb7acecfSBarry Smith 
77421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
774388bdff64SToby Isaac @*/
7744d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7745d71ae5a4SJacob Faibussowitsch {
774688bdff64SToby Isaac   PetscFunctionBegin;
774788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
774888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
774989d734beSBarry Smith   if (dm == fdm) fdm = NULL;
77509566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
77519566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
775288bdff64SToby Isaac   dm->fineMesh = fdm;
77533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
775488bdff64SToby Isaac }
775588bdff64SToby Isaac 
7756a6ba4734SToby Isaac /*@C
7757bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7758a6ba4734SToby Isaac 
775920f4b53cSBarry Smith   Collective
7760783e2ec8SMatthew G. Knepley 
7761a6ba4734SToby Isaac   Input Parameters:
7762bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7763bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7764a6ba4734SToby Isaac . name     - The BC name
776545480ffeSMatthew G. Knepley . label    - The label defining constrained points
7766bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
776745480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7768a6ba4734SToby Isaac . field    - The field to constrain
776945480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7770a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7771a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
777256cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7773a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7774a6ba4734SToby Isaac 
777545480ffeSMatthew G. Knepley   Output Parameter:
777645480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
777745480ffeSMatthew G. Knepley 
7778a6ba4734SToby Isaac   Options Database Keys:
7779a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7780a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7781a6ba4734SToby Isaac 
778220f4b53cSBarry Smith   Level: intermediate
778320f4b53cSBarry Smith 
7784bb7acecfSBarry Smith   Notes:
778537fdd005SBarry Smith   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is\:
778656cf3b9cSMatthew G. Knepley 
778720f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
778856cf3b9cSMatthew G. Knepley 
7789bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
779056cf3b9cSMatthew G. Knepley 
779120f4b53cSBarry Smith .vb
779220f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
779320f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
779420f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
779520f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
779620f4b53cSBarry Smith .ve
779756cf3b9cSMatthew G. Knepley + dim - the spatial dimension
779856cf3b9cSMatthew G. Knepley . Nf - the number of fields
779956cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
780056cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
780156cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
780256cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
780356cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
780456cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
780556cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
780656cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
780756cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
780856cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
780956cf3b9cSMatthew G. Knepley . t - current time
781056cf3b9cSMatthew G. Knepley . x - coordinates of the current point
781156cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
781256cf3b9cSMatthew G. Knepley . constants - constant parameters
781356cf3b9cSMatthew G. Knepley - bcval - output values at the current point
781456cf3b9cSMatthew G. Knepley 
78151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7816a6ba4734SToby Isaac @*/
7817d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], DMLabel label, PetscInt Nv, const PetscInt values[], PetscInt field, PetscInt Nc, const PetscInt comps[], void (*bcFunc)(void), void (*bcFunc_t)(void), void *ctx, PetscInt *bd)
7818d71ae5a4SJacob Faibussowitsch {
7819e5e52638SMatthew G. Knepley   PetscDS ds;
7820a6ba4734SToby Isaac 
7821a6ba4734SToby Isaac   PetscFunctionBegin;
7822a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7823783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
782445480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
782545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
782645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
782745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
782801a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
78299566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7830799db056SMatthew G. Knepley   /* Complete label */
7831799db056SMatthew G. Knepley   if (label) {
7832799db056SMatthew G. Knepley     PetscObject  obj;
7833799db056SMatthew G. Knepley     PetscClassId id;
7834799db056SMatthew G. Knepley 
7835799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7836799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7837799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7838799db056SMatthew G. Knepley       DM plex;
7839799db056SMatthew G. Knepley 
7840799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7841799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7842799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7843799db056SMatthew G. Knepley     }
7844799db056SMatthew G. Knepley   }
78459566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
78463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7847a6ba4734SToby Isaac }
7848a6ba4734SToby Isaac 
784945480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7850d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7851d71ae5a4SJacob Faibussowitsch {
7852e5e52638SMatthew G. Knepley   PetscDS     ds;
7853dff059c6SToby Isaac   DMBoundary *lastnext;
7854e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7855e6f8dbb6SToby Isaac 
7856e6f8dbb6SToby Isaac   PetscFunctionBegin;
78579566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7858e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
785947a1f5adSToby Isaac   if (dm->boundary) {
786047a1f5adSToby Isaac     DMBoundary next = dm->boundary;
786147a1f5adSToby Isaac 
786247a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
78633ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
786447a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
786547a1f5adSToby Isaac     while (next) {
786647a1f5adSToby Isaac       DMBoundary b = next;
786747a1f5adSToby Isaac 
786847a1f5adSToby Isaac       next = b->next;
78699566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7870a6ba4734SToby Isaac     }
787147a1f5adSToby Isaac     dm->boundary = NULL;
7872a6ba4734SToby Isaac   }
787347a1f5adSToby Isaac 
7874dff059c6SToby Isaac   lastnext = &(dm->boundary);
7875e6f8dbb6SToby Isaac   while (dsbound) {
7876e6f8dbb6SToby Isaac     DMBoundary dmbound;
7877e6f8dbb6SToby Isaac 
78789566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7879e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
788045480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
788147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7882dff059c6SToby Isaac     *lastnext = dmbound;
7883dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7884dff059c6SToby Isaac     dsbound   = dsbound->next;
7885a6ba4734SToby Isaac   }
78863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7887a6ba4734SToby Isaac }
7888a6ba4734SToby Isaac 
7889bb7acecfSBarry Smith /* TODO: missing manual page */
7890d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7891d71ae5a4SJacob Faibussowitsch {
7892b95f2879SToby Isaac   DMBoundary b;
7893a6ba4734SToby Isaac 
7894a6ba4734SToby Isaac   PetscFunctionBegin;
7895a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7896534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7897a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
78989566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7899b95f2879SToby Isaac   b = dm->boundary;
7900a6ba4734SToby Isaac   while (b && !(*isBd)) {
7901e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7902e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7903a6ba4734SToby Isaac     PetscInt   i;
7904a6ba4734SToby Isaac 
790545480ffeSMatthew G. Knepley     if (label) {
79069566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7907a6ba4734SToby Isaac     }
7908a6ba4734SToby Isaac     b = b->next;
7909a6ba4734SToby Isaac   }
79103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7911a6ba4734SToby Isaac }
79124d6f44ffSToby Isaac 
79134d6f44ffSToby Isaac /*@C
7914bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7915a6e0b375SMatthew G. Knepley 
791620f4b53cSBarry Smith   Collective
79174d6f44ffSToby Isaac 
79184d6f44ffSToby Isaac   Input Parameters:
7919bb7acecfSBarry Smith + dm      - The `DM`
79200709b2feSToby Isaac . time    - The time
79214d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
79224d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79234d6f44ffSToby Isaac - mode    - The insertion mode for values
79244d6f44ffSToby Isaac 
79254d6f44ffSToby Isaac   Output Parameter:
79264d6f44ffSToby Isaac . X - vector
79274d6f44ffSToby Isaac 
792820f4b53cSBarry Smith    Calling sequence of `funcs`:
792920f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
79304d6f44ffSToby Isaac +  dim - The spatial dimension
79318ec8862eSJed Brown .  time - The time at which to sample
79324d6f44ffSToby Isaac .  x   - The coordinates
793377b739a6SMatthew Knepley .  Nc  - The number of components
79344d6f44ffSToby Isaac .  u   - The output field values
79354d6f44ffSToby Isaac -  ctx - optional user-defined function context
79364d6f44ffSToby Isaac 
79374d6f44ffSToby Isaac   Level: developer
79384d6f44ffSToby Isaac 
7939bb7acecfSBarry Smith   Developer Notes:
7940bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7941bb7acecfSBarry Smith 
7942bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7943bb7acecfSBarry Smith 
79441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
79454d6f44ffSToby Isaac @*/
7946d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7947d71ae5a4SJacob Faibussowitsch {
79484d6f44ffSToby Isaac   Vec localX;
79494d6f44ffSToby Isaac 
79504d6f44ffSToby Isaac   PetscFunctionBegin;
79514d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7952708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
79539566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7954f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79559566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
79569566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79579566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79589566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
7959708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
79603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79614d6f44ffSToby Isaac }
79624d6f44ffSToby Isaac 
7963a6e0b375SMatthew G. Knepley /*@C
7964bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7965a6e0b375SMatthew G. Knepley 
796620f4b53cSBarry Smith   Not Collective
7967a6e0b375SMatthew G. Knepley 
7968a6e0b375SMatthew G. Knepley   Input Parameters:
7969bb7acecfSBarry Smith + dm      - The `DM`
7970a6e0b375SMatthew G. Knepley . time    - The time
7971a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7972a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7973a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7974a6e0b375SMatthew G. Knepley 
7975a6e0b375SMatthew G. Knepley   Output Parameter:
7976a6e0b375SMatthew G. Knepley . localX - vector
7977a6e0b375SMatthew G. Knepley 
797820f4b53cSBarry Smith    Calling sequence of `funcs`:
797920f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7980a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7981a6e0b375SMatthew G. Knepley .  x   - The coordinates
798277b739a6SMatthew Knepley .  Nc  - The number of components
7983a6e0b375SMatthew G. Knepley .  u   - The output field values
7984a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7985a6e0b375SMatthew G. Knepley 
7986a6e0b375SMatthew G. Knepley   Level: developer
7987a6e0b375SMatthew G. Knepley 
7988bb7acecfSBarry Smith   Developer Notes:
7989bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7990bb7acecfSBarry Smith 
7991bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7992bb7acecfSBarry Smith 
79931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7994a6e0b375SMatthew G. Knepley @*/
7995d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
7996d71ae5a4SJacob Faibussowitsch {
79974d6f44ffSToby Isaac   PetscFunctionBegin;
79984d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7999064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
80009566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
80013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80024d6f44ffSToby Isaac }
80034d6f44ffSToby Isaac 
8004a6e0b375SMatthew G. Knepley /*@C
8005bb7acecfSBarry 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.
8006a6e0b375SMatthew G. Knepley 
800720f4b53cSBarry Smith   Collective
8008a6e0b375SMatthew G. Knepley 
8009a6e0b375SMatthew G. Knepley   Input Parameters:
8010bb7acecfSBarry Smith + dm      - The `DM`
8011a6e0b375SMatthew G. Knepley . time    - The time
8012bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8013a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8014bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8015a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8016a6e0b375SMatthew G. Knepley 
8017a6e0b375SMatthew G. Knepley   Output Parameter:
8018a6e0b375SMatthew G. Knepley . X - vector
8019a6e0b375SMatthew G. Knepley 
802020f4b53cSBarry Smith    Calling sequence of `funcs`:
802120f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8022a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8023a6e0b375SMatthew G. Knepley .  x   - The coordinates
802477b739a6SMatthew Knepley .  Nc  - The number of components
8025a6e0b375SMatthew G. Knepley .  u   - The output field values
8026a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8027a6e0b375SMatthew G. Knepley 
8028a6e0b375SMatthew G. Knepley   Level: developer
8029a6e0b375SMatthew G. Knepley 
8030bb7acecfSBarry Smith   Developer Notes:
8031bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8032bb7acecfSBarry Smith 
8033bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8034bb7acecfSBarry Smith 
80351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8036a6e0b375SMatthew G. Knepley @*/
8037d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
8038d71ae5a4SJacob Faibussowitsch {
80392c53366bSMatthew G. Knepley   Vec localX;
80402c53366bSMatthew G. Knepley 
80412c53366bSMatthew G. Knepley   PetscFunctionBegin;
80422c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80439566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8044f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80459566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80469566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80479566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80489566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
80493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80502c53366bSMatthew G. Knepley }
80512c53366bSMatthew G. Knepley 
8052a6e0b375SMatthew G. Knepley /*@C
8053bb7acecfSBarry 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.
8054a6e0b375SMatthew G. Knepley 
805520f4b53cSBarry Smith   Not Collective
8056a6e0b375SMatthew G. Knepley 
8057a6e0b375SMatthew G. Knepley   Input Parameters:
8058bb7acecfSBarry Smith + dm      - The `DM`
8059a6e0b375SMatthew G. Knepley . time    - The time
8060bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8061a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8062a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8063a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8064a6e0b375SMatthew G. Knepley 
8065a6e0b375SMatthew G. Knepley   Output Parameter:
8066a6e0b375SMatthew G. Knepley . localX - vector
8067a6e0b375SMatthew G. Knepley 
806820f4b53cSBarry Smith    Calling sequence of `funcs`:
806920f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8070a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8071a6e0b375SMatthew G. Knepley .  x   - The coordinates
807277b739a6SMatthew Knepley .  Nc  - The number of components
8073a6e0b375SMatthew G. Knepley .  u   - The output field values
8074a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8075a6e0b375SMatthew G. Knepley 
8076a6e0b375SMatthew G. Knepley   Level: developer
8077a6e0b375SMatthew G. Knepley 
8078bb7acecfSBarry Smith   Developer Notes:
8079bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8080bb7acecfSBarry Smith 
8081bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8082bb7acecfSBarry Smith 
80831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8084a6e0b375SMatthew G. Knepley @*/
8085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
8086d71ae5a4SJacob Faibussowitsch {
80874d6f44ffSToby Isaac   PetscFunctionBegin;
80884d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8089064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
80909566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80924d6f44ffSToby Isaac }
80932716604bSToby Isaac 
8094a6e0b375SMatthew G. Knepley /*@C
8095bb7acecfSBarry 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.
8096a6e0b375SMatthew G. Knepley 
809720f4b53cSBarry Smith   Not Collective
8098a6e0b375SMatthew G. Knepley 
8099a6e0b375SMatthew G. Knepley   Input Parameters:
8100bb7acecfSBarry Smith + dm      - The `DM`
8101a6e0b375SMatthew G. Knepley . time    - The time
810220f4b53cSBarry Smith . localU  - The input field vector; may be `NULL` if projection is defined purely by coordinates
8103a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8104a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8105a6e0b375SMatthew G. Knepley 
8106a6e0b375SMatthew G. Knepley   Output Parameter:
8107a6e0b375SMatthew G. Knepley . localX  - The output vector
8108a6e0b375SMatthew G. Knepley 
810920f4b53cSBarry Smith    Calling sequence of `funcs`:
811020f4b53cSBarry Smith .vb
811120f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
811220f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
811320f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
811420f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
811520f4b53cSBarry Smith .ve
8116a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8117a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8118a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8119a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8120a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8121a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8122a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8123a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8124a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8125a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8126a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8127a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8128a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8129a6e0b375SMatthew G. Knepley .  t            - The current time
8130a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8131a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8132a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8133a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8134a6e0b375SMatthew G. Knepley 
8135bb7acecfSBarry Smith   Note:
8136bb7acecfSBarry 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.
8137bb7acecfSBarry 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
8138bb7acecfSBarry 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
8139a6e0b375SMatthew 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.
8140a6e0b375SMatthew G. Knepley 
8141a6e0b375SMatthew G. Knepley   Level: intermediate
8142a6e0b375SMatthew G. Knepley 
8143bb7acecfSBarry Smith   Developer Notes:
8144bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8145bb7acecfSBarry Smith 
8146bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8147bb7acecfSBarry Smith 
81481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8149a6e0b375SMatthew G. Knepley @*/
8150d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8151d71ae5a4SJacob Faibussowitsch {
81528c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81538c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8154eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
81558c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
81569566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
81573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81588c6c5593SMatthew G. Knepley }
81598c6c5593SMatthew G. Knepley 
8160a6e0b375SMatthew G. Knepley /*@C
8161a6e0b375SMatthew 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.
8162a6e0b375SMatthew G. Knepley 
816320f4b53cSBarry Smith   Not Collective
8164a6e0b375SMatthew G. Knepley 
8165a6e0b375SMatthew G. Knepley   Input Parameters:
8166bb7acecfSBarry Smith + dm      - The `DM`
8167a6e0b375SMatthew G. Knepley . time    - The time
8168bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8169a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8170a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8171bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
817220f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8173a6e0b375SMatthew G. Knepley . localU  - The input field vector
8174a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8175a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8176a6e0b375SMatthew G. Knepley 
8177a6e0b375SMatthew G. Knepley   Output Parameter:
8178a6e0b375SMatthew G. Knepley . localX  - The output vector
8179a6e0b375SMatthew G. Knepley 
818020f4b53cSBarry Smith    Calling sequence of `funcs`:
818120f4b53cSBarry Smith .vb
818220f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
818320f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
818420f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
818520f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
818620f4b53cSBarry Smith .ve
8187a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8188a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8189a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8190a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8191a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8192a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8193a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8194a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8195a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8196a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8197a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8198a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8199a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8200a6e0b375SMatthew G. Knepley .  t            - The current time
8201a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8202a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8203a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8204a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8205a6e0b375SMatthew G. Knepley 
8206bb7acecfSBarry Smith   Note:
8207bb7acecfSBarry 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.
8208bb7acecfSBarry 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
8209bb7acecfSBarry 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
8210a6e0b375SMatthew 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.
8211a6e0b375SMatthew G. Knepley 
8212a6e0b375SMatthew G. Knepley   Level: intermediate
8213a6e0b375SMatthew G. Knepley 
8214bb7acecfSBarry Smith   Developer Notes:
8215bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8216bb7acecfSBarry Smith 
8217bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8218bb7acecfSBarry Smith 
82191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8220a6e0b375SMatthew G. Knepley @*/
8221d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8222d71ae5a4SJacob Faibussowitsch {
82238c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82248c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8225064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8226064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
82279566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
82283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82298c6c5593SMatthew G. Knepley }
82308c6c5593SMatthew G. Knepley 
82312716604bSToby Isaac /*@C
8232d29d7c6eSMatthew 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.
8233d29d7c6eSMatthew G. Knepley 
823420f4b53cSBarry Smith   Not Collective
8235d29d7c6eSMatthew G. Knepley 
8236d29d7c6eSMatthew G. Knepley   Input Parameters:
8237bb7acecfSBarry Smith + dm      - The `DM`
8238d29d7c6eSMatthew G. Knepley . time    - The time
8239bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8240d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8241d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8242bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
824320f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8244d29d7c6eSMatthew G. Knepley . U       - The input field vector
8245d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8246d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8247d29d7c6eSMatthew G. Knepley 
8248d29d7c6eSMatthew G. Knepley   Output Parameter:
8249d29d7c6eSMatthew G. Knepley . X       - The output vector
8250d29d7c6eSMatthew G. Knepley 
825120f4b53cSBarry Smith    Calling sequence of `funcs`:
825220f4b53cSBarry Smith .vb
825320f4b53cSBarry Smith   void func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
825420f4b53cSBarry Smith             const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
825520f4b53cSBarry Smith             const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
825620f4b53cSBarry Smith             PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
825720f4b53cSBarry Smith .ve
8258d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8259d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8260d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8261d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8262d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8263d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8264d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8265d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8266d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8267d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8268d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8269d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8270d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8271d29d7c6eSMatthew G. Knepley .  t            - The current time
8272d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8273d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8274d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8275d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8276d29d7c6eSMatthew G. Knepley 
8277bb7acecfSBarry Smith   Note:
8278bb7acecfSBarry 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.
8279bb7acecfSBarry 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
8280bb7acecfSBarry 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
8281d29d7c6eSMatthew 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.
8282d29d7c6eSMatthew G. Knepley 
8283d29d7c6eSMatthew G. Knepley   Level: intermediate
8284d29d7c6eSMatthew G. Knepley 
8285bb7acecfSBarry Smith   Developer Notes:
8286bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8287bb7acecfSBarry Smith 
8288bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8289bb7acecfSBarry Smith 
82901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8291d29d7c6eSMatthew G. Knepley @*/
8292d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec X)
8293d71ae5a4SJacob Faibussowitsch {
8294d29d7c6eSMatthew G. Knepley   DM  dmIn;
8295d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8296d29d7c6eSMatthew G. Knepley 
8297d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8298d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8299d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8300d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8301d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8302f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
830372fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
830472fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8305d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8306d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8307d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8308d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8309d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
83103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8311d29d7c6eSMatthew G. Knepley }
8312d29d7c6eSMatthew G. Knepley 
8313d29d7c6eSMatthew G. Knepley /*@C
8314ece3a9fcSMatthew 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.
8315ece3a9fcSMatthew G. Knepley 
831620f4b53cSBarry Smith   Not Collective
8317ece3a9fcSMatthew G. Knepley 
8318ece3a9fcSMatthew G. Knepley   Input Parameters:
8319bb7acecfSBarry Smith + dm      - The `DM`
8320ece3a9fcSMatthew G. Knepley . time    - The time
8321bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8322ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8323ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8324bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
832520f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8326ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8327ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8328ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8329ece3a9fcSMatthew G. Knepley 
8330ece3a9fcSMatthew G. Knepley   Output Parameter:
8331ece3a9fcSMatthew G. Knepley . localX  - The output vector
8332ece3a9fcSMatthew G. Knepley 
833320f4b53cSBarry Smith    Calling sequence of `funcs`:
833420f4b53cSBarry Smith .vb
833520f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
833620f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
833720f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
833820f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
833920f4b53cSBarry Smith .ve
8340ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8341ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8342ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8343ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8344ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8345ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8346ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8347ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8348ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8349ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8350ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8351ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8352ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8353ece3a9fcSMatthew G. Knepley .  t            - The current time
8354ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8355ece3a9fcSMatthew G. Knepley .  n            - The face normal
8356ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8357ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8358ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8359ece3a9fcSMatthew G. Knepley 
8360ece3a9fcSMatthew G. Knepley   Note:
8361bb7acecfSBarry 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.
8362bb7acecfSBarry 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
8363bb7acecfSBarry 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
8364ece3a9fcSMatthew 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.
8365ece3a9fcSMatthew G. Knepley 
8366ece3a9fcSMatthew G. Knepley   Level: intermediate
8367ece3a9fcSMatthew G. Knepley 
8368bb7acecfSBarry Smith   Developer Notes:
8369bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8370bb7acecfSBarry Smith 
8371bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8372bb7acecfSBarry Smith 
83731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8374ece3a9fcSMatthew G. Knepley @*/
8375d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8376d71ae5a4SJacob Faibussowitsch {
8377ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8378ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8379064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8380064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83819566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
83823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8383ece3a9fcSMatthew G. Knepley }
8384ece3a9fcSMatthew G. Knepley 
8385ece3a9fcSMatthew G. Knepley /*@C
83862716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83872716604bSToby Isaac 
838820f4b53cSBarry Smith   Collective
8389bb7acecfSBarry Smith 
83902716604bSToby Isaac   Input Parameters:
8391bb7acecfSBarry Smith + dm    - The `DM`
83920709b2feSToby Isaac . time  - The time
83932716604bSToby Isaac . funcs - The functions to evaluate for each field component
83942716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8395574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83962716604bSToby Isaac 
83972716604bSToby Isaac   Output Parameter:
83982716604bSToby Isaac . diff - The diff ||u - u_h||_2
83992716604bSToby Isaac 
84002716604bSToby Isaac   Level: developer
84012716604bSToby Isaac 
8402bb7acecfSBarry Smith   Developer Notes:
8403bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8404bb7acecfSBarry Smith 
8405bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8406bb7acecfSBarry Smith 
84071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84082716604bSToby Isaac @*/
8409d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8410d71ae5a4SJacob Faibussowitsch {
84112716604bSToby Isaac   PetscFunctionBegin;
84122716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8413b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84149566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
84153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84162716604bSToby Isaac }
8417b698f381SToby Isaac 
8418b698f381SToby Isaac /*@C
8419b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8420b698f381SToby Isaac 
842120f4b53cSBarry Smith   Collective
8422d083f849SBarry Smith 
8423b698f381SToby Isaac   Input Parameters:
8424bb7acecfSBarry Smith + dm    - The `DM`
8425b698f381SToby Isaac , time  - The time
8426b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8427b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8428574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8429b698f381SToby Isaac - n     - The vector to project along
8430b698f381SToby Isaac 
8431b698f381SToby Isaac   Output Parameter:
8432b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8433b698f381SToby Isaac 
8434b698f381SToby Isaac   Level: developer
8435b698f381SToby Isaac 
8436bb7acecfSBarry Smith   Developer Notes:
8437bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8438bb7acecfSBarry Smith 
8439bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8440bb7acecfSBarry Smith 
84411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8442b698f381SToby Isaac @*/
8443d71ae5a4SJacob 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)
8444d71ae5a4SJacob Faibussowitsch {
8445b698f381SToby Isaac   PetscFunctionBegin;
8446b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8447b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84489566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
84493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8450b698f381SToby Isaac }
8451b698f381SToby Isaac 
84522a16baeaSToby Isaac /*@C
84532a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84542a16baeaSToby Isaac 
845520f4b53cSBarry Smith   Collective
8456d083f849SBarry Smith 
84572a16baeaSToby Isaac   Input Parameters:
8458bb7acecfSBarry Smith + dm    - The `DM`
84592a16baeaSToby Isaac . time  - The time
84602a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84612a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8462574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84632a16baeaSToby Isaac 
84642a16baeaSToby Isaac   Output Parameter:
84652a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84662a16baeaSToby Isaac 
84672a16baeaSToby Isaac   Level: developer
84682a16baeaSToby Isaac 
8469bb7acecfSBarry Smith   Developer Notes:
8470bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8471bb7acecfSBarry Smith 
8472bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8473bb7acecfSBarry Smith 
84741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84752a16baeaSToby Isaac @*/
8476d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8477d71ae5a4SJacob Faibussowitsch {
84782a16baeaSToby Isaac   PetscFunctionBegin;
84792a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84802a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84819566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84832a16baeaSToby Isaac }
84842a16baeaSToby Isaac 
8485df0b854cSToby Isaac /*@C
8486bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8487502a2867SDave May 
8488502a2867SDave May  Not Collective
8489502a2867SDave May 
8490502a2867SDave May  Input Parameter:
8491bb7acecfSBarry Smith .  dm    - The `DM`
8492502a2867SDave May 
84930a19bb7dSprj-  Output Parameters:
84940a19bb7dSprj- +  nranks - the number of neighbours
84950a19bb7dSprj- -  ranks - the neighbors ranks
8496502a2867SDave May 
84979bdbcad8SBarry Smith  Level: beginner
84989bdbcad8SBarry Smith 
8499bb7acecfSBarry Smith  Note:
8500bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8501502a2867SDave May 
85021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8503502a2867SDave May @*/
8504d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8505d71ae5a4SJacob Faibussowitsch {
8506502a2867SDave May   PetscFunctionBegin;
8507502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85089566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
85093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8510502a2867SDave May }
8511502a2867SDave May 
8512531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8513531c7667SBarry Smith 
8514531c7667SBarry Smith /*
8515531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
85162b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8517531c7667SBarry Smith */
8518d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8519d71ae5a4SJacob Faibussowitsch {
8520531c7667SBarry Smith   PetscFunctionBegin;
8521531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8522531c7667SBarry Smith     Vec x1local;
8523531c7667SBarry Smith     DM  dm;
85249566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85257a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
85269566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
85279566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
85289566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8529531c7667SBarry Smith     x1 = x1local;
8530531c7667SBarry Smith   }
85319566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8532531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8533531c7667SBarry Smith     DM dm;
85349566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85359566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8536531c7667SBarry Smith   }
85373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8538531c7667SBarry Smith }
8539531c7667SBarry Smith 
8540531c7667SBarry Smith /*@
8541bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8542531c7667SBarry Smith 
8543531c7667SBarry Smith     Input Parameter:
8544bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8545531c7667SBarry Smith 
85469bdbcad8SBarry Smith     Level: advanced
85479bdbcad8SBarry Smith 
8548bb7acecfSBarry Smith     Developer Note:
8549bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8550531c7667SBarry Smith 
85511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8552531c7667SBarry Smith @*/
8553d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8554d71ae5a4SJacob Faibussowitsch {
8555531c7667SBarry Smith   PetscFunctionBegin;
8556531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
85573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8558531c7667SBarry Smith }
85598320bc6fSPatrick Sanan 
85608320bc6fSPatrick Sanan /*@
8561bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85628320bc6fSPatrick Sanan 
85638320bc6fSPatrick Sanan     Collective
85648320bc6fSPatrick Sanan 
85658320bc6fSPatrick Sanan     Input Parameters:
8566bb7acecfSBarry Smith +    dm1 - the first `DM`
8567bb7acecfSBarry Smith -    dm2 - the second `DM`
85688320bc6fSPatrick Sanan 
85698320bc6fSPatrick Sanan     Output Parameters:
8570bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8571bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85728320bc6fSPatrick Sanan 
857320f4b53cSBarry Smith     Level: advanced
857420f4b53cSBarry Smith 
85758320bc6fSPatrick Sanan     Notes:
8576bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85773d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85788320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8579bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85803d862458SPatrick Sanan     decomposition, but hold different data.
85818320bc6fSPatrick Sanan 
85828320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8583bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85848320bc6fSPatrick Sanan 
8585bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85868320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85878320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8588bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85898320bc6fSPatrick Sanan 
8590bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8591bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85928320bc6fSPatrick Sanan .vb
85938320bc6fSPatrick Sanan   ...
85949566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85958320bc6fSPatrick Sanan   if (set && compatible)  {
85969566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
85979566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
85989566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
85998320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86008320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86018320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86028320bc6fSPatrick Sanan       }
86038320bc6fSPatrick Sanan     }
86049566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
86059566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
86068320bc6fSPatrick Sanan   } else {
86078320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86088320bc6fSPatrick Sanan   }
86098320bc6fSPatrick Sanan   ...
86108320bc6fSPatrick Sanan .ve
86118320bc6fSPatrick Sanan 
8612bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
86138320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
86148320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
86158320bc6fSPatrick Sanan     always check the "set" output parameter.
86168320bc6fSPatrick Sanan 
8617bb7acecfSBarry Smith     A `DM` is always compatible with itself.
86188320bc6fSPatrick Sanan 
8619bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
86208320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
86218320bc6fSPatrick Sanan     incompatible.
86228320bc6fSPatrick Sanan 
86238320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8624bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
86258320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
86268320bc6fSPatrick Sanan 
8627bb7acecfSBarry Smith     Developer Note:
8628bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
86293d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8630a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8631bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
86328320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
86333d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8634bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
86358320bc6fSPatrick Sanan 
86361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
86378320bc6fSPatrick Sanan @*/
8638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8639d71ae5a4SJacob Faibussowitsch {
86408320bc6fSPatrick Sanan   PetscMPIInt compareResult;
86418320bc6fSPatrick Sanan   DMType      type, type2;
86428320bc6fSPatrick Sanan   PetscBool   sameType;
86438320bc6fSPatrick Sanan 
86448320bc6fSPatrick Sanan   PetscFunctionBegin;
8645a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
86468320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
86478320bc6fSPatrick Sanan 
86488320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8649a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86508320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86518320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86523ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86538320bc6fSPatrick Sanan   }
86548320bc6fSPatrick Sanan 
86558320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86568320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86578320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86588320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86599566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
86608320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86618320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86628320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86633ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86648320bc6fSPatrick Sanan   }
86658320bc6fSPatrick Sanan 
86668320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8667a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8668dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
86693ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
86708320bc6fSPatrick Sanan   }
86718320bc6fSPatrick Sanan 
8672a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86738320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86749566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86759566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86769566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86778320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8678dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86798320bc6fSPatrick Sanan   } else {
86808320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86818320bc6fSPatrick Sanan   }
86823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86838320bc6fSPatrick Sanan }
8684c0f0dcc3SMatthew G. Knepley 
8685c0f0dcc3SMatthew G. Knepley /*@C
8686bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8687c0f0dcc3SMatthew G. Knepley 
868820f4b53cSBarry Smith   Logically Collective
8689c0f0dcc3SMatthew G. Knepley 
8690c0f0dcc3SMatthew G. Knepley   Input Parameters:
8691bb7acecfSBarry Smith + DM - the `DM`
8692c0f0dcc3SMatthew G. Knepley . f - the monitor function
869320f4b53cSBarry Smith . mctx - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
869420f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8695c0f0dcc3SMatthew G. Knepley 
869620f4b53cSBarry Smith   Options Database Key:
8697bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8698c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8699c0f0dcc3SMatthew G. Knepley 
87009bdbcad8SBarry Smith   Level: intermediate
87019bdbcad8SBarry Smith 
8702bb7acecfSBarry Smith   Note:
8703c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8704bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8705c0f0dcc3SMatthew G. Knepley   order in which they were set.
8706c0f0dcc3SMatthew G. Knepley 
8707bb7acecfSBarry Smith   Fortran Note:
8708bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8709bb7acecfSBarry Smith 
8710bb7acecfSBarry Smith   Developer Note:
8711bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8712c0f0dcc3SMatthew G. Knepley 
87131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8714c0f0dcc3SMatthew G. Knepley @*/
8715d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8716d71ae5a4SJacob Faibussowitsch {
8717c0f0dcc3SMatthew G. Knepley   PetscInt m;
8718c0f0dcc3SMatthew G. Knepley 
8719c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8720c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8721c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8722c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8723c0f0dcc3SMatthew G. Knepley 
87249566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
87253ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8726c0f0dcc3SMatthew G. Knepley   }
87277a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8728c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8729c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8730c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
87313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8732c0f0dcc3SMatthew G. Knepley }
8733c0f0dcc3SMatthew G. Knepley 
8734c0f0dcc3SMatthew G. Knepley /*@
8735bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8736c0f0dcc3SMatthew G. Knepley 
873720f4b53cSBarry Smith   Logically Collective
8738c0f0dcc3SMatthew G. Knepley 
8739c0f0dcc3SMatthew G. Knepley   Input Parameter:
8740c0f0dcc3SMatthew G. Knepley . dm - the DM
8741c0f0dcc3SMatthew G. Knepley 
8742c0f0dcc3SMatthew G. Knepley   Options Database Key:
8743c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8744bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8745c0f0dcc3SMatthew G. Knepley   set via the options database
8746c0f0dcc3SMatthew G. Knepley 
87479bdbcad8SBarry Smith   Level: intermediate
87489bdbcad8SBarry Smith 
8749bb7acecfSBarry Smith   Note:
8750bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8751c0f0dcc3SMatthew G. Knepley 
87521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8753c0f0dcc3SMatthew G. Knepley @*/
8754d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8755d71ae5a4SJacob Faibussowitsch {
8756c0f0dcc3SMatthew G. Knepley   PetscInt m;
8757c0f0dcc3SMatthew G. Knepley 
8758c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8759c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8760c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87619566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8762c0f0dcc3SMatthew G. Knepley   }
8763c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
87643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8765c0f0dcc3SMatthew G. Knepley }
8766c0f0dcc3SMatthew G. Knepley 
8767c0f0dcc3SMatthew G. Knepley /*@C
8768c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8769c0f0dcc3SMatthew G. Knepley 
877020f4b53cSBarry Smith   Collective
8771c0f0dcc3SMatthew G. Knepley 
8772c0f0dcc3SMatthew G. Knepley   Input Parameters:
8773bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8774c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8775c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8776c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8777c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8778bb7acecfSBarry 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
8779c0f0dcc3SMatthew G. Knepley 
8780c0f0dcc3SMatthew G. Knepley   Output Parameter:
8781c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8782c0f0dcc3SMatthew G. Knepley 
8783c0f0dcc3SMatthew G. Knepley   Level: developer
8784c0f0dcc3SMatthew G. Knepley 
87851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8786db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8787db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8788db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8789c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8790db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8791bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8792c0f0dcc3SMatthew G. Knepley @*/
8793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8794d71ae5a4SJacob Faibussowitsch {
8795c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8796c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8797c0f0dcc3SMatthew G. Knepley 
8798c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8799c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88009566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8801c0f0dcc3SMatthew G. Knepley   if (*flg) {
8802c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8803c0f0dcc3SMatthew G. Knepley 
88049566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
88059566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
88069566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
88079566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8808c0f0dcc3SMatthew G. Knepley   }
88093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8810c0f0dcc3SMatthew G. Knepley }
8811c0f0dcc3SMatthew G. Knepley 
8812c0f0dcc3SMatthew G. Knepley /*@
8813c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8814c0f0dcc3SMatthew G. Knepley 
881520f4b53cSBarry Smith    Collective
8816c0f0dcc3SMatthew G. Knepley 
88172fe279fdSBarry Smith    Input Parameter:
8818bb7acecfSBarry Smith .  dm - The `DM`
8819c0f0dcc3SMatthew G. Knepley 
8820c0f0dcc3SMatthew G. Knepley    Level: developer
8821c0f0dcc3SMatthew G. Knepley 
8822bb7acecfSBarry Smith    Question:
8823bb7acecfSBarry Smith    Note should indicate when during the life of the `DM` the monitor is run. It appears to be related to the discretization process seems rather specialized
8824bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8825bb7acecfSBarry Smith 
88261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8827c0f0dcc3SMatthew G. Knepley @*/
8828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8829d71ae5a4SJacob Faibussowitsch {
8830c0f0dcc3SMatthew G. Knepley   PetscInt m;
8831c0f0dcc3SMatthew G. Knepley 
8832c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
88333ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8834c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
883548a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
88363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8837c0f0dcc3SMatthew G. Knepley }
88382e4af2aeSMatthew G. Knepley 
88392e4af2aeSMatthew G. Knepley /*@
8840bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
88412e4af2aeSMatthew G. Knepley 
884220f4b53cSBarry Smith   Collective
88432e4af2aeSMatthew G. Knepley 
88442e4af2aeSMatthew G. Knepley   Input Parameters:
8845bb7acecfSBarry Smith + dm     - The `DM`
88466b867d5aSJose E. Roman - sol    - The solution vector
88472e4af2aeSMatthew G. Knepley 
88486b867d5aSJose E. Roman   Input/Output Parameter:
884920f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
88506b867d5aSJose E. Roman            contains the error in each field
88516b867d5aSJose E. Roman 
88526b867d5aSJose E. Roman   Output Parameter:
885320f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
885420f4b53cSBarry Smith 
885520f4b53cSBarry Smith   Level: developer
88562e4af2aeSMatthew G. Knepley 
8857bb7acecfSBarry Smith   Note:
8858bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88592e4af2aeSMatthew G. Knepley 
88601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88612e4af2aeSMatthew G. Knepley @*/
8862d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8863d71ae5a4SJacob Faibussowitsch {
88642e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88652e4af2aeSMatthew G. Knepley   void    **ctxs;
88662e4af2aeSMatthew G. Knepley   PetscReal time;
88672e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88682e4af2aeSMatthew G. Knepley 
88692e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88709566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88719566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88729566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88732e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88742e4af2aeSMatthew G. Knepley     PetscDS         ds;
88752e4af2aeSMatthew G. Knepley     DMLabel         label;
88762e4af2aeSMatthew G. Knepley     IS              fieldIS;
88772e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88782e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88792e4af2aeSMatthew G. Knepley 
888007218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
88819566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88829566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88832e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88842e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88859566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88862e4af2aeSMatthew G. Knepley     }
88879566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88882e4af2aeSMatthew G. Knepley   }
8889ad540459SPierre 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);
88909566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88919566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88922e4af2aeSMatthew G. Knepley   if (errorVec) {
88932e4af2aeSMatthew G. Knepley     DM             edm;
88942e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
88952e4af2aeSMatthew G. Knepley     PetscBool      simplex;
88962e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
88972e4af2aeSMatthew G. Knepley 
88989566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
88999566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
89009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
89022e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
89039566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
89042e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
89052e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
89062e4af2aeSMatthew G. Knepley       PetscQuadrature q;
89072e4af2aeSMatthew G. Knepley       const char     *name;
89082e4af2aeSMatthew G. Knepley 
89099566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
89109566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
89119566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
89129566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
89139566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
89149566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
89159566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
89169566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
89172e4af2aeSMatthew G. Knepley     }
89189566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
89192e4af2aeSMatthew G. Knepley 
89209566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
89219566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
89229566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
89239566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
89242e4af2aeSMatthew G. Knepley   }
89259566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
89263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89272e4af2aeSMatthew G. Knepley }
89289a2a23afSMatthew G. Knepley 
89299a2a23afSMatthew G. Knepley /*@
8930bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
89319a2a23afSMatthew G. Knepley 
893220f4b53cSBarry Smith   Not Collective
89339a2a23afSMatthew G. Knepley 
89349a2a23afSMatthew G. Knepley   Input Parameter:
8935bb7acecfSBarry Smith . dm     - The `DM`
89369a2a23afSMatthew G. Knepley 
89379a2a23afSMatthew G. Knepley   Output Parameter:
8938a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
89399a2a23afSMatthew G. Knepley 
89409a2a23afSMatthew G. Knepley   Level: advanced
89419a2a23afSMatthew G. Knepley 
89421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
89439a2a23afSMatthew G. Knepley @*/
8944d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8945d71ae5a4SJacob Faibussowitsch {
89469a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89479a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89489566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89509a2a23afSMatthew G. Knepley }
89519a2a23afSMatthew G. Knepley 
89529a2a23afSMatthew G. Knepley /*@
8953ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89549a2a23afSMatthew G. Knepley 
895520f4b53cSBarry Smith   Not Collective
89569a2a23afSMatthew G. Knepley 
89579a2a23afSMatthew G. Knepley   Input Parameters:
8958bb7acecfSBarry Smith + dm     - The `DM`
8959bb7acecfSBarry Smith . label  - The `DMLabel`
8960ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8961ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89629a2a23afSMatthew G. Knepley 
89639a2a23afSMatthew G. Knepley   Output Parameter:
8964bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89659a2a23afSMatthew G. Knepley 
89669bdbcad8SBarry Smith   Level: advanced
89679bdbcad8SBarry Smith 
8968bb7acecfSBarry Smith   Note:
8969bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
897004c51a94SMatthew G. Knepley 
89711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89729a2a23afSMatthew G. Knepley @*/
8973d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8974d71ae5a4SJacob Faibussowitsch {
8975ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
897604c51a94SMatthew G. Knepley   PetscBool       has;
89779a2a23afSMatthew G. Knepley 
89789a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89799a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89809a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89819a2a23afSMatthew G. Knepley   key.label = label;
89829a2a23afSMatthew G. Knepley   key.value = value;
8983ac17215fSMatthew G. Knepley   key.part  = part;
89849566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89859566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89869566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89889a2a23afSMatthew G. Knepley }
89899a2a23afSMatthew G. Knepley 
89909a2a23afSMatthew G. Knepley /*@
8991bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89929a2a23afSMatthew G. Knepley 
899320f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
89949a2a23afSMatthew G. Knepley 
89959a2a23afSMatthew G. Knepley   Input Parameters:
8996bb7acecfSBarry Smith + dm     - The `DM`
8997bb7acecfSBarry Smith . label  - The `DMLabel`
89989a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8999ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
9000bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
90019a2a23afSMatthew G. Knepley 
90029a2a23afSMatthew G. Knepley   Level: advanced
90039a2a23afSMatthew G. Knepley 
90041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
90059a2a23afSMatthew G. Knepley @*/
9006d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9007d71ae5a4SJacob Faibussowitsch {
90089a2a23afSMatthew G. Knepley   Vec             old;
90099a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
90109a2a23afSMatthew G. Knepley 
90119a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90129a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90139a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90149a2a23afSMatthew G. Knepley   key.label = label;
90159a2a23afSMatthew G. Knepley   key.value = value;
9016ac17215fSMatthew G. Knepley   key.part  = part;
90179566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
90189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
90199566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
90209566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
90219566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
90223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90239a2a23afSMatthew G. Knepley }
90249a2a23afSMatthew G. Knepley 
90259a2a23afSMatthew G. Knepley /*@C
9026bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
90279a2a23afSMatthew G. Knepley 
902820f4b53cSBarry Smith   Not Collective
90299a2a23afSMatthew G. Knepley 
90309a2a23afSMatthew G. Knepley   Input Parameter:
9031bb7acecfSBarry Smith . dm      - The `DM`
90329a2a23afSMatthew G. Knepley 
90339a2a23afSMatthew G. Knepley   Output Parameters:
9034bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
9035bb7acecfSBarry Smith . values  - The label values for each `Vec`
9036bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
90379a2a23afSMatthew G. Knepley 
90389bdbcad8SBarry Smith   Level: advanced
90399bdbcad8SBarry Smith 
9040bb7acecfSBarry Smith   Note:
9041bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
90429a2a23afSMatthew G. Knepley 
90431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
90449a2a23afSMatthew G. Knepley @*/
9045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9046d71ae5a4SJacob Faibussowitsch {
90479a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
90489a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90499a2a23afSMatthew G. Knepley 
90509a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90519a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90529a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9053dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9054dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
90559566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90579566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
90589371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
90599371c9d4SSatish Balay     labels[i] = keys[i].label;
90609371c9d4SSatish Balay     values[i] = keys[i].value;
90619371c9d4SSatish Balay     parts[i]  = keys[i].part;
90629371c9d4SSatish Balay   }
90639566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90659a2a23afSMatthew G. Knepley }
90669a2a23afSMatthew G. Knepley 
90679a2a23afSMatthew G. Knepley /*@
9068bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90699a2a23afSMatthew G. Knepley 
907020f4b53cSBarry Smith   Not Collective
90719a2a23afSMatthew G. Knepley 
90729a2a23afSMatthew G. Knepley   Input Parameter:
9073bb7acecfSBarry Smith . dm    - The `DM`
90749a2a23afSMatthew G. Knepley 
90759a2a23afSMatthew G. Knepley   Output Parameter:
9076bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90779a2a23afSMatthew G. Knepley 
90789a2a23afSMatthew G. Knepley   Level: advanced
90799a2a23afSMatthew G. Knepley 
9080bb7acecfSBarry Smith   Note:
9081bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9082bb7acecfSBarry Smith 
90831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90849a2a23afSMatthew G. Knepley @*/
9085d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9086d71ae5a4SJacob Faibussowitsch {
90879a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90889a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90899566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90909566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90929a2a23afSMatthew G. Knepley }
9093b5a892a1SMatthew G. Knepley 
9094b5a892a1SMatthew G. Knepley /*@C
9095bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9096b5a892a1SMatthew G. Knepley 
909720f4b53cSBarry Smith   Not Collective
9098b5a892a1SMatthew G. Knepley 
9099b5a892a1SMatthew G. Knepley   Input Parameters:
9100bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9101b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9102b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9103b5a892a1SMatthew G. Knepley 
9104b5a892a1SMatthew G. Knepley   Output Parameters:
9105bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9106b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9107b5a892a1SMatthew G. Knepley 
9108b5a892a1SMatthew G. Knepley   Level: advanced
9109b5a892a1SMatthew G. Knepley 
9110bb7acecfSBarry Smith   Note:
9111bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9112bb7acecfSBarry Smith 
9113bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9114bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9115bb7acecfSBarry Smith 
9116bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9117bb7acecfSBarry Smith 
91181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9119b5a892a1SMatthew G. Knepley @*/
9120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9121d71ae5a4SJacob Faibussowitsch {
9122b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9123b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9124b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9125b5a892a1SMatthew G. Knepley 
9126b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91279371c9d4SSatish Balay   if (!nO) {
91289371c9d4SSatish Balay     *ornt  = 0;
91299371c9d4SSatish Balay     *found = PETSC_TRUE;
91303ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
91319371c9d4SSatish Balay   }
9132b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9133b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9134b5a892a1SMatthew G. Knepley 
91359371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91369371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
91379371c9d4SSatish Balay     if (c == cS) {
91389371c9d4SSatish Balay       *ornt = o;
91399371c9d4SSatish Balay       break;
91409371c9d4SSatish Balay     }
9141b5a892a1SMatthew G. Knepley   }
9142b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
91433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9144b5a892a1SMatthew G. Knepley }
9145b5a892a1SMatthew G. Knepley 
9146b5a892a1SMatthew G. Knepley /*@C
9147bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9148b5a892a1SMatthew G. Knepley 
914920f4b53cSBarry Smith   Not Collective
9150b5a892a1SMatthew G. Knepley 
9151b5a892a1SMatthew G. Knepley   Input Parameters:
9152bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9153b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9154b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9155b5a892a1SMatthew G. Knepley 
91562fe279fdSBarry Smith   Output Parameter:
9157bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9158b5a892a1SMatthew G. Knepley 
9159b5a892a1SMatthew G. Knepley   Level: advanced
9160b5a892a1SMatthew G. Knepley 
9161bb7acecfSBarry Smith   Note:
9162bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9163bb7acecfSBarry Smith 
9164bb7acecfSBarry Smith   Developer Note:
9165bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9166bb7acecfSBarry Smith 
91671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9168b5a892a1SMatthew G. Knepley @*/
9169d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9170d71ae5a4SJacob Faibussowitsch {
9171b5a892a1SMatthew G. Knepley   PetscBool found;
9172b5a892a1SMatthew G. Knepley 
9173b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91749566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91757a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
91763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9177b5a892a1SMatthew G. Knepley }
9178b5a892a1SMatthew G. Knepley 
9179b5a892a1SMatthew G. Knepley /*@C
9180bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9181b5a892a1SMatthew G. Knepley 
918220f4b53cSBarry Smith   Not Collective
9183b5a892a1SMatthew G. Knepley 
9184b5a892a1SMatthew G. Knepley   Input Parameters:
9185bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9186b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9187b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9188b5a892a1SMatthew G. Knepley 
9189b5a892a1SMatthew G. Knepley   Output Parameters:
9190bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9191b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9192b5a892a1SMatthew G. Knepley 
9193b5a892a1SMatthew G. Knepley   Level: advanced
9194b5a892a1SMatthew G. Knepley 
9195bb7acecfSBarry Smith   Note:
9196bb7acecfSBarry Smith   An arrangement is a vertex order
9197bb7acecfSBarry Smith 
9198bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9199bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9200bb7acecfSBarry Smith 
9201bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9202bb7acecfSBarry Smith 
92031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9204b5a892a1SMatthew G. Knepley @*/
9205d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9206d71ae5a4SJacob Faibussowitsch {
9207b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9208b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9209b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9210b5a892a1SMatthew G. Knepley 
9211b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92129371c9d4SSatish Balay   if (!nO) {
92139371c9d4SSatish Balay     *ornt  = 0;
92149371c9d4SSatish Balay     *found = PETSC_TRUE;
92153ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92169371c9d4SSatish Balay   }
9217b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9218b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9219b5a892a1SMatthew G. Knepley 
92209371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92219371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
92229371c9d4SSatish Balay     if (c == cS) {
92239371c9d4SSatish Balay       *ornt = o;
92249371c9d4SSatish Balay       break;
92259371c9d4SSatish Balay     }
9226b5a892a1SMatthew G. Knepley   }
9227b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9229b5a892a1SMatthew G. Knepley }
9230b5a892a1SMatthew G. Knepley 
9231b5a892a1SMatthew G. Knepley /*@C
9232bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9233b5a892a1SMatthew G. Knepley 
923420f4b53cSBarry Smith   Not Collective
9235b5a892a1SMatthew G. Knepley 
9236b5a892a1SMatthew G. Knepley   Input Parameters:
9237bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9238b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9239b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9240b5a892a1SMatthew G. Knepley 
92412fe279fdSBarry Smith   Output Parameter:
9242bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9243b5a892a1SMatthew G. Knepley 
9244b5a892a1SMatthew G. Knepley   Level: advanced
9245b5a892a1SMatthew G. Knepley 
9246bb7acecfSBarry Smith   Note:
9247bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9248bb7acecfSBarry Smith 
9249bb7acecfSBarry Smith   Developer Note:
9250bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9251bb7acecfSBarry Smith 
92521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9253b5a892a1SMatthew G. Knepley @*/
9254d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9255d71ae5a4SJacob Faibussowitsch {
9256b5a892a1SMatthew G. Knepley   PetscBool found;
9257b5a892a1SMatthew G. Knepley 
9258b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92599566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
92607a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
92613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9262b5a892a1SMatthew G. Knepley }
9263012bc364SMatthew G. Knepley 
9264012bc364SMatthew G. Knepley /*@C
9265012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9266012bc364SMatthew G. Knepley 
926720f4b53cSBarry Smith   Not Collective
9268012bc364SMatthew G. Knepley 
9269012bc364SMatthew G. Knepley   Input Parameters:
9270bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9271012bc364SMatthew G. Knepley - point - Coordinates of the point
9272012bc364SMatthew G. Knepley 
92732fe279fdSBarry Smith   Output Parameter:
9274012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9275012bc364SMatthew G. Knepley 
9276012bc364SMatthew G. Knepley   Level: advanced
9277012bc364SMatthew G. Knepley 
92781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9279012bc364SMatthew G. Knepley @*/
9280d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9281d71ae5a4SJacob Faibussowitsch {
9282012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9283012bc364SMatthew G. Knepley   PetscInt  d;
9284012bc364SMatthew G. Knepley 
9285012bc364SMatthew G. Knepley   PetscFunctionBegin;
9286012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9287012bc364SMatthew G. Knepley   switch (ct) {
9288012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9289012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9290012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92919371c9d4SSatish Balay       if (point[d] < -1.0) {
92929371c9d4SSatish Balay         *inside = PETSC_FALSE;
92939371c9d4SSatish Balay         break;
92949371c9d4SSatish Balay       }
9295012bc364SMatthew G. Knepley       sum += point[d];
9296012bc364SMatthew G. Knepley     }
92979371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
92989371c9d4SSatish Balay       *inside = PETSC_FALSE;
92999371c9d4SSatish Balay       break;
93009371c9d4SSatish Balay     }
9301012bc364SMatthew G. Knepley     break;
9302012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9303012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9304012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
93059371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
93069371c9d4SSatish Balay         *inside = PETSC_FALSE;
9307012bc364SMatthew G. Knepley         break;
93089371c9d4SSatish Balay       }
93099371c9d4SSatish Balay     break;
9310d71ae5a4SJacob Faibussowitsch   default:
9311d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9312012bc364SMatthew G. Knepley   }
93133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9314012bc364SMatthew G. Knepley }
9315